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. Friend function of a class cannot access private members of the given class.

  2. It is not mandatory to have a pure virtual function(s) in an abstract class.

  3. We can create object for an abstract class.

  4. If a class inherits abstract class and doesn't implement pure virtual functions of base class, then derived class also becomes abstract class.

  5. None of the above

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

This option is true because the derived class doesn't implement all pure virtual functions of the abstract class(base class) it turns to abstract class itself. Again we have to inherit the derived class to another derived class where we must implement all the pure virtual functions. If we don't implement at present derived class it also turn out to abstract class. Finally an abstract class should be followed by a class where all implementations of pure virtual functions are done completely.

Multiple choice
  1. Multiple inheritance

  2. Single or simple inheritance

  3. Hierarchical inheritance

  4. Multilevel inheritance

  5. None of the above

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Given code snippet contains single base class (A) nd three derived classes(B,C,D) from base class (A). It depicts hierarchical inheritance where a single base class is inherited by two or more derived classes. This option is true.

Multiple choice
  1. carries out, the details of an object for

  2. hides, the details of an object from

  3. reveals, the details of an object to

  4. extends, the details of an object beyond

  5. none of these

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

In object-oriented programming, encapsulation means an object hides its internal implementation details from the programmer, exposing only necessary interfaces through public methods. This information hiding is a core principle that protects data integrity and reduces complexity.

Multiple choice
  1. To avoid ambiguity

  2. For reusability

  3. For faster processing

  4. None of the above

  5. All 1, 2 and 3

Reveal answer Fill a bubble to check yourself
E Correct answer
Explanation

Virtual base class is used to reduce ambiguity, it cannot be used for faster processing and reusability.

Multiple choice
  1. Data is hidden and cannot be accessed by external functions.

  2. Follows bottom-up approach in program design.

  3. New data and functions cannot be added whenever necessary.

  4. Program is divided into what are known as objects.

  5. None of the above

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

New data and functions can be added whenever necessary. For e.g. for(int i=0;i<4;i++) is valid in C++.

Multiple choice
  1. Only a

  2. Both a and b

  3. All a, b and c

  4. Both are same

  5. Both b and c

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

In overriding both functions must have same signature or prototype. In overloading multiple functions have same name but different arguments like different number of arguments and different type of arguments.

Multiple choice
  1. Same function name with different number of arguments

  2. Same function name with different return type

  3. Same function name with different sequence of arguments

  4. Same function name with different types of arguments

  5. All of the above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Function overloading doesn’t uses the return type for identification of function, so option 2 is correct, e.g. int fun(int ,int);double fun(int,double); is not allowed in function overloading.

Multiple choice
  1. Constructor has same name as that of the class.

  2. They don’t return value and are not of type void.

  3. Constructor can be private, public or protected.

  4. A class can have more than one constructor.

  5. None of the above

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The constructors are always defined as public.

Multiple choice
  1. as decision making statement

  2. to achieve generic programming

  3. to reduce ambiguity

  4. to achieve polymorphism in C++

  5. none of the above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Templates enable us to define generic classes and functions and thus provide support for generic programming.

Multiple choice
  1. We can not use the object name(with dot operator), as runtime polymorphism is achieved only when a virtual function is accessed through a pointer to the base class.

  2. We need to use single pointer variable to refer to the objects of different classes.

  3. When function is made virtual, C++ determines which function to use at run time based on the type of object pointed by the base class.

  4. All the above.

  5. None of these

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The necessity of virtual function is given by all the above 3 options, so option 4 is the correct answer.

Multiple choice
  1. derived class

  2. base class

  3. produces compile time error

  4. produces runtime error

  5. none of the above

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Base b; Base *p; Derived d; p=&d; p->xyz(); as pointer is holding the address of derived class object and functions are defined virtual, derived class function xyz() is called.