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. public abstract class Canine { public Bark speak(); }

  2. public abstract class Canine { public Bark speak() { } }

  3. public class Canine { public abstract Bark speak(); }

  4. public class Canine abstract { public abstract Bark speak(); }

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

An abstract class is declared using the abstract modifier. It can have concrete methods with a body {}. Option 2 has a valid abstract class containing a concrete method. Option 1 fails because the method has no body and is not marked abstract. Options 3 and 4 have syntax errors.

Multiple choice technology programming languages
  1. X extends Y is correct if and only if X is a class and Y is an interface.

  2. X extends Y is correct if and only if X is an interface and Y is a class.

  3. X extends Y is correct if X and Y are either both classes or both interfaces.

  4. X extends Y is correct for all combinations of X and Y being classes and/or interfaces.

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

In Java, the extends keyword is used for inheritance between the same kinds of types: a class can extend another class, and an interface can extend another interface. A class implements an interface using the implements keyword.

Multiple choice technology packaged enterprise solutions
  1. Javascript for creating baseids

  2. Javascript helper functions

  3. Javascript for querying Database

  4. Javascript helper objects

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

BaseObject.js in common legacy web development libraries (such as those associated with older O'Reilly Java/JS books or specific frameworks) contains utility functions and helper methods designed to support object operations.

Multiple choice technology programming languages
  1. No output

  2. Hello World!

  3. Compiler Error

  4. Exception

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

The abstract class implements the interface but doesn't need to provide the implementation - that's valid. Abstract classes implementing interfaces can defer implementation to concrete subclasses. The main method is static and independent, so it executes successfully, printing 'Hello World!'

Multiple choice technology programming languages
  1. YXYZ

  2. Z

  3. ZX

  4. ZXYY

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

When creating Z(), initialization order is: parent X's field b = new Y() runs first (prints 'Y'), then X() constructor executes (prints 'X'), then Z's field y = new Y() runs (prints 'Y'), finally Z() constructor executes (prints 'Z'). Output: YXYZ. This demonstrates that parent fields initialize before parent constructor, which runs before child fields/constructor.

Multiple choice technology programming languages
  1. True

  2. False

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

Overloading requires methods to have different signatures (different parameter lists). Methods with identical signatures would create ambiguity and compilation errors. The statement confuses overloading with overriding - overridden methods MUST have the same signature, but overloaded methods MUST have different signatures.

Multiple choice technology programming languages
  1. True

  2. False

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

Overriding enables runtime polymorphism because the JVM determines which method implementation to execute based on the actual object type at runtime, not the reference type. This is the core of dynamic method dispatch in Java. When you call an overridden method on a superclass reference pointing to a subclass object, the subclass version executes.

Multiple choice technology web technology
  1. BD

  2. DB

  3. BDC

  4. DBC

  5. Compilation fails

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

The Bottom2 constructor must call a constructor from its parent class Top, but Top doesn't have a no-argument constructor. Java requires either an explicit super() call with arguments, or the parent must have a no-arg constructor. Neither condition is met, causing compilation failure.

Multiple choice technology web technology
  1. Cohesion is the OO principle most closely associated with hiding implementation details

  2. Cohesion is the OO principle most closely associated with making sure that classes know about other classes only through their APIs

  3. Cohesion is the OO principle most closely associated with making sure that a class is designed with a single, well-focused purpose

  4. Cohesion is the OO principle most closely associated with allowing a single object to be seen as having many types

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

Cohesion refers to how focused a class is on a single responsibility. High cohesion means a class has one well-defined purpose, making it easier to maintain and understand. Options A and B describe encapsulation and loose coupling, which are different OO principles. Option D describes polymorphism.

Multiple choice technology web technology
  1. furry bray

  2. stripes bray

  3. furry generic noise

  4. stripes generic noise

  5. Compilation fails

  6. An exception is thrown at runtime

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

Field access in Java is bound at compile-time based on the reference type, not the actual object type. The variable m is declared as Mammal, so m.name refers to Mammal's 'furry' field. However, method calls are polymorphic - makeNoise() invokes Zebra's overridden method returning 'bray'.

Multiple choice technology web technology
  1. ClassD has low cohesion

  2. ClassA has weak encapsulation

  3. ClassB has weak encapsulation

  4. ClassB has strong encapsulation

  5. ClassC is tightly coupled to ClassA

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

Encapsulation is the practice of hiding a class's internal data and requiring all interaction to go through public methods. When ClassA uses public variables in ClassB instead of accessor methods, ClassB exposes its internal implementation details, representing weak encapsulation.

Multiple choice technology web technology
  1. Change the Carnivore interface to interface Carnivore<E extends Plant> extends Hungry<E> {}

  2. Change the Herbivore interface to interface Herbivore<E extends Animal> extends Hungry<E> {}

  3. Change the Sheep class to class Sheep extends Animal implements Herbivore<Plant> { public void munch(Grass x) {} }

  4. Change the Sheep class to class Sheep extends Plant implements Carnivore<Wolf> { public void munch(Wolf x) {} }

  5. Change the Wolf class to class Wolf extends Animal implements Herbivore<Grass> { public void munch(Grass x) {} }

  6. No changes are necessary

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

Sheep implements Herbivore, which requires Sheep to extend Plant because of interface Herbivore. Since Sheep extends Animal, this fails compilation. Changing Herbivore to interface Herbivore allows Sheep to be the type argument E because Sheep is an Animal.

Multiple choice technology web technology
  1. return new ArrayList<Inn>();

  2. return new ArrayList<Hotel>();

  3. return new ArrayList<Object>();

  4. return new ArrayList<Business>();

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

In Java, generic types must match exactly. The method return type is ArrayList, so the returned object must be of type ArrayList. Subtyping of the type arguments does not apply to the generic class itself (i.e., ArrayList is not a subtype of ArrayList).

Multiple choice technology web technology
  1. "X extends Y" is correct if and only if X is a class and Y is an interface

  2. "X extends Y" is correct if and only if X is an interface and Y is a class

  3. "X extends Y" is correct if X and Y are either both classes or both interfaces

  4. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces

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

In Java, 'extends' is used for both class inheritance and interface extension, but the types must match. A class can extend another class, and an interface can extend another interface. Option C correctly states that X extends Y works when both are classes or both are interfaces. Options A and B are wrong because a class cannot extend an interface (that's 'implements'), and an interface cannot extend a class. Option D is wrong because mixing classes and interfaces with 'extends' is invalid.

Multiple choice technology web technology
  1. -124

  2. -134

  3. -424

  4. -434

  5. -444

  6. Compilation fails

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

aa (A[]) matches sifter(A[]...) via varargs, resolving to "1" if no other method matches, but varargs is lowest priority. ba (B[]) matches sifter(B[]...) and sifter(B[] b1). sifter(B[] b1) is a direct match, so it's chosen, yielding "3". sifter(7) (int) boxes to Integer and matches sifter(Object o), yielding "4". Total is s += "1" (for aa?), wait. sifter(aa): is there a better match? A[] matches Object o directly (non-varargs) or A[]... (varargs). Since A[] is an object, sifter(Object) (non-varargs) is chosen over sifter(A[]...) (varargs). So sifter(aa) prints "4". sifter(ba) matches sifter(B[] b1) (exact non-varargs match), printing "3". sifter(7) matches sifter(Object), printing "4". Output is -434.