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 programming languages
  1. Beagle b2 = (Beagle) dog1;

  2. Beagle b3 = (Beagle) dog2;

  3. Beagle b4 = dog2;

  4. None of the above statements will compile.

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

Beagle extends Dog, so a Beagle reference can hold a Dog object if cast down. Option A: (Beagle) dog1 is a downcast - dog1 is Dog, so explicit cast is required. Option B: (Beagle) dog2 works because dog2 actually refers to a Beagle object (line 8). Option C fails without explicit cast.

Multiple choice technology programming languages
  1. x2.do2( );

  2. (Y) x2. do2( );

  3. ((Y)x2).do2();

  4. None of the above statements will compile.

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

To solve this question, the user needs to know about method invocation and upcasting/downcasting in Java.

Line 6 creates a new instance of class X. Line 7 creates a new instance of class Y and assigns it to a reference variable of type X. Line 8 creates a new instance of class Y.

At line 9, we need to insert code that will compile. Since the reference variable x2 is of type X, it does not have access to the method do2() that is defined in class Y. However, we know that x2 refers to an object of class Y, which does have access to the do2() method. Therefore, we need to cast x2 to type Y in order to access the do2() method.

Now, let's go through each option and explain why it is right or wrong:

A. x2.do2( ); - This option is incorrect because the reference variable x2 is of type X, which does not have access to the do2() method that is defined in class Y.

B. (Y) x2. do2( ); - This option is incorrect because the cast operator (Y) is applied to the method call, not to the reference variable x2. This is not valid syntax.

C. ((Y)x2).do2(); - This option is correct. The cast operator is applied to the reference variable x2, casting it to type Y. This allows us to access the do2() method that is defined in class Y.

D. None of the above statements will compile. - This option is incorrect because option C will compile.

Therefore, the correct answer is:

The Answer is: C. ((Y)x2).do2();

Multiple choice technology programming languages
  1. Woop is-a Hmpf and has-a zing.

  2. zing is-a Woop and has-a Hmpf.

  3. Hmpf has-a Woop and Woop is-a Zing.

  4. Woop has-a Hmpf and Woop is-a zing.

  5. Zing has-a Hmpf and Zing is-a Woop.

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

Woop extends Zing, so Woop IS-A Zing (not the reverse). Zing has a protected Hmpf field, so Woop inherits it - thus Woop HAS-A Hmpf. Option D is correct: Woop has-a Hmpf (through inheritance) and Woop is-a Zing (extends). Other options reverse relationships or claim incorrect associations.

Multiple choice technology programming languages
  1. Object Oriented

  2. IL

  3. XML documentation

  4. Garbage Collection

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

XML documentation comments (using ///) are a distinctive feature of C# designed to generate structured API documentation directly from source code. Other options like object-oriented programming, Intermediate Language (IL), and garbage collection are general concepts shared by many other languages and the broader .NET ecosystem.

Multiple choice technology programming languages
  1. Structs cannot be inherited.

  2. Structs are passed by value, not by reference.

  3. Struct is stored on the stack, not the heap.

  4. structs can be inherited

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

Structs in C# cannot be inherited from and cannot serve as base classes - they are sealed by nature. While structs are value types (passed by value), that's not the ONLY difference. Option B is a distractor because structs ARE passed by value, but option A is the more fundamental difference. Option D is false because structs CANNOT be inherited.

Multiple choice technology programming languages
  1. Classes that are both in the same assembly and derived from the declaring class.

  2. Only methods that are in the same class as the method in question.

  3. Classes within the same assembly, and classes derived from the declaring class.

  4. Classes in different assembly

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

In C#, protected internal is an "OR" access modifier. It allows access from any code within the same assembly, as well as from any derived classes even if they reside in a different assembly. Distractors like "both" (intersection) or "only same class" (private) incorrectly restrict this access.

Multiple choice technology programming languages
  1. void run();

  2. protected void go();

  3. public abstract set();

  4. none of these

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

Interface methods in Java are implicitly public and abstract, making void run(); a perfectly valid declaration. Using the protected modifier is illegal for interface methods, and public abstract set(); is invalid because it fails to specify a return type.

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

Option A is false because interfaces only declare constants (static final), not instance variables. Option B is false because abstract and static cannot be combined - abstract methods must be overridden in subclasses, but static methods belong to the class. Option C is true - the native modifier can only be applied to methods (indicating implementation in native code like C/C++). Option D is false because synchronized applies to methods and code blocks, not to classes.

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.