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. using System;using System.Collections.Generic;class A{ public void Demo() { Console.WriteLine(The Demo is called); }} class Test { static void Main(string[] args) { A a=new A(); a.Demo(); } }

  2. using System;using System.Collections.Generic;class A{ protected void Demo() { Console.WriteLine(The Demo is called); }} class Test { static void Main(string[] args) { A a=new A(); a.Demo(); } }

  3. using System;using System.Collections.Generic;class A{ protected void Demo() { Console.WriteLine(The Demo is called);}}class B:A { public B() { Demo(); } static void Main(string[] args) { B b=new B(); } }

  4. none of the above

  5. both (2) and (3)

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

This code will give compile error protected members can be accesses either in the same class or in the base class of that class. These protected members cannot be accessed outside the class.

Multiple choice
  1. Protected members can not be accessed in the subclass.

  2. Base class can not be inherited.

  3. Base can class can not be a type of sealed.

  4. The code will produce exception.

  5. No error

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

Yes, class A is of sealed type and we can not inherit a sealed class.

Multiple choice
  1. The Demo in base class

  2. The Demo in child class

  3. Compilation error.

  4. The code will compile properly but will not produce any output.

  5. None of the above

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

Yes, the code will give compilation error as there is 'virtual' keyword missing in the method of the base class, hence it will not be inherited.

Multiple choice
  1. It will print the method of the base class.

  2. It will print the method of the child class.

  3. It will not compile because base class method is not marked as virtual.

  4. It will not compile because child class method is not marked as override.

  5. None of the above

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

Yes, the base class method is called as we used the new keyword in the child class, so the method can not be override. Hence the method of the B class would not be called.

Multiple choice
  1. Open recursion

  2. Inheritance

  3. Encapsulation

  4. Class based programming

  5. Dynamic dispatch

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

Too many functions or external classes declared as friends of a class with protected or private data may lessen the value of encapsulation of separate classes in object-oriented programming and may indicate a problem in the overall architecture design.

Multiple choice
  1. Namespace

  2. Multiple inheritance

  3. Template

  4. Operator overloading

  5. Exception handling

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

In object-oriented programming, operator overloading—less commonly known as operator ad-hoc polymorphism—is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading is generally defined by the language, the programmer, or both.

Multiple choice
  1. default

  2. copy

  3. parameterized

  4. none of the above

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

This option is correct as the copy constructor is invoked when an object is passed by value to a function. The pass by value method requires a copy of passed arguments to be created for the function to operate upon.