Author name: admin

Which of the following mechanisms is used to achieve thread synchronization in Java?

Java MCQ: Which of the following mechanisms is used to achieve thread synchronization in Java? a) Semaphore b) Synchronized block c) Deadlock d) Thread pool Answer: b) Synchronized block Explanation: The synchronized block in Java is used to achieve thread synchronization. Synchronization is the mechanism that ensures that only one thread can access a resource

Which of the following mechanisms is used to achieve thread synchronization in Java? Read More »

Which of the following best describes the lifecycle of a Java thread?

Java MCQ: Which of the following best describes the lifecycle of a Java thread? a) New, Runnable, Running, Blocked, Dead b) New, Ready, Executing, Blocked, Terminated c) New, Runnable, Blocked, Waiting, Terminated d) New, Runnable, Running, Waiting, Terminated Answer: d) New, Runnable, Running, Waiting, Terminated Explanation: The lifecycle of a Java thread consists of the

Which of the following best describes the lifecycle of a Java thread? Read More »

Which method must be overridden when implementing the Runnable interface?

Java MCQ: Which method must be overridden when implementing the Runnable interface? a) start() b) run() c) execute() d) call() Answer: b) run() Explanation: When implementing the Runnable interface in Java, you must override the run() method. The run() method contains the code that defines the task to be executed by the thread. The Runnable

Which method must be overridden when implementing the Runnable interface? Read More »

Can you customize the default behavior of the canonical constructor in Java Records?

Java MCQ: Can you customize the default behavior of the canonical constructor in Java Records? a) No, the canonical constructor cannot be customized b) Yes, by defining a compact constructor c) Yes, by overriding the constructor method d) Yes, but only for records with a single field Answer: b) Yes, by defining a compact constructor

Can you customize the default behavior of the canonical constructor in Java Records? Read More »

Which of the following best describes the difference between Java Records and Lombok?

Java MCQ: Which of the following best describes the difference between Java Records and Lombok? a) Lombok generates boilerplate code using annotations, while records are a built-in language feature b) Lombok is used only for testing, while records are used in production code c) Lombok is part of the Java standard library, while records are

Which of the following best describes the difference between Java Records and Lombok? Read More »

How can Java Records be used with Spring Boot applications?

Java MCQ: How can Java Records be used with Spring Boot applications? a) As a replacement for all Spring Beans b) To define immutable data transfer objects (DTOs) c) To replace all Spring Controllers d) As a substitute for the application.properties file Answer: b) To define immutable data transfer objects (DTOs) Explanation: Java Records can

How can Java Records be used with Spring Boot applications? Read More »

Which of the following is true about the constructors in Java Records?

Java MCQ: Which of the following is true about the constructors in Java Records? a) Records cannot have custom constructors b) Records can only have parameterless constructors c) Records can have custom constructors that validate input or compute derived fields d) Records must have at least two constructors Answer: c) Records can have custom constructors

Which of the following is true about the constructors in Java Records? Read More »

What is the output of the following code snippet involving a Java Record?

Java MCQ: What is the output of the following code snippet involving a Java Record? public record Point(int x, int y) {} public class Main { public static void main(String[] args) { Point p1 = new Point(1, 2); Point p2 = new Point(1, 2); System.out.println(p1.equals(p2)); } } a) true b) false c) Compilation error d)

What is the output of the following code snippet involving a Java Record? Read More »

How do records differ from traditional classes in Java?

Java MCQ: How do records differ from traditional classes in Java? a) Records are mutable by default, while classes are immutable by default b) Records automatically generate methods like equals(), hashCode(), and toString() c) Records cannot implement interfaces d) Records support inheritance, while classes do not Answer: b) Records automatically generate methods like equals(), hashCode(),

How do records differ from traditional classes in Java? Read More »

Which method is used to obtain the superclass of a class using Java Reflection?

Java MCQ: Which method is used to obtain the superclass of a class using Java Reflection? a) getSuperclass() b) getParentClass() c) getBaseClass() d) getSuperClass() Answer: a) getSuperclass() Explanation: The getSuperclass() method in Java is used to obtain the superclass of a class using reflection. This method is part of the Class class and returns a

Which method is used to obtain the superclass of a class using Java Reflection? Read More »

Which utility method in Java Reflection is used to check if a class is an array type?

Java MCQ: Which utility method in Java Reflection is used to check if a class is an array type? a) isArray() b) isArrayType() c) checkArray() d) isArrayClass() Answer: a) isArray() Explanation: The isArray() method in Java is used to check if a class is an array type. This method is part of the Class class

Which utility method in Java Reflection is used to check if a class is an array type? Read More »

How can you access the enum constants of an enum type using Java Reflection?

Java MCQ: How can you access the enum constants of an enum type using Java Reflection? a) getEnumConstants() b) getEnumValues() c) getConstants() d) getEnums() Answer: a) getEnumConstants() Explanation: The getEnumConstants() method in Java is used to access the enum constants of an enum type using reflection. This method is part of the Class class and

How can you access the enum constants of an enum type using Java Reflection? Read More »

How can you retrieve the length of an array using Java Reflection?

Java MCQ: How can you retrieve the length of an array using Java Reflection? a) getArrayLength() b) length() c) getLength() d) arrayLength() Answer: c) getLength() Explanation: The getLength() method in the java.lang.reflect.Array class is used to retrieve the length of an array using Java Reflection. This method is particularly useful when working with arrays of

How can you retrieve the length of an array using Java Reflection? Read More »

Which method is used to create an instance of a class using its constructor with Java Reflection?

Java MCQ: Which method is used to create an instance of a class using its constructor with Java Reflection? a) newInstance() b) create() c) construct() d) build() Answer: a) newInstance() Explanation: The newInstance() method in Java is used to create a new instance of a class using its constructor via reflection. This method is part

Which method is used to create an instance of a class using its constructor with Java Reflection? Read More »

Which method is used to access a private field using Java Reflection?

Java MCQ: Which method is used to access a private field using Java Reflection? a) getField() b) getDeclaredField() c) getPrivateField() d) accessField() Answer: b) getDeclaredField() Explanation: The getDeclaredField() method in Java is used to access a private field of a class using reflection. This method is part of the Class class and returns a Field

Which method is used to access a private field using Java Reflection? Read More »

Which method is used to invoke a method on an object using Java Reflection?

Java MCQ: Which method is used to invoke a method on an object using Java Reflection? a) invokeMethod() b) call() c) execute() d) invoke() Answer: d) invoke() Explanation: The invoke() method in Java is used to invoke a method on an object using reflection. This method is part of the Method class, which is found

Which method is used to invoke a method on an object using Java Reflection? Read More »

Which class is used to obtain information about a class in Java Reflection?

Java MCQ: Which class is used to obtain information about a class in Java Reflection? a) ClassInfo b) ClassInspector c) Class d) ReflectionClass Answer: c) Class Explanation: The Class class in Java is the primary class used to obtain information about a class in Java Reflection. The Class class is part of the java.lang package

Which class is used to obtain information about a class in Java Reflection? Read More »

Which class is used to compress files into ZIP format in Java?

Java MCQ: Which class is used to compress files into ZIP format in Java? a) ZipOutputStream b) ZipFile c) ZipCompressor d) ZipWriter Answer: a) ZipOutputStream Explanation: The ZipOutputStream class in Java is used to compress files into the ZIP format. It is part of the java.util.zip package and provides methods to write compressed data to

Which class is used to compress files into ZIP format in Java? Read More »

Scroll to Top