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. Methods and variables

  2. class

  3. variables

  4. Methods

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

To answer this question, the user needs to know the basic concepts of object-oriented programming (OOP) and the differences between method overriding and method overloading.

Option A: Methods and variables - This option is partially correct. Overriding and overloading both apply to methods in OOP, not to variables. Therefore, this option is only correct for the first part of the question.

Option B: Class - This option is not correct. Overriding and overloading are not for classes, but for methods within classes.

Option C: Variables - This option is not correct. Overriding and overloading are not for variables, but for methods within classes.

Option D: Methods - This option is correct. Overriding and overloading are techniques used for methods in OOP. Method overriding means providing a new implementation of a method in a subclass that is already defined in its superclass. Method overloading means defining multiple methods with the same name in the same class, but with different parameters.

Therefore, the correct answer is:

The answer is: D. Methods

Multiple choice java
  1. method

  2. native

  3. subclasses

  4. reference

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

native is a Java keyword used to indicate that a method is implemented in platform-dependent code (like C or C++). 'method', 'subclasses', and 'reference' are descriptive terms used in programming but are not reserved keywords in the Java language specification.

Multiple choice java
  1. public double methoda();

  2. public final double methoda();

  3. static void methoda(double d1);

  4. protected void methoda(double d1);

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

To solve this question, the user needs to understand the syntax and rules for declaring methods within an interface in Java.

In Java, an interface is a collection of abstract methods, which means the methods declared within an interface do not have a body. The purpose of an interface is to define a contract that classes can implement, specifying the methods they must provide.

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

A. public double methoda(); This option is a valid declaration within an interface definition. It declares a public method named "methoda" that returns a double value. Since interfaces only contain abstract methods, there is no need to provide a method body.

B. public final double methoda(); This option is not a valid declaration within an interface definition. The "final" keyword cannot be used to modify a method declaration within an interface. The "final" keyword is used to indicate that a method cannot be overridden by a subclass.

C. static void methoda(double d1); This option is not a valid declaration within an interface definition. The "static" keyword cannot be used to modify a method declaration within an interface. Static methods belong to the class itself, not an instance of the class.

D. protected void methoda(double d1); This option is not a valid declaration within an interface definition. The "protected" keyword cannot be used to modify a method declaration within an interface. Protected methods are accessible within the same package and by subclasses.

Therefore, the valid declaration within an interface definition is:

The Answer is: A. public double methoda();

Multiple choice java
  1. public

  2. private

  3. protected

  4. default access

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation
  • Public access is the least restrictive access modifier. Members with public access can be accessed from anywhere in the program.
  • Private access is the most restrictive access modifier. Members with private access can only be accessed from within the class in which they are declared.
  • Protected access is a middle ground between public and private access. Members with protected access can be accessed from within the class in which they are declared, and from subclasses of that class.
  • Default access is also known as package-private access. Members with default access can be accessed from any class in the same package.

So, the most restrictive access modifier that allows a class in the same package to access members of another class in the same package is Default.

Here is a table that summarizes the different access modifiers in Java:

Access modifier Visibility
public Anywhere in the program
private Only within the class in which it is declared
protected Within the class in which it is declared, and from subclasses of that class
default Only within the same package

The difference between protected and default access is that protected allows access from subclasses in other packages, while default does not. For example, if you have a class Animal in package A, and a class Dog in package B that extends Animal, then the class Dog can access the protected members of Animal, but not the default members. However, if you have a class Cat in package A that extends Animal, then the class Cat can access both the protected and the default members of Animal.

Multiple choice java
  1. protected int a;

  2. transient int b = 3;

  3. private synchronized int e;

  4. volatile int d;

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

The 'synchronized' modifier applies only to methods and code blocks, not to class-level variable declarations. Therefore, 'private synchronized int e' is invalid. 'transient' and 'volatile' are valid field modifiers, and 'protected' is a valid access modifier for fields.

Multiple choice java
  1. public static short stop = 23;

  2. protected short stop = 23;

  3. transient short stop = 23;

  4. final void madness(short stop);

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

Variables in an interface are implicitly public, static, and final. Therefore, explicitly declaring them as public static final (with any primitive type like short) is legal. Interface fields cannot be protected or transient, and interface methods cannot be final.

Multiple choice java
  1. TreeMap

  2. HashMap

  3. LinkedHashMap

  4. The answer depends on the implementation of the existing instance.

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

To solve this question, the user needs to know the differences between the concrete implementations of the Map interface and their iteration orders.

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

A. TreeMap: This option is incorrect because TreeMap sorts its elements in natural order or by a custom Comparator, whereas the iteration order of the existing instance of the Map may not be sorted. Thus, creating a new instance of TreeMap would not preserve the same iteration order as the existing instance.

B. HashMap: This option is incorrect because HashMap does not guarantee any particular iteration order. Thus, creating a new instance of HashMap would not preserve the same iteration order as the existing instance.

C. LinkedHashMap: This option is correct. LinkedHashMap is similar to HashMap but maintains a doubly-linked list running through all its entries, which allows it to maintain the insertion order. Thus, creating a new instance of LinkedHashMap would preserve the same iteration order as the existing instance.

D. The answer depends on the implementation of the existing instance: This option is incorrect because the iteration order of the existing instance of the Map may not be guaranteed by all implementations of the Map interface. Thus, it is not a reliable solution to assume that any implementation of the Map interface would preserve the same iteration order as the existing instance.

The Answer is: C. LinkedHashMap

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.

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

To answer this question, one needs to understand the concept of an anonymous inner class in Java.

An anonymous inner class is a class that is defined and instantiated at the same time, without explicitly giving it a name. It is typically used when you need to create a class that is used only once and does not need to be reused.

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

A. It can extend exactly one class and implement exactly one interface. This option is incorrect. An anonymous inner class can extend a class or implement an interface, but it cannot do both at the same time. It can either extend a class or implement an interface, but not both simultaneously.

B. It can extend exactly one class and can implement multiple interfaces. This option is incorrect. As mentioned earlier, an anonymous inner class cannot extend a class and implement multiple interfaces at the same time. It can either extend a class or implement an interface, but not both simultaneously.

C. It can extend exactly one class or implement exactly one interface. This option is correct. An anonymous inner class can either extend a class or implement an interface. It cannot do both at the same time, but it can choose to extend a class or implement an interface based on the requirements.

D. It can implement multiple interfaces regardless of whether it also extends a class. This option is incorrect. An anonymous inner class can implement multiple interfaces, but only if it does not extend a class. If it extends a class, it can only implement one interface.

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

A method-local inner class is defined within a method body. It can be marked as abstract or final, but it cannot have access modifiers like public, private, or protected, nor can it be marked static because it is local to an instance method's scope.

Multiple choice java
  1. You must have a reference to an instance of the enclosing class in order to instantiate it.

  2. It does not have access to nonstatic members of the enclosing class.

  3. It's variables and methods must be static.

  4. It must extend the enclosing class.

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

To answer this question, you need to have an understanding of nested classes in Java.

A static nested class is a class that is defined inside another class, and it is marked as static. Here is an explanation of each option:

A. You must have a reference to an instance of the enclosing class in order to instantiate it. This statement is false. Unlike an inner class, a static nested class does not require an instance of the enclosing class to be instantiated. You can create an instance of a static nested class without having an instance of the enclosing class.

B. It does not have access to nonstatic members of the enclosing class. This statement is true. Since a static nested class is static, it does not have access to nonstatic members (variables or methods) of the enclosing class. It can only access static members of the enclosing class.

C. Its variables and methods must be static. This statement is false. Although the static nested class is static, it can have both static and nonstatic variables and methods. It is not required for all of its members to be static.

D. It must extend the enclosing class. This statement is false. A static nested class does not have to extend the enclosing class. It is a separate class and can have its own inheritance hierarchy.

Based on the explanations above, the correct statement about a static nested class is:

The Answer is: B. It does not have access to nonstatic members of the enclosing class.

Multiple choice java
  1. Runnable r = new Runnable() { };

  2. Runnable r = new Runnable(public void run() { });

  3. Runnable r = new Runnable { public void run(){}};

  4. System.out.println(new Runnable() {public void run() { }});

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

Option D constructs and uses an anonymous inner class instance of Runnable by passing it directly to the println method. Options A and B are syntax errors (A is missing a body or needs braces; B has invalid syntax inside parens), and Option C is missing parentheses required for the constructor invocation.

Multiple choice java
  1. The code will compile without changes.

  2. The code will compile if public Tree() { Plant(); } is added to the Tree class.

  3. The code will compile if public Plant() { Tree(); } is added to the Plant class.

  4. The code will compile if public Plant() { this("fern"); } is added to the Plant class.

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

This code defines two classes, Plant and Tree. Tree extends Plant, meaning that it inherits all of the fields and methods of Plant.

The Plant class has a single constructor that takes a String argument and sets the name field to that value. It also has a getter method for the name field.

The Tree class has two methods, growFruit() and dropLeaves(), but no constructors.

Option A) The code will compile without changes. - This is incorrect because the Tree class does not have a constructor, and it needs to call the constructor of the parent class when an instance of Tree is created.

Option B) The code will compile if public Tree() { Plant(); } is added to the Tree class. - This is incorrect because the correct syntax to call the parent constructor is super(), not the name of the parent class.

Option C) The code will compile if public Plant() { Tree(); } is added to the Plant class. - This is incorrect because it does not make sense to call the constructor of a subclass from the constructor of the parent class.

Option D) The code will compile if public Plant() { this("fern"); } is added to the Plant class. - This is correct because it adds a new constructor to the Plant class that takes no arguments and calls another constructor of the same class with a default argument. This allows the Tree class to inherit this constructor and call it with the default argument when an instance of Tree is created.

Therefore, the correct answer is D.