Which statement accurately describes the guarantees and limitations of `java.io.File.deleteOnExit()`?
✅ Correct Answer: A) The files are deleted when the Java Virtual Machine terminates normally, but there is no guarantee if the JVM crashes or is forcibly terminated. The order of deletion is not specified.
`deleteOnExit()` registers a hook to delete the file when the JVM exits normally. It provides no guarantee for abnormal termination and does not specify the order in which multiple registered files will be deleted.
Q2662hard
Evaluate the Java expression: `5 + 2 * 3 ^ 4 | 1`. Assume `^` is bitwise XOR and `|` is bitwise OR.
✅ Correct Answer: D) 15
According to operator precedence: `2 * 3` is `6`. Then `5 + 6` is `11`. Next, `11 ^ 4` (1011 XOR 0100) is `1111` (15). Finally, `15 | 1` (1111 OR 0001) is `1111` (15).
Q2663easy
When a thread enters a `synchronized` block or method, what type of lock does it acquire?
✅ Correct Answer: C) Intrinsic lock (monitor lock)
The `synchronized` keyword in Java uses an intrinsic lock, also known as a monitor lock, which is associated with every object.
Q2664medium
Does changing only the access modifier (e.g., from `public` to `private`) allow you to overload a method in Java?
✅ Correct Answer: C) No, access modifiers are not part of the method signature for overloading.
Access modifiers are not part of a method's signature for the purpose of overloading. Overloading requires a change in the parameter list (number, type, or order of parameters), not just access level.
Q2665mediumcode output
What is the output of this Java code?
java
import java.util.TreeSet;
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
TreeSet<Integer> numbers = new TreeSet<>(Comparator.reverseOrder());
numbers.add(5);
numbers.add(2);
numbers.add(8);
numbers.add(1);
System.out.println(numbers);
}
}
✅ Correct Answer: A) [8, 5, 2, 1]
The `TreeSet` is initialized with `Comparator.reverseOrder()`, which sorts elements in descending order instead of the natural ascending order.
Q2666hard
What is the primary use case for calling `ObjectOutputStream.enableReplaceObject()` and overriding `replaceObject()` in a subclass of `ObjectOutputStream`?
✅ Correct Answer: A) To allow replacement of objects with proxies *during* the serialization process before they are written to the stream.
`enableReplaceObject()` combined with `replaceObject()` allows an `ObjectOutputStream` to substitute one object with another during serialization, before it's actually written to the stream, useful for security or object transformation.
Q2667easycode error
What kind of error will occur when compiling the following Java code snippet?
java
import java.io.File;
public class FileError7 {
public static void main(String[] args) {
File file = new File(123);
System.out.println(file.getPath());
}
}
✅ Correct Answer: B) A compilation error: `incompatible types: int cannot be converted to String`.
The `File` class constructor primarily expects a `String` representing the path. Providing an `int` literal (123) directly will cause a compilation error because Java cannot implicitly convert `int` to `String` in this context.
Q2668easy
Consider a `for` loop structured as `for (int i = 0; i < N; i++)`. If `N` has a positive integer value, how many times will the loop's body be executed?
✅ Correct Answer: B) `N` times
When `i` starts at 0 and increments until it is no longer less than `N`, the loop will execute `N` times (for `i = 0, 1, ..., N-1`).
Q2669easy
What happens if an exception is thrown in a `try` block, but there is no `catch` block that can handle that specific exception type?
✅ Correct Answer: C) The exception will propagate up the call stack, potentially terminating the program if not caught higher up.
If an exception is not caught by any matching `catch` block, it is rethrown (propagates) up the call stack until it finds a handler or reaches the main method, potentially terminating the program.
Q2670easycode error
What is the compile-time error in the following Java code?
java
public class DataTypeBigIntError {
public static void main(String[] args) {
int hugeNumber = 3000000000;
System.out.println(hugeNumber);
}
}
✅ Correct Answer: A) Integer number too large
The literal `3000000000` (3 billion) exceeds the maximum value an `int` can hold (which is approximately 2.147 billion). Integer literals are treated as `int` by default, so this results in an 'integer number too large' compile-time error.
Q2671medium
What is a significant characteristic of the `LocalDate` and `LocalDateTime` classes introduced in Java 8's Date and Time API?
✅ Correct Answer: B) They are inherently immutable and thus thread-safe.
The `java.time` classes, including `LocalDate` and `LocalDateTime`, are designed to be immutable. This design choice makes them inherently thread-safe and prevents unexpected side effects from concurrent modifications.
Opening a `FileWriter` (in non-append mode) creates or truncates the file. Writing an empty string does not add any content, resulting in a 0-byte file that exists. Reading an empty file yields a null or empty string.
Q2673easy
Which operator is used to calculate the remainder of a division in Java?
✅ Correct Answer: C) %
The modulus operator (%) returns the remainder of a division operation. It is commonly used with integer types.
Q2674easycode output
What is the output of this code?
java
public class EmptyTask implements Runnable {
@Override
public void run() {
// This run method is empty
}
public static void main(String[] args) {
System.out.println("Starting task...");
Thread thread = new Thread(new EmptyTask());
thread.start();
System.out.println("Task started.");
}
}
✅ Correct Answer: A) Starting task...
Task started.
The `run()` method can be empty. When `thread.start()` is called, a new thread is indeed created and its `run()` method is invoked. Since the `run()` method is empty, it simply completes without printing anything. The main thread's print statements execute as expected.
Q2675mediumcode error
What is the error in the following Java code?
java
package com.example;
class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000.0);
System.out.println(account.balance);
}
}
✅ Correct Answer: A) Compilation error: The field BankAccount.balance is not visible.
The 'balance' field is declared as private, meaning it cannot be accessed directly from outside the BankAccount class, including from the Main class. This will result in a compilation error.
Q2676hardcode error
What is the result of running this Java code?
java
import java.util.Comparator;
import java.util.TreeMap;
public class TreeMapError5 {
public static void main(String[] args) {
Comparator<String> lenComparator = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return Integer.compare(s1.length(), s2.length());
}
};
TreeMap<String, Integer> map = new TreeMap<>(lenComparator);
map.put("hello", 5);
map.put("world", 5);
map.put(null, 0);
System.out.println(map.size());
}
}
✅ Correct Answer: B) A NullPointerException is thrown.
When a null key is inserted into a `TreeMap` with a custom `Comparator`, the `compare` method of that `Comparator` will be invoked with a null argument. If the `Comparator` implementation does not explicitly handle nulls, it will result in a `NullPointerException` when attempting to call methods on the null reference.
Q2677hardcode error
What compilation error will occur when compiling this Java code?
java
public class NonExistentLabelBreak {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
if (i == 2) {
break myMissingLabel;
}
System.out.println(i);
}
}
}
✅ Correct Answer: A) `label myMissingLabel not found`
The label `myMissingLabel` is not declared anywhere in the accessible scope before its usage, leading to a "label not found" compilation error.
Q2678easy
Which keyword is used in Java to implement synchronization?
✅ Correct Answer: C) `synchronized`
The `synchronized` keyword in Java is specifically designed to enforce mutual exclusion on critical sections of code or methods.
Q2679hardcode output
What is the output of this code?
java
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
String data = "A\nB\nC";
BufferedReader br = new BufferedReader(new StringReader(data));
System.out.print((char)br.read()); // A
br.readLine(); // Consumes \n and 'B\n'
System.out.print((char)br.read()); // C
System.out.println(br.readLine()); // null (after C and EOF)
br.close();
}
}
✅ Correct Answer: A) ACnull
The first `read()` consumes 'A'. The stream is at '\n'. The `readLine()` consumes '\nB\n', leaving the stream at 'C'. The next `read()` consumes 'C'. The stream is now at EOF. The final `readLine()` returns `null`, which is then printed as "null" by `println`.
Q2680hard
In Java versions prior to Java 7, without `try-with-resources`, what was considered the most robust pattern for ensuring a `BufferedWriter` was closed in a `finally` block, especially if a `write` operation also threw an `IOException`?
✅ Correct Answer: B) Wrap `writer.close()` in its own `try-catch` block inside the `finally`, logging or handling any `IOException` from `close()` separately, allowing the primary exception to propagate.
Prior to Java 7, the most robust way was to place the `close()` call within its own `try-catch` block inside the `finally`. This ensures the stream is closed even if an exception occurs during the `close()`, and crucially, it prevents the `close()` exception from overriding or suppressing the original exception from the `try` block, which would then propagate as intended.