☕ Java MCQ Questions – Page 16

Questions 301–320 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q301 hard
A 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`?
Q302 hard code error
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");
        };
    }
}
Q303 easy code 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());
    }
}
Q304 easy code output
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());
    }
}
Q305 medium
What is the primary purpose of using the `instanceof` operator before performing a downcast in Java?
Q306 medium code output
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();
        }
    }
}
Q307 easy code error
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());
        }
    }
}
Q308 easy
Which type of loop is a do-while loop categorized as?
Q309 medium code error
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");
        }
    }
}
Q310 hard code error
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();
    }
}
Q311 hard code output
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());
        }
    }
}
Q312 medium code output
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());
    }
}
Q313 easy code output
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());
    }
}
Q314 easy code error
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);
    }
}
Q315 easy code error
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
    }
}
Q316 easy
Can an abstract class be instantiated directly in Java?
Q317 hard
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?
Q318 easy code output
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();
    }
}
Q319 medium code error
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
}
Q320 easy code error
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);
    }
}
← Prev 1415161718 Next → Page 16 of 200 · 3994 questions