
This set tests the building blocks of object oriented programming: how classes define objects, the different kinds of constructors, copy and move semantics, destructors and the order in which objects are created and destroyed. The C++ and Java output questions mirror what appears in university semester exams, GATE-style reasoning and placement or interview rounds, so work through each trace before checking the answer.
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 18 questions.
Topics covered in this set
- Class vs object
- Default, parameterised and converting constructors
- Copy constructor, shallow vs deep copy
- Move constructor basics
- Destructors and order of destruction
- Object lifetime and garbage collection in Java
- Initialisation blocks and member initialisation order
Object Oriented Programming MCQs with answers and explanations
Q1. Which statement best describes the relationship between a class and an object?
a) A class is a blueprint that defines state and behaviour; an object is a concrete instance of it with its own state
b) An object is a template from which many classes are created at run time
c) A class and an object are the same thing, named differently in different languages
d) A class occupies memory for its data members, while an object only stores method addresses
Q2. What is printed by this C++ program?
#include <iostream>
using namespace std;
struct T {
int id;
T(int i) : id(i) { cout << "C" << id; }
~T() { cout << "D" << id; }
};
int main() {
T a(1);
{ T b(2); }
T c(3);
}
a) C1C2C3D3D2D1
b) C1C2D2C3D3D1
c) C1C2D2C3D1D3
d) C1C2C3D1D2D3
Q3. Why must the parameter of a C++ copy constructor be a reference (usually const T&) rather than a value?
a) Because references are faster than pointers for all types
b) Because the compiler cannot copy objects that contain private members
c) Because passing by value would itself need a copy constructor call, leading to infinite recursion
d) Because a value parameter would make the constructor static
Q4. What is printed by this C++ program?
#include <iostream>
using namespace std;
struct P {
P() { cout << "d"; }
P(const P&) { cout << "c"; }
};
void f(P x) {}
int main() {
P a;
P b = a;
f(b);
}
a) dc
b) ddc
c) dcc
d) dccc
Q5. When does a C++ compiler implicitly declare a default (no-argument) constructor for a class?
a) Always, even if the programmer writes other constructors
b) Only when the class has at least one virtual function
c) Only when every data member is public
d) Only when the programmer has not declared any constructor at all
Q6. In C++, which of the following situations does NOT invoke the copy constructor?
a) Initialising a new object from an existing one: Point q = p;
b) Passing an object by value to a function
c) Direct-initialising a new object: Point q(p);
d) Assigning one already-existing object to another: q = p;
Q7. A C++ class holds a raw pointer to a heap buffer and frees it in its destructor, but relies on the compiler-generated copy constructor. What is the most likely problem after copying an object?
a) Both objects point to the same buffer, so the buffer is deleted twice when they are destroyed
b) The copy gets a fresh buffer with garbage values
c) The program will not compile because pointers cannot be copied
d) The original object's pointer is set to nullptr automatically
Q8. What is printed by this C++ program?
#include <iostream>
using namespace std;
struct X {
static int n;
X() { ++n; }
~X() { --n; }
};
int X::n = 0;
int main() {
X a[3];
X* p = new X;
cout << X::n;
delete p;
cout << X::n;
}
a) 43
b) 44
c) 33
d) 40
Q9. Which statement about C++ destructors is correct?
a) A class may overload its destructor with different parameter lists
b) A class has exactly one destructor, and it takes no arguments and has no return type
c) A destructor must always be called explicitly by the programmer
d) A destructor runs before the constructor when an object is created
Q10. What is printed when this Java program runs?
class A {
A() { System.out.print("A"); }
{ System.out.print("I"); }
static { System.out.print("S"); }
}
public class Main {
public static void main(String[] args) {
new A();
new A();
}
}
a) SAISAI
b) SIASIA
c) SIAIA
d) ISAIA
Q11. What is printed by this C++ program?
#include <iostream>
using namespace std;
struct M {
int v;
M(int a = 7) : v(a) { cout << "M" << v; }
};
int main() {
M x;
M y(3);
M z = 5;
M w();
}
a) M7M3M5M7
b) M7M3M5
c) M7M3
d) Compilation error
Q12. A C++ class Date has only the constructor Date(int d = 1, int m = 1). Which statement is correct?
a) Date cannot be created without passing arguments
b) This constructor also acts as the default constructor of Date
c) The compiler additionally generates a separate no-argument constructor
d) The constructor must be declared explicit to compile
Q13. In Java, when does an object become eligible for garbage collection?
a) As soon as the method that created it returns, regardless of references
b) Only when System.gc() is called on it explicitly
c) When it is no longer reachable through any chain of references from live threads or static fields
d) When its finalize() method has been called by the programmer
Q14. In C++, data members of a class are initialised in which order?
a) The order they appear in the constructor's member initialiser list
b) Alphabetical order of member names
c) Reverse order of declaration
d) The order in which they are declared in the class, regardless of the initialiser list order
Q15. Java has no destructors. Which mechanism is the recommended way to release a resource such as a file handle deterministically?
a) A try-with-resources statement on an object implementing AutoCloseable
b) Overriding finalize() in the class
c) Setting the reference to null and calling System.gc()
d) Declaring the field as transient
Q16. What is the typical signature and purpose of a C++11 move constructor for class Buf?
a) Buf(const Buf&) - duplicates every heap resource of the source
b) Buf(Buf*) - copies only the pointer of the source object
c) Buf(Buf&&) - takes over the resources of a temporary or expiring object, leaving it in a valid empty state
d) Buf(const Buf&&) - makes a deep copy that cannot be modified
Q17. What is a common reason for declaring all constructors of a class private?
a) To make the class abstract so it must be inherited
b) To stop outside code creating instances directly, for example in utility classes or classes that create objects only through static factory methods
c) To allow the class to be instantiated without memory allocation
d) To force the compiler to generate a public copy constructor
Q18. What is printed by this Java program?
class Pt {
int x;
Pt(int x) { this.x = x; }
}
public class Main {
public static void main(String[] args) {
Pt p = new Pt(4);
Pt q = p;
q.x = 9;
Pt r = new Pt(9);
System.out.print(p.x + " " + (q == p) + " " + (r == p));
}
}
a) 4 true false
b) 9 true true
c) 4 false false
d) 9 true false
Quick revision notes
- A class is a blueprint; each object is an instance with its own copy of non-static data.
- Declaring any constructor stops the compiler from generating the default constructor.
- Copy constructor signature: T(const T&); taking T by value would recurse forever.
- Default copying is shallow; classes owning raw resources need the Rule of Three (or Five).
- Local objects are destroyed in reverse order of construction when their scope ends.
- Members are initialised in declaration order, not initialiser-list order.
- T x(); declares a function, not an object (most vexing parse).
- Java: static blocks run once per class; instance blocks run before each constructor body.
Frequently asked questions
What is the difference between a copy constructor and an assignment operator?
A copy constructor creates a new object from an existing one, while the copy assignment operator replaces the contents of an object that already exists. For example, T b = a; calls the copy constructor, but b = a; on an existing b calls operator=.
Can a constructor be virtual in C++?
No. A constructor builds the object, including its vtable pointer, so there is nothing to dispatch on yet. Destructors, however, can and often should be virtual in polymorphic base classes.
Does Java have destructors?
No. Memory is reclaimed by the garbage collector, and other resources should be released with close() through try-with-resources. The old finalize() method is deprecated and unreliable.
Comments
Post a Comment