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. (Y)x2.do2();

  2. 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 inheritance, method overriding, and type casting.

In the given code, class X defines a method do1() and class Y extends class X and includes a new method do2(). In the main method, three objects are created, x1 is an object of class X, x2 is a reference of class X that points to an object of class Y, and y1 is an object of class Y.

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

A. (Y)x2.do2(); This option will not compile because x2 is a reference of class X and does not have access to the method do2() of class Y. To call a method of a subclass, we need to cast the reference to that subclass. However, we cannot cast x2 to class Y because it is not an object of class Y.

B. x2.do2(); This option will not compile because the reference x2 is of class X and does not have access to the method do2(). Since x2 is a reference of class X, only the methods defined in class X or its superclasses can be called using this reference.

C. ((Y)x2).do2(); This option will compile because we are casting the reference x2 to class Y, which has access to the do2() method. Since x2 is pointing to an object of class Y, we can safely cast it to class Y. The casted reference ((Y)x2) can then access the do2() method of 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. Beagle b2 = (Beagle) dog1;

  2. Runtime Exception

  3. Beagle b4 = dog2;

  4. None of the above statements will compile

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

To determine which statements will compile, let's analyze each option:

A. Beagle b2 = (Beagle) dog1; This statement will not compile because it is trying to cast a Dog object (dog1) to a Beagle object. However, dog1 is not an instance of Beagle, so the cast is not valid.

B. Runtime Exception This option is not applicable as it is not a valid code statement.

C. Beagle b4 = dog2; This statement will not compile because it is trying to assign a Dog object (dog2) to a Beagle variable (b4). In Java, you cannot assign an object of a superclass to a variable of its subclass type without explicit casting.

D. None of the above statements will compile This option is incorrect because statement A will compile.

Therefore, the correct answer is A) Beagle b2 = (Beagle) dog1.

Multiple choice technology programming languages
  1. Synchronized

  2. Native

  3. Strictfp

  4. All of these

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

Instance variable modifiers: synchronized and native are method-only modifiers. strictfp applies to classes, interfaces, and methods but not to individual fields. None of these (synchronized, native, strictfp) can modify instance variables, making 'All of these' correct.

Multiple choice technology platforms and products
  1. True

  2. False

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

Unlike COM, .NET does NOT support cross-language inheritance. A class written in one .NET language cannot inherit from a base class written in a different .NET language. While .NET supports cross-language integration and interfaces, inheritance across different language assemblies is not supported. Therefore the statement is false.

Multiple choice technology programming languages
  1. True

  2. False

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

An object becomes eligible for garbage collection only when there are no live threads holding active references to it, not simply when the number of accessing threads is low. If even a single active thread holds a reference, the object remains reachable and cannot be garbage collected.

Multiple choice technology programming languages
  1. Methods cannot be overriden to be more private

  2. static methods cannot be overloaded

  3. private methods cannot be overloaded

  4. An overloaded method cannot throw exceptions not checked in the base class

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

Java rules for method overriding dictate that a subclass method cannot reduce the visibility of the inherited method (e.g., overriding a public method as private). Static and private methods can be overloaded, and overloaded methods have no restriction on checked exceptions.

Multiple choice technology programming languages
  1. This() is used to invoke a constructor of the same class. super() cannot invoke a superclass constructor.

  2. Super() is used to invoke a constructor of the same class. This() is used to invoke a superclass constructor.

  3. This() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor.

  4. None of the above.

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

In Java, this() is used to call another constructor in the same class (constructor chaining), while super() is used to call a constructor in the superclass. Both must be the first statement in a constructor if used.

Multiple choice technology
  1. IIr reflect

  2. IIr context

  3. IIr XOM

  4. IIr Reflect XOM

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

In the JRules API, IIrReflect is the Java class that provides concrete implementation for reflection over the XOM (Executable Object Model). It allows inspection and manipulation of the XOM structure at runtime, enabling dynamic access to class definitions, properties, and methods in the executable model.

Multiple choice technology
  1. IIr reflect

  2. IIr rule engine

  3. IIr ruleset

  4. IIr Context

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

The IlrContext class controls the rule engine execution context in IBM ODM, managing the working memory and rule execution state. The other options are not the controlling class - IIrRuleEngine is the engine itself but not the context controller, IIrRuleset is the collection of rules, and IIrReflect deals with reflection.

Multiple choice technology
  1. IIr reflect

  2. Iir rule engine

  3. Iir context

  4. Iir rule

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

In rule engine APIs, 'IirContext' represents a rule engine instance or session. It provides the context for executing rules and managing working memory. 'IIrReflect' deals with introspection, 'IirRuleEngine' and 'IirRule' are not standard interface names in common rule engine APIs.