Bird
Raised Fist0
C++programming~20 mins

Classes and objects in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Master of Classes and Objects
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of class method call
What is the output of this C++ program?
C++
#include <iostream>
class Counter {
public:
    int count = 0;
    void increment() { count++; }
    void print() { std::cout << count << std::endl; }
};

int main() {
    Counter c;
    c.increment();
    c.increment();
    c.print();
    return 0;
}
A0
B1
C2
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Think about how many times the increment method is called before printing.
❓ Predict Output
intermediate
2:00remaining
Value of object attribute after constructor
What will be the value of obj.x after this program runs?
C++
#include <iostream>
class MyClass {
public:
    int x;
    MyClass(int val) { x = val * 2; }
};

int main() {
    MyClass obj(5);
    std::cout << obj.x << std::endl;
    return 0;
}
A10
BCompilation error
C0
D5
Attempts:
2 left
πŸ’‘ Hint
Look at how the constructor sets the value of x.
πŸ”§ Debug
advanced
2:00remaining
Identify the error in this class definition
What error will this code produce when compiled?
C++
#include <iostream>
class Sample {
public:
    int value;
    Sample(int v) {
        value = v
    }
};

int main() {
    Sample s(10);
    std::cout << s.value << std::endl;
    return 0;
}
ARuntime error: uninitialized variable
BSyntax error: missing semicolon after assignment
CCompilation error: missing return statement in main
DNo error, outputs 10
Attempts:
2 left
πŸ’‘ Hint
Check punctuation inside the constructor.
❓ Predict Output
advanced
2:00remaining
Output of method modifying object state
What is the output of this program?
C++
#include <iostream>
class BankAccount {
    int balance;
public:
    BankAccount() : balance(100) {}
    void deposit(int amount) { balance += amount; }
    void withdraw(int amount) { balance -= amount; }
    int getBalance() { return balance; }
};

int main() {
    BankAccount acc;
    acc.deposit(50);
    acc.withdraw(30);
    std::cout << acc.getBalance() << std::endl;
    return 0;
}
A150
BCompilation error
C80
D120
Attempts:
2 left
πŸ’‘ Hint
Calculate balance after deposit and withdrawal.
🧠 Conceptual
expert
3:00remaining
Number of objects created and output
Consider this program. How many objects are created and what is the output?
C++
#include <iostream>
class Item {
public:
    static int count;
    Item() { count++; }
};

int Item::count = 0;

int main() {
    Item a;
    Item b;
    Item c = b;
    std::cout << Item::count << std::endl;
    return 0;
}
A3 objects created, output: 2
B2 objects created, output: 2
C3 objects created, output: 3
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Copy construction uses the implicit copy constructor, which does not execute the default constructor body.

Practice

(1/5)
1. What is the main purpose of a class in C++?
easy
A. To group data and functions that represent a real-world concept
B. To execute a program faster
C. To store only numbers
D. To replace variables with constants

Solution

  1. Step 1: Understand what a class represents

    A class groups related data (variables) and functions (methods) to model real-world things.
  2. Step 2: Compare options with the definition

    Only To group data and functions that represent a real-world concept correctly describes this purpose; others are unrelated to classes.
  3. Final Answer:

    To group data and functions that represent a real-world concept -> Option A
  4. Quick Check:

    Class = group data + functions [OK]
Hint: Classes bundle data and actions together [OK]
Common Mistakes:
  • Thinking classes only store numbers
  • Confusing classes with program speed
  • Believing classes replace variables
2. Which of the following is the correct way to declare a class named Car in C++?
easy
A. class Car { };
B. Car class { };
C. class = Car { };
D. class Car();

Solution

  1. Step 1: Recall C++ class declaration syntax

    The correct syntax is: class ClassName { }; with a semicolon at the end.
  2. Step 2: Check each option

    class Car { }; matches the correct syntax. Options A, C, and D have syntax errors or wrong order.
  3. Final Answer:

    class Car { }; -> Option A
  4. Quick Check:

    class ClassName { }; syntax [OK]
Hint: Remember: class name follows 'class' keyword, ends with semicolon [OK]
Common Mistakes:
  • Writing 'Car class' instead of 'class Car'
  • Missing semicolon after class body
  • Using parentheses after class name
3. What will be the output of this C++ code?
class Box {
public:
  int length;
  void setLength(int len) { length = len; }
  int getLength() { return length; }
};

int main() {
  Box box1;
  box1.setLength(10);
  std::cout << box1.getLength();
  return 0;
}
medium
A. length
B. 0
C. Compilation error
D. 10

Solution

  1. Step 1: Understand the class methods

    The method setLength sets the length to 10 for box1. The method getLength returns this value.
  2. Step 2: Trace the output statement

    std::cout << box1.getLength(); prints the value 10 stored in length.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    setLength(10) then getLength() = 10 [OK]
Hint: Setter sets value, getter returns it [OK]
Common Mistakes:
  • Forgetting to call setLength before getLength
  • Expecting default value 0 without initialization
  • Confusing method names
4. Identify the error in this class definition:
class Person {
  string name;
  int age;
public:
  void setName(string n) { name = n; }
  void setAge(int a) { age = a; }
};
medium
A. Missing semicolon after class definition
B. Missing public: before data members
C. Missing #include <string> header
D. Incorrect function return types

Solution

  1. Step 1: Check class syntax

    The class ends with a semicolon, and functions have correct return types (void). Data members are private by default, which is allowed.
  2. Step 2: Check for missing includes

    The code uses string but does not include <string> header, which is required for std::string.
  3. Final Answer:

    Missing #include <string> header -> Option C
  4. Quick Check:

    Use #include <string> for string type [OK]
Hint: Always include headers for standard types [OK]
Common Mistakes:
  • Assuming private members need public keyword
  • Forgetting to add semicolon after class
  • Confusing function return types
5. Given this class:
class Student {
  int id;
  string name;
public:
  Student(int i, string n) : id(i), name(n) {}
  void display() {
    std::cout << "ID: " << id << ", Name: " << name << std::endl;
  }
};

int main() {
  Student s1(101, "Alice");
  s1.display();
  return 0;
}

What concept does this code demonstrate?
hard
A. Inheritance between classes
B. Constructor initializing object data
C. Function overloading
D. Static class members

Solution

  1. Step 1: Identify the special function in the class

    The function Student(int i, string n) : id(i), name(n) {} is a constructor initializing the object's data members.
  2. Step 2: Check other options

    There is no inheritance, no function overloading, and no static members shown.
  3. Final Answer:

    Constructor initializing object data -> Option B
  4. Quick Check:

    Constructor sets initial values [OK]
Hint: Look for function named as class for constructor [OK]
Common Mistakes:
  • Confusing constructor with regular method
  • Thinking this shows inheritance
  • Assuming static members without keyword
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