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

  2. DirectCast

  3. InheritenceCast

  4. LevelCasting

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

In VB.NET, DirectCast is used for casting reference types up and down the inheritance hierarchy. It is more efficient than CType when the conversion type is known at compile time because it avoids runtime type checking overhead. Option A is too generic; Options C and D are not valid VB.NET keywords.

Multiple choice technology programming languages
  1. True

  2. False

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

A final class in Java cannot be extended (inherited from). The final keyword prevents subclassing by design. This is used when you want to prevent modification or extension of a class for security, design, or performance reasons.

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.