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. Inheritance defines a has-a relationship between a superclass and its subclasses

  2. Every Java object has a public method named equals

  3. A class can extend any number of other classes

  4. A non-final class can be extended by any number of classes

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

Option B is correct - every Java object inherits the equals() method from Object class. Option D is correct - a non-final class can be extended by unlimited subclasses. Option A is wrong - inheritance defines an 'is-a' relationship, not 'has-a'. Option C is wrong - Java supports only single inheritance, not multiple classes.

Multiple choice technology programming languages
  1. True

  2. False

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

The wildcard ? super Dog represents an unknown type that is a supertype of Dog. Because the type is guaranteed to be a supertype of Dog, it is completely legal to add a Dog (or its subclasses) to this list. Thus, the statement claiming it is NOT legal is false.

Multiple choice technology programming languages
  1. True

  2. False

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

A HashSet in Java does not require elements to be mutually comparable. It uses hashCode() and equals() to manage elements, so adding a String and an Integer is perfectly legal and will not throw a ClassCastException at runtime.

Multiple choice technology programming languages
  1. True

  2. False

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

TreeSet maintains natural ordering and requires elements to be mutually comparable. When you add a String (JAVA) then an Integer (5), line 3 triggers ClassCastException at add() time because TreeSet tries to compare Integer with String to determine position, and they're not comparable. HashSet (block 129673) doesn't sort, so no exception.

Multiple choice technology programming languages
  1. True

  2. False

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

HashSet uses hashing, not natural ordering. It doesn't require elements to be comparable. Adding a String and then an Integer works fine - they coexist in the same HashSet. No ClassCastException occurs. The exception only happens with TreeSet (as in block 129672) because TreeSet attempts to compare elements.

Multiple choice technology programming languages
  1. True

  2. False

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

This is a duplicate of block 129672. TreeSet requires natural ordering and comparable elements. Adding String then Integer triggers ClassCastException because TreeSet cannot compare Integer with existing String elements when determining insertion position.

Multiple choice technology programming languages
  1. True

  2. False

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

This is a duplicate of blocks 129672 and 129674. TreeSet requires elements to implement Comparable for natural ordering. Adding String then Integer fails with ClassCastException because the Integer cannot be compared with the existing String when TreeSet tries to position it.

Multiple choice technology programming languages
  1. True

  2. False

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

This is a duplicate of blocks 129672, 129674, and 129675. TreeSet maintains sorted order and requires mutually comparable elements. Adding Integer after String causes ClassCastException during the comparison operation at insertion time.

Multiple choice technology programming languages
  1. True

  2. False

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

This is a duplicate of blocks 129672, 129674, 129675, and 129676. TreeSet uses natural ordering; when you add Integer after String, it attempts to compare them and throws ClassCastException because Integer and String are not mutually comparable.

Multiple choice technology
  1. Visible only inside the package

  2. Visible only in the class and its subclass in the same package

  3. Visible in all classes in the same package and subclasses in other packages

  4. Visible only in the class where it is declared

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

Protected visibility in Java spans two scopes: (1) all classes within the same package (like default access), plus (2) subclasses in other packages. This makes protected broader than package-private but narrower than public.

Multiple choice technology
  1. Line 1, line 2 and line 3 only

  2. Line 1 only

  3. Line 1 and line 2 only

  4. Line 2 only

  5. All are true

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

Statement 1 is true - abstract classes cannot be instantiated directly. Statement 2 is true - constructors are never abstract; they're concrete by definition. Statement 3 is false - a subclass can remain abstract and not implement all abstract methods. Statement 4 is false - static methods belong to the class, not instances, so they cannot be abstract.

Multiple choice technology
  1. All of these

  2. (A), (C) & (E)

  3. (C) & (E)

  4. (B), (C) & (E)

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

Java access control keywords are: default (package-private), protected, and public. Abstract, interface are not access modifiers - they define class/method types. The correct set is abstract (B), protected (C), and public (E).