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 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 sections: class name (top), attributes (middle), and operations (bottom). The basic notation is simply a rectangle with the class name. Option B describes object notation, C is incorrect syntax, and D describes sequence diagram lifelines.

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 shown as a rectangular box with the object name, a colon, and the class name (e.g., 'myObject:ClassName'). The object name may be underlined to indicate it's an instance. Option C describes class notation, B describes constraints, and E describes sequence diagram lifelines.

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 diagrams display three compartments in a class box: name (top), attributes (middle), and operations/methods (bottom). Derived classes are shown separately with inheritance arrows, not inside the parent class. This standardized format makes class structure immediately visible.

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

Parameterized (generic/template) classes in UML are represented by a normal class box with a dotted outline. The template parameters are listed in a small dashed rectangle in the top right corner. This notation shows that the class is parameterized by types like Class. Shading and arrows are not used for this purpose.

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 notation, parameterized (generic/templated) classes are shown as a normal class rectangle with a dashed outline in the top-right corner containing the template parameter names. This is the standard UML 2.x notation for templates/generics. They are not shaded grey (option B), and they certainly can be specified in UML (option E is wrong). The dotted arrow (option A) refers to dependencies, not parameterization.

Multiple choice technology web technology
  1. If the equals() method returns true, the hashCode() comparison == might return false

  2. If the equals() method returns false, the hashCode() comparison == might return true

  3. If the hashCode() comparison == returns true, the equals() method must return true

  4. If the hashCode() comparison == returns true, the equals() method might return true

  5. If the hashCode() comparison != returns true, the equals() method might return true

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

The equals/hashCode contract: equal objects MUST have equal hashes, but unequal objects CAN have equal hashes (hash collisions are allowed). Option B: If equals() returns false, they might still have the same hashCode (collisions allowed). Option D: If hashCode matches, equals() might return true (but must not always). Options A, C, E violate the contract.

Multiple choice technology web technology
  1. Replace line 13 with private Map<String, int> accountTotals = new HashMap<String, int>();

  2. Replace line 13 with private Map<String, Integer> accountTotals = new HashMap<String, Integer>();

  3. Replace line 13 with private Map<String<Integer>> accountTotals = new HashMap<String<Integer>>();

  4. Replace lines 17–20 with int total = accountTotals.get(accountName); if (total == null) total = 0; return total;

  5. Replace lines 17–20 with Integer total = accountTotals.get(accountName); if (total == null) total = 0; return total;

  6. Replace line 24 with accountTotals.put(accountName, amount);

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

Changing the map to Map enables generics. In lines 17–20, total must be declared as Integer to check for null safely before returning, which automatically unboxes to int. For line 24, autoboxing allows passing the primitive amount directly into the map.

Multiple choice technology programming languages
  1. compile time error at line1

  2. compile time error at line2

  3. Run time exception

  4. None Of the above

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

When overriding a method, you cannot reduce visibility. The parent class C1 has m1() with default (package-private) access. The child class C2 attempts to override it with private access, which is MORE restrictive than package-private. This violates the override rules, causing a compile-time error at line 2 where the overriding method is declared.

Multiple choice technology programming languages
  1. Single Inheritence

  2. Double Inheritence

  3. Multiple Inheritence

  4. Class Inheritence

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

Java supports single class inheritance, meaning a class can only extend one superclass directly. Java does not support multiple inheritance of classes (only multiple inheritance of interfaces through implementation). Double inheritance is not a standard term, and class inheritance is a general concept rather than a type.

Multiple choice technology programming languages
  1. the new method should return int

  2. the new method can return any type of values

  3. the argument list of new method should exactly match that of overriden method

  4. the return type of new method should exactly match that of overriden method

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

When overriding a method in Java, the signature (parameter list) must match exactly, and the return type must be the same or a covariant subtype (not changed arbitrarily). Options A and B are incorrect.

Multiple choice technology programming languages
  1. abstract double area() { }

  2. abstract double area()

  3. abstract double area();

  4. abstract double area(); { }

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

An abstract method in Java is declared with the abstract keyword and has no method body (just a semicolon). Option C shows correct syntax. Option A has a body (not abstract), B is incomplete, D has invalid syntax.

Multiple choice technology programming languages
  1. Thread

  2. Runnable

  3. Implements

  4. None of the above

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

In Java, to make a class runnable as a thread, you implement the Runnable interface which contains the run() method. This is the preferred way over extending the Thread class because it allows your class to extend another class if needed. Option A is incorrect because Thread is a class, not an interface you implement.

Multiple choice technology programming languages
  1. Interface and Class

  2. Class and Interface

  3. interface and interface

  4. class and class

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

Map is an interface in Java that defines the contract for key-value mappings, while HashMap is a concrete class implementing that interface. The question asks for HashMap first, then Map.