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
In .NET, classes declared at the namespace level can only use public or internal access modifiers. The Protected modifier is only valid for class members (nested classes, methods, properties), not for top-level class declarations.
B
Correct answer
Explanation
Classes declared directly within a namespace cannot be declared as private in .NET. Namespace-level classes can only be public or internal. The Private modifier is only valid for nested class declarations inside another class.
-
Custom Attributes
-
Funtional Attributes
-
Predefined Attributes
-
Both A & C
D
Correct answer
Explanation
.NET supports both Custom Attributes (user-defined) and Predefined Attributes (built-in like [Serializable], [Obsolete]). Functional Attributes do not exist as a category in .NET.
-
run-time
-
test object
-
both a&b
-
none
C
Correct answer
Explanation
The Object Spy in HP/Micro Focus UFT allows users to view both test object properties (static properties stored in the repository) and run-time object properties (dynamic properties of the actual object in the running application) along with their values.
A
Correct answer
Explanation
In QTP/UFT, the .Object property provides access to the native properties and methods of the actual runtime object, allowing interaction with features not exposed through the test object hierarchy.
-
Function generator
-
Step generator
-
Object repository
-
none of the above
A
Correct answer
Explanation
The Function Generator in QTP allows you to insert test object methods, function calls, and various logic steps into your test. Step Generator and Object Repository are separate features with different purposes.
-
Adding state variables breaks referential transparency (you no longer can understand a statement or expression on its own: you need to understand it in the context of the settings of the global variables)
-
State variables lessen the cohesion of a program: you need to know more to understand how something works. A major point of Object-Oriented programming is to break up global state into more easily understood collections of local state.
-
When you add one variable, you limit the use of your program to one instance. What you thought was global, someone else might think of as local: they may want to run two copies of your program at once.
-
All of above
D
Correct answer
Explanation
All three statements describe why Java avoids global variables: they break referential transparency, reduce cohesion, and limit reusability by tying code to a single instance. Since each individual reason is valid, the combined choice “All of above” correctly captures the rationale.
-
private
-
protected
-
Both
-
None
B
Correct answer
Explanation
In Java, the String class is declared as final, which prevents any class from extending it. Therefore, you cannot inherit from or extend the String class.
-
this() can be used to invoke a constructor of the same class
-
this() will create the object of the same class
-
There is no function called this(). It is a key word.
-
this() will invoke the constructor of subclass
A
Correct answer
Explanation
In Java, this() is a special syntax used inside a constructor to invoke another overloaded constructor within the same class (constructor chaining). It must be the first statement in the constructor. It does not instantiate new objects directly or call subclass constructors.
-
singleton pattern.
-
creational pattern.
-
command design pattern.
-
composite design pattern.
B
Correct answer
Explanation
The Factory Pattern is classified as a creational design pattern because it provides an interface for creating objects without specifying their exact classes. It delegates object instantiation to subclasses or factory methods, promoting loose coupling by letting code depend on abstractions rather than concrete implementations. This is in contrast to structural patterns like composite or behavioral patterns like command.
-
making all constructors private.
-
making all constructors public.
-
removing all constructors.
-
making all constructor static.
A
Correct answer
Explanation
The singleton pattern requires a private constructor to prevent external instantiation. This ensures only one instance can exist. Making constructors public would allow multiple instances, violating the singleton principle.
A
Correct answer
Explanation
In standard object-oriented theory and subtyping relationships, subclassing is reflexive: every class is considered a subclass of itself (a non-strict subclass). However, in languages like Java, a class is not a strict subclass of itself. Given standard OOP type theory, the statement is accepted as True.