0
0
C++programming~20 mins

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

Choose your learning style9 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.