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
Correct answer
Explanation
In Java, both Error and Exception classes extend Throwable. Throwable is the root class of the exception hierarchy. Error represents serious unrecoverable errors (like OutOfMemoryError), while Exception represents recoverable conditions that applications should handle. Both inherit from Throwable.
-
Public
-
Private
-
Abstract
-
Protected
A,C
Correct answer
Explanation
An interface declaration can use the public modifier to allow access from other packages, and is implicitly abstract. Private and protected modifiers are not allowed for top-level interface declarations in Java.
-
Preconditions
-
Postconditions
-
Exceptions
-
Class invariants
C
Correct answer
Explanation
Assertions in Java are used to verify assumptions during development for preconditions, postconditions, and class invariants. They are NOT designed for handling exceptions - try-catch blocks and exception handling mechanisms serve that purpose.
B
Correct answer
Explanation
The super keyword in Java is used to refer to the parent class and invoke its methods or constructors. The this keyword refers to the current instance, final prevents modification, and static means class-level rather than instance-level.
A
Correct answer
Explanation
In Java, a lock can indeed be acquired on a Class object using synchronized blocks or static synchronized methods. Class-level locks are used to synchronize access to static methods or static data across all instances of the class. The lock is associated with the Class object itself, not with individual instances.
B
Correct answer
Explanation
Constructors are NOT inherited in Java. A subclass does not inherit constructors from its superclass. However, a subclass can call a superclass constructor using super() as its first statement. If no explicit constructor is defined in the subclass, the compiler generates a default no-arg constructor that calls super() implicitly.
-
FileWriter
-
CharWriter
-
Writer
-
OutputStream
-
FileOutputStream
C
Correct answer
Explanation
Writer is the abstract superclass for all character stream classes that write characters. FileWriter, PrintWriter, BufferedWriter, and other character-writing classes all extend Writer. OutputStream is for byte streams, not character streams. CharWriter is not a standard Java class. FileOutputStream writes bytes, not characters.
-
Frame
-
Component
-
Public
-
Super
B
Correct answer
Explanation
In Java's AWT hierarchy, java.awt.Container directly extends java.awt.Component, making Component its immediate superclass. Other options like Frame are subclasses of Container, whereas Public is an access modifier and Super is a keyword, neither of which represent Java classes.
-
True False True True True True
-
True False False True True True
-
True False True True True False
-
True False True True False True
A
Correct answer
Explanation
t1 and t3 are instances of tester, so instanceof tester is true for both, but false for t2 (which is a test). Since tester inherits from test, all three objects (t1, t2, and t3) are instances of test, making instanceof test true for all of them.
-
Root Tree Tree 15 15 15
-
Root Tree Tree 10 15 15
-
Root Tree Root 15 15 10
-
Root Tree Tree 15 15 10
-
Root Tree Tree 15 10 10
D
Correct answer
Explanation
Calling r.get() prints Root. Calling t.get() and t1.get() invokes the overridden get() method in Tree, printing Tree and setting bark to 15. Since bark is inherited from Root and not shadowed in Tree, t.bark and t1.bark both print 15, while r.bark remains 10 as it was never modified.
-
Methods
-
Functions
-
Classes
-
Parameters
A
Correct answer
Explanation
When creating unit tests from source code, you select individual methods to test. Unit testing focuses on testing the smallest testable units - individual methods within a class - rather than entire classes or functions. You create test methods that verify specific method behaviors with different inputs.
-
Multiple inheritance from other classes
-
Single inheritance from other classes
-
No inheritance
-
Only inherit interfaces
B
Correct answer
Explanation
C# allows a class to inherit from only one base class (single inheritance), but it can implement multiple interfaces. This design avoids the diamond problem and complexity of multiple class inheritance while maintaining flexibility through interface implementation.
-
Microsoft
-
MIcrosoft.Windows
-
System.Object
-
System.Windows
C
Correct answer
Explanation
Every class in C# implicitly inherits from System.Object (or the object keyword alias), making it the universal base class. This provides common methods like ToString(), Equals(), and GetHashCode() to all objects in the .NET framework.
A
Correct answer
Explanation
C# is object-oriented and allows classes to implement multiple interfaces, though they can only inherit from a single base class. Multiple interface implementation enables polymorphism without the complexity issues of multiple class inheritance.
B
Correct answer
Explanation
In JavaScript, built-in objects like String and Math cannot have prototype extended in all environments. While some browsers allow it, it's not universally supported and is generally considered bad practice due to potential conflicts and pollution of shared prototypes.