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
B
Correct answer
Explanation
Java source files must use the .java extension as required by the Java compiler (javac). The .h extension is for C/C++ header files, and .class is the extension for compiled bytecode files produced by the compiler, not source files. Option C '>java' appears to be a typo or formatting error.
D
Correct answer
Explanation
When Java source code is compiled, each class is converted to bytecode and stored in a separate file with the .class extension. This bytecode file contains instructions that the JVM can execute. The filename matches the class name (case-sensitive).
-
Dependent
-
Neutral
-
Independent
-
None of the above
B
Correct answer
Explanation
Java is Architectural Neutral because the compiled bytecode can run on any processor architecture. The JVM abstracts the underlying hardware, allowing the same bytecode to execute on different platforms without recompilation. This is a key feature that enables Java's 'write once, run anywhere' capability.
A
Correct answer
Explanation
A constructor can be declared as private. This is a common design pattern in Java, frequently used to prevent instantiation or to implement the Singleton pattern.
A
Correct answer
Explanation
Overloaded methods are completely independent methods that share the same name but have different parameter lists. They are free to have different access modifiers.
-
Prototype
-
Factory method
-
Abstract factory
-
Proxy
A
Correct answer
Explanation
The Prototype pattern specifies object creation by cloning a prototypical instance instead of creating new instances from scratch. It's useful when creating a new object is expensive or complex, and you want to create new objects by copying this prototype and then modifying as needed.
-
Decorator
-
Observer
-
State
-
Strategy
C
Correct answer
Explanation
The State pattern allows an object to change its behavior when its internal state changes, making it appear as if the object has changed its class. This is exactly what the question describes. Decorator adds responsibilities dynamically, Observer notifies dependents of state changes, and Strategy encapsulates interchangeable algorithms.
-
Decorator
-
Observer
-
State
-
Strategy
C
Correct answer
Explanation
The State pattern allows an object to change its behavior when its internal state changes, making it appear as if the object has changed its class. This is exactly what the question describes. Decorator adds responsibilities dynamically, Observer notifies dependents of state changes, and Strategy encapsulates interchangeable algorithms.
-
Meditator
-
Template method
-
Adaptor
-
Bridge
C
Correct answer
Explanation
The Adapter pattern converts the interface of a class into another interface clients expect. Option A should be 'Mediator' not 'Meditator' (typo). Template method defines skeleton of algorithm, Bridge separates abstraction from implementation. Adaptor (Adapter) is correct.
-
Mediator
-
Memento
-
Strategy
-
Decorator
D
Correct answer
Explanation
The Decorator pattern attaches additional responsibilities to an object dynamically, providing a flexible alternative to subclassing for extending functionality. Mediator defines how objects interact, Memento captures/restores state, and Strategy encapsulates interchangeable algorithms.
-
Prototype
-
Builder
-
Template method
-
Momento
B
Correct answer
Explanation
The Builder pattern separates the construction of a complex object from its representation, allowing the same construction process to create different representations. Prototype clones objects, Template method defines algorithm skeleton, and Momento (Memento) captures/restores state.
-
Visitor
-
Momento
-
Mediator
-
Bridge
C
Correct answer
Explanation
The Mediator pattern defines an object that encapsulates how a set of objects interact, promoting loose coupling by keeping objects from referring to each other explicitly and letting you vary their interaction independently. Visitor separates operations from object structure, Momento captures state, and Bridge separates abstraction from implementation.
-
a) By using the mutable="false" attribute in the class mapping.
-
b) By using the ismutable="false" attribute in the class mapping.
-
c) By using the ismutable="no" attribute in the class mapping.
-
d) None
A
Correct answer
Explanation
In Hibernate ORM mapping, the mutable="false" attribute tells Hibernate that the entity's state never changes after initialization. This allows Hibernate to optimize performance by skipping dirty checking during flush operations for this entity. The ismutable attribute name doesn't exist in Hibernate.
-
a) Only local or packaged sub programs can be overloaded.
-
b) Overloading allows different functions with the same name that differ only in their return types.
-
c) Overloading allows different subprograms with the same number, type and order of the parameter.
-
d) Overloading allows different subprograms with the same name and same number or type of the parameters.
-
e) Overloading allows different subprograms with the same name but different in either number or type or order of parameter.
A,E
Correct answer
Explanation
Package overloading allows multiple subprograms with the same name if they differ in the number, type, or order of parameters (option E). Only local (private) subprograms within the package body or packaged subprograms in the specification can be overloaded (option A). Option B is false because subprograms cannot differ only in return type - the parameter signature must be different. Option C is false because identical parameters would create a duplicate, not an overload. Option D is false because the parameters must differ in some way.
-
a) Packages can be nested.
-
b) You can pass parameters to packages.
-
c) A package is loaded into memory each time it is invoked.
-
d) The contents of packages can be shared by many applications.
-
e) You can achieve information hiding by making package constructs private.
D,E
Correct answer
Explanation
Packages are designed to be shared - their contents (specification and body) can be used by many applications and users simultaneously (option D). Information hiding is achieved by declaring constructs in the package body only, making them private and inaccessible outside the package (option E). Packages cannot be nested (option A). Parameters are passed to procedures/functions within packages, not to packages themselves (option B). Packages are loaded once per session and cached, not reloaded each invocation (option C).