Classes & Objects Question Bank for C-CAT
Topic-wise Classes & Objects MCQs for CDAC C-CAT preparation with answers and explanations.
Show Answer & Explanation
Correct Answer: D - private
In C++, class members are private by default.
Show Answer & Explanation
Correct Answer: B - public
In C++, struct members are public by default (unlike class).
Show Answer & Explanation
Correct Answer: B - Blueprint for objects
A class is a blueprint/template that defines properties and behaviors for objects.
Show Answer & Explanation
Correct Answer: B - Instance of class
An object is an instance of a class.
Show Answer & Explanation
Correct Answer: B - When object is created
Constructor is automatically called when an object is created.
Show Answer & Explanation
Correct Answer: C - ~ClassName
Destructor name is class name prefixed with tilde (~).
Show Answer & Explanation
Correct Answer: B - Constructor and Destructor
Both constructor and destructor have same name as class (destructor with ~).
Show Answer & Explanation
Correct Answer: C - Reference to same class object
Copy constructor takes a reference to object of same class.
Show Answer & Explanation
Correct Answer: B - All objects of class
Static members are shared by all objects of the class.
Show Answer & Explanation
Correct Answer: B - Private and protected members
Friend function can access private and protected members of the class.
Show Answer & Explanation
Correct Answer: D - private
In C++, class members are private by default, unlike struct members which are public.
Show Answer & Explanation
Correct Answer: B - Object is initialized from another object
Copy constructor is invoked when an object is initialized using another object of the same class.
Show Answer & Explanation
Correct Answer: C - :: scope resolution
Scope resolution (::), member access (.), sizeof, and ternary (?:) operators cannot be overloaded.
Show Answer & Explanation
Correct Answer: C - ~ tilde
Destructor is named with tilde (~) followed by class name, e.g., ~ClassName().
Show Answer & Explanation
Correct Answer: D - Address of current object
this pointer holds the address of the current object invoking the member function.
Show Answer & Explanation
Correct Answer: C - Unlimited
There is no limit on the number of objects that can be created from a class (limited only by memory).
Show Answer & Explanation
Correct Answer: D - Only static members
Static member functions can only access static data members as they have no this pointer.
Show Answer & Explanation
Correct Answer: B - Losing derived class data when assigned to base
Object slicing occurs when a derived class object is assigned to base class, losing derived-specific data.
Show Answer & Explanation
Correct Answer: C - Modification in const member function
Mutable members can be modified even inside const member functions.
Show Answer & Explanation
Correct Answer: D - Inside class definition
Functions defined inside the class body are implicitly inline functions.
Show Answer & Explanation
Correct Answer: B - private
In C++, the default access specifier for class members is private. This means members are accessible only within the class unless explicitly specified otherwise.
What will be the output of the following code?
class A {
int x;
public:
A() { x = 5; }
void show() { cout << x; }
};
int main() {
A obj;
obj.show();
}Show Answer & Explanation
Correct Answer: C - 5
The constructor A() initializes x to 5. When show() is called, it prints the value of x which is 5.
Show Answer & Explanation
Correct Answer: A - Constructors are called automatically when an object is created
Constructors are special member functions that are automatically invoked when an object is created. They cannot be virtual, cannot have a return type, and the compiler provides a default constructor if none is defined.
Show Answer & Explanation
Correct Answer: D - A function that is called when an object goes out of scope or is deleted
A destructor is a special member function with the same name as the class prefixed with a tilde (~). It is called automatically when an object goes out of scope or is explicitly deleted.
What will be the output?
class Box {
public:
int length;
Box() : length(10) {}
};
int main() {
Box b;
cout << b.length;
}Show Answer & Explanation
Correct Answer: A - 10
The constructor uses an initializer list to set length to 10. Since length is public, it can be accessed directly and prints 10.
Show Answer & Explanation
Correct Answer: D - Reference variable without initialization
A reference variable must be initialized when declared. Since class member references cannot be initialized at the point of declaration (prior to C++11 in-class initializers), they must be initialized in the constructor's initializer list.
Show Answer & Explanation
Correct Answer: A - To point to the current object of the class
The 'this' pointer is an implicit pointer available inside non-static member functions that points to the object for which the member function is called.
What will be the output?
class Test {
int x;
public:
Test(int x) { this->x = x; }
void display() { cout << x; }
};
int main() {
Test t(25);
t.display();
}Show Answer & Explanation
Correct Answer: A - 25
The 'this' pointer is used to distinguish between the member variable x and the parameter x. this->x refers to the member variable, which is set to 25.
How many times will the destructor be called in the following code?
class A {
public:
~A() { cout << "Dest "; }
};
int main() {
A a1, a2, a3;
}Show Answer & Explanation
Correct Answer: D - 3
Three objects a1, a2, and a3 are created, so the destructor will be called three times when they go out of scope at the end of main().
Show Answer & Explanation
Correct Answer: A - A constructor that creates an object by copying another object of the same class
A copy constructor creates a new object as a copy of an existing object of the same class. It takes a reference to the same class type as its parameter.
Show Answer & Explanation
Correct Answer: B - MyClass(MyClass &obj)
The copy constructor takes a reference (usually const reference) to the same class type: MyClass(const MyClass &obj). Passing by value would cause infinite recursion.
Show Answer & Explanation
Correct Answer: B - public
In C++, the default access specifier for struct members is public, unlike class members which default to private. This is the primary difference between struct and class in C++.
Show Answer & Explanation
Correct Answer: C - They can be called without an object
Static member functions can be called using the class name without creating an object. They do not have a 'this' pointer and cannot directly access non-static data members.
What will be the output?
class Counter {
static int count;
public:
Counter() { count++; }
static int getCount() { return count; }
};
int Counter::count = 0;
int main() {
Counter c1, c2, c3;
cout << Counter::getCount();
}Show Answer & Explanation
Correct Answer: C - 3
Each time a Counter object is created, the static variable count is incremented. After creating 3 objects, count becomes 3.
Show Answer & Explanation
Correct Answer: A - Compilation error
Accessing a private member from outside the class results in a compilation error. Private members can only be accessed within the class or by friend functions/classes.
Show Answer & Explanation
Correct Answer: A - A way to initialize member variables before the constructor body executes
An initializer list appears after the constructor's parameter list and before the constructor body, prefixed with a colon. It initializes members before the constructor body executes.
Show Answer & Explanation
Correct Answer: D - Const data members
Const data members and reference members must be initialized using the initializer list because they cannot be assigned values inside the constructor body.
What will be the output?
class A {
public:
A() { cout << "Con "; }
~A() { cout << "Des "; }
};
int main() {
A *p = new A();
delete p;
}Show Answer & Explanation
Correct Answer: A - Con Des
new A() calls the constructor printing 'Con ', and delete p calls the destructor printing 'Des '. So the output is 'Con Des'.
Show Answer & Explanation
Correct Answer: C - To define member functions outside the class
The scope resolution operator (::) is used to define member functions outside the class definition, access static members, and resolve scope ambiguity.
Show Answer & Explanation
Correct Answer: B - The compiler provides a default constructor
If no constructor is defined, the C++ compiler automatically provides a default constructor that initializes the object with default values (for built-in types, values may be indeterminate).
Show Answer & Explanation
Correct Answer: A - It takes at least one argument
A parameterized constructor accepts one or more arguments to initialize object members with specific values. It can coexist with other constructors (constructor overloading).
What will be the output?
class Demo {
public:
int x, y;
Demo(int a, int b) : x(a), y(b) {}
};
int main() {
Demo d(3, 7);
cout << d.x + d.y;
}Show Answer & Explanation
Correct Answer: D - 10
The parameterized constructor initializes x = 3 and y = 7. d.x + d.y = 3 + 7 = 10.
Show Answer & Explanation
Correct Answer: C - It guarantees not to modify the object's state
A const member function (declared with const after the parameter list) promises not to modify any data members of the object. It can be called on both const and non-const objects.
What will be the output?
class A {
public:
A() { cout << "1"; }
A(const A&) { cout << "2"; }
};
int main() {
A a;
A b = a;
}Show Answer & Explanation
Correct Answer: A - 12
A a; calls the default constructor printing '1'. A b = a; calls the copy constructor printing '2'. Output is '12'.
Show Answer & Explanation
Correct Answer: D - Allows a data member to be modified even in a const member function
The mutable keyword allows a class data member to be modified even inside a const member function, overriding the const restriction for that particular member.
Show Answer & Explanation
Correct Answer: A - By value
In C++, objects are passed by value by default, which invokes the copy constructor. To avoid this overhead, objects should be passed by reference or const reference.
What will be the output?
class Point {
public:
int x, y;
Point() : x(0), y(0) {}
};
int main() {
Point p;
cout << p.x << " " << p.y;
}Show Answer & Explanation
Correct Answer: D - 0 0
The default constructor initializes both x and y to 0. So the output is '0 0'.
Show Answer & Explanation
Correct Answer: A - They cannot be overloaded
Destructors cannot be overloaded because a class can have only one destructor. Destructors take no parameters and have no return type.
Show Answer & Explanation
Correct Answer: A - Allocates memory on the heap and calls the constructor
The 'new' operator allocates memory on the heap (free store) and calls the constructor of the object. It returns a pointer to the allocated memory.
What will be the output?
class Singleton {
static Singleton* instance;
Singleton() {}
public:
static Singleton* getInstance() {
if (!instance) instance = new Singleton();
return instance;
}
};
Singleton* Singleton::instance = nullptr;
int main() {
Singleton* s1 = Singleton::getInstance();
Singleton* s2 = Singleton::getInstance();
cout << (s1 == s2);
}Show Answer & Explanation
Correct Answer: C - 1
This is the Singleton design pattern. getInstance() creates only one instance and returns it for all subsequent calls. So s1 and s2 point to the same object, making s1 == s2 true (1).