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 architecture
  1. System.ServiceProcess.ServiceInstaller

  2. System.ServiceProcess.ServiceController

  3. System.ServiceProcess.ServiceBase

  4. System.ServiceProcess.ServiceProcessInstaller

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

All Windows services in .NET must inherit from ServiceBase, which provides the foundational service functionality and lifecycle methods (OnStart, OnStop, OnPause, OnContinue, etc.). ServiceController controls existing services, while ServiceInstaller and ServiceProcessInstaller are for installation purposes - none serve as a service base class.

Multiple choice technology programming languages
  1. Flip a Clidlet

  2. Flip a Clidder

  3. Flip a Clidder Flip a Clidlet

  4. Flip a Clidlet Flip a Clidder

  5. Compilation fails

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

The Clidders.flipper() method is marked final, which means it cannot be overridden in subclasses. When Clidlets attempts to override flipper(), it causes a compilation error. The final keyword prevents method overriding by design.

Multiple choice technology programming languages
  1. public abstract class Frob implements Frobnicate { public abstract void twiddle(String s){} }

  2. public abstract class Frob implements Frobnicate { }

  3. public class Frob extends Frobnicate { public void twiddle(Integer i) { } }

  4. public class Frob implements Frobnicate { public void twiddle(Integer i) { } }

  5. public class Frob implements Frobnicate { public void twiddle(String i) { } public void twiddle(Integer s) { } }

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

An abstract class implementing an interface does not need to implement its methods, making 544165 correct. A concrete class must implement all interface methods; 544168 correctly implements twiddle(String) and overloads it with twiddle(Integer). Other options fail due to syntax errors like abstract methods with bodies or using extends instead of implements.

Multiple choice technology programming languages
  1. BD

  2. DB

  3. BDC

  4. DBC

  5. Compilation fails

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

Compilation fails because Bottom2's constructor implicitly calls the no-argument constructor of its superclass Top via super(). Since Top defines a parameterized constructor, the compiler does not generate a default no-argument constructor, leading to a compilation error.

Multiple choice technology programming languages
  1. Clidlet

  2. Clidder

  3. Clidder Clidlet

  4. Clidlet Clidder

  5. Compilation fails.

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

Clidder.flipper() is private, so it is not visible or inherited by Clidlet. The Clidlet class can declare its own flipper() method without any conflict - it's not overriding, just defining a new method. When main() calls new Clidlet().flipper(), it prints "Clidlet".

Multiple choice technology programming languages
  1. 6

  2. 7

  3. 8

  4. 9

  5. Compilation fails.

  6. An exception is thrown.

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

Minor() calls super(y), invoking Uber(int x). That constructor calls this() to Uber(), which increments y from 2 to 3. Then y *= 2 makes y = 6. Back in Minor(), y += 3 makes y = 9. The static variable y holds these values across constructor calls.

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
  1. True

  2. False

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

In ILOG JRules, you cannot directly use superclass and subclass objects in the same rule without proper type definitions or inheritance hierarchies. Each rule typically works with objects of a consistent type to avoid type safety issues during rule execution.

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.