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
-
Class A 'is-a' Class B
-
Class B 'is-a' Class A
-
Class A 'has-a' Class B
-
Class B 'has-a' Class A
B
Correct answer
Explanation
Inheritance represents an 'is-a' relationship. When class B extends class A, B is a subclass of A, meaning 'Class B is-a Class A'. The reverse ('Class A is-a Class B') is incorrect because subclasses have more specific behavior. Composition represents 'has-a'.
-
Class
-
Constructor
-
Object
-
a and b
B
Correct answer
Explanation
Interfaces define abstract contracts with method signatures only, without implementation details like fields or method bodies. This is the fundamental purpose of interfaces - to specify what a type can do without defining how. Concrete types that implement interfaces provide the actual implementation. Therefore, the statement is false.
-
multiple inheritance
-
interface
-
abstract
-
None of the above
A
Correct answer
Explanation
C# does not support multiple inheritance of classes to avoid the diamond problem and complexity. A class can only inherit from one base class. However, C# supports interfaces (option B) and abstract classes (option C), which are alternatives to achieve similar functionality. The correct answer is A - multiple inheritance (of classes) is not supported.
-
Interface
-
abstract class
-
static class
-
All the above
A
Correct answer
Explanation
C# uses interfaces as the primary alternative to multiple inheritance. A class can implement multiple interfaces, gaining polymorphic behavior from each, while still inheriting implementation from only one base class. Abstract classes (option B) are single-inherited like regular classes. Static classes (option C) cannot be inherited at all. Therefore, interfaces are the correct alternative.
-
Inheritance
-
Composition
-
Association
-
Specialization
B
Correct answer
Explanation
To solve this question, the user needs to have a basic understanding of object-oriented programming concepts.
The book and page relationship can be best characterized as a composition relationship. Composition is a "has-a" relationship between two or more classes, where one class is a part of another class. In this case, a book has one or more pages, and each page is a part of the book. Therefore, the best option is:
The Answer is: B (Composition)
B
Correct answer
Explanation
Object diagrams show specific instances of classes with their actual attribute values at a given moment. While class diagrams define the structure and relationships, object diagrams require runtime state information and specific instance data that cannot be derived from class definitions alone. You need to know what objects actually exist and their current state to create an object diagram.
-
Interface
-
Class
-
Abstract Class
-
Filter
-
None
C
Correct answer
Explanation
GenericServlet is an abstract class in the Java Servlet API, not an interface or concrete class. It implements the Servlet interface and provides basic implementations for most methods, except service() which remains abstract. Subclasses must implement service().
-
Interface
-
Class
-
Abstract Class
-
All
-
None
C
Correct answer
Explanation
Abstract classes can have both abstract methods (without implementation) and concrete methods (with implementation). This allows partial implementation - some methods are implemented, others are left abstract for subclasses to implement. Interfaces require all methods to be implemented by the implementing class, and concrete classes must provide complete implementation.
-
Interface
-
Class
-
Abstract Class
-
Filter
-
None
C
Correct answer
Explanation
GenericServlet is an abstract class in Java Servlet API that implements the Servlet interface. It provides basic implementation of most Servlet methods except service(), which must be implemented by subclasses. HttpServlet extends GenericServlet to provide HTTP-specific functionality. GenericServlet is not an interface, not a concrete class, and not a Filter.
-
Interface
-
Class
-
Abstract Class
-
All
-
None
C
Correct answer
Explanation
Abstract classes are designed specifically to provide partial implementation - they can have both abstract methods (without implementation) and concrete methods (with implementation). Interfaces require all methods to be implemented (before Java 8) and regular classes must provide complete implementations. Only abstract classes allow the flexibility of implementing some methods while leaving others for subclasses to complete.
-
Can create instances of concrete classes
-
Must have patterns & direct inheritance
-
Must have direct inheritance and need not have pattern inheritance.
-
Need not have either pattern or direct inheritance
A,C
Correct answer
Explanation
Concrete classes in Pega are classes from which instances can be created (option A is correct). They must have direct inheritance (a parent class) but are not required to have pattern inheritance (option C is correct). Pattern inheritance is optional for concrete classes, so they need not have it (making C correct, not B). Option D is incorrect because they must have at least direct inheritance.
-
List<string>
-
IEnumerable<string>
-
List<object>
-
IEnumerable<object>
B
Correct answer
Explanation
The Where() extension method on List returns IEnumerable, the deferred execution sequence interface. This is the standard LINQ return type for filtering operations. The actual runtime type is a nested iterator class, but the declared return type is IEnumerable. It is not List or the object variant.
B
Correct answer
Explanation
Static methods belong to the class itself, not to any specific instance. They can only access static variables (class variables) and cannot directly reference instance variables, which require an object instance to exist. This is a fundamental Java concept regarding method types and variable scope.
-
Two methods defined in that interface
-
Only certain methods in an interface
-
Any methods in a class
-
All methods defined in that interface
D
Correct answer
Explanation
When a class implements an interface, it enters a contract to provide concrete implementations for ALL methods declared in that interface. This is a fundamental Java principle - interfaces define what a class MUST do, and the implementing class provides the specific behavior for every method.