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
-
A rectangular box with the name of the class in the box
-
A rectangular box with the name of the object, a : and the class name that the object belongs to
-
A rectangular box with the class name prefixed by the word "class"
-
A thin vertical line with key method calls shown as outward arrows
-
A rectangular box with an inserted dashed box on the top right corner.
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.
-
A comment or explanatory note associated with a class
-
A rectangular box with the object name and the constraint "{object}" immediatly following it
-
A rectangular box with the name of the object in the box.
-
A rectangular box with the name of the object, a : and the class name that the object belongs to.
-
A thin vertical line with key method calls shown as outward arrows .
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.
-
its name, attributes, operations, and derived classes.
-
its name, attributes and operations.
-
its name, and attributes.
-
its name, and operations.
-
just its name.
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.
-
the normal class representation with a dotted arrow pointing at the template parameter classes
-
the normal class representation but shaded grey.
-
the normal class representation with a dotted outline and the names of its parameter classes listed on the top right-hand corner.
-
the normal class representation with a rectangular box in its top left-hand corner.
-
Its a trick question - parameterized classes can't be specified in the UML notation.
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.
-
the normal class representation with a dotted arrow pointing at the template parameter classes
-
the normal class representation but shaded grey.
-
the normal class representation with a dotted outline and the names of its parameter classes listed on the top right-hand corner.
-
the normal class representation with a rectangular box in its top left-hand corner.
-
Its a trick question - parameterized classes can't be specified in the UML notation.
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.
-
If the equals() method returns true, the hashCode() comparison == might return false
-
If the equals() method returns false, the hashCode() comparison == might return true
-
If the hashCode() comparison == returns true, the equals() method must return true
-
If the hashCode() comparison == returns true, the equals() method might return true
-
If the hashCode() comparison != returns true, the equals() method might return true
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.
-
Replace line 13 with private Map<String, int> accountTotals = new HashMap<String, int>();
-
Replace line 13 with private Map<String, Integer> accountTotals = new HashMap<String, Integer>();
-
Replace line 13 with private Map<String<Integer>> accountTotals = new HashMap<String<Integer>>();
-
Replace lines 17–20 with int total = accountTotals.get(accountName); if (total == null) total = 0; return total;
-
Replace lines 17–20 with Integer total = accountTotals.get(accountName); if (total == null) total = 0; return total;
-
Replace line 24 with accountTotals.put(accountName, amount);
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.
-
compile time error at line1
-
compile time error at line2
-
Run time exception
-
None Of the above
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.
-
Single Inheritence
-
Double Inheritence
-
Multiple Inheritence
-
Class Inheritence
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.
-
the new method should return int
-
the new method can return any type of values
-
the argument list of new method should exactly match that of overriden method
-
the return type of new method should exactly match that of overriden method
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.
-
abstract double area() { }
-
abstract double area()
-
abstract double area();
-
abstract double area(); { }
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.
-
Thread
-
Runnable
-
Implements
-
None of the above
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.
-
Private
-
Public
-
Abstract
-
Protected
A
Correct answer
Explanation
Workbook_Open() defaults to Private scope in the ThisWorkbook module, meaning it can only be called from within that module, not from outside. Private is the default access modifier for procedures in class modules like ThisWorkbook.
-
Interface and Class
-
Class and Interface
-
interface and interface
-
class and class
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.
-
Inner Class
-
Sub class
-
Private Class
-
Anonymous class
D
Correct answer
Explanation
Anonymous classes are declared and instantiated simultaneously, making it impossible to declare explicit constructors. They can only use instance initializers or pass arguments to the superclass constructor.