☕ Java MCQ Questions – Page 131
Questions 2601–2620 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat kind of error will occur when compiling this Java code?
java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("output.txt"));
// The write() method can throw IOException, but it's not caught by NullPointerException
bw.write("Test data.");
} catch (NullPointerException e) {
System.err.println("Caught NPE: " + e.getMessage());
}
}
}
Under what circumstance would a `do-while` loop in Java result in an infinite loop?
Which compile-time error will occur when compiling the `Child` class?
java
class Parent {
Parent(int x) {
System.out.println("Parent: " + x);
}
}
class Child extends Parent {
Child() {
System.out.println("Child default constructor");
super(10);
}
}
public class Main {
public static void main(String[] args) {
new Child();
}
}
What is the most likely output of this code?
java
public class JoinWithoutSyncTest {
private int counter = 0; // Not volatile, not synchronized
public void increment() {
for (int i = 0; i < 1000; i++) {
counter++;
}
}
public static void main(String[] args) throws InterruptedException {
JoinWithoutSyncTest test = new JoinWithoutSyncTest();
Thread t1 = new Thread(test::increment);
Thread t2 = new Thread(test::increment);
t1.start();
t2.start();
t1.join(); // Main waits for t1 to finish
t2.join(); // Main waits for t2 to finish
System.out.println("Final counter: " + test.counter);
}
}
What is the compile-time error in the `Human` class?
java
abstract class LivingBeing {
abstract void breath();
public void eat() {
System.out.println("Eating...");
}
}
class Human extends LivingBeing { // Line 7
public void talk() {
System.out.println("Talking...");
}
}
public class World {
public static void main(String[] args) {
LivingBeing h = new Human();
h.eat();
}
}
What is the minimum number of times the code inside a do-while loop will execute?
You are designing an API for a critical financial system. A specific operation might fail due to an invalid input parameter provided by the *caller*, indicating a programming error in the calling code, rather than a transient system issue. Which type of custom exception is most appropriate for this scenario, and why?
Which of the following methods must be called from within a `synchronized` block or method?
Which of the following methods, when called on a `Thread` object, *can* throw an `InterruptedException`?
What is the primary advantage of using an `if-else if-else` ladder compared to a series of independent `if` statements (without `else`) for handling multiple mutually exclusive conditions?
`FileReader` is designed to read data as what type of stream?
What does the following Java code print?
java
import java.io.IOException;
import java.io.FileNotFoundException;
class Processor {
void processFile() throws IOException {
System.out.println("Processor handling generic file");
}
}
class ImageProcessor extends Processor {
@Override
void processFile() throws FileNotFoundException { // Valid: FileNotFoundException is a subclass of IOException
System.out.println("ImageProcessor handling image file");
}
}
public class Main {
public static void main(String[] args) {
Processor p = new ImageProcessor();
try {
p.processFile();
} catch (IOException e) {
System.out.println("Caught exception");
}
}
}
What does this Java code print?
java
import java.io.File;
import java.io.IOException;
public class FileTest {
public static void main(String[] args) {
File file = new File("temp_file_q10.txt");
try {
file.createNewFile();
System.out.println(file.isFile() + " " + file.isDirectory());
} catch (IOException e) {
System.out.println("Error");
} finally {
file.delete(); // Clean up
}
}
}
What is the error in this code?
java
import java.util.TreeMap;
public class Test {
public static void main(String[] args) {
TreeMap<Object, String> map = new TreeMap<>();
map.put("StringKey", "Value1");
map.put(100, "Value2"); // Integer key
System.out.println(map.size());
}
}
What happens if you directly call the `run()` method of an object that implements `Runnable` instead of passing it to a `Thread` and calling `start()`?
What is the final state of thread `t` printed in the output?
java
public class ThreadStateDemo9 {
private static final Object monitor = new Object();
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
synchronized (monitor) {
try {
System.out.println("Thread is timed waiting.");
monitor.wait(100); // Wait for 100ms
System.out.println("Thread resumed after timeout.");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
t.start();
Thread.sleep(50); // Ensure thread enters wait()
System.out.println("State of t (mid-wait): " + t.getState());
Thread.sleep(100); // Allow timeout
System.out.println("State of t (final): " + t.getState());
}
}
What does this code print?
java
import java.util.LinkedList;
public class Test {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("One");
list.add("Two");
String[] arr = list.toArray(new String[0]);
System.out.println(arr.length + " " + arr[0]);
}
}
What kind of error will occur when compiling this Java code?
java
public class Main {
public static void main(String[] args) {
int score = 85;
else {
System.out.println("Invalid score");
}
}
}
What is the output of this code snippet regarding StringBuilder equality?
java
public class StringBuilderTest {
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder("test");
StringBuilder sb2 = new StringBuilder("test");
String s1 = "test";
System.out.println(sb1.equals(sb2) + ", " + sb1.toString().equals(s1));
}
}
What is the return type of a constructor in Java?