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
A
Correct answer
Explanation
Virtual functions enable runtime polymorphism through dynamic binding. When a virtual function is overridden in derived classes, the correct function version is called based on the actual object type, not the pointer type. This allows different behaviors for the same function call across different classes in an inheritance hierarchy.
-
It is initialized to 0 when the first object is created.
-
It is visible outside the class also.
-
Every time an object is created, it is initialized to 1.
-
None of the above
A
Correct answer
Explanation
Static member variables are initialized to 0 when the first object of the class is created, and this initialization happens only once. They are shared among all objects of the class and retain their values between object creations. They are not re-initialized to 1 each time, and they are not visible outside the class unless declared public.
-
Declaration of a friend function in private or public section makes a difference to its meaning.
-
It lies outside the scope of a class to which it is a friend.
-
Unlike member functions, it can't access member names directly.
-
It can be invoked without the help of any object.
A
Correct answer
Explanation
Friend functions are declared inside a class but defined outside. They're not affected by public/private/protected sections - placement makes no difference to meaning. They're not member functions, so they need object references to access members, and can be called independently.
B
Correct answer
Explanation
A default constructor takes no parameters. Option B has star() with no parameters, making it the default constructor. Options A and C have constructors with parameters (parameterized constructors), not default constructors.
-
Multiple inheritance
-
Relationships
-
Data model
-
Directed acyclic graph
-
Non-leaf classes
E
Correct answer
Explanation
Non-leaf classes are abstract and can have further subclasses.
A
Correct answer
Explanation
Virtual functions in base classes enable runtime polymorphism through dynamic binding. Even if not directly used in the base class, they must be declared to establish the interface for derived classes to override.
-
hierarchical inheritance
-
multiple inheritance
-
multilevel inheritance
-
hybrid inheritance
C
Correct answer
Explanation
Multilevel inheritance occurs when a derived class itself becomes a base class for another class, forming a chain. Hierarchical inheritance has multiple derived classes from one base. Multiple inheritance means deriving from multiple base classes.
B
Correct answer
Explanation
Object pointers cannot directly access private members. Access specifiers apply regardless of whether you use an object or a pointer to an object. Private members are only accessible within member functions or friends.
A
Correct answer
Explanation
C++ has exactly four storage classes: auto (default for local variables), register (suggests CPU register storage), static (preserves value between function calls), and extern (for global/external linkage).
-
private
-
public
-
protected
-
none of these
B
Correct answer
Explanation
In C++, structures (struct) have public access by default, unlike classes where members are private by default. This is a fundamental distinction between struct and class. Protected is used for inheritance scenarios, and 'none of these' is incorrect since public is the default.
C
Correct answer
Explanation
This is the correct answer, as there are 4 constructors of HashTable Class in java collections framework. These constructors are Hashtable, Hashtable(int initialCapacity, Hashtable(int initialCapacity, float loadFactor)Hashtable(Map t).
C
Correct answer
Explanation
This is the correct answer. There are four constructors of TreeMap. These are :-TreeMap():- create an empty Map, TreeMap(Comparator comp):- The second form constructs an empty tree-based map that is sorted by using the Comparator comp, TreeMap(Map m):- The third form initialises a tree map with the entries from m, which is sorted by using the natural order of the keys, and TreeMap(SortedMap sm):- The fourth form initialises a tree map with the entries from sm, which is sorted in the same order as sm.
C
Correct answer
Explanation
This is correct, as there are 4 constructors of HashMap class. These are :-HashMap() :-
Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).
HashMap(int initialCapacity) :-
Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).
HashMap(int initialCapacity, float loadFactor) :-
Constructs an empty HashMap with the specified initial capacity and load factor.
HashMap(Map<? extends K,? extends V> m) :-
Constructs a new HashMap with the same mappings as the specified Map.
A
Correct answer
Explanation
The final keyword in Java prevents a class from being inherited (subclassed). When applied to a class, it makes the class non-inheritable, which is useful for security or design purposes when you want to prevent extension.