Back to Practice OOP with C++

Classes & Objects - Practice MCQs for CCAT

50 Questions Section B: Programming OOP with C++

Classes & Objects Question Bank for C-CAT

Topic-wise Classes & Objects MCQs for CDAC C-CAT preparation with answers and explanations.

Q1.
What is the default access specifier for class members in C++?
Apublic
Bfriend
Cprotected
Dprivate
Show Answer & Explanation

Correct Answer: D - private

In C++, class members are private by default.

Q2.
What is the default access specifier for struct members in C++?
Aprivate
Bpublic
Cprotected
DNone
Show Answer & Explanation

Correct Answer: B - public

In C++, struct members are public by default (unlike class).

Q3.
A class is a:
AVariable
BBlueprint for objects
CFunction
DData type only
Show Answer & Explanation

Correct Answer: B - Blueprint for objects

A class is a blueprint/template that defines properties and behaviors for objects.

Q4.
Object is also called:
AClass
BInstance of class
CFunction
DMethod
Show Answer & Explanation

Correct Answer: B - Instance of class

An object is an instance of a class.

Q5.
Constructor is called:
AWhen object is destroyed
BWhen object is created
CManually by user
DNever automatically
Show Answer & Explanation

Correct Answer: B - When object is created

Constructor is automatically called when an object is created.

Q6.
Destructor name is:
Adelete ClassName
BClassName~
C~ClassName
D-ClassName
Show Answer & Explanation

Correct Answer: C - ~ClassName

Destructor name is class name prefixed with tilde (~).

Q7.
Which can have same name as class?
AAny function
BConstructor and Destructor
CDestructor only
DConstructor only
Show Answer & Explanation

Correct Answer: B - Constructor and Destructor

Both constructor and destructor have same name as class (destructor with ~).

Q8.
Copy constructor takes:
ANo argument
BPointer
CReference to same class object
DInteger
Show Answer & Explanation

Correct Answer: C - Reference to same class object

Copy constructor takes a reference to object of same class.

Q9.
Static member variable is shared by:
AOne object
BAll objects of class
CNo object
DDerived class only
Show Answer & Explanation

Correct Answer: B - All objects of class

Static members are shared by all objects of the class.

Q10.
Friend function can access:
AOnly public members
BPrivate and protected members
COnly protected
DNothing
Show Answer & Explanation

Correct Answer: B - Private and protected members

Friend function can access private and protected members of the class.

Q11.
What is the default access specifier for class members in C++?
Apublic
Binternal
Cprotected
Dprivate
Show Answer & Explanation

Correct Answer: D - private

In C++, class members are private by default, unlike struct members which are public.

Q12.
Copy constructor is called when:
AObject is assigned
BObject is initialized from another object
CObject is destroyed
DObject is created with new
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.

Q13.
Which operator cannot be overloaded?
A+ operator
B= assignment
C:: scope resolution
D[] subscript
Show Answer & Explanation

Correct Answer: C - :: scope resolution

Scope resolution (::), member access (.), sizeof, and ternary (?:) operators cannot be overloaded.

Q14.
Destructor name is prefixed with:
A@ symbol
B# hash
C~ tilde
D$ dollar
Show Answer & Explanation

Correct Answer: C - ~ tilde

Destructor is named with tilde (~) followed by class name, e.g., ~ClassName().

Q15.
this pointer contains:
AAddress of main function
BAddress of base class
CNULL always
DAddress of current object
Show Answer & Explanation

Correct Answer: D - Address of current object

this pointer holds the address of the current object invoking the member function.

Q16.
How many objects can be created from a class?
AOnly one
BOnly two
CUnlimited
DDepends on compiler
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).

Q17.
Static member functions can access:
ANo members
BOnly non-static members
CBoth static and non-static
DOnly static members
Show Answer & Explanation

Correct Answer: D - Only static members

Static member functions can only access static data members as they have no this pointer.

Q18.
What is object slicing?
ADividing object memory
BLosing derived class data when assigned to base
CSplitting object into parts
DMemory optimization
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.

Q19.
Mutable keyword allows:
AMaking variable constant
BCreating global variable
CModification in const member function
DThread safety
Show Answer & Explanation

Correct Answer: C - Modification in const member function

Mutable members can be modified even inside const member functions.

Q20.
Inline member functions are defined:
AOutside class only
BUsing extern keyword
CIn header file only
DInside class definition
Show Answer & Explanation

Correct Answer: D - Inside class definition

Functions defined inside the class body are implicitly inline functions.

Q21.
What is the default access specifier for members of a class in C++?
Apublic
Bprivate
Cprotected
Dfriend
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.

Q22.

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();
}
A0
BGarbage value
C5
DCompilation error
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.

Q23.
Which of the following is true about constructors in C++?
AConstructors are called automatically when an object is created
BConstructors can have a return type
CConstructors can be virtual
DConstructors must always be defined explicitly
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.

Q24.
What is a destructor in C++?
AA function that creates objects
BA function that initializes static members
CA function that copies objects
DA function that is called when an object goes out of scope or is deleted
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.

Q25.

What will be the output?

class Box {
public:
  int length;
  Box() : length(10) {}
};
int main() {
  Box b;
  cout << b.length;
}
A10
B0
CGarbage value
DCompilation error
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.

Q26.
Which of the following cannot be a member of a C++ class?
AStatic function
BVirtual function
CConstant function
DReference variable without initialization
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.

Q27.
What is the purpose of the 'this' pointer in C++?
ATo point to the current object of the class
BTo point to the base class
CTo point to the derived class
DTo access static members
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.

Q28.

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();
}
A25
B0
CGarbage value
DCompilation error
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.

Q29.

How many times will the destructor be called in the following code?

class A {
public:
  ~A() { cout << "Dest "; }
};
int main() {
  A a1, a2, a3;
}
A1
B2
C0
D3
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().

Q30.
What is a copy constructor?
AA constructor that creates an object by copying another object of the same class
BA constructor that copies data from one class to another
CA constructor that copies static members
DA constructor that copies only public members
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.

Q31.
What is the correct syntax for a copy constructor of class MyClass?
AMyClass(MyClass obj)
BMyClass(MyClass &obj)
CMyClass(MyClass *obj)
DMyClass(const MyClass obj)
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.

Q32.
What is the default access specifier for members of a struct in C++?
Aprivate
Bpublic
Cprotected
DNo default; must be specified
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++.

Q33.
Which of the following statements about static member functions is correct?
AThey can access non-static data members directly
BThey have a 'this' pointer
CThey can be called without an object
DThey cannot be declared public
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.

Q34.

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();
}
A0
B1
C3
DCompilation error
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.

Q35.
What happens when you try to access a private member of a class from outside the class?
ACompilation error
BRuntime error
CIt returns 0
DIt returns garbage value
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.

Q36.
What is an initializer list in C++?
AA way to initialize member variables before the constructor body executes
BA list of variables declared inside main()
CA list of all constructors in a class
DA list of objects created from a class
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.

Q37.
Which member must be initialized using an initializer list?
APointer members
BStatic members
CPublic data members
DConst data members
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.

Q38.

What will be the output?

class A {
public:
  A() { cout << "Con "; }
  ~A() { cout << "Des "; }
};
int main() {
  A *p = new A();
  delete p;
}
ACon Des
BDes
CCon
DNo output
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'.

Q39.
What is the scope resolution operator (::) used for in C++?
ATo delete objects
BTo create new objects
CTo define member functions outside the class
DTo copy objects
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.

Q40.
What will happen if a class has no constructor defined?
ACompilation error
BThe compiler provides a default constructor
CObjects cannot be created
DRuntime error
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).

Q41.
Which of the following is true about a parameterized constructor?
AIt takes at least one argument
BIt cannot have default arguments
CIt cannot coexist with a default constructor
DIt is called only with the new operator
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).

Q42.

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;
}
A37
BCompilation error
C0
D10
Show Answer & Explanation

Correct Answer: D - 10

The parameterized constructor initializes x = 3 and y = 7. d.x + d.y = 3 + 7 = 10.

Q43.
What is the role of a 'const' member function?
AIt can modify data members
BIt can only be called on const objects
CIt guarantees not to modify the object's state
DIt initializes constant members
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.

Q44.

What will be the output?

class A {
public:
  A() { cout << "1"; }
  A(const A&) { cout << "2"; }
};
int main() {
  A a;
  A b = a;
}
A12
B1
C11
D22
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'.

Q45.
What does the 'mutable' keyword do in C++?
AMakes a variable constant
BPrevents inheritance
CMakes a function virtual
DAllows a data member to be modified even in a const member function
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.

Q46.
How is a class object passed by default in C++ function arguments?
ABy value
BBy pointer
CBy reference
DBy address
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.

Q47.

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;
}
A0 1
BGarbage values
CCompilation error
D0 0
Show Answer & Explanation

Correct Answer: D - 0 0

The default constructor initializes both x and y to 0. So the output is '0 0'.

Q48.
Which of the following is true about destructors?
AThey cannot be overloaded
BThey can accept parameters
CThey can be overloaded
DThey can return values
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.

Q49.
What does 'new' operator do in C++?
AAllocates memory on the heap and calls the constructor
BAllocates memory on the stack
COnly allocates memory without calling constructor
DCreates a static object
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.

Q50.

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);
}
A0
BCompilation error
C1
DRuntime error
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).

Showing 1-10 of 50 questions