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?
Direct access to private fields
Using public accessor methods
Improper use of static fields
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?
Inheritance
Encapsulation
Abstraction
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?
Hiding class implementation details
Exposing class fields directly
Encouraging the use of getter and setter methods
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?
Encapsulation leading to improper initialization
Abstraction leading to missing method implementations
Inheritance leading to overridden method issues
Polymorphism leading to incorrect method calls
Q65
Q65 What is an interface in Java?
A class that is used to implement inheritance
A collection of abstract methods and static constants
A template for creating objects
A method that interfaces with the user
Q66
Q66 What is the purpose of a package in Java?
To provide access control mechanisms
To group related classes and interfaces
To speed up the execution time of programs
To serve as containers for Java applets and applications
Q67
Q67 Which of the following statements is true about interfaces in Java?
An interface can contain static methods that can have a body
An interface can contain default methods that have a body
Interfaces can have constructors like classes
All of the above
Q68
Q68 How can a class in one package access a class in another package?
By using the import keyword
By using the package keyword
By using the extends keyword
By using the implements keyword
Q69
Q69 What is a default method in an interface?
A method that is automatically invoked by default when an object is created
A method that provides a default implementation which can be overridden by implementing classes
A static method in an interface
A final method in an interface
Q70
Q70 In Java, which keyword is used to inherit multiple interfaces?
extends
implements
inherits
uses
Q71
Q71 What advantage do packages offer in a Java application?
They allow classes to be reused in multiple applications
They prevent naming conflicts and control access
They increase execution speed
They reduce the memory usage of applications
Q72
Q72 How does the protected access modifier work in the context of packages?
It allows access from any class within the same package or any subclass outside the package
It restricts access to classes within the same package only
It allows access from any class in any package
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();
}
}
Hello, World!
No output, as interfaces cannot have default methods with a body
A compilation error as the method is not overridden in the class
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?
Overriding
Overloading
Polymorphism
Encapsulation
Q75
Q75 How do you make a class that is in a different package accessible to your code?
Declare the class as public and use the extends keyword
Declare the class as public and use the import statement
Use the package keyword
None of the above
Q76
Q76 Which of the following correctly declares a package in a Java file?
package com.example;
package:com.example;
Package com.example;
import com.example;
Q77
Q77 In the context of Java interfaces, what does it mean for a method to be implicitly public?
The method is public by default and does not need the public keyword specified
The method can only be accessed within its package
The method must be explicitly declared as public
None of the above
Q78
Q78 A developer mistakenly tries to instantiate an interface directly in Java.
What type of error will this produce?
Syntax error
Runtime error
Compilation error
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?
Make the class abstract
Remove the interface
Implement all missing methods
Declare the methods as private
Q80
Q80 If a package-private class is mistakenly accessed from outside its package, what error is generated?
Syntax error
Access control error
Compilation error
Runtime error
Q81
Q81 A class implements an interface but uses different method signatures than those declared in the interface.
What must be corrected?
Change the class's method signatures to match the interface
Change the interface's method signatures to match the class
Make the class methods static
None of the above
Q82
Q82 What is an exception in Java?
A type of error that terminates the program
A runtime error that can be handled by the program
A compile-time error
An error that can only occur in development environments
Q83
Q83 Which statement is true about the finally block in Java?
It runs only if an exception is thrown
It always runs, regardless of whether an exception is thrown
It is used to throw exceptions
It runs only if the exception is caught
Q84
Q84 Which keyword is used to manually throw an exception in Java?
throw
throws
error
exception
Q85
Q85 What is the difference between checked and unchecked exceptions in Java?
Checked exceptions are caught at compile time; unchecked exceptions are caught at runtime
Checked exceptions are errors; unchecked exceptions are runtime exceptions
Checked exceptions must be declared or handled; unchecked exceptions do not need to be declared or handled
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?
By using the Error keyword
By using the throw keyword in its body
By including an exception type in its throws clause
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?
try block
catch block
finally block
do block
Q88
Q88 What happens if a try block is followed by multiple catch blocks?
The first catch block executes regardless of the exception thrown
Only the catch block that matches the exception type is executed
All catch blocks are executed in the order they appear
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");
}
}
}
Arithmetic Exception caught
Finally block executed
Arithmetic Exception caught\nFinally block executed
Compilation error
Q90
Q90 How do you handle multiple exceptions in a single catch block in Java?
By separating exceptions with a comma in the catch block
By using the | symbol between exception types in the catch block
By listing each exception type in its own catch block
By using the & symbol between exception types in the catch block