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
  1. 10 20 20

  2. 20 10 20

  3. 10 20 10

  4. 20 20 10

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

To solve this question, the user needs to understand the concept of inheritance in Java, as well as the difference between instance variables and class variables.

The Base class has an instance variable x with a value of 10, and the Derived class has an instance variable x with a value of 20. When a Derived object is created, it will have both the x variables, but when a Base object is created, it will only have the x variable from the Base class. When a Derived object is assigned to a Base reference, the Base reference can only access the x variable of the Base class.

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

A. 10 20 10: This option is correct. When b.x is evaluated, it refers to the x variable of the Base class, which has a value of 10. When d.x is evaluated, it refers to the x variable of the Derived class, which has a value of 20. When bd.x is evaluated, it refers to the x variable of the Base class (since bd is a Base reference), which has a value of 10.

B. 20 20 10: This option is incorrect. b.x is 10 (the x variable of the Base class), d.x is 20 (the x variable of the Derived class), and bd.x is 10 (the x variable of the Base class).

C. 10 20 20: This option is incorrect. b.x is 10 (the x variable of the Base class), d.x is 20 (the x variable of the Derived class), and bd.x is 20 (the x variable of the Derived class`).

D. 20 10 20: This option is incorrect. b.x is 10 (the x variable of the Base class), d.x is 20 (the x variable of the Derived class), and bd.x is 20 (the x variable of the Derived class`).

Therefore, the answer is: A. 10 20 10

Multiple choice technology
  1. (B) & (C)

  2. (A) & (C)

  3. A

  4. D

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

Java doesn't support multiple class inheritance, but multiple interface implementation achieves similar behavior. A class can extend one class (B) and implement multiple interfaces (C). Option A 'extending two or more classes' is impossible in Java. Options B and C together represent how Java implements the concept of multiple inheritance.

Multiple choice technology
  1. D

  2. C

  3. B

  4. A

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

Static nested classes are associated with the enclosing class itself, not an instance, so they can be instantiated without a reference to an enclosing class instance (A is false). They can only access static members of the enclosing class (B is true, C is false). They do not need to extend the enclosing class (D is false).

Multiple choice technology programming languages
  1. testObj = CTest

  2. testObj = CTest()

  3. CTest testObj = CTest()

  4. testObj = new CTest()

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

In Python, a class is instantiated by calling the class name as if it were a function, e.g., testObj = CTest(). Option 621437 does not instantiate it but assigns the class reference. Options 621439 and 621440 use static typing or new keywords from other languages.

Multiple choice technology programming languages
  1. an operator

  2. a method

  3. nothing

  4. a keyword in Java

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

instanceof is a binary operator in Java used to check if an object is an instance of a specific class or implements an interface. It returns a boolean value and is commonly used in type-checking scenarios before casting.

Multiple choice technology programming languages
  1. Same package as B

  2. In different package

  3. mark a default acess modifier to the class

  4. None

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

In Java, methods with no access modifier (default/package-private) are only accessible to classes within the same package. For Object A to access a default-access method of Object B, both must be in the same package.

Multiple choice technology programming languages
  1. True

  2. False

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

Abstract classes are allowed to contain both abstract methods (without bodies) and non-abstract (concrete) methods (with implementations). This permits abstract classes to provide default behavior to subclasses while leaving other methods to be overridden. Thus, the statement is true and the distractors are wrong.

Multiple choice technology programming languages
  1. True

  2. False

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

Constructors are used to initialize new object instances and cannot be declared static. Making a constructor static would prevent it from accessing instance variables and instance methods, which contradicts its purpose. Therefore, constructors can never be static, and the stored answer 'False' is correct.