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

Multiple choice
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. It is initialized to 0 when the first object is created.

  2. It is visible outside the class also.

  3. Every time an object is created, it is initialized to 1.

  4. None of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. Declaration of a friend function in private or public section makes a difference to its meaning.

  2. It lies outside the scope of a class to which it is a friend.

  3. Unlike member functions, it can't access member names directly.

  4. It can be invoked without the help of any object.

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. A

  2. B

  3. C

  4. None of these

Reveal answer Fill a bubble to check yourself
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 choice
  1. hierarchical inheritance

  2. multiple inheritance

  3. multilevel inheritance

  4. hybrid inheritance

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. 4

  2. 5

  3. 3

  4. None of the above

Reveal answer Fill a bubble to check yourself
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).

Multiple choice
  1. private

  2. public

  3. protected

  4. none of these

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. 6

  2. 5

  3. 4

  4. 3

  5. 2

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. 6

  2. 5

  3. 4

  4. 3

  5. 2

Reveal answer Fill a bubble to check yourself
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.