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. Inheritance

  2. Encapsulation

  3. Abstraction

  4. Procedural programming

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

Procedural programming is a programming methodology in which the code of a program is kept as concise as possible. Procedural programming is not suitable for coding complex problems because the code can become very complex as the program grows.

Multiple choice
  1. Public member of a class

  2. Private member of a class

  3. Protected member of a class

  4. Members of the child class

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

A friend function can only access the members of the class of which it is a friend. It does not have the power of accessing child class members. In inheritance, the derived class can access the members of the parent class, not the other way around.

Multiple choice
  1. When an object is used.

  2. When a class is declared.

  3. When an object is declared.

  4. When a class is used.

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

A constructor is a special function, which has the same name as of the class. It is called implictly when an object is declared. The user is not required to call it explicitly like a normal method. There are three types of constructors - default, parameterized and copy constructor. A class can have more than one constructor, which is called as 'constructor overloading'

Multiple choice
  1. All the methods must have different return values.

  2. All the methods must be declared in the base class and redeclared in the child class.

  3. All the methods must differ in arguments.

  4. The name of the arguments should be different.

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

To overload a method we must keep some difference in their arguments. The difference in the arguments can be either in their numbers, orders or datatypes. For example, we can have two methods with the name sum(), first with two integer arguments and second with three arguments. If the arguments are same in number, datatype or order the compiler will throw an ambiguity error which will mean that it is not able to differentitate one method with the other. Example: void sum(int); void sum(int,int); void sum(double, int);

Multiple choice
  1. A destructor can be declared only when a constructor has also been declared.

  2. We cannot overload destructors.

  3. It has the same name as of the class.

  4. During inheritance, the child class destructor is invoked first and then the parent class destructor.

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

Declaring a destructor does not need a constructor to be declared. A destructor can be declared even when there is no constructor.

Multiple choice
    • (Asterisk)
  1. ? (Question mark)

  2. : (Colon)

  3. :: (Scope resolution operator)

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

The (::) Scope Resolution Operator is used in C++ to define a class method, outside the class. Although the method is defined outside the class, it is still a member of the class. The scope resolution operator is used for some other purposes also. To define a method outside the class, we can use the code as given in the following example : class sample { int a=10; public:    void show(); }; void sample :: show() // defining the method outside the class. { cout<

Multiple choice
  1. private

  2. public

  3. protected

  4. friend

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

A class has two types of members- variables and methods. To specify the accessibility of a class member, we can use access specifiers. There are three types of access specifiers available in C++ - private, public and protected. If none of the these is specified, the members have by default 'private' accessibility. A private member can be accessed inside the class but cannot be accessed using a class object. A private member cannot be inherited by a derived class also.

Multiple choice
  1. The compiler will show an error when the code is compiled.

  2. The dervied class member will hide the defintion of the base class member.

  3. The base class member will be called even with the child class object.

  4. Only child class member will be called irrespective of the object.

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

If the derived class and the base class contain a member with the same name, it is called 'Member Overriding'. For example, there is a class named 'A', which has a function named 'show()'. If we declare a child class of 'A' named 'B' and declare a function with the same name show () in B, the show() declared in B will hide the show() declared in A. If an object of class B' makes a call of show() , the one defined in the child class(B) will be processed. If an object of the base class makes a call to show(), then the one defined in the base class will be called.

Multiple choice
  1. virtual class

  2. friend class

  3. abstract class

  4. derived class

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

An abstract class is a class that cannot be instantiated or in other words, it cannot be used to create an object. Since object declaration of such a class is not allowed, the only way to access its members is through inheritance. We can declare a child class of the abstract class and call the members of the class. To declare a class as abstract, we must declare a 'Pure Virtual Function' in it.

Multiple choice
  1. overloading

  2. overriding

  3. ambiguity

  4. destructor

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

Ambiguity is a state when there are two or more methods with the same prototype, the compile cannot differentiate between them and therefore displays an error. For example, if we have two functions called 'sum(int)' , the compiler will not be able to decide which method to call when the user writes a statement like 'sum(10)'. 

Multiple choice
  1. They are compulsory to be overloaded by the child class.

  2. Are optional to be overridden by the child class.

  3. A class containing a pure virtual function is just like a normal class.

  4. Cannot have arguments.

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

 A pure virtual function is a function that is declared as virtual in the base class and redeclared in the child class. In the base class, its declaration ends with the '=0' symbol. It cannot be defined in the base class rather all the child classes must override (provide definition) the pure virtual function as per their requirements. If a derived class fails to override the pure virtual function, the compiler displays an error.

Multiple choice
  1. It used for defining class methods, outside the class.

  2. It is used to inherit a base class to declare a derived class.

  3. It is used to help the compiler differentiate between a local and a global variable.

  4. It used to call overridden member of the base class from the derived class.

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

The scope resolution operator is not used to inherit a class. The syntax for inheritance has a colon (:), not a scope operator. For example, to inherit a class named 'B' from a class named 'A', the syntax will be class B : public A

Multiple choice
  1. A class must have a constructor and a destructor defined to make required variable initializations and to deallocate memory at the end.

  2. Both can be declared with arguments (parameterized)

  3. Both are invoked only once per object.

  4. The names of constructor and destructor can be anything.

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

The constructor and destructor of a class can be invoked (called) only once per object. An object of a class can call a normal method as many times as required but a constructor can be called just once. The same rule applies to the destructor also.

Multiple choice
  1. All objects of a class can have different values of the static member variable.

  2. A static method of a class can access only the static member variables.

  3. A static member variable cannot be accessed from a non static (normal) method.

  4. A class cannot have more than one static member.

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

A function declared using the static keyword can access only the static variable of a class. If a call to a non- static member variable is made in a static function, the compiler will return an error. The syntax to call a static function is  classname::functionname() A static function is not called using a class object but by using the class name along with the scope resolution operator.