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
To solve this question, the user needs to have knowledge of Java programming language.
The statement is True.
In Java, a source file is officially called a "compilation unit". It contains the source code for a single Java class or interface. When the Java compiler compiles the source code, it generates a bytecode file for each compilation unit, which can then be executed on any platform that has a Java Virtual Machine (JVM). Therefore, option A is correct.
The Answer is: A. True
-
build path
-
runtime classpath
-
Both (A) and (B )
-
None
A
Correct answer
Explanation
HttpServlet subclasses should override doGet() or doPost() methods (not service() directly) to handle specific HTTP request types. This is standard servlet API pattern.
A
Correct answer
Explanation
In object-oriented programming, a class can implement multiple interfaces. Interfaces define contracts without implementation, and a class can conform to multiple contracts simultaneously. This is a fundamental OOP principle that enables flexible design and multiple inheritance of type specifications without the complexities of multiple inheritance of implementation.
-
Parameter
-
Value
-
Index
-
Object
B
Correct answer
Explanation
In many programming languages, the implicit parameter name for a property setter is 'value'. This is a convention where the setter receives the new value to be assigned to the property. For example, in C# and VB.NET, 'value' is the implicit keyword that represents the parameter being assigned to the property.
B
Correct answer
Explanation
In C#, virtual or override methods cannot be declared static. The static modifier cannot be combined with virtual, abstract, or override modifiers because static methods are bound at compile time, whereas overrides require dynamic runtime dispatch.
B
Correct answer
Explanation
Once you define any constructor in C#, the compiler no longer provides the default parameterless constructor. To keep a parameterless constructor along with your parameterized one, you must explicitly write both constructors.
B
Correct answer
Explanation
Private methods cannot be overridden because they are not accessible outside the class in which they are defined. Overriding requires the method to be visible to derived classes, and private methods are explicitly restricted to the defining class only. Even if a private method is marked virtual, the private access modifier takes precedence for accessibility.
-
Defines multiple access points for a singleton
-
supports multiple visibility forms for a singleton
-
allows the creation of multiple class instances in the future without affecting the clients of the singleton class
-
All of the above
-
Allows you to represent composite objects in the form of a hierarchy
-
It uses independent interfaces for representing individual and composite objects
-
It uses an individual object that is composed of two or more composite objects
-
It represents just a collection of different objects
A
Correct answer
Explanation
The Composite pattern's key concept is representing part-whole hierarchies of objects, allowing clients to treat individual objects and compositions uniformly. This enables tree structures of objects where a parent node can have multiple children. Option A correctly captures this hierarchical representation concept.
-
Adapter pattern is used to access facades
-
The Adapter pattern adds an indirection object for low coupling
-
The Façade pattern hides the complexity of a subsystem from a client
-
None of the above
C
Correct answer
Explanation
The Facade pattern provides a simplified interface to a complex subsystem, hiding its complexity from clients. This reduces coupling and makes the subsystem easier to use. Option C correctly states this purpose. Options A and B incorrectly reference the Adapter pattern.
-
Assigns responsibility to an intermediate object
-
Creates a convenient class that supports high cohesion and low coupling
-
Creates a convenient class that supports low cohesion and high coupling
-
Protects the system from unexpected variations
B
Correct answer
Explanation
The Pure Fabrication pattern is a GRASP pattern that creates an artificial class to support high cohesion and low coupling when no existing domain class is appropriate. It helps assign responsibilities that don't naturally fit into domain objects. Option B correctly captures this concept.
-
Handles the message generated when a system event or operation takes place
-
assigns the responsibility for creating new class instance or object
-
Determine how a system handles conditional variations
-
Creates a convenient class
C
Correct answer
Explanation
The Polymorphism pattern in GRASP deals with handling variation and alternatives by assigning responsibility to varying behaviors to the types that need them. It determines how a system should handle conditional variations using polymorphic operations rather than explicit conditional logic. Option C correctly identifies this.
-
Is a behavioral pattern that defines how a single class can be notified of a change
-
Consider the objects displaying the data and the objects containing the data as a single entity.
-
Is also known as the publish-subscribe model where an object can publish an event that has no subscribers
-
None of the above
C
Correct answer
Explanation
The Observer pattern (also called Publish-Subscribe) defines a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and updated automatically. Publishers (subjects) maintain a list of subscribers (observers) and notify them of state changes. Option C correctly identifies this as the publish-subscribe model and notes that events can have zero subscribers (publishing is independent of subscription). Option A incorrectly limits notification to a single class. Option B incorrectly suggests Observer merges data and display into one entity, when it actually separates them. Option D is incorrect since option C is valid.
-
TObect
-
TUnknown
-
IUnknown
-
TBase
-
TComponent
A
Correct answer
Explanation
In Delphi and Object Pascal, TObject (typographed as TObect in the option) is the ultimate base class from which all classes, including TComponent, derive. IUnknown is a base interface rather than an object class.
-
Yes
-
No
-
Can't Say
-
I don't know
A
Correct answer
Explanation
C# absolutely supports private constructors. They're commonly used in design patterns like Singleton to prevent direct instantiation from outside the class, or for static utility classes that should never be instantiated. The private constructor can only be called from within the class itself, typically through a static method or property that controls instance creation.