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
-
No
-
Yes
-
Can be in special cases
-
None
A
Correct answer
Explanation
Static methods belong to the class rather than any specific object instance. Consequently, they do not have access to an instance context and cannot use the this keyword. Attempting to use this inside a static method results in a compile-time error, making 'No' the only correct option.
-
public
-
synchronise
-
private
-
transient
A
Correct answer
Explanation
Top-level classes (not nested inside another class) can only be marked 'public' or have default (package) access. Synchronized, private, and transient modifiers are not allowed at the top level - synchronized is used for methods, and private/transient are for class members. Options B, C, D are incorrect modifiers for top-level classes.
-
java.lang.object
-
java.lang
-
none
-
java.lang.string
A
Correct answer
Explanation
In Java, all classes implicitly extend from java.lang.Object, which is the root of the class hierarchy. The package name is java.lang, but the actual class name is Object (capitalized). Options B and D are incorrect - java.lang is a package, java.lang.String is a specific class. Option A has the class name capitalized correctly.
-
When a class is not to be instantiated,
-
declare a method without providing the implementation
-
cannot use a field declared abstract
-
none
A,B
Correct answer
Explanation
The abstract keyword in Java serves two main purposes: preventing class instantiation and declaring methods without implementation. Option A is correct because abstract classes cannot be instantiated directly - they must be extended by concrete subclasses. Option B is correct because abstract methods declare the method signature without providing a body, leaving the implementation to concrete subclasses. Options C and D are incorrect - abstract can be used with fields in certain contexts (though not abstract fields themselves), and abstract is a valid part of Java.
-
No
-
Yes
-
Will have to use an instance
-
None
A
Correct answer
Explanation
Private members of a superclass are NOT accessible in subclasses. This is a fundamental principle of encapsulation in Java - private access means class-level only. Even though subclasses inherit the parent, they cannot directly access private fields or methods. They must use public/protected getters or setters if available. Option A 'No' is correct.
-
Run-Time Object
-
Test Object
-
Smart Identification Object
-
Assistive Object
A
Correct answer
Explanation
The Object property in QTP provides access to the actual Run-Time Object's native methods and properties, bridging the test automation with the live application object. Test Objects are QTP's abstraction of application elements and don't expose the native runtime methods.
A
Correct answer
Explanation
When creating B, constructors chain: prints 'A' then 'B'. During deserialization, constructors are NOT called for the serialized object itself, but the superclass no-arg constructor IS called for the non-serializable superclass A, printing 'A' again. Output: 'A B A'.
-
True
-
False
-
cant be said
-
none of the above
B
Correct answer
Explanation
Method overloading (defining multiple methods with the same name but different parameters) does not block inheritance. A subclass can overload methods while still inheriting all other methods from its superclass. Overloading and inheritance are independent concepts - a class can both inherit from a superclass and overload its own methods.
-
native
-
synchronize
-
protected or private.
-
All of the above
D
Correct answer
Explanation
Interface declarations in Java can only be public or package-private (default access). They cannot be declared with modifiers like native, synchronized, protected, or private. These keywords apply to specific methods or class features, not to interface declarations themselves.
-
By class and properties
-
By class and methods
-
By methods and properties
-
By class only
A
Correct answer
Explanation
QTP identifies objects by their class (type) and a set of properties that uniquely distinguish them from other objects. Methods define what actions can be performed on the object but are not used for identification during recording.
A
Correct answer
Explanation
Java allows constructor chaining using this() for same-class constructors or super() for parent class constructors. This prevents code duplication and ensures common initialization is performed consistently. The claimed answer True is correct.
A
Correct answer
Explanation
Parent's 'set' field is public, so Child can access it. The package syntax is valid. Java's Set accepts String values, so set.add("Hello") compiles. There are no compilation errors, so the answer True is correct.
-
public, abstract
-
abstract, final
-
final
-
public
A
Correct answer
Explanation
Methods in an interface are implicitly public and abstract. The 'public' modifier ensures they can be accessed from anywhere, and 'abstract' indicates they must be implemented by the implementing class. Methods cannot be 'final' in an interface because that would prevent implementation, which defeats the purpose of an interface.
-
string
-
class
-
delegate
-
struct
-
interface
D
Correct answer
Explanation
In C#, value types are stored directly on the stack and contain their actual data. Structs are value types, while classes, delegates, interfaces, and strings are reference types that store references to heap-allocated objects.