☕ Java MCQ Questions – Page 129
Questions 2561–2580 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is the implicit return type of a Java constructor?
What is the consequence of passing a `null` `Boolean` object to an `if` condition, such as `if (myBooleanObject)`?
What is the error encountered when running this Java code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
public class FileReaderError10 {
public static void main(String[] args) {
File file = new File("test_fr10.txt");
try {
file.createNewFile();
try (FileReader fr = new FileReader(file)) {
fr.close(); // Close the stream
boolean readyStatus = fr.ready(); // Check ready on a closed stream
System.out.println("Ready status: " + readyStatus);
}
} catch (IOException e) {
System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
} finally {
file.delete();
}
}
}
What kind of error will occur when compiling or running the following Java code?
java
public class SBError {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Start"); // Length 5
String toInsert = "End";
// Attempt to insert at index 6 (length + 1)
sb.insert(sb.length() + 1, toInsert);
System.out.println(sb);
}
}
What is the runtime behavior of this Java `TreeMap` code?
java
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<String, Integer> map = new TreeMap<>();
map.put("One", 1);
map.put("Two", 2);
Object nonStringKey = new Integer(3);
map.get(nonStringKey);
}
}
Where is the `throws` keyword used in a Java program?
Is it generally possible for `FileWriter.write(char[] cbuf, int off, int len)` to successfully write *fewer* than `len` characters to the underlying file without throwing an `IOException`?
What kind of error will occur when executing the following Java code?
java
public class MyMonitor {
public void myMethod() {
try {
this.wait(); // Calling wait() without holding the monitor
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
public static void main(String[] args) {
MyMonitor monitor = new MyMonitor();
new Thread(monitor::myMethod).start();
}
}
What does this Java code print to the console?
java
public class StringBufferTest {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("HelloWorld");
sb.replace(5, 10, " Java");
System.out.print(sb);
}
}
If you want to create a custom exception that callers are *not required* to handle or declare, which class should it extend?
Which exception will be thrown when executing this Java code?
java
public class StringError7 {
public static void main(String[] args) {
String val = "3.14.15";
double d = Double.parseDouble(val);
System.out.println(d);
}
}
If a class has two methods: `void calculate(int a, int b)` and `void calculate(int... numbers)`, which method will be invoked by the call `calculate(10, 20)`?
A thread instance is created using `new Thread()`. What is its initial state before the `start()` method is invoked?
What is the output of this Java code?
java
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileWriterCharArrayOffset {
public static void main(String[] args) {
String filename = "char_array_offset.txt";
char[] data = {'J', 'a', 'v', 'a', 'I', 'O', 'S', 't', 'r', 'e', 'a', 'm'};
try (FileWriter fw = new FileWriter(filename)) {
fw.write(data, 4, 3);
fw.write(" is ");
fw.write(data, 0, 4);
} catch (IOException e) { System.out.println("Error: " + e.getMessage()); }
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
System.out.println(br.readLine());
} catch (IOException e) { System.out.println("Error reading: " + e.getMessage());
} finally { try { Files.deleteIfExists(Path.of(filename)); } catch (IOException ignored) {} }
}
}
What does this code print?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
public class BufferedReaderTest {
public static void main(String[] args) {
String data = "HelloWorld";
char[] buffer = new char[5];
try (BufferedReader br = new BufferedReader(new StringReader(data))) {
int charsRead = br.read(buffer, 0, 5);
System.out.println(charsRead);
System.out.println(new String(buffer));
charsRead = br.read(buffer, 0, 3);
System.out.println(charsRead);
System.out.println(new String(buffer, 0, charsRead));
} catch (IOException e) {
e.printStackTrace();
}
}
}
When running a Java application with a restrictive `SecurityManager`, which type of exception is most likely to be thrown by `java.io.File` methods like `delete()`, `mkdir()`, or `exists()` if the required permissions are not granted?
Which method must be implemented by a class that implements the `Runnable` interface?
If a HashMap is initialized with a capacity of 17 and a load factor of 0.75, what will be its actual internal initial capacity and when does the first automatic rehashing operation typically occur?
A method `foo()` declares `throws CustomCheckedException`. What must a calling method do if it invokes `foo()`?
What does this Java code snippet using a switch expression with `yield` print?
java
public class SwitchTest {
public static void main(String[] args) {
int month = 4; // April
String quarter = switch (month) {
case 1, 2, 3 -> {
yield "Q1";
}
case 4, 5, 6 -> {
yield "Q2";
}
case 7, 8, 9 -> {
yield "Q3";
}
default -> {
yield "Q4";
}
};
System.out.println(quarter);
}
}