Computer Knowledge
Object-Oriented Programming
2,239 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
-
Abstract class MyApplet extends java.applet.Applet {
-
Public class MyApplet extends java.applet.Applet {
-
Class MyApplet implements Applet {
-
Public class MyApplet extends applet implements Runnable {
B
Correct answer
Explanation
A valid applet declaration must be a public class that extends java.applet.Applet (or javax.swing.JApplet for Swing). Option B correctly declares 'Public class MyApplet extends java.applet.Applet' (assuming 'Public' is a typo for 'public'). Option A incorrectly uses 'abstract', option C incorrectly uses 'implements', and option D incorrectly combines 'extends applet' (should be 'extends java.applet.Applet') with 'implements Runnable'.
-
abstract double area() { }
-
abstract double area()
-
abstract double area();
-
abstract double area(); { }
C
Correct answer
Explanation
An abstract method declares a method signature without implementation. It ends with a semicolon and has no body (no braces). Option C shows the correct syntax with semicolon and no braces.
-
Can be overridden
-
Cannot be overridden
-
Can be inherited
-
Cannot be inherited
B
Correct answer
Explanation
A final method cannot be overridden in subclasses. This enforces that the implementation remains unchanged throughout the inheritance hierarchy. However, final methods can still be inherited and called by subclasses.
-
%SerialObject
-
%RegisteredObject
-
%Persistent
-
%Transient
A
Correct answer
Explanation
In Caché ObjectScript, %SerialObject is the base class for serial objects - objects that can be serialized but don't have independent storage. This is the correct inheritance for creating a serial class.
-
System
-
User defined
-
parametrized
-
Hidden
A
Correct answer
Explanation
In Caché ObjectScript, %classes refer to system classes - classes starting with % are provided by the system. User-defined classes don't use the % prefix. The claimed answer A (System) is correct.
-
%SerialObject
-
%RegisteredObject
-
%Persistent
-
%Transient
B
Correct answer
Explanation
In Caché, %RegisteredObject is the base class for transient objects - objects that exist only in memory and are not stored in the database. This is the correct inheritance for creating a transient class.
-
Clientdatatype
-
Abstract
-
super
-
classtype
-
final
A,B,C,D,E
Correct answer
Explanation
All five options are valid class keywords in Cache ObjectScript. Clientdatatype defines a custom data type class, Abstract prevents instantiation, super references the superclass, classtype defines the class category, and final prevents further inheritance or overriding. These keywords control class behavior and inheritance patterns.
-
Data type classes cannot be instantiated
-
Data type classes can contain properties
-
Data type classes automatically includes methods for validation and translation
-
Data type classes cannot be inherited
A,C
Correct answer
Explanation
Data type classes in Cache ObjectScript cannot be directly instantiated (they're abstract by nature) and automatically include methods for validation and translation when used. However, they CAN contain properties to define the data structure, and they CAN be inherited to create specialized data types. The claimed answer A and C are correct.
-
keywords are not inherted
-
keywords from first superclass are inherited
-
keywords from last superclass are inherited
-
keywords from all superclasses are inherited
B
Correct answer
Explanation
In Cache ObjectScript multiple inheritance, class keywords are inherited only from the first superclass in the inheritance list. Keywords from subsequent superclasses are not inherited. This prevents conflicts when multiple superclasses define conflicting keywords. The inheritance is determined by the order of superclasses in the class definition.
-
BD
-
DB
-
BDC
-
DBC
-
Compilation fails
E
Correct answer
Explanation
When a superclass lacks a no-argument constructor, subclasses must explicitly call a superclass constructor with matching arguments using super(...). Bottom2 fails to do this, causing compilation to fail because the implicit super() call cannot find a valid constructor in Top. The Top class only has Top(String s), so Bottom2's constructor must explicitly call super(s) or super(someString).
-
Cohesion is the OO principle most closely associated with hiding implementation details
-
Cohesion is the OO principle most closely associated with making sure that classes know about other classes only through their APIs
-
Cohesion is the OO principle most closely associated with making sure that a class is designed with a single, well-focused purpose
-
Cohesion is the OO principle most closely associated with allowing a single object to be seen as having many types
C
Correct answer
Explanation
Cohesion measures how focused and single-purpose a class is. High cohesion means a class performs one logical task. Hiding implementation details is encapsulation, interface-only interaction is loose coupling, and treating objects as multiple types is polymorphism.
-
furry bray
-
stripes bray
-
furry generic noise
-
stripes generic noise
-
Compilation fails
-
An exception is thrown at runtime
A
Correct answer
Explanation
Fields in Java are not polymorphic - they bind to the declared type (Mammal), so m.name returns 'furry '. Methods ARE polymorphic and use the actual object type (Zebra), so m.makeNoise() returns 'bray'. This is a key distinction between field access and method invocation in Java's inheritance model. Even though the object is a Zebra, field access is determined at compile time based on the reference type.
-
ClassD has low cohesion
-
ClassA has weak encapsulation
-
ClassB has weak encapsulation
-
ClassB has strong encapsulation
-
ClassC is tightly coupled to ClassA
C
Correct answer
Explanation
Direct access to another class's public variables violates encapsulation principles. ClassB should protect its data with private fields and provide access through getter/setter methods, making ClassB responsible for its own data management. When ClassA directly uses ClassB's public variables, ClassB has weak encapsulation because it exposes its internal implementation details.