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
-
SystemException
-
ApplicationException
-
Both (A) and (B)
-
None of the Above
B
Correct answer
Explanation
In Java EE and CORBA, user-defined application exceptions must extend ApplicationException (or a subclass thereof). SystemException is for system-level errors (transient failures, resource limits). The two hierarchies are separate - application exceptions for business logic errors, system exceptions for runtime failures.
-
public
-
private
-
synchronised
-
native
A
Correct answer
Explanation
Interface methods in Java are implicitly public and abstract. They cannot be declared as private, synchronized, or native - these modifiers are not allowed for interface method declarations. The access modifier for interface methods is always public, which matches the question's requirement.
-
x2.do2();
-
(Y)x2.do2();
-
((Y)x2).do2();
-
None of the above statements will compile
C
Correct answer
Explanation
The reference x2 has compile-time type X but points to a Y object. Since do2() is only defined in Y, we must downcast x2 to Y before calling it. Option C correctly applies the cast before the method call with ((Y)x2).do2(). The extra parentheses ensure casting occurs first.
-
fa fa
-
fa la
-
la la
-
Compilation fails
B
Correct answer
Explanation
Static methods in Java are bound at compile-time based on the reference type, not the actual object type. The variable t has type Tenor, so t.sing() calls Tenor.sing() returning 'fa'. The variable s has type Singer, so s.sing() calls Singer.sing() returning 'la', despite s referencing a Tenor object.
-
h hn x
-
hn x h
-
b h hn x
-
b hn x h
-
bn x h hn x
C
Correct answer
Explanation
The House(String) constructor calls this() to invoke House(), which implicitly calls super() to execute Building(). Building() prints 'b ', then House() prints 'h ', and finally House(String) prints 'hn x'. The complete output is 'b h hn x'.
-
Using NEW keyword
-
Using CREATEOBJECT keyword
-
Using GETOBJECT keyword
-
Both A & B
A
Correct answer
Explanation
In VB.NET, objects are instantiated using the 'New' keyword (e.g., 'Dim obj As New ClassName()'). CREATEOBJECT is used for COM objects (legacy VB6 compatibility) and GETOBJECT retrieves existing object references - neither is the standard .NET way.
-
Casting of variables
-
Boxing/Unboxing
-
Structure & Class conversion
-
Stack & Heap Conversion
B
Correct answer
Explanation
Boxing converts a value type to a reference type (object), while unboxing converts it back. This is the standard terminology in .NET for these conversions. Options A, C, and D are not the correct terms.
-
Runtime Error
-
Compiler Error
-
Compiler Throws a warning message
-
Both A & B
C
Correct answer
Explanation
In VB.NET, if a derived class inherits a method and declares it with Public Overridable Sub F() instead of using the Overrides keyword, it implicitly hides the base member. This compiles but throws a warning message suggesting the use of the Shadows keyword.
-
Using NEW keyword
-
Using CREATEOBJECT keyword
-
Using GETOBJECT keyword
-
Both A & B
A
Correct answer
Explanation
In VB.NET, objects are instantiated using the NEW keyword. While CREATEOBJECT and GETOBJECT exist for COM/ActiveX interoperability, the standard way to create .NET objects is using NEW. Option D is incorrect because CREATEOBJECT is not for .NET objects.
-
Casting of variables
-
Boxing/Unboxing
-
Structure & Class conversion
-
Stack & Heap Conversion
B
Correct answer
Explanation
Boxing converts a value type to a reference type (object), while unboxing converts a reference type back to a value type. These operations involve copying data between stack and heap memory.
-
The run-time or test object properties of any object in an open application.
-
The test object properties and methods of any object in an open application.
-
The run-time or test object properties and methods of any object in an open application.
-
The run-time object properties and methods of any object in an open application.
C
Correct answer
Explanation
Object Spy in QTP shows both runtime properties (actual values from the application) and test object properties (expected values) plus their available methods. Option A omits methods, B misses runtime properties, and D omits test object properties, making C the complete answer.
-
Run-Time Object
-
Test Object
-
Smart Identification Object
-
Assistive Object
A
Correct answer
Explanation
In UFT (Unified Functional Testing), the Object property is used to access the native Run-Time Object, which represents the actual application object being tested during test execution. This allows you to use methods and properties that belong to the real application object rather than UFT's test object abstraction. The Test Object is UFT's internal representation, while Run-Time Object gives you direct access to the application's native object.
-
Data Type
-
Abstract Type
-
User Defined Type
-
All of these options
C
Correct answer
Explanation
In object-oriented programming, a class is indeed a user-defined type that encapsulates data and behavior. Unlike primitive data types (int, float, char) which are built into the language, classes are defined by programmers to model real-world entities or abstract concepts. Classes combine data attributes and methods into a single reusable unit.
B
Correct answer
Explanation
Encapsulation is the practice of bundling data and methods that operate on that data into a single unit while restricting direct access. The process of basing one class on another is inheritance, not encapsulation, making the statement false.
-
57 22
-
45 38
-
45 57
-
D. An exception occurs at runtime
A
Correct answer
Explanation
An anonymous subclass of AbstractTest overrides getNum() to return 22. An anonymous subclass of AbstractTest.Bar overrides getNum() to return 57. The output f.getNum() + " " + t.getNum() prints 57 22.