oops banner

OOPs Multiple Choice Questions (MCQs) and Answers

Master Object-Oriented Programming (OOPs) with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of OOPs concepts in Java. Begin your placement preparation journey now!

Q61

Q61 A developer attempts to access a private field directly from outside its class and encounters a compilation error.
What encapsulation feature is being violated?

A

Direct access to private fields

B

Using public accessor methods

C

Improper use of static fields

D

None of the above

Q62

Q62 A Java class implements an interface but fails to provide concrete implementations for all interface methods, leading to a compilation error.
Which principle is not being correctly applied?

A

Inheritance

B

Encapsulation

C

Abstraction

D

Polymorphism

Q63

Q63 In attempting to refactor code for better encapsulation, a developer accidentally makes all class fields public.
What encapsulation best practice is being violated?

A

Hiding class implementation details

B

Exposing class fields directly

C

Encouraging the use of getter and setter methods

D

All of the above

Q64

Q64 A program crashes at runtime when trying to invoke a method on an object that was supposed to be fully initialized.
The likely issue is a failure in applying which concept?

A

Encapsulation leading to improper initialization

B

Abstraction leading to missing method implementations

C

Inheritance leading to overridden method issues

D

Polymorphism leading to incorrect method calls

Q65

Q65 What is an interface in Java?

A

A class that is used to implement inheritance

B

A collection of abstract methods and static constants

C

A template for creating objects

D

A method that interfaces with the user

Q66

Q66 What is the purpose of a package in Java?

A

To provide access control mechanisms

B

To group related classes and interfaces

C

To speed up the execution time of programs

D

To serve as containers for Java applets and applications

Q67

Q67 Which of the following statements is true about interfaces in Java?

A

An interface can contain static methods that can have a body

B

An interface can contain default methods that have a body

C

Interfaces can have constructors like classes

D

All of the above

Q68

Q68 How can a class in one package access a class in another package?

A

By using the import keyword

B

By using the package keyword

C

By using the extends keyword

D

By using the implements keyword

Q69

Q69 What is a default method in an interface?

A

A method that is automatically invoked by default when an object is created

B

A method that provides a default implementation which can be overridden by implementing classes

C

A static method in an interface

D

A final method in an interface

Q70

Q70 In Java, which keyword is used to inherit multiple interfaces?

A

extends

B

implements

C

inherits

D

uses

Q71

Q71 What advantage do packages offer in a Java application?

A

They allow classes to be reused in multiple applications

B

They prevent naming conflicts and control access

C

They increase execution speed

D

They reduce the memory usage of applications

Q72

Q72 How does the protected access modifier work in the context of packages?

A

It allows access from any class within the same package or any subclass outside the package

B

It restricts access to classes within the same package only

C

It allows access from any class in any package

D

None of the above

Q73

Q73 What will be the output of the following interface and class implementation in Java?
interface Speakable {
default void speak() {
System.out.println("Hello, World!");
}
}
class Speaker implements Speakable {
public static void main(String[] args) {
new Speaker().speak();
}
}

A

Hello, World!

B

No output, as interfaces cannot have default methods with a body

C

A compilation error as the method is not overridden in the class

D

None of the above

Q74

Q74 Given an interface Animal and classes Dog and Cat that implement Animal, which Java feature allows a method in a different class to accept any Animal type?

A

Overriding

B

Overloading

C

Polymorphism

D

Encapsulation

Q75

Q75 How do you make a class that is in a different package accessible to your code?

A

Declare the class as public and use the extends keyword

B

Declare the class as public and use the import statement

C

Use the package keyword

D

None of the above

Q76

Q76 Which of the following correctly declares a package in a Java file?

A

package com.example;

B

package:com.example;

C

Package com.example;

D

import com.example;

Q77

Q77 In the context of Java interfaces, what does it mean for a method to be implicitly public?

A

The method is public by default and does not need the public keyword specified

B

The method can only be accessed within its package

C

The method must be explicitly declared as public

D

None of the above

Q78

Q78 A developer mistakenly tries to instantiate an interface directly in Java.
What type of error will this produce?

A

Syntax error

B

Runtime error

C

Compilation error

D

Logical error

Q79

Q79 A Java class fails to compile because it does not provide implementations for all methods of an implemented interface.
What is the most direct solution to this problem?

A

Make the class abstract

B

Remove the interface

C

Implement all missing methods

D

Declare the methods as private

Q80

Q80 If a package-private class is mistakenly accessed from outside its package, what error is generated?

A

Syntax error

B

Access control error

C

Compilation error

D

Runtime error

Q81

Q81 A class implements an interface but uses different method signatures than those declared in the interface.
What must be corrected?

A

Change the class's method signatures to match the interface

B

Change the interface's method signatures to match the class

C

Make the class methods static

D

None of the above

Q82

Q82 What is an exception in Java?

A

A type of error that terminates the program

B

A runtime error that can be handled by the program

C

A compile-time error

D

An error that can only occur in development environments

Q83

Q83 Which statement is true about the finally block in Java?

A

It runs only if an exception is thrown

B

It always runs, regardless of whether an exception is thrown

C

It is used to throw exceptions

D

It runs only if the exception is caught

Q84

Q84 Which keyword is used to manually throw an exception in Java?

A

throw

B

throws

C

error

D

exception

Q85

Q85 What is the difference between checked and unchecked exceptions in Java?

A

Checked exceptions are caught at compile time; unchecked exceptions are caught at runtime

B

Checked exceptions are errors; unchecked exceptions are runtime exceptions

C

Checked exceptions must be declared or handled; unchecked exceptions do not need to be declared or handled

D

Checked exceptions are fatal; unchecked exceptions are not fatal

Q86

Q86 How can a method signal to its caller that it might throw certain types of exceptions?

A

By using the Error keyword

B

By using the throw keyword in its body

C

By including an exception type in its throws clause

D

By catching the exception within the method

Q87

Q87 In exception handling, which block is used to execute important code such as closing connections, whether an exception occurs or not?

A

try block

B

catch block

C

finally block

D

do block

Q88

Q88 What happens if a try block is followed by multiple catch blocks?

A

The first catch block executes regardless of the exception thrown

B

Only the catch block that matches the exception type is executed

C

All catch blocks are executed in the order they appear

D

None of the catch blocks are executed unless they all match the exception

Q89

Q89 What will be the output of the following Java code?
public class Main {
public static void main(String[] args) {
try { int divideByZero = 5 / 0;
}
catch (ArithmeticException e) {
System.out.println("Arithmetic Exception caught");
} finally {
System.out.println("Finally block executed");
}
}
}

A

Arithmetic Exception caught

B

Finally block executed

C

Arithmetic Exception caught\nFinally block executed

D

Compilation error

Q90

Q90 How do you handle multiple exceptions in a single catch block in Java?

A

By separating exceptions with a comma in the catch block

B

By using the | symbol between exception types in the catch block

C

By listing each exception type in its own catch block

D

By using the & symbol between exception types in the catch block

ad verticalad vertical
ad