☕ Java MCQ Questions – Page 3

Questions 41–60 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q41 medium code output
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Queue;

public class QueueIsEmpty {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();
        queue.offer("A");
        queue.offer("B");
        queue.poll();
        System.out.println(queue.isEmpty());
        queue.poll();
        System.out.println(queue.isEmpty());
    }
}
Q42 easy code output
What is the output of this code?
java
public class LambdaTest {
    public static void main(String[] args) {
        Runnable r = () -> System.out.println("Hello Lambda!");
        r.run();
    }
}
Q43 easy code output
What does this Java code print?
java
import java.io.File;

public class FileTest {
    public static void main(String[] args) {
        File directory = new File("nonExistentDir");
        System.out.println(directory.exists() + " " + directory.isDirectory());
    }
}
Q44 hard
Which of the following statements regarding Java's single-dimensional array covariance is TRUE?
Q45 medium code error
What error will this Java code produce when executed?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][] jaggedArray = new int[2][];
        jaggedArray[0] = new int[]{1, 2};
        for (int i = 0; i < jaggedArray.length; i++) {
            for (int j = 0; j < jaggedArray[i].length; j++) {
                System.out.print(jaggedArray[i][j] + " ");
            }
        }
    }
}
Q46 easy
To compare two String objects for equality, considering character case, which method should be used?
Q47 medium code error
What kind of error will occur when compiling this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        arr.length = 5;
        System.out.println(arr.length);
    }
}
Q48 easy code output
What is the output of this Java code?
java
class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Runnable running.");
    }
}

public class TestThread {
    public static void main(String[] args) {
        MyRunnable r = new MyRunnable();
        r.run();
        System.out.println("Main finished.");
    }
}
Q49 hard code error
What does this code print?
java
import java.io.*;

class FaultyReader extends Reader {
    private int count = 0;
    @Override
    public int read(char[] cbuf, int off, int len) throws IOException {
        if (count++ > 0) { // Throws on second read attempt
            throw new IOException("Faulty Reader encountered an error!");
        }
        if (len == 0) return 0;
        cbuf[off] = 'A'; // Simulate reading one char
        return 1;
    }

    @Override
    public void close() throws IOException { /* Do nothing */ }
}

public class FaultyBufferedReaderTest {
    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new FaultyReader())) {
            br.read(); // First read, okay
            br.readLine(); // Second read, underlying reader throws
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
Q50 hard code error
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("Initial: ");
        char[] chars = {'J', 'a', 'v', 'a'}; // Length 4
        // Try to append 5 characters starting from offset 0
        sb.append(chars, 0, 5);
        System.out.println(sb);
    }
}
Q51 hard code output
What is the output of this code?
java
class CustomException1 extends Exception { public CustomException1(String msg) { super(msg); } }
class CustomException2 extends Exception { public CustomException2(String msg) { super(msg); } }
public class Main {
    public static void methodA() throws CustomException1 { throw new CustomException1("Problem from A"); }
    public static void main(String[] args) {
        try { methodA(); }
        catch (CustomException1 e) {
            System.out.println("Caught CE1: " + e.getMessage());
            try { throw new CustomException2("Problem from Catch1"); }
            catch (CustomException2 ce2) { System.out.println("Caught CE2: " + ce2.getMessage()); }
        } catch (Exception e) { System.out.println("Caught general: " + e.getMessage()); }
        finally { System.out.println("Finally executed."); }
    }
}
Q52 medium
Which of the following is a key requirement for method overloading in Java?
Q53 easy
Which of the following describes a key characteristic of a custom *checked* exception?
Q54 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        String result = "";
        outer: for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (i == 1 && j == 1) {
                    break outer;
                }
                result += "(" + i + "," + j + ")";
            }
        }
        System.out.print(result);
    }
}
Q55 easy code output
What is the output of this Java code?
java
class InitOrder {
    { 
        System.out.println("Instance Initializer Block");
    }
    InitOrder() {
        System.out.println("Constructor");
    }
}
public class Main {
    public static void main(String[] args) {
        InitOrder obj = new InitOrder();
    }
}
Q56 hard code output
What does this code print?
java
import java.util.function.Function;

class Box<T> {
    T item;
    public Box(T item) { this.item = item; }
    @Override public String toString() { return "Box(" + item + ")"; }
}

public class GenericConstructorRef {
    public static void main(String[] args) {
        Function<String, Box<String>> stringBoxCreator = Box::new;
        Box<String> stringBox = stringBoxCreator.apply("Hello");

        Function<Integer, Box<Integer>> intBoxCreator = Box::new;
        Box<Integer> intBox = intBoxCreator.apply(123);

        System.out.println(stringBox);
        System.out.println(intBox);
    }
}
Q57 hard code error
What is the output of this code?
java
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Arrays;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<Character> chars = new LinkedList<>(Arrays.asList('a', 'b', 'c'));
        ListIterator<Character> it = chars.listIterator();
        it.next(); // Cursor after 'a'
        it.remove(); // Removes 'a'
        it.remove(); // Second remove without next/previous
        System.out.println(chars);
    }
}
Q58 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        String result = "";
        for (int i = 0; i < 3; i++) {
            try {
                if (i == 1) {
                    break;
                }
                result += i;
            } finally {
                result += "F";
            }
        }
        System.out.print(result);
    }
}
Q59 easy
Can an abstract class in Java have constructors?
Q60 hard
Consider the following Java code: java public class ArrayManipulator { public static void modifyArray(int[][] arr) { arr[0][0] = 99; arr = new int[1][1]; // Reassigns local 'arr' arr[0][0] = 100; } public static void main(String[] args) { int[][] myArr = new int[2][2]; myArr[0][0] = 1; myArr[0][1] = 2; myArr[1][0] = 3; myArr[1][1] = 4; modifyArray(myArr); System.out.println(myArr[0][0]); } } What will be the output?
← Prev 12345 Next → Page 3 of 200 · 3994 questions