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
Which design pattern is used to define a set of classes that share a common interface, allowing objects to be composed dynamically?
-
Composite
-
Decorator
-
Adapter
-
Proxy
A
Correct answer
Explanation
The Composite design pattern is used to define a set of classes that share a common interface, allowing objects to be composed dynamically.
Which design pattern is used to provide a surrogate or placeholder for another object to control access to it?
-
Proxy
-
Decorator
-
Adapter
-
Facade
A
Correct answer
Explanation
The Proxy design pattern is used to provide a surrogate or placeholder for another object to control access to it.
Which design pattern is used to provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation?
-
Iterator
-
Composite
-
Decorator
-
Adapter
A
Correct answer
Explanation
The Iterator design pattern is used to provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Which design pattern is used to define an object that encapsulates how a set of objects interact?
-
Mediator
-
Observer
-
Command
-
Strategy
A
Correct answer
Explanation
The Mediator design pattern is used to define an object that encapsulates how a set of objects interact.
Which design pattern is used to define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically?
-
Observer
-
Mediator
-
Command
-
Strategy
A
Correct answer
Explanation
The Observer design pattern is used to define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Which design principle emphasizes the separation of concerns and promotes loose coupling?
-
Encapsulation
-
Inheritance
-
Polymorphism
-
Aggregation
A
Correct answer
Explanation
Encapsulation involves bundling data and methods together into distinct units, reducing dependencies and promoting loose coupling between components.
Which design pattern is commonly used to provide flexibility and extensibility by allowing new behaviors to be added without modifying existing code?
-
Singleton
-
Factory
-
Strategy
-
Builder
C
Correct answer
Explanation
The Strategy pattern allows different algorithms or behaviors to be selected at runtime, providing flexibility and extensibility without modifying the core logic.
Which programming paradigm is known for its encapsulation, inheritance, and polymorphism features?
-
Imperative Programming
-
Declarative Programming
-
Object-Oriented Programming
-
Functional Programming
C
Correct answer
Explanation
Object-oriented programming organizes code into objects, which contain both data and methods, and allows for inheritance and polymorphism.
Which of the following is a common software design pattern?
-
Singleton
-
Factory
-
Observer
-
All of the above
D
Correct answer
Explanation
Singleton, Factory, and Observer are all common software design patterns. Design patterns are reusable solutions to commonly occurring problems in software design.
Which of the following is NOT a common design pattern?
-
Factory Method
-
Singleton
-
Observer
-
Composite
D
Correct answer
Explanation
Composite is not a design pattern; it is a structural pattern that describes how objects can be composed into larger structures.
What is the main principle behind object-oriented programming?
-
Encapsulation
-
Inheritance
-
Polymorphism
-
Abstraction
D
Correct answer
Explanation
Abstraction is the fundamental principle of object-oriented programming, which involves hiding the implementation details of an object and exposing only its essential characteristics.
Which of the following is NOT a common software design anti-pattern?
-
Spaghetti Code
-
God Object
-
Big Ball of Mud
-
Singleton
D
Correct answer
Explanation
Singleton is not an anti-pattern; it is a design pattern that restricts the instantiation of a class to only one object.
Which of the following is NOT a common code smell?
-
Long methods.
-
Duplicated code.
-
Unused code.
-
Well-commented code.
D
Correct answer
Explanation
Well-commented code is generally not considered a code smell. In fact, it is often seen as a good practice to add comments to code to make it more readable and understandable.
What are the different types of casting calls?
-
Open calls
-
Closed calls
-
Targeted calls
-
All of the above
D
Correct answer
Explanation
There are three main types of casting calls: open calls, closed calls, and targeted calls. Open calls are open to anyone who wants to audition, while closed calls are only open to actors who have been invited. Targeted calls are open to actors who have specific skills or experience that are relevant to the project.
Which of the following is NOT a fundamental principle of OOP?
-
Encapsulation
-
Inheritance
-
Polymorphism
-
Modularity
D
Correct answer
Explanation
Modularity is not a fundamental principle of OOP. The fundamental principles of OOP are encapsulation, inheritance, and polymorphism.