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
-
interface declares constant and instance variables.
-
The abstract modifier can be combined with the static modifier .
-
native modifier can be applied only on method.
-
synchronized modifier can be applied to method as well as class.
-
none of these
C
Correct answer
Explanation
The native modifier in Java is used to indicate that a method is implemented in platform-dependent code (like C/C++), so it applies only to methods. Interfaces cannot have instance variables, abstract and static are mutually exclusive, and classes cannot be marked as synchronized.
-
Extension methods
-
Lambda Expression
-
Anonymous methods
-
Expression trees
A
Correct answer
Explanation
Extension methods allow adding methods to existing types without inheritance or recompilation. This feature was introduced in C# 3.0 / .NET Framework 3.5. Lambda expressions, anonymous methods, and expression trees are related features but don't extend existing classes directly.
-
A static method may override another static method
-
A static method cannot override a non-static method
-
A non-static method cannot override a static method
-
A non-static method may be overloaded by a static method
-
A synchronized method cannot be overridden
B,C,D
Correct answer
Explanation
In Java, static methods cannot be overridden (only hidden), meaning a static method cannot override a non-static method, and vice versa. However, overloading allows methods with the same name but different signatures, regardless of static modifiers. Synchronized methods can be overridden freely.
-
Factory Pattern
-
Abstract Factory Pattern
-
Builder Pattern
-
Prototype Pattern
A
Correct answer
Explanation
The Factory Pattern provides a simple decision-making class that returns one of several possible subclasses of an abstract base class depending on the data provided. It encapsulates object creation logic and allows runtime determination of which concrete class to instantiate.
-
Flyweight Pattern
-
Façade Pattern
-
Composite Pattern
-
Decorator Pattern
D
Correct answer
Explanation
The Decorator Pattern attaches additional responsibilities to an object dynamically. It provides a flexible alternative to subclassing for extending functionality by wrapping the original object and passing unchanged methods through to it.
-
Adapter Pattern
-
Observer Pattern
-
Chain of Responsibility Pattern
-
Strategy Pattern
C
Correct answer
Explanation
The Chain of Responsibility Pattern decouples sender from receiver by giving more than one object a chance to handle a request. The request passes along the chain until an object handles it, promoting loose coupling between classes.
-
Visitor Pattern
-
Notifier Pattern
-
Observer Pattern
-
Template Pattern
C
Correct answer
Explanation
The Observer pattern establishes a one-to-many relationship where a subject (the object being observed) automatically notifies all registered observers when its state changes. This enables loose coupling between objects since observers don't need to know about each other. The Visitor pattern is for separating algorithms from object structures, Template Pattern defines the skeleton of an algorithm in a base class, and 'Notifier Pattern' is not a standard GoF pattern.
-
The pattern that creates many instances of a single class in one shot
-
This pattern is to have only a single constructor to a class
-
Provides a class of which there can be no more than an instance, and provides a single global point of access to that instance
-
Creates and maintains a pool of objects, using which a single object can used and returned to the pool
C
Correct answer
Explanation
Singleton pattern ensures a class has only one instance and provides a global access point to it. This is useful for shared resources like database connections or logging services. Option A incorrectly describes creating many instances, which contradicts Singleton's purpose. Option B is insufficient because a single constructor alone doesn't prevent multiple instances. Option D describes the Object Pool pattern, which manages reusable objects.
-
Façade Pattern
-
Adapter Pattern
-
Bridge Pattern
-
Proxy Pattern
A
Correct answer
Explanation
The Facade pattern provides a simplified interface to a complex subsystem of classes, making it easier to use. It doesn't encapsulate the subsystem but provides a higher-level interface that reduces complexity for clients. Adapter Pattern converts one interface to another, Bridge Pattern separates abstraction from implementation, and Proxy Pattern controls access to an object.
B
Correct answer
Explanation
FileStream is a low-level stream class for reading and writing bytes. To write simple types (int, string, etc.) directly to a binary file, you need BinaryWriter or use serialization with FileStream. FileStream's Write methods only accept byte arrays, not typed data.
-
virtual,override
-
override,new
-
new,base
-
base,override
A
Correct answer
Explanation
In C#, method overriding requires the base class method to be marked 'virtual' (or abstract) and the derived class method to be marked 'override'. The 'virtual' keyword allows the method to be overridden, while 'override' explicitly indicates that the method overrides a base class method. 'new' hides the base method, 'base' calls the base class implementation.
-
Creates the class Test : Form
-
Creates the class Test that inherits the class Form
-
Creates the class form that inherits the class Test
-
a and b
B
Correct answer
Explanation
In C# inheritance syntax 'class derived : base', the class name before the colon (test) is the derived/child class, and the class name after the colon (Form) is the base/parent class. This means 'test' inherits from 'Form', not the reverse.
-
Delegate
-
Array
-
Enum
-
Class
C
Correct answer
Explanation
Enum is a value type in C#, while Delegate, Array, and Class are all reference types. Value types store their data directly, while reference types store a reference to the data's location in memory. Enums are derived from System.ValueType.
-
The root class(super class) of all classes in C# is Object
-
Constructors in C# can be static
-
When you override <= operator, you must override >= operator
-
If you write your own constructor in a class, C# also provides a default constructor
D
Correct answer
Explanation
If you define any custom constructor in a C# class, the compiler no longer automatically provides the parameterless default constructor. The other statements are true: Object is the root class, static constructors are supported, and comparison operators like <= and >= must be overloaded in pairs.
-
B, C
-
C, B and A
-
A, B and C
-
Object, A, B and C
D
Correct answer
Explanation
In C#, constructor execution starts from the highest base class in the inheritance hierarchy down to the derived class. Since all classes implicitly inherit from System.Object, its constructor runs first, followed by A, B, and finally C. Distractors fail to include Object or list the execution order in reverse.