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 technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

The code is NOT legal. In Java, catch blocks must be ordered from most specific to most general. Since ExceptionB extends ExceptionA, catching ExceptionA first would also catch any ExceptionB instances, making the second catch block unreachable (a compile-time error). To fix this, reverse the order: catch ExceptionB first, then ExceptionA.

Multiple choice technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

The code IS legal. When overriding a method, Java allows the subclass to narrow the declared exception types - ExceptionB is more specific than ExceptionA (its parent). This follows the principle that subclasses can be more restrictive but not more permissive than the superclass contract.

Multiple choice technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

Java requires catch blocks to be ordered from most specific to most general. ExceptionB extends ExceptionA, so ExceptionB is more specific. Catching ExceptionA first would make the ExceptionB catch unreachable since any ExceptionB would already be caught by ExceptionA. This causes a compilation error.

Multiple choice technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

When overriding a method, the child class can declare a narrower subset of exceptions. ExceptionB extends ExceptionA, so throwing ExceptionB is more restrictive than throwing ExceptionA. This is valid - the child is promising to throw fewer types of exceptions than the parent declared.

Multiple choice technology programming languages
  1. Set

  2. SortedSet

  3. List

  4. Tree

  5. SortedMap

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

TreeMap is a Red-Black tree-based NavigableMap implementation. It implements the SortedMap interface (which extends NavigableMap), which provides guaranteed ordering based on natural ordering or a custom comparator. It does not implement Set, List, or Tree (Tree is not a standard Java collection interface).

Multiple choice technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

The catch block order violates Java's rule: more specific exceptions must be caught before their parent types. ExceptionB is a subclass of ExceptionA, so catching ExceptionA first makes the ExceptionB handler unreachable code. This causes a compilation error.

Multiple choice technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

When overriding a method, the subclass can narrow the declared exception types. ExceptionB is a subclass of ExceptionA, so throwing ExceptionB is compatible with the parent's throws ExceptionA clause. This is legal overriding.

Multiple choice technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

This is a duplicate of question 149712. The catch blocks are in the wrong order: the parent ExceptionA is caught before the child ExceptionB, making the ExceptionB handler unreachable. This violates Java's exception handling rules and causes a compilation error.

Multiple choice technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

Java allows overriding methods to declare narrower checked exceptions than those declared by the superclass method. Since ExceptionB extends ExceptionA, the subclass method throws a more specific exception, which complies with Java overriding rules and compiles without issue.

Multiple choice technology programming languages
  1. Set

  2. SortedSet

  3. List

  4. Tree

  5. SortedMap

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

TreeMap implements the NavigableMap interface, which directly extends the SortedMap interface. The other options are incorrect because Set and SortedSet handle unique collections rather than key-value maps, List represents ordered sequences, and Tree is not a standard Java Collections interface.

Multiple choice technology web technology
  1. The subclass has additional attributes of interest

  2. . The subclass has additional associations of interest.

  3. Some of the attributes/operations in super class are not applicable to subclass

  4. The subclass concept is operated on, handled, reacted to, or manipulated differently than the superclass or other subclasses in ways that are of interest.

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

Valid subclassing reasons include adding attributes/associations or needing different operations. Statement C violates the Liskov Substitution Principle - if a subclass can't handle superclass operations, it shouldn't inherit.

Multiple choice technology web technology
  1. . Class Diagram in design model doesn't show real world objects; rather it shows software classes only

  2. Design Model illustrates the specifications for software classes and interfaces

  3. Setters and getters should not be included in the Design Model.

  4. . Programming Language native libraries are shown in the Design Model

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

The design model details software-specific structures like classes, interfaces, and methods. While standard getters/setters are often omitted for clarity, including programming language native libraries in the model is false because it clutters diagrams with standard components that developers already know.

Multiple choice technology web technology
  1. An open headed arrow pointing to the inheriting class

  2. A closed headed arrow pointing to the inheriting class

  3. A closed headed dashed arrow pointing to the parent class

  4. . A closed headed arrow pointing to the parent class

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

In UML class diagrams, inheritance is represented by a generalization relationship, symbolized by a solid line with a hollow, closed arrowhead pointing from the inheriting subclass to the parent superclass. Open head arrows are used for association, and dashed lines represent realization.