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. True

  2. False

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

Method overloading in Java requires methods to have different parameter lists (different number, types, or order of parameters). Return type alone is insufficient to distinguish overloaded methods - the compiler uses the parameter signature to resolve which method to call.

Multiple choice technology programming languages
  1. java.lang.Exception

  2. java.lang.Error

  3. java.lang.Throwable

  4. none of the above

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

In Java, the Throwable class is the common root superclass of all exceptions and errors in the language. Only objects that are instances of Throwable or its subclasses can be thrown by the JVM or the throw statement. Exception and Error both inherit directly from Throwable.

Multiple choice technology web technology
  1. A rectangular box with the name of the class in the box

  2. A rectangular box with the name of the object, a : and the class name that the object belongs to

  3. A rectangular box with the class name prefixed by the word "class"

  4. A thin vertical line with key method calls shown as outward arrows

  5. A rectangular box with an inserted dashed box on the top right corner.

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

In UML class diagrams, a class is represented by a rectangular box divided into three compartments: class name (top), attributes (middle), and operations (bottom). Option A correctly describes the basic representation as a rectangular box with the class name.

Multiple choice technology web technology
  1. A comment or explanatory note associated with a class

  2. A rectangular box with the object name and the constraint "{object}" immediatly following it

  3. A rectangular box with the name of the object in the box.

  4. A rectangular box with the name of the object, a : and the class name that the object belongs to.

  5. A thin vertical line with key method calls shown as outward arrows .

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

In UML object diagrams, an object is represented as a rectangular box showing the object name, a colon separator, and the class name (e.g., myObject:MyClass). The object name may be underlined to indicate it's an instance. Option D correctly describes this notation.

Multiple choice technology web technology
  1. its name, attributes, operations, and derived classes.

  2. its name, attributes and operations.

  3. its name, and attributes.

  4. its name, and operations.

  5. just its name.

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

UML class diagram notation divides a class box into three compartments: class name at the top, attributes in the middle, and operations (methods) at the bottom. This provides a complete visual specification of the class's structure and behavior.

Multiple choice technology web technology
  1. the normal class representation with a dotted arrow pointing at the template parameter classes

  2. the normal class representation but shaded grey.

  3. the normal class representation with a dotted outline and the names of its parameter classes listed on the top right-hand corner.

  4. the normal class representation with a rectangular box in its top left-hand corner.

  5. Its a trick question - parameterized classes can't be specified in the UML notation.

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

In UML, parameterized (generic) classes are shown with a dotted outline box in the top right corner listing the template parameters. This distinguishes them from regular classes. Option E is incorrect because UML does support generics notation.

Multiple choice technology web technology
  1. the normal class representation with a dotted arrow pointing at the template parameter classes

  2. the normal class representation but shaded grey.

  3. the normal class representation with a dotted outline and the names of its parameter classes listed on the top right-hand corner.

  4. the normal class representation with a rectangular box in its top left-hand corner.

  5. Its a trick question - parameterized classes can't be specified in the UML notation.

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

In UML, parameterized (generic) classes are shown with a dotted outline box in the top right corner listing the template parameters. This distinguishes them from regular classes. Option E is incorrect because UML does support generics notation.

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

The compilation fails because Sheep implements Herbivore, but Herbivore requires E extends Plant while Sheep extends Animal (not Plant). Changing Herbivore to accept E extends Animal fixes this since Sheep is an Animal. Carnivore already works correctly because it accepts E extends Animal, and Sheep qualifies.

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

The method return type is ArrayList. Option B works because you can return an ArrayList directly - the types match exactly. Option A fails because Inn is a subtype of Hotel, and ArrayList is not assignable to ArrayList (generics are invariant). Option C fails because Object is not a Hotel. Option D fails because Business is a supertype of Hotel, and ArrayList is not assignable to 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. 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 incorrect because 'extends' works in both directions (class-from-class and interface-from-interface), not just one. Option D is incorrect because a class cannot extend an interface (it implements it), and an interface cannot extend a class.

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

This tests varargs and method overloading resolution. For sifter(aa), A[] matches varargs A[]... so output '1'. For sifter(ba), B[] can match either B[]... (more specific) or B[] varargs - Java chooses the varargs version B[]... giving '2'. For sifter(7), the int autoboxes to Integer, which doesn't match array types, so Object is chosen giving '4'. Result: -1-2-4 = '-124'. But wait - the correct answer is '-434'. Let me reconsider: sifter(aa) calls sifter(A[]...)? A[] can match A[]... (varargs), but it can also match Object since arrays are Objects. The most specific is A[]..., so '1'. sifter(ba): B[] matches B[]... more specifically than Object, but there's also B[](not varargs). Java prefers the non-varargs version when both apply, so '3'. sifter(7): matches Object, so '4'. Result: -1-3-4 = '-134'. Hmm, still not matching. Let me re-examine the varargs rules.

Multiple choice technology programming languages
  1. True

  2. False

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

In most statically-typed languages including Java, method overloading requires different parameter lists - return type alone is insufficient to distinguish methods. The compiler would be unable to determine which method to call when the return value is not used or assigned. Overloaded methods must have unique signatures based on parameter types and count.

Multiple choice technology programming languages
  1. java.lang.Exception

  2. java.lang.Error

  3. java.lang.Throwable

  4. none of the above

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

In Java's exception hierarchy, all exceptions and errors inherit from java.lang.Throwable, which is the root class. Exception and Error are direct subclasses of Throwable. Exception is for recoverable conditions, Error for serious system errors. Throwable itself is rarely used directly in application code.