Computer Knowledge
Object-Oriented Programming
2,239 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
Constructor chaining allows calling one constructor from another using the this() keyword in C# or similar syntax in other languages. This avoids code duplication when initializing objects through different constructor overloads.
A
Correct answer
Explanation
The Set interface is designed for collections that contain no duplicate elements. Implementations like HashSet enforce uniqueness without imposing any ordering requirement. Lists allow duplicate elements, Maps store key-value pairs, and Vector (a legacy List implementation) also permits duplicates.
B
Correct answer
Explanation
In object-oriented programming and Java, a class is not considered a subclass of itself; it is only identical to itself. Inheritance relationships require two distinct classes.
B
Correct answer
Explanation
Method overloading requires different parameter lists (different number, types, or order of parameters). Return type alone is insufficient because the compiler wouldn't know which method to call at compile time - the signature must be unique based on parameters, not return type.
-
Interface in Java
-
pointer in C or C++
-
function pointer in C or C++
-
Interface in .NET
C
Correct answer
Explanation
Delegates in .NET are type-safe object references that encapsulate a method. This concept is conceptually identical to function pointers in C or C++.
-
private
-
protected
-
protected internal
-
It is not possible to do it
C
Correct answer
Explanation
protected internal combines both access modifiers: accessible within the current assembly (internal) AND to derived classes in other assemblies (protected). private is class-only, protected excludes current assembly, and option D is incorrect.
B
Correct answer
Explanation
The Serializable interface in Java is a marker interface, meaning it contains no methods that must be overridden. Classes simply implement it to signal serializability.
B
Correct answer
Explanation
Serializable is a marker interface - it has NO methods. The JVM checks for this interface at runtime to enable serialization, but there's no requirement to override any methods. The statement claiming it has two methods that must be overridden is false.
-
Server-side code
-
Client-side code
-
Both A) and B)
-
None of the above
A
Correct answer
Explanation
In ASP.NET, the code-behind file contains the server-side logic (written in C# or VB.NET) that runs on the web server to process page events, while the client-side code runs in the browser.
-
A method is an implementation of an abstraction
-
A method is an attribute defining the property of a particular abstraction
-
A method is an operation defining the behavior for a particular abstraction
-
A method is a blueprint for making operations
C
Correct answer
Explanation
In object-oriented programming, a method is an operation that defines the behavior for a particular abstraction. It encapsulates the actions or behaviors that objects of a class can perform. Option A is incorrect because methods define behavior, not implementation of abstraction. Option B is incorrect because attributes (not methods) define properties.
-
They pass messages by modifying each other's fields
-
They pass messages by calling each other's instance methods
-
They pass messages by modifying the static variables of each other's classes
-
They pass messages by calling static methods of each other's classes
B
Correct answer
Explanation
In object-oriented programming, objects interact and communicate by invoking (calling) each other's instance methods. Modifying fields directly violates encapsulation, and static methods belong to classes rather than individual object instances.
B
Correct answer
Explanation
Unlike TreeSet, a HashSet does not sort its elements and does not compare them using Comparable. It uses hashCode() and equals(), which are defined on all objects. Therefore, adding a String and an Integer to a HashSet will not cause a ClassCastException.
A
Correct answer
Explanation
A TreeSet relies on natural ordering or a comparator to sort its elements. When adding elements, it compares them using compareTo(). Comparing a String to an Integer throws a ClassCastException at runtime because they are incompatible types.
-
abstract class Link { }
-
native class Link { }
-
final class Link { }
-
abstract final class Link { }
C
Correct answer
Explanation
In Java, the final modifier prevents a class from being subclassed (extended). The abstract modifier requires a class to be extended, making abstract final an illegal combination. native is not a valid modifier for classes.