☕ Java MCQ Questions – Page 89

Questions 1761–1780 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1761 medium code output
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 = "123456789";
        try (BufferedReader br = new BufferedReader(new StringReader(data))) {
            System.out.print((char) br.read());
            br.mark(5);
            System.out.print((char) br.read());
            System.out.print((char) br.read());
            br.reset();
            System.out.print((char) br.read());
            System.out.print((char) br.read());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q1762 easy
When using `BufferedWriter`, why is it important to call the `flush()` method or `close()` the writer?
Q1763 medium code error
What is the compilation error in the provided Java code?
java
class Base {
    public int getId() {
        return 1;
    }
}

class Derived extends Base {
    @Override
    public Integer getId() { // ERROR: incompatible return type
        return 2;
    }
}

public class Main {
    public static void main(String[] args) {
        Derived d = new Derived();
        System.out.println(d.getId());
    }
}
Q1764 hard code error
What error occurs when running this Java code?
java
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class CustomAbstractListUOE {
    public static void main(String[] args) {
        List<String> myCustomList = new AbstractList<String>() {
            private List<String> internal = new ArrayList<>(Arrays.asList("X", "Y", "Z"));

            @Override
            public String get(int index) { return internal.get(index); }

            @Override
            public int size() { return internal.size(); }
            
            // remove(int index) is NOT overridden here, so AbstractList's default is used
        };

        Iterator<String> it = myCustomList.iterator();
        it.next(); // "X"
        it.remove(); // Expect UOE
    }
}
Q1765 medium code output
What does this code print?
java
import java.util.concurrent.atomic.AtomicInteger;

public class Main {
    private static AtomicInteger counter = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        Runnable task = () -> {
            for (int i = 0; i < 5000; i++) {
                counter.incrementAndGet();
            }
        };

        Thread t1 = new Thread(task);
        Thread t2 = new Thread(task);

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(counter.get());
    }
}
Q1766 easy code output
What is the output of this Java code snippet?
java
public class Main {
    public static void main(String[] args) {
        String prefix = "Value: ";
        int number = 100;
        System.out.println(prefix + number);
    }
}
Q1767 hard
Considering Java 17, how does a `switch` *expression* handle a `null` selector expression if there is no explicit `case null` label?
Q1768 easy code error
What is the compile-time error in this Java code snippet?
java
public class ArrayTest {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30};
        double index = 1.0;
        System.out.println(numbers[index]);
    }
}
Q1769 hard code output
What does this code print?
java
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

public class LambdaCaptureHard {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3);
        AtomicInteger sum = new AtomicInteger(0);
        int factor = 10;

        Consumer<Integer> processor = n -> {
            sum.getAndAdd(n * factor);
            System.out.println("Processing " + n + ", current sum: " + sum.get());
        };

        numbers.forEach(processor);
        
        System.out.println("Final sum: " + sum.get());
    }
}
Q1770 easy
Which class must a custom checked exception in Java directly or indirectly extend?
Q1771 easy code error
What error will occur when compiling the following Java code?
java
import java.util.HashSet;
import java.util.Set;

public class MyClass {
    public static void main(String[] args) {
        Set<String> colors = new HashSet<>();
        String[] colorArray = {"Red", "Green", "Blue"};
        colors.addAll(colorArray); // This line will cause an error
        System.out.println(colors.size());
    }
}
Q1772 easy
Which of the following data types is NOT directly supported in a Java `switch` statement's expression (before Java 7)?
Q1773 easy code error
What will happen when this Java code is compiled?
java
public class MyClass {
    public static void main(String[] args) {
        int count;
        System.out.println(count);
    }
}
Q1774 easy code error
What error occurs during compilation for the following code snippet?
java
class MyThread extends Thread {
    public void run() {
        System.out.println("Child thread running.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread t = new MyThread();
        t.start();
        t.join(); // Attempting to join without handling InterruptedException
    }
}
Q1775 medium
If `file.isDirectory()` returns `true` for a `File` object named `file`, what can be definitively concluded about `file.isFile()`?
Q1776 hard code error
Identify the compilation error in the following Java code using the `var` keyword.
java
public class VarNullInit {
    public static void main(String[] args) {
        var myVar = null; // Illegal: 'var' cannot infer type from null literal
        System.out.println(myVar);
    }
}
Q1777 medium code error
What type of error will occur when compiling the following Java code?
java
public class Main {
    public static void main(String[] args) {
        double d = 100.0;
        int i = d;
        System.out.println(i);
    }
}
Q1778 easy code output
What will be the content of the file 'output.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {
    public static void main(String[] args) {
        FileWriter writer = null;
        try {
            writer = new FileWriter("output.txt");
            writer.write("Final Content");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Q1779 easy code output
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    public static void main(String[] args) {
        Queue<String> myQueue = new LinkedList<>();
        myQueue.offer("First");
        myQueue.poll();
        System.out.println(myQueue.peek());
    }
}
Q1780 hard code error
What is the compile-time error in this Java code?
java
public class LambdaReturnError {
    public static void main(String[] args) {
        Runnable myTask = () -> { // Line 3
            return "Hello";
        };
        new Thread(myTask).start();
    }
}
← Prev 8788899091 Next → Page 89 of 200 · 3994 questions