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

Multiple choice technology programming languages
  1. interface declares constant and instance variables.

  2. The abstract modifier can be combined with the static modifier .

  3. native modifier can be applied only on method.

  4. synchronized modifier can be applied to method as well as class.

  5. none of these

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology web technology
  1. Extension methods

  2. Lambda Expression

  3. Anonymous methods

  4. Expression trees

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. A static method may override another static method

  2. A static method cannot override a non-static method

  3. A non-static method cannot override a static method

  4. A non-static method may be overloaded by a static method

  5. A synchronized method cannot be overridden

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Factory Pattern

  2. Abstract Factory Pattern

  3. Builder Pattern

  4. Prototype Pattern

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Flyweight Pattern

  2. Façade Pattern

  3. Composite Pattern

  4. Decorator Pattern

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Adapter Pattern

  2. Observer Pattern

  3. Chain of Responsibility Pattern

  4. Strategy Pattern

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Visitor Pattern

  2. Notifier Pattern

  3. Observer Pattern

  4. Template Pattern

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. The pattern that creates many instances of a single class in one shot

  2. This pattern is to have only a single constructor to a class

  3. Provides a class of which there can be no more than an instance, and provides a single global point of access to that instance

  4. Creates and maintains a pool of objects, using which a single object can used and returned to the pool

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Façade Pattern

  2. Adapter Pattern

  3. Bridge Pattern

  4. Proxy Pattern

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. virtual,override

  2. override,new

  3. new,base

  4. base,override

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. Creates the class Test : Form

  2. Creates the class Test that inherits the class Form

  3. Creates the class form that inherits the class Test

  4. a and b

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. The root class(super class) of all classes in C# is Object

  2. Constructors in C# can be static

  3. When you override <= operator, you must override >= operator

  4. If you write your own constructor in a class, C# also provides a default constructor

Reveal answer Fill a bubble to check yourself
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 &lt;= and &gt;= must be overloaded in pairs.

Multiple choice technology programming languages
  1. B, C

  2. C, B and A

  3. A, B and C

  4. Object, A, B and C

Reveal answer Fill a bubble to check yourself
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.