Bird
Raised Fist0
C++programming~20 mins

Creating objects in C++ - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Object Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of object creation and member access
What is the output of this C++ code when creating an object and accessing its member?
C++
class Box {
public:
    int length;
    Box(int l) : length(l) {}
};

int main() {
    Box b(10);
    std::cout << b.length << std::endl;
    return 0;
}
A0
BRuntime error
CCompilation error
D10
Attempts:
2 left
πŸ’‘ Hint
Look at how the constructor initializes the length member.
❓ Predict Output
intermediate
2:00remaining
Output when creating an object with default constructor
What will be the output of this code snippet?
C++
class Point {
public:
    int x, y;
    Point() { x = 5; y = 7; }
};

int main() {
    Point p;
    std::cout << p.x << "," << p.y << std::endl;
    return 0;
}
ACompilation error
B0,0
C5,7
DGarbage values
Attempts:
2 left
πŸ’‘ Hint
The default constructor sets x and y explicitly.
❓ Predict Output
advanced
2:00remaining
Output of object creation with private constructor
What happens when you try to compile and run this code?
C++
class Secret {
private:
    Secret() {}
public:
    static Secret create() { return Secret(); }
};

int main() {
    Secret s = Secret::create();
    return 0;
}
ACompiles and runs without output
BCompilation error: constructor is private
CRuntime error
DLinker error
Attempts:
2 left
πŸ’‘ Hint
The static method can access the private constructor.
❓ Predict Output
advanced
2:00remaining
Output of object creation with deleted copy constructor
What will happen when this code is compiled and run?
C++
class NoCopy {
public:
    NoCopy() {}
    NoCopy(const NoCopy&) = delete;
};

int main() {
    NoCopy a;
    NoCopy b = a;
    return 0;
}
ARuns and copies object
BCompilation error due to deleted copy constructor
CRuntime error
DLinker error
Attempts:
2 left
πŸ’‘ Hint
Copying is disabled explicitly.
🧠 Conceptual
expert
2:00remaining
Number of objects created in this code
How many objects are created in total when this code runs?
C++
class Sample {
public:
    Sample() {}
};

Sample createSample() {
    Sample s;
    return s;
}

int main() {
    Sample a = createSample();
    return 0;
}
A2
B1
C3
D0
Attempts:
2 left
πŸ’‘ Hint
Consider the local object and the returned object.

Practice

(1/5)
1. Which of the following correctly creates an object named car1 from a class Car?
easy
A. new Car car1;
B. car1 Car;
C. Car car1;
D. Car = car1;

Solution

  1. Step 1: Understand object creation syntax in C++

    Objects are created by writing the class name followed by the object name and a semicolon.
  2. Step 2: Check each option

    Car car1; uses correct syntax: Car car1;. Others are incorrect because they misuse order or keywords.
  3. Final Answer:

    Car car1; -> Option C
  4. Quick Check:

    ClassName objectName; = Car car1; [OK]
Hint: Class name first, then object name, end with semicolon [OK]
Common Mistakes:
  • Swapping class and object names
  • Using assignment operator instead of declaration
  • Using 'new' keyword without pointer
2. Which of the following is the correct syntax to create an object book1 of class Book using a constructor explicitly?
easy
A. Book book1();
B. Book book1;
C. Book book1{};
D. Book book1 = Book();

Solution

  1. Step 1: Understand constructor call syntax

    To call a constructor explicitly, you can use ClassName object = ClassName(); which calls the default constructor.
  2. Step 2: Analyze options

    Book book1(); is a function declaration, not an object. Book book1; creates object but does not explicitly call constructor. Book book1{}; uses brace initialization but may not call constructor explicitly. Book book1 = Book(); correctly calls constructor.
  3. Final Answer:

    Book book1 = Book(); -> Option D
  4. Quick Check:

    Explicit constructor call = Book book1 = Book(); [OK]
Hint: Use ClassName object = ClassName(); to call constructor explicitly [OK]
Common Mistakes:
  • Writing ClassName object(); which is a function declaration
  • Forgetting parentheses for constructor call
  • Using braces without understanding initialization
3. What will be the output of the following code?
class Dog {
public:
  Dog() { age = 5; }
  int age;
};

int main() {
  Dog d1;
  std::cout << d1.age;
  return 0;
}
medium
A. 5
B. 0
C. Compilation error
D. Random value

Solution

  1. Step 1: Understand constructor initialization

    The constructor sets age = 5; when the object d1 is created.
  2. Step 2: Check output statement

    The program prints d1.age, which is 5 as set by the constructor.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Constructor sets age to 5, output = 5 [OK]
Hint: Constructor sets values; printed value matches constructor assignment [OK]
Common Mistakes:
  • Assuming default int is zero without constructor
  • Thinking constructor is not called
  • Confusing uninitialized variables with constructor initialization
4. Identify the error in the following code that tries to create an object student1 of class Student:
class Student {
public:
  Student(int id) { studentId = id; }
  int studentId;
};

int main() {
  Student student1;
  return 0;
}
medium
A. Missing semicolon after class definition
B. No default constructor available for Student
C. studentId is not initialized
D. student1 is declared as a pointer but not initialized

Solution

  1. Step 1: Check constructor availability

    The class has only a constructor that requires an int parameter. No default (no-argument) constructor exists.
  2. Step 2: Analyze object creation

    Creating Student student1; calls default constructor, which is missing, causing a compilation error.
  3. Final Answer:

    No default constructor available for Student -> Option B
  4. Quick Check:

    Constructor needs int, no default constructor [OK]
Hint: If constructor needs arguments, default object creation fails [OK]
Common Mistakes:
  • Assuming compiler creates default constructor when custom exists
  • Ignoring constructor parameters
  • Confusing uninitialized variables with constructor errors
5. Given the class below, which code correctly creates two objects p1 and p2 with different starting values for x and y?
class Point {
public:
  int x, y;
  Point(int a, int b) { x = a; y = b; }
};
hard
A. Point p1(1, 2), p2(3, 4);
B. Point p1 = Point(1, 2); Point p2 = Point(3);
C. Point p1; Point p2(3, 4);
D. Point p1(1); Point p2(3, 4);

Solution

  1. Step 1: Understand constructor parameters

    The constructor requires two integers to initialize x and y.
  2. Step 2: Check each option for correct calls

    Point p1(1, 2), p2(3, 4); correctly creates both objects with two integers. Point p1 = Point(1, 2); Point p2 = Point(3); fails because second call has only one argument. Point p1; Point p2(3, 4); fails because p1 uses default constructor which does not exist. Point p1(1); Point p2(3, 4); fails because p1 has only one argument.
  3. Final Answer:

    Point p1(1, 2), p2(3, 4); -> Option A
  4. Quick Check:

    Constructor needs two ints, both objects must pass two ints [OK]
Hint: Pass all required constructor arguments for each object [OK]
Common Mistakes:
  • Calling constructor with wrong number of arguments
  • Trying to create object without required constructor
  • Assuming default constructor exists when it doesn't
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard