☕ Java MCQ Questions – Page 16
Questions 301–320 of 3994 total — Java interview practice
▶ Practice All Java QuestionsA developer encounters issues on Windows with `File` operations (e.g., `exists()`, `delete()`) failing for paths exceeding approximately 260 characters. What is the fundamental reason for this behavior when using `java.io.File`?
What compilation error will occur when compiling the following Java code?
java
import java.io.IOException;
interface MyConsumer<T> {
void accept(T t);
}
public class LambdaCheckedException {
public static void main(String[] args) {
MyConsumer<String> mc = s -> {
throw new IOException("Simulated IO error");
};
}
}
What error will occur when compiling the following Java code?
java
import java.util.HashSet;
import java.util.Set;
import java.util.Arrays;
public class MyClass {
public static void main(String[] args) {
Set<String> colors = new HashSet<>();
colors.addAll({"Red", "Green", "Blue"}); // This line will cause an error
System.out.println(colors.size());
}
}
What does this code print?
java
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> myQueue = new LinkedList<>();
System.out.println(myQueue.poll());
}
}
What is the primary purpose of using the `instanceof` operator before performing a downcast in Java?
What is the output of this code?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
public class BufferedReaderTest {
public static void main(String[] args) {
String data = "First\n\nThird\n";
try (BufferedReader br = new BufferedReader(new StringReader(data))) {
System.out.println("1: " + br.readLine().isEmpty());
System.out.println("2: " + br.readLine().isEmpty());
System.out.println("3: " + br.readLine());
System.out.println("4: " + (br.readLine() == null));
System.out.println("5: " + (br.readLine() == null));
} catch (IOException e) {
e.printStackTrace();
}
}
}
What compile-time error will occur in the `main` method?
java
class MyCustomCheckedException extends Exception {
public MyCustomCheckedException(String message) { super(message); }
}
public class Main {
public static void someMethod() throws MyCustomCheckedException {
throw new MyCustomCheckedException("Specific error");
}
public static void main(String[] args) {
try {
someMethod();
} catch (Error e) { // This catch block cannot handle MyCustomCheckedException
System.out.println("Caught an error: " + e.getMessage());
}
}
}
Which type of loop is a do-while loop categorized as?
What is the output of this Java code?
java
public class ExceptionFlow3 {
public static void main(String[] args) {
System.out.println(testMethod());
}
public static String testMethod() {
try {
System.out.println("Try block");
return "Returned from try";
} catch (Exception e) {
System.out.println("Catch block");
return "Returned from catch";
} finally {
System.out.println("Finally block");
}
}
}
What is the compile-time error in the `Beta` class?
java
class Alpha {
private void show() {
System.out.println("Alpha show");
}
}
class Beta extends Alpha {
@Override
public void show() { // Line 8
System.out.println("Beta show");
}
}
public class Driver {
public static void main(String[] args) {
Beta b = new Beta();
b.show();
}
}
What will be printed to the console?
java
public class MultiArrayQ4 {
public static void main(String[] args) {
int[][] data = new int[2][];
data[0] = new int[3];
data[1] = null;
try {
System.out.println(data[1][0]);
} catch (Exception e) {
System.out.println(e.getClass().getSimpleName());
}
}
}
What is the output of this code?
java
public class StringTest {
public static void main(String[] args) {
String s1 = " Hello World ";
String s2 = s1.trim();
System.out.println(s2.length());
}
}
What does this program print?
java
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int sumCoordinates() {
return x + y;
}
public static void main(String[] args) {
Point p1 = new Point(5, 10);
System.out.println(p1.sumCoordinates());
}
}
Find the compile-time error in the following Java code.
java
public class DataTypeNegativeCharError {
public static void main(String[] args) {
char unicodeChar = -5;
System.out.println(unicodeChar);
}
}
What compile-time error will occur in the lambda expression?
java
import java.util.concurrent.Callable;
class TaskFailedException extends Exception {
public TaskFailedException(String message) { super(message); }
}
public class Main {
public static void main(String[] args) {
Callable<String> task = () -> {
if (true) {
// Checked exception thrown without being caught or declared
throw new TaskFailedException("Task could not be completed");
}
return "Success";
};
// No direct call to task.call() here, error is in lambda definition
}
}
Can an abstract class be instantiated directly in Java?
What is the precise return value of `BufferedReader.readLine()` when it reaches the end of the stream without any more characters to read, after processing all previous lines?
What is the output of this Java code?
java
public class LoopTask implements Runnable {
@Override
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println("Loop iteration: " + i);
}
}
public static void main(String[] args) {
new Thread(new LoopTask()).start();
}
}
What is the compilation error in the provided Java code?
java
interface Service {
void execute();
}
interface AdvancedService extends Service {
void setup();
}
public class MyService {
// Missing implementation
}
What error will occur when compiling the following Java code?
java
public class Main {
public static void main(String[] args) {
boolean flag = true;
int result = ~flag;
System.out.println(result);
}
}