Computer Knowledge
Object-Oriented Programming
2,686 Questions
Object-oriented programming questions test core computer science concepts like classes, inheritance, polymorphism, and encapsulation. The focus includes Java program structures, method overloading, and memory allocation for objects. This topic is essential for technical sections in various recruitment tests.
Java class definitionsMethod overriding rulesPolymorphism conceptsGeneric type parametersMemory allocation in objects
Object-Oriented Programming Questions
-
virtual destructor
-
virtual constructor
-
Both A and B
-
None of the Above
A
Correct answer
Explanation
C++ supports virtual destructors but not virtual constructors. Virtual destructors ensure proper cleanup when deleting derived class objects through base class pointers. Virtual constructors don't exist because you cannot construct an object without knowing its concrete type at compile time - that contradicts the purpose of constructors.
-
Explicit Instantiation
-
Implicit instantiation
-
Both A and B
-
None of the Above
C
Correct answer
Explanation
C++ templates can be instantiated both explicitly (using template<> declaration) and implicitly (when the compiler deduces template arguments from usage). Both mechanisms are valid and commonly used in C++ programming, making option C the correct answer.
-
Extraction Operator
-
Insertion Operator
-
Scope resolution operator
-
None of the Above
C
Correct answer
Explanation
The scope resolution operator (::) is used to define member functions outside the class definition, specifying which class the function belongs to. The extraction (>>) and insertion (<<) operators are used for I/O streams, not for defining class members.
-
Virtual Function
-
Namespaces
-
copy constructor
-
None of the Above
C
Correct answer
Explanation
A copy constructor is a special constructor in C++ used to initialize a newly declared object using an existing object of the same class. Virtual functions enable polymorphism, and namespaces organize code to prevent name collisions.
-
ostream
-
ifstream
-
strstream
-
None of the Above
C
Correct answer
Explanation
The strstream class in C++ is specifically designed for reading and writing to an array in memory. It implements stream operations on character arrays stored in memory. Ostream is for output streams only, ifstream is for input file streams, so strstream is the correct answer for in-memory array I/O operations.
B
Correct answer
Explanation
A copy constructor must accept a reference to an object of the same class, not the object itself. If it accepted an object by value, it would need to call the copy constructor to make the copy, creating infinite recursion. Therefore, copy constructors always take a reference parameter.
-
Extraction Operator
-
Insertion Operator
-
Scope resolution operator
-
None of the Above
C
Correct answer
Explanation
The scope resolution operator (::) is used to define member functions outside the class definition. It connects the function name to its class scope, such as ClassName::memberFunction. The extraction operator (>>) is for input, insertion operator (<<) is for output - neither is used for defining methods outside classes.
-
Virtual Function
-
Namespaces
-
copy constructor
-
None of the Above
C
Correct answer
Explanation
A copy constructor is a special constructor that creates a new object as a copy of an existing object. Virtual functions are for runtime polymorphism, namespaces organize code, and this is specifically about initialization from existing variables.
-
Virtual Function
-
Namespaces
-
copy constructor
-
None of the Above
C
Correct answer
Explanation
A copy constructor is a special constructor that initializes a new object as a copy of an existing object of the same class. It takes a reference to an object of the same class as its parameter and creates a new object with identical values. Virtual functions and namespaces are unrelated to variable initialization.
-
virtual destructor
-
virtual constructor
-
Both A and B
-
None of the Above
A
Correct answer
Explanation
C++ supports virtual destructors but not virtual constructors. Virtual destructors ensure proper cleanup when deleting derived class objects through base class pointers. Virtual constructors don't exist because you cannot construct an object without knowing its exact type at compile time - construction requires the complete type information.
-
Explicit Instantiation
-
Implicit instantiation
-
Both A and B
-
None of the Above
C
Correct answer
Explanation
Templates can be instantiated both explicitly and implicitly. Implicit instantiation occurs when you use a template with specific types and the compiler generates the code automatically. Explicit instantiation happens when you explicitly tell the compiler to instantiate a template with specific types using template<> syntax.
-
Extraction Operator
-
Insertion Operator
-
Scope resolution operator
-
None of the Above
C
Correct answer
Explanation
The scope resolution operator (::) is used in C++ to define a member function outside of its class declaration. Insertion (<<) and extraction (>>) operators are used for stream I/O, not for specifying class scope.
-
ostream
-
ifstream
-
strstream
-
None of the Above
C
Correct answer
Explanation
In C++, the strstream class (from `) provides stream input and output operations on character arrays in memory.ostreamis for general output streams, andifstream` is specifically for reading from files on disk.
B
Correct answer
Explanation
A copy constructor cannot accept an object of the same class by value because doing so would require calling the copy constructor itself to pass the parameter, leading to infinite recursion. Thus, it must accept a reference to the object.
-
(B), (C) & (E)
-
(B), (C), (E) & (F)
-
B), (E) & (F)
-
(A), (B), (C), (E) & (F)
B
Correct answer
Explanation
The java.lang package contains fundamental classes that are automatically imported. Object, Math, and String are in java.lang, as is StringBuffer. Stack, Vector, and Random are in other packages (java.util). Option B correctly identifies all four classes that belong to java.lang: (B) Object, (C) Math, (E) String, and (F) StringBuffer.