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

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.

Multiple choice technology programming languages
  1. Compilation succeeds.

  2. Compilation fails due only to an error on line 3.

  3. Compilation fails due only to an error on line 4.

  4. Compilation fails due only to an error on line 5.

  5. Compilation fails due only to an error on line 3 and 5.

  6. Compilation fails due only to an error on line 3,4 and 5.

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

All lines compile correctly. Line 3's array syntax 'int[] a []' is valid (array of arrays). Line 4 can assign a long array to an Object reference (upcasting). Line 5 assigns a String array to an Object array reference (covariant arrays are allowed in Java).

Multiple choice technology programming languages
  1. After line 8 runs.

  2. After line 9 runs.

  3. After line 10 runs.

  4. After line 11 runs.

  5. Compilation fails.

  6. An exception is thrown at runtime.

Reveal answer Fill a bubble to check yourself
F Correct answer
Explanation

Line 10 attempts 'e2.e = el' but 'el' is undefined (typo for 'e1'). Since e2 was set to null on line 8, this would cause a NullPointerException. The GC eligibility questions become moot because the code throws an exception before completing.

Multiple choice technology programming languages
  1. Faster f = Faster.Higher;

  2. Faster f = Better.Faster.Higher;

  3. Better.Faster f = Better.Faster.Higher;

  4. Bigger.Faster f = Bigger.Faster.Higher;

  5. Better. Faster f2; f2 = Better.Faster.Longer;

  6. Better b; b.Faster = f3; f3 = Better.Faster.Longer;

Reveal answer Fill a bubble to check yourself
C,E Correct answer
Explanation

Since Faster is a nested enum inside the class Better, it must be referenced using its outer class name. Thus, Better.Faster is the correct type, and Better.Faster.Higher or Better.Faster.Longer are the correct value references. Extra whitespace in Better. Faster is ignored by the compiler, making both selected options valid.

Multiple choice technology programming languages
  1. pre b1 b2 r3 r2 hawk

  2. pre b2 b1 r2 r3 hawk

  3. pre b2 b1 r2 r3 hawk r1 r4

  4. r1 r4 pre b1 b2 r3 r2 hawk

  5. r1 r4 pre b2 b1 r2 r3 hawk

  6. Compilation fails.

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Java initialization order: static blocks execute top-to-bottom when class is loaded, then for each object: instance initializers (top-to-bottom) then constructor. The trace shows: Bird static (prints nothing), then Raptor static (r1, r4), then 'pre', then Hawk() calls Raptor() which calls Bird(): Bird instance initializer (b1), Bird constructor (b2), then back to Raptor instance initializer (r3), Raptor constructor (r2), then 'hawk'. Result: r1 r4 pre b1 b2 r3 r2 hawk.

Multiple choice technology architecture
  1. They can be applied to both data & methods

  2. They must precede a class's data variables or methods

  3. They can follow a class's data variables or methods

  4. They can appear in any order

  5. They must be applied to data variables first and then to methods

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Access modifiers (public, private, protected) must appear BEFORE the variable's type or the method's return type in Java declarations. The syntax is: [modifier] type name = value. Option A is true but B is more specific about placement. Options C and E are wrong - modifiers cannot follow the declaration. Option D is wrong - order matters.

Multiple choice technology programming languages
  1. A collection of method declarations and constant values

  2. An abstract base class

  3. A way to get multiple inheritance

  4. A way to inherit variables and method implementations

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

An interface in Java is a collection of abstract method declarations and constant values. It defines a contract without implementation - classes implement interfaces to provide specific behavior while achieving a form of multiple inheritance of type.