Computer Knowledge

Object-Oriented Programming

2,239 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 technology programming languages
  1. It is possible for a single class to implement multiple interfaces

  2. It is possible for a class to inherit from multiple classes

  3. It is possible for a class to inherit from a Sealed class

  4. All of above

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

A class can implement multiple interfaces because interfaces only define contracts without implementation. Multiple inheritance from classes is not allowed in C# due to complexity and ambiguity issues (the diamond problem). Sealed classes explicitly prevent inheritance by design.

Multiple choice technology programming languages
  1. To get information about event-handler

  2. To get or set property values

  3. Classes in other namespace can use reflection to access data and to determine which fields to persist

  4. None of the above

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

Reflection can inspect event handlers, get/set property values dynamically, and allow cross-namespace access to type metadata. All these are valid capabilities of the System.Reflection namespace in .NET.

Multiple choice technology programming languages
  1. using System.Collections.Generic; Dictionary < string , StringBuilder >() dctobj = new Dictionary < string , StringBuilder >;

  2. using System.Collections.Generic; Dictionary < string , StringBuilder > dctobj = new Dictionary < string , StringBuilder >();

  3. using System.Collections.Generic; Dictionary < string , StringBuilder > dctobj = new Dictionary < string , StringBuilder >;

  4. using System.Text; Dictionary < string , StringBuilder > dctobj = new Dictionary < string , StringBuilder >();

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

Option B shows correct Dictionary initialization syntax with proper type parameters and parentheses for instantiation. Option A is missing instantiation parentheses. Option C is missing parentheses. Option D uses wrong namespace (System.Text for StringBuilder is fine, but Dictionary needs Collections.Generic).

Multiple choice technology programming languages
  1. Throwable

  2. Catchable

  3. Runable

  4. Problem

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

To solve this question, the user needs to know the hierarchy of Java exceptions and errors.

Both class Error and class Exception are children of the parent class Throwable. This means that any class that extends Throwable can be used to represent an error or an exception that occurs during the execution of a Java program.

Option A is correct because Throwable is the parent class of both Error and Exception.

Option B is incorrect because there is no class called Catchable in Java.

Option C is incorrect because Runnable is an interface in Java, not a class that represents an error or an exception.

Option D is incorrect because there is no class called Problem in Java that represents an error or an exception.

Therefore, the answer is: A

Multiple choice technology programming languages
  1. True

  2. False

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

In exception handling hierarchies, a catch block can catch exceptions of its declared type or any subclass of that type due to polymorphism. If you catch Exception ( superclass), it will also catch IOException, SQLException, etc. (subclasses). This is a fundamental principle of polymorphic exception handling in object-oriented programming.

Multiple choice technology programming languages
  1. True

  2. False

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

In Java, a class cannot be both final and abstract because these modifiers have contradictory purposes. 'final' prevents inheritance (no subclasses allowed), while 'abstract' requires inheritance (must be subclassed to be instantiated). This creates an oxymoron - a class that both requires and forbids subclasses simultaneously. The compiler will reject this combination.

Multiple choice technology programming languages
  1. True

  2. False

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

Abstract classes can and often do have constructors. Although you cannot instantiate an abstract class directly, its constructor is called when a concrete subclass is instantiated using super() in the subclass constructor. This is useful for initializing fields defined in the abstract class that are common to all subclasses. The constructor executes before the subclass constructor's code.

Multiple choice technology programming languages
  1. True

  2. False

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

In Java, a method with no access modifier (package-private) can indeed be overridden by a method marked protected. This is valid because protected provides broader access than package-private, allowing subclasses (even in different packages) to access the method. The Liskov substitution principle permits widening access visibility in overriding methods.

Multiple choice technology programming languages
  1. Imports, package declaration, classes

  2. Classes, imports, package declarations

  3. Package declaration must come first; order for imports and class definitions is not

  4. Package declaration, imports, classes

  5. Imports must come first; order for package declaration and class definitions is not

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

Java requires a specific order for top-level elements in a source file: the package declaration (if present) must come first, followed by import statements, and then class/interface/enum definitions. This order ensures the compiler can properly resolve package and import information before processing type definitions. Options A, B, and E have incorrect ordering, while option C incorrectly states that imports and classes can be in any order.

Multiple choice technology web technology
  1. line 1,2,3,4

  2. line 3,4

  3. line 3

  4. line 2,3,4

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

In Java interfaces, all methods are implicitly public and abstract. Explicitly adding modifiers like public (line 2), protected (line 3), or private (line 4) is not allowed. Lines 3 and 4 with protected and private modifiers are invalid. Option B correctly identifies lines 3 and 4 as erroneous.