
If you are preparing for a C++ interview or a university exam, the basics of classes come up every time. This first C++ set covers access specifiers, constructors and destructors, initializer lists, static members and references - with small code snippets to trace by hand. Assume a standard-conforming compiler and that iostream is included with using namespace std.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 15 questions.
Q1. By default, members of a C++ class are:
a) public
b) private
c) protected
d) It depends on the compiler
Q2. What is the only real difference between a struct and a class in C++?
a) A struct cannot have member functions
b) Default member access and default inheritance are public for struct, private for class
c) A struct cannot have constructors
d) A struct cannot be inherited from
Q3. What is printed?
class A {
public:
A() { cout << "C"; }
~A() { cout << "D"; }
};
int main() { A a; A b; }
a) CDCD
b) CCDD
c) CC
d) DDCC
Q4. Given an existing object a of class A, which special member function does the statement A b = a; call?
a) Default constructor
b) Copy constructor
c) Copy assignment operator
d) Move assignment operator
Q5. What happens when this code is compiled?
class A {
public:
A(int x) {}
};
int main() { A a; }
a) It compiles and a is zero-initialised
b) Compilation error: no default constructor
c) Runtime error
d) It compiles with a warning only
Q6. Which statement about C++ references is true?
a) A reference can be null
b) A reference can be re-bound to refer to another object
c) A reference must be initialised when it is declared
d) You must use * to read the value through a reference
Q7. What is printed?
int x = 5; int &r = x; r++; cout << x;
a) 5
b) 6
c) Address of x
d) Compilation error
Q8. Which kind of data member MUST be initialised in the constructor's member initializer list?
a) A static int member
b) A const int member
c) A plain int member
d) An int* member
Q9. What does x hold after the constructor runs?
class A {
int x;
int y;
public:
A() : y(5), x(y) {}
};
a) 5
b) 0
c) An indeterminate (garbage) value
d) Compilation error
Q10. A static data member of a class is:
a) Created separately for every object
b) Shared by all objects of the class
c) Only accessible from static member functions
d) Stored on the stack
Q11. What is printed?
class A {
public:
static int count;
A() { count++; }
};
int A::count = 0;
int main() {
A a, b, arr[3];
cout << A::count;
}
a) 2
b) 3
c) 5
d) 0
Q12. What is the purpose of the explicit keyword on a constructor?
a) It makes the constructor public
b) It prevents the constructor from being used for implicit conversions
c) It forces the constructor to be inlined
d) It makes the constructor run at compile time
Q13. Which statement about destructors is true?
a) A class can have several overloaded destructors
b) A destructor takes no arguments and has no return type
c) A destructor can be declared static
d) A destructor must be called manually for stack objects
Q14. Inside a non-static member function, what does the this pointer point to?
a) The class definition
b) The object on which the member function was called
c) The first data member only
d) The base class
Q15. A class owns a raw pointer to heap memory and frees it in its destructor. Why should it define its own copy constructor?
a) To make copying faster
b) The default copy is shallow, so two objects would free the same memory (double delete)
c) Because the default copy constructor is deleted for such classes
d) To allow the class to be inherited
Recommended MCQs
C Programming MCQs - Pointers and Dynamic Memory (Set 1)
Comments
Post a Comment