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 java
  1. Change line 2 to: public int a;

  2. Change line 2 to :protected int a;

  3. Change line 13 to :public Sub() { this(5); }

  4. Change line 13 to :public Sub() { super(5); }

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

To solve this question, the user needs to know about access modifiers, constructors, and inheritance in Java.

Line 2 declares a private instance variable a in class Super, which cannot be accessed from the subclass Sub. Line 3 is a constructor that initializes the value of a. Line 11 declares a subclass Sub that inherits from Super.

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

A. Change line 2 to: public int a; This option is incorrect because changing the access modifier of a to public would make it accessible from anywhere, including the subclass Sub. However, the a variable is still not visible in Sub, because it is declared in the superclass as private. Therefore, this option will not allow Sub to compile.

B. Change line 2 to: protected int a; This option is correct because changing the access modifier of a to protected would make it accessible from the subclass Sub. Since a is now declared as protected in the superclass, it can be accessed from the subclass. Therefore, this option will allow Sub to compile.

C. Change line 13 to: public Sub() { this(5); } This option is incorrect because changing the no-argument constructor to call another constructor with an argument using this would not compile since the superclass constructor is expecting an argument. Therefore, this option will not allow Sub to compile.

D. Change line 13 to: public Sub() { super(5); } This option is correct because changing the no-argument constructor to call the superclass constructor that takes an argument will initialize the a variable in the superclass with the value of 5. Therefore, this option will allow Sub to compile.

Therefore, the correct answer is:

The Answer is: D

Multiple choice java
  1. An encapsulated, public class promotes re-use.

  2. Classes that share the same interface are always tightly encapsulated.

  3. An encapsulated class allows subclasses to overload methods, but does NOT allow overriding methods.

  4. An encapsulated class allows a programmer to change an implementation without affecting outside code.

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

Encapsulation hides internal implementation details, allowing a programmer to change the code (implementation) without breaking dependent code (A, D). Sharing an interface (B) does not guarantee encapsulation, and encapsulation does not restrict overriding methods (C).

Multiple choice java
  1. The class is fully encapsulated.

  2. The code demonstrates polymorphism.

  3. The ownerName variable breaks encapsulation.

  4. The cardID and limit variables break polymorphism.

  5. The setCardInformation method breaks encapsulation.

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

To solve this question, the user needs to have an understanding of object-oriented programming concepts such as encapsulation and polymorphism.

Now, let's go through each statement and evaluate its correctness:

A. The class is fully encapsulated: This statement is incorrect. Encapsulation refers to the practice of keeping the internal state and implementation details of an object hidden and accessible only through defined methods. In this case, the class has private instance variables (cardID and limit) and a public method (setCardInformation) to modify their values. However, the class also has a public instance variable (ownerName) that can be accessed directly, which breaks encapsulation.

B. The code demonstrates polymorphism: This statement is incorrect. Polymorphism refers to the ability of objects of different classes to be treated as objects of a common parent class. The code provided does not demonstrate polymorphism.

C. The ownerName variable breaks encapsulation: This statement is correct. The ownerName variable is declared as public, which means it can be accessed and modified directly from outside the class, breaking encapsulation.

D. The cardID and limit variables break polymorphism: This statement is incorrect. The cardID and limit variables have no relation to polymorphism. Polymorphism involves the ability to treat objects of different classes as objects of a common parent class.

E. The setCardInformation method breaks encapsulation: This statement is incorrect. The setCardInformation method is designed to modify the values of the private instance variables (cardID, ownerName, limit), which is a common practice in encapsulation.

The Answer is: C

Multiple choice java
  1. int foo() { /* more code here */ }

  2. void foo() { /* more code here */ }

  3. public void foo() { /* more code here */ }

  4. private void foo() { /* more code here */ }

  5. protected void foo() { /* more code here */ }

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

In Java, overriding methods cannot have more restrictive access modifiers than the parent. Since One.foo() has package-private access, subclasses can override it with package-private, protected, or public access. Option 234 is invalid due to a return type change, and 237 is invalid because private is more restrictive.

Multiple choice java
  1. User-defined types are also known as reference types.

  2. User-defined types are only defined via classes and interfaces.

  3. An array type is signified by an identifier and one or more pairs of square brackets.

  4. The declaration int[] x; introduces an array type.

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

In Java, user-defined types are reference types, but they are not limited to classes and interfaces. Enums and annotation types are also user-defined types. Furthermore, arrays are reference types but are not 'defined' by the user in the same way classes are, making the statement technically incorrect.

Multiple choice java
  1. the new method should return int

  2. the new method can return any type of values

  3. the argument list of new method should exactly match that of overriden method

  4. the return type of new method should exactly match that of overriden method

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

For a method to successfully override a superclass method, the signature (method name and parameter list) must be identical. The return type must be compatible (covariant), not necessarily exactly the same (though void must match void). Option A is incorrect because the original method is void. Option B is incorrect because types cannot be arbitrary. Option C is correct regarding the argument list, though the prompt phrasing 'overriden' suggests a typo.

Multiple choice java
  1. Thread

  2. Runnable

  3. Both of these

  4. none of these

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

In Java, a class can extend the Thread class or implement the Runnable interface to define threading behavior. Option A implies implementing 'interface Thread', which is incorrect because Thread is a class. Implementing Runnable is the standard method for enabling a class to be executed by a thread.

Multiple choice java
  1. abstract double area() { }

  2. abstract double area()

  3. abstract double area();

  4. abstract double area(); { }

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

To pass the reference of one EJB to another, you can use Dependency Injection (DI) or JNDI (Java Naming and Directory Interface). In DI, the reference to the EJB is passed through the constructor or a setter method. In JNDI, the EJB is bound to a JNDI name, and the other EJB looks up the name to get a reference to the EJB.

Option C is the correct syntax for an abstract method in a Java abstract class. The abstract keyword is used to indicate that the method has no implementation and must be overridden in a subclass. The parentheses after the method name are used to define the parameter list, and the semicolon at the end indicates the end of the method signature.

Option A is incorrect because it includes a method body, which is not allowed in an abstract method.

Option B is also incorrect because it does not include parentheses to define the parameter list, which is necessary even if the method takes no parameters.

Option D is incorrect because it includes a method body after the semicolon, which is not allowed in an abstract method.

Therefore, the correct answer is:

The Answer is: C

Multiple choice java
  1. It must be marked final

  2. It can be marked abstract

  3. It can be marked public

  4. It can be marked static

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

Method-local inner classes are defined within a method body. They can be marked as abstract or final. However, they cannot have access modifiers like public, private, or protected because their scope is strictly limited to the method, nor can they be static as they are tied to an instance.

Multiple choice java
  1. It can extend exactly one class and implement exactly one interface

  2. It can extend exactly one class and can implement multiple interfaces

  3. It can extend exactly one class or implement exactly one interface

  4. It can implement multiple interfaces regardless of whether it also extends a class

  5. It can implement multiple interfaces if it does not extend a class

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

Anonymous inner classes have specific syntax limitations. They can extend one class OR implement one interface, but not both simultaneously (unlike a named class which can extend a class and implement interfaces). They cannot implement multiple interfaces.

Multiple choice java
  1. X extends Y is correct if and only if X is a class and Y is an interface

  2. X extends Y is correct if and only if X is an interface and Y is a class

  3. X extends Y is correct if X and Y are either both classes or both interfaces

  4. X extends Y is correct for all combinations of X and Y being classes and/or interfaces.

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

The 'extends' keyword is used for inheritance. A class can extend another class (single inheritance). An interface can extend another interface (multiple inheritance allowed). A class cannot extend an interface (it implements it), and an interface cannot extend a class.

Multiple choice java
  1. The attributes of the class are all private

  2. The class refers to a small number of other objects

  3. The object contains only a small number of variables

  4. The object is referred to using an anonymous variable, not directly

  5. The reference variable is declared for an interface type, not a class. The interface provides a small number of methods

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

Low coupling implies minimal dependencies between components. Using an interface type (Option E) reduces dependency on a specific implementation class. Combined with 'small number of methods', this indicates the 'small interface' principle (Interface Segregation), which promotes loose coupling.

Multiple choice java
  1. depth must be an int

  2. dive must be a method.

  3. dive must be the name of an instance field.

  4. submarine must be the name of a class

  5. submarine must be a method.

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

In the statement submarine.dive(depth), the dot operator (.) is used to access a member of the object referenced by 'submarine'. Since parentheses follow 'dive', it is a method call. 'depth' is an argument (could be int, double, etc., not necessarily int), and 'submarine' is a reference variable, not the class name itself.

Multiple choice java
  1. Static

  2. Private

  3. Default

  4. None of the above

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

In Java, top-level classes cannot be declared as private or protected; they must be public or have default (package-private) access. While inner classes can be private, the general rule for classes is that they cannot be private.

Multiple choice java
  1. Compilation error

  2. Runtime Error

  3. Runtime Exception

  4. Other Execptions

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

In Java, you cannot assign a superclass instance to a subclass reference without an explicit cast (and even then, it would fail at runtime if the object isn't actually an instance of the subclass). 'B b = new A();' fails compilation because A is not a B.