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
Inner classes are regular class members and can extend any class or implement any interface, just like outer classes can. There's no restriction on inheritance for inner classes.
A
Correct answer
Explanation
Anonymous classes are declared and instantiated in one expression using the class instantiation syntax 'new SuperClass() { ... }' or 'new Interface() { ... }'. The extends/implements is implied by the type being instantiated, not explicitly declared.
-
Yes
-
No
-
In specific conditions
-
Not sure
B
Correct answer
Explanation
Java anonymous classes do not have a name, which prevents developers from declaring constructors, since a constructor must share the name of its declaring class. Instead, custom initialization is achieved using instance initializers or by passing parameters to the superclass constructor, so defining a constructor is not allowed.
-
Yes
-
No
-
In specific conditions
-
Not sure
B
Correct answer
Explanation
In Java, inner classes are non-static nested classes associated with an instance of the outer class. Consequently, they cannot declare static members (such as static methods or fields) unless they are compile-time constant fields (static final primitives/Strings). Hence, the general answer is no.
-
Yes
-
No
-
In specific conditions
-
Not sure
B
Correct answer
Explanation
In Java, the 'synchronized' keyword can only be applied to methods or code blocks, not to class declarations. Classes themselves cannot be declared synchronized.
-
Classes that are both in the same assembly and derived from the declaring class.
-
Only methods that are in the same class as the method in question.
-
Internal methods can be only be called using reflection.
-
Classes within the same assembly, and classes derived from the declaring class.
D
Correct answer
Explanation
The protected internal access modifier in C# represents a logical OR combination, allowing access from any code within the same assembly, or from derived classes in other assemblies. It does not require classes to be both in the same assembly and derived, making that distractor incorrect.
-
Encapsulating an object in a value type.
-
Encapsulating a copy of an object in a value type.
-
Encapsulating a value type in an object.
-
Encapsulating a copy of a value type in an object.
D
Correct answer
Explanation
Boxing in .NET is the process of converting a value type (such as int or struct) to a reference type (object). When you box a value, the CLR creates a new object on the heap and copies the value type's contents into it. This is different from simply encapsulating an object (which is already a reference type).
-
The conversion of one type of object to another.
-
The runtime resolution of method calls.
-
The exposition of data.
-
The separation of interface and implementation.
D
Correct answer
Explanation
Encapsulation in OOP is the practice of hiding internal implementation details while exposing only necessary functionality through a public interface. This bundling of data with methods that operate on that data, and controlling access through visibility modifiers (private, public, protected), enables information hiding and reduces system complexity. Options A and C describe type casting and exposure (the opposite of encapsulation), while B describes polymorphism.
-
Chain of responsibility
-
Adapter
-
Decorator
-
Composite
C
Correct answer
Explanation
The Decorator design pattern attaches additional responsibilities to an object dynamically without affecting other objects of the same class. It provides a flexible alternative to subclassing for extending functionality by wrapping the original object and adding behavior at runtime. Chain of Responsibility handles request passing, Adapter converts interfaces, and Composite composes objects into tree structures.
-
Abstract Factory
-
Factory Method
-
Builder
-
Prototype
B
Correct answer
Explanation
The Factory Method pattern defines an interface for creating an object but lets subclasses decide which class to instantiate. It defers instantiation to subclasses, allowing a class to delegate instantiation to child classes. Abstract Factory creates families of related objects, Builder constructs complex objects step-by-step, and Prototype clones existing objects.
-
Chain of responsibility
-
Template method
-
Interpreter
-
Mediator
B
Correct answer
Explanation
The Template Method pattern defines the skeleton of an algorithm in a base class, allowing subclasses to override specific steps without changing the overall structure. This promotes code reuse and ensures the algorithm's invariant steps remain consistent while enabling customization of variable steps. Chain of Responsibility handles request passing through a chain, Interpreter interprets grammar, and Mediator centralizes communication between objects.
-
flouted
-
routed
-
scouted
-
touted
D
Correct answer
Explanation
Early literature on OOP often 'touted' (promoted or praised as a major advantage) that objects map naturally to real-world objects. 'Touted' means highlighted or recommended enthusiastically, which fits the context of promoting OOP benefits.
-
obtuse
-
abstract
-
abstruse
-
oblique
B
Correct answer
Explanation
The sentence states that object mapping encourages thinking in the problem domain rather than 'abstract' computer science terms. This means focusing on concrete, domain-specific concepts instead of theoretical computing abstractions.
-
contributions
-
tribulations
-
attributes
-
tributes
C
Correct answer
Explanation
Object-Oriented languages support quality 'attributes' (characteristics like cohesion and loose coupling) if programmers take the time to pursue them in their design. Attributes are the qualities or characteristics that make code maintainable and reliable.
-
class Application1
-
public class Application1
-
public static void Application1
-
static Application1
B
Correct answer
Explanation
For a standalone class that serves as the application entry point, it must be declared public. "public class Application1" is the proper declaration. Option A lacks the public modifier needed for a main class, while options C and D use incorrect syntax.