Sample questions
Question 1Easy
What is the output on typical compilers such as GCC, Clang or MSVC?
#include <iostream>
using namespace std;
struct Empty {};
int main() {
cout << sizeof(Empty);
return 0;
}
- 1
- 4
- Compilation error
- 0
Answer: A. 1
Explanation
An object of an empty class still needs a distinct address, so its size is non-zero; mainstream compilers make it 1 byte.
Step-by-step solution
- Sizeof gives the size of a type in bytes.
- Every object needs a unique address, so an empty class still has a size of at least 1 byte.
- GCC, Clang and MSVC give 1, so the output is 1: option A.
Why the other options are wrong
- B. The size is 1 byte, not 4; no padding is added to an empty class.
- C. The code compiles, since sizeof on a complete type is valid.
- D. An object of an empty class is never size 0 in standard C++.
Tip: An empty class has size 1 in C++, not 0.
Question 2Easy
What happens when this program is compiled?
#include <iostream>
using namespace std;
class Box {
int w = 3;
};
int main() {
Box b;
cout << b.w;
return 0;
}
- It prints 0
- It prints a garbage value
- It prints 3
- Compilation error: 'w' is private
Answer: D. Compilation error: 'w' is private
Explanation
Members of a class are private by default, so main cannot access b.w. With struct instead of class the member would be public and 3 would be printed.
Step-by-step solution
- Members of a class are private by default.
- The member w has no public label, so it is private.
- Main is outside the class and reads b.w, which is not allowed.
- The compiler reports an error, so option D is right.
Why the other options are wrong
- A. Nothing is printed, because the program does not compile.
- B. No garbage value appears; the code does not compile at all.
- C. 3 would print only if w were public, as it would be in a struct.
Tip: In a class, members are private unless marked public; in a struct they are public by default.
Question 3Easy
What is the output?
#include <iostream>
using namespace std;
class Item {
public:
int qty = 5;
Item() {}
Item(int q) : qty(q) {}
};
int main() {
Item a;
Item b(7);
cout << a.qty << " " << b.qty;
return 0;
}
- 5 5
- 7 7
- 5 7
- 0 7
Answer: C. 5 7
Explanation
The default member initialiser (5) is used when a constructor does not mention qty; a member-initialiser-list entry overrides it.
Step-by-step solution
- A uses the default constructor, which does not set qty, so the default member initialiser 5 applies.
- B uses Item(int q), whose initialiser list sets qty to 7.
- Cout prints 5 for a and 7 for b.
- The answer is option C: 5 7.
Why the other options are wrong
- A. B uses the constructor with argument 7, so its qty is 7.
- B. A does not get argument 7; its qty is 5.
- D. 0 is not used; the default member initialiser gives 5.
Tip: An initialiser-list entry overrides the default member initialiser for that object.
Question 4Easy
What is the output?
#include <iostream>
using namespace std;
class Student {
public:
static int count;
Student() { count++; }
};
int Student::count = 0;
int main() {
Student a, b;
Student c;
cout << Student::count << " " << a.count;
return 0;
}
- 1 1
- 3 3
- 3 1
- 0 3
Answer: B. 3 3
Explanation
A static data member is shared by all objects; three constructions increment the single counter to 3, and a.count names the same variable.
Step-by-step solution
- count is static and shared by all objects.
- Three objects are created, so count is 3.
- Both expressions read the same variable: 3 3.
Why the other options are wrong
- A. count is not per object.
- C. a.count is the same shared variable.
- D. count was incremented three times.
Tip: Static = one copy for the class.
Question 5Medium
Data members is also called...?
- Attribute
- Method
- Class
- Object
Answer: A. Attribute
Explanation
the attribute is show the properties of the Entity.
Same hear the member data of a class is shows the properties of the class.
Step-by-step solution
- Data members are variables declared inside a class to hold its state.
- In object-oriented terms these are called attributes.
- A method is a function in a class, a class is the blueprint, and an object is an instance.
- So the answer is Attribute.
Why the other options are wrong
- B. A method is a function in a class, not a data member.
- C. A class is the blueprint that holds data members, not a data member itself.
- D. An object is an instance created from a class.
Tip: Attribute means data, and method means function.
Question 6Medium
What is the output?
#include <iostream>
using namespace std;
struct A {
void f() { cout << "N"; }
void f() const { cout << "C"; }
};
int main() {
A a;
const A b{};
a.f();
b.f();
return 0;
}
- NN
- NC
- CC
- Compilation error
Answer: B. NC
Explanation
Member functions can be overloaded on const: the non-const object picks the non-const overload and the const object can only call the const one.
Step-by-step solution
- The struct has two overloads of f: one non-const and one const.
- A is non-const, so a.f() picks the non-const overload and prints N.
- B is const, so b.f() picks the const overload and prints C.
- The output is NC: option B.
Why the other options are wrong
- A. B is const, so it cannot call the non-const overload.
- C. A is not const, so it picks the non-const overload, which prints N.
- D. Overloading on const is allowed, so the code compiles.
Tip: Member functions can be overloaded on const; the constness of the object picks the overload.
Question 7Medium
What is the output?
#include <iostream>
using namespace std;
struct S {
int a;
static int b;
};
int S::b = 7;
int main() {
cout << (sizeof(S) == sizeof(int));
return 0;
}
- 0
- Compilation error
- 8
- 1
Answer: D. 1
Explanation
Static data members are not stored inside each object, so S contains only a and its size equals sizeof(int).
Step-by-step solution
- The static member b is stored once, outside every object, so it does not add to sizeof(S).
- S holds only int a, so sizeof(S) equals sizeof(int).
- The comparison is true, and cout prints a bool true as 1.
- The output is 1: option D.
Why the other options are wrong
- A. The comparison is true, so the output is 1, not 0.
- B. The code compiles; a struct can have a static data member.
- C. 8 would be a size, but the output is the bool result, which is 1.
Tip: Static data members take no space inside each object.
Question 8Medium
When we are using heap operations what do we need to do to save the memory?
- rename the objects
- delete the objects after processing
- both a & b
- none of the mentioned
Answer: B. delete the objects after processing
Explanation
when you allocate memory from the heap, you must remember to clean up objects when you are done!Failure to do so is called a memory leak.
Step-by-step solution
- Heap memory needs manual cleanup after use.
- Deleting objects frees up memory space.
- Not deleting leads to memory leaks.
- So, delete the objects after processing.
- Final answer: B (delete the objects after processing).
Why the other options are wrong
- A. Renaming does not free memory.
- C. Only deleting is needed, renaming is irrelevant.
- D. Deleting is required, so this is incorrect.
Tip: Always delete heap-allocated objects when done.
Question 9Hard
The setter below does not change the object. Which replacement for the body of set fixes it?
#include <iostream>
using namespace std;
class Temp {
int value = 0;
public:
void set(int value) { int v = value; (void)v; }
int get() const { return value; }
};
int main() {
Temp t;
t.set(42);
cout << t.get();
return 0;
}
this->value = value;value = value;Temp::value == value;int value = this->value;
Answer: A. this->value = value;
Explanation
The parameter hides the member, so inside set the plain name value means the parameter. this->value (or Temp::value) names the member explicitly; value = value just assigns the parameter to itself.
Step-by-step solution
- Inside set, the plain name value refers to the parameter, not the member.
- This->value names the member explicitly, so assigning to it changes the object.
- This->value = value; stores the parameter in the member, so t.get() returns 42.
- So the fix is option A.
Why the other options are wrong
- B. This assigns the parameter to itself, so the member is not changed.
- C. This compares the two values and does nothing; it is not an assignment.
- D. This declares a new local variable, so the member stays 0.
Tip: Use this-> when a parameter has the same name as a member.
Question 10Hard
Since C++17, how can a non-const static data member be both declared and defined inside the class body?
- Declare it
constexpr int count = 0; without static - Declare it
inline, for example static inline int count = 0; - Mark the class itself as
inline - It is impossible; a definition outside the class is always required
Answer: B. Declare it inline, for example static inline int count = 0;
Explanation
C++17 inline variables let a static data member be defined in the class, so no separate out-of-class definition is needed in one .cpp file.
Step-by-step solution
- Since C++17, inline variables let a static data member be defined inside the class.
- Static inline int count = 0; inside the class is both a declaration and a definition.
- No definition outside the class is needed.
- So option B is the correct way.
Why the other options are wrong
- A. Constexpr without static makes a non-static member, not a static data member.
- C. Marking the class inline does not make its static data members inline.
- D. This is possible since C++17, so the claim that it is impossible is false.
Tip: From C++17, static inline lets a static data member be defined in the class body.
Practise all 35 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →