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

  2. False

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

In Java, objects are always passed by reference, not by value. When you pass an object to a method, you're passing a reference to the same object in memory. Any changes made to the object's state inside the method will be visible to the caller. However, reassigning the reference itself inside the method won't affect the caller's reference.

Multiple choice technology programming languages
  1. True

  2. False

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

Static methods belong to the class itself, not to any instance. They cannot access instance variables because instance variables exist only when an object is created. Static methods can only access other static members (static variables and static methods). This is a fundamental distinction between static and instance members in Java.

Multiple choice technology programming languages
  1. True

  2. False

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

Method overloading in Java requires differences in the parameter list (number, type, or order of parameters). Return type alone is insufficient to distinguish overloaded methods because the compiler cannot determine which method to call based only on the return type, especially when the method call result isn't assigned or used.

Multiple choice technology programming languages
  1. True

  2. False

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

Interface methods in Java are implicitly public and abstract. When a class implements an interface, the implementing methods must be declared as public (or broader, but public is the only valid broader access). You cannot reduce the visibility of interface methods - they must remain public to maintain the interface contract.

Multiple choice technology programming languages
  1. True

  2. False

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

Interface methods in Java are implicitly public and abstract. When a class implements an interface, the implementing methods must be declared as public (or broader, but public is the only valid broader access). You cannot reduce the visibility of interface methods - they must remain public to maintain the interface contract.

Multiple choice technology programming languages
  1. a

  2. d

  3. c

  4. b

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

In Java, a local class defined inside a method can only access local variables of that method if they are final or effectively final. Since d is a non-final local variable, it cannot be referenced inside the inner class under strict rules.

Multiple choice technology programming languages
  1. The class can access any variable

  2. The class can only access static variables

  3. The class can only access transient variables

  4. The class can only access final variables

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

To answer this question, the user needs to know about nested classes and how they interact with the enclosing method.

When a class is defined inside a method, it is called a local inner class. The local inner class can access any final variables or effectively final variables of the enclosing method. An effectively final variable is a non-final variable whose value does not change after it is initialized.

The correct answer is:

D. The class can only access final variables

Option A is incorrect because the class cannot access any variable of the enclosing method, only final variables.

Option B is incorrect because the class cannot only access static variables of the enclosing method, only final variables.

Option C is incorrect because the class cannot only access transient variables of the enclosing method, only final variables.

Therefore, the correct answer is option D. The class can only access final variables of the enclosing method.

Multiple choice technology programming languages
  1. At the root of the collection hierarchy is a class called Collection

  2. The collection interface contains a method called enumerator

  3. The Set interface is designed for unique elements

  4. The interator method returns an instance of the Vector class

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

The Set interface is specifically designed to contain unique elements - it does not allow duplicate values based on equals(). Collection is an interface, not a class (A is false). The Collection interface does not have an enumerator() method (B is false). The iterator() method returns an Iterator, not a Vector (D is false). The Set interface's contract explicitly prohibits duplicate elements.

Multiple choice technology testing
  1. Instance View

  2. Allocation View

  3. Leak Doctor View

  4. Object View

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

The Allocation View in JProbe displays information about where and how objects are allocated in memory during application runtime. Distractors like Instance View, Leak Doctor, and Object View serve other specific tasks like tracking memory leaks or inspecting individual object instances rather than listing allocations.

Multiple choice technology programming languages
  1. The use of This; local to a function is illegal

  2. Access modifiers can not be applied to local variables

  3. All classes must be declared public

  4. None of these

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

In Java, local variables (declared inside methods) cannot have access modifiers like private, public, protected, or static. Access modifiers can only be applied to class-level members (fields, methods, classes). The statement 'private int x = 7;' inside a method will cause a compiler error because local variables cannot be declared private.

Multiple choice technology programming languages
  1. True

  2. False

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

In Java, a class can be declared abstract even without abstract methods. This prevents instantiation and is commonly used when a class provides partial implementation or when future subclasses are expected to add abstract methods. The abstract keyword signals intent to extend rather than use directly.

Multiple choice technology programming languages
  1. Only A

  2. Only B

  3. None of these

  4. Both A & B

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

In Java, both static initialization blocks and static nested classes are valid. Static initialization blocks run once when the class loads, useful for complex static initialization. Static nested classes are associated with the outer class rather than an instance, independent of outer class instances.