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
-
A class that is instantiated exactly once producing just one object of that class
-
A complete program that takes up only one line of code
-
A data structure such as a linked list that contains only one member
-
None of the above
A
Correct answer
Explanation
The singleton pattern restricts the instantiation of a class to one single instance, ensuring global access to that specific object across the application.
-
To sort its data structures in memory so that they can be accessed more quickly
-
To ensure that it is thread-safe by eliminating any deadlocks or race conditions
-
To break it into simpler subobjects known as "episodes"
-
To convert it into a form that can be saved to persistent storage
D
Correct answer
Explanation
Serialization converts an object's state into a format (like JSON, XML, or binary) that can be saved to disk, sent over a network, or stored in a database, and later reconstructed. It's about persistence, not performance optimization, thread safety, or decomposition.
-
Versioned Object
-
Versioned Object Base
-
Version Objects
-
Versioned Objects
-
Is another name for an Alerter.
-
Is a preset restriction created in Designer.
-
Can be used to define incompatible objects.
-
Is used to join aggregate tables.
B
Correct answer
Explanation
A conditional object in Business Objects Designer is a predefined restriction that can be reused across multiple queries and universes. It's not an alerter (A), not used for defining incompatible objects (C), and not for joining aggregate tables (D).
-
A static method may be invoked before even a single instance of the class is constructed.
-
A static method cannot access non-static methods of the class.
-
Abstract modifier can appear before a class or a method but not before a variable.
-
final modifier can appear before a class or a variable but not before a method.
-
Synchronized modifier may appear before a method or a variable but not before a class.
A,B,C
Correct answer
Explanation
A: True - static methods belong to the class, not instances, so they can be called before any object is created. B: True - static methods cannot directly access instance members (non-static methods/fields) without an object reference. C: True - abstract can modify classes and methods, but not variables. D is false because final CAN modify methods (to prevent overriding). E is false because synchronized only modifies methods or code blocks, not variables.
-
Encapsulation is a form of data hiding.
-
A tightly encapsulated class is always immutable.
-
Encapsulation is always used to make programs run faster.
-
Encapsulation helps to protect data from corruption.
-
Encapsulation allows for changes to the internal design of a class while the public interface remains unchanged.
A,D,E
Correct answer
Explanation
Encapsulation hides internal implementation and protects data from unauthorized access (A, D). It allows internal changes without breaking public contracts (E). Encapsulation is not about performance (C) and does not require immutability (B).
-
A top-level class can not be called "tightly encapsulated" unless it is declared private.
-
Encapsulation enhances the maintainability of the code.
-
A tightly encapsulated class allows fast public access to member fields.
-
A tightly encapsulated class allows access to data only through accessor and mutator methods.
-
Encapsulation usually reduces the size of the code.
-
A tightly encapsulated class might have mutator methods that validate data before it is loaded into the internal data model.
B,D,F
Correct answer
Explanation
Encapsulation improves maintainability (B). A tightly encapsulated class restricts field access to getters/setters (D). Mutators can validate data (F). Tightly encapsulated classes need not be private (A), don't need fast public access (C), and don't necessarily reduce code size (E).
-
The class is declared final.
-
All local variables are declared private.
-
All method parameters are declared final.
-
No method returns a reference to any object that is referenced by an internal data member.
-
None of the above
E
Correct answer
Explanation
Tight encapsulation requires all instance variables to be private and access to them controlled via getters/setters. None of the listed options (such as declaring the class final or local variables private) are strict prerequisites for tight encapsulation.
-
GFC501
-
GFC502
-
GFC503
-
None of the above
-
GFC506
-
GFC507
-
GFC508
-
None of the above
B
Correct answer
Explanation
GFC507 is NOT tightly encapsulated because it declares 'String name' without any access modifier (package-private), breaking encapsulation. GFC506 and GFC508 keep their name fields private with proper access control.
-
If assertions are not enabled at run time it prints an error message.
-
If assertions are not enabled at run time it prints nothing.
-
With assertions enabled it prints an error message.
-
With assertions enabled it prints nothing.
-
The assert statement is being used to check a class invariant--something that must be true about each instance of the class
-
The assert statements are being used to check a precondition--something that must be true when the method is invoked.
B,D,E
Correct answer
Explanation
If assertions are disabled, assert statements are ignored, printing nothing. With assertions enabled, the constructor and setter calls succeed because the invariant c > a + 2 * b holds (e.g., 251 > 50 + 200 is true), so nothing is printed. The assertions check a class invariant.
-
vtable entry
-
this pointer
-
Constructors
-
default value
B
Correct answer
Explanation
The this pointer is a hidden pointer passed to non-static member functions that points to the object invoking the method. It allows the compiler to resolve which object's data members are being accessed when multiple objects of the same class exist.
-
A single copy of the data member exists
-
The data member is initialised to zero before the first object is created
-
Requires a global definition
-
Storage is allocated to the static data member on declaration
D
Correct answer
Explanation
Static data members are declared in the class but storage is NOT allocated at declaration - only at definition outside the class. This makes option D the false statement. The other options correctly describe static member behavior.
A
Correct answer
Explanation
In Java, Throwable is the superclass of all errors and exceptions in the Java language. Both Error and Exception directly extend Throwable, making it the base class for the entire exception hierarchy.
B
Correct answer
Explanation
A final class in Java cannot be extended or overridden. The final keyword prevents inheritance by design.