☕ Java MCQ Questions – Page 105

Questions 2081–2100 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2081 easy code output
What does this code print?
java
import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest {
    public static void main(String[] args) {
        // Assume test.txt exists and contains a single character 'X'
        try (FileReader reader = new FileReader("test.txt")) {
            reader.read(); // Reads 'X'
            int charCode = reader.read(); // Tries to read again
            System.out.print(charCode);
        } catch (IOException e) {
            System.out.print("Error");
        }
    }
}
Q2082 hard code error
What is the output of this code?
java
import java.io.File;
import java.io.IOException;

public class ListFilesOnFile {
    public static void main(String[] args) {
        File regularFile = new File("my_test_file.txt");
        try {
            regularFile.createNewFile(); // Create it as a file
            File[] files = regularFile.listFiles();
            if (files == null) {
                System.out.println("listFiles returned null.");
            } else {
                System.out.println("Number of files: " + files.length);
            }
        } catch (IOException e) {
            System.out.println("Error: " + e.getClass().getSimpleName());
        } finally {
            regularFile.delete();
        }
    }
}
Q2083 easy
Which type of expression must the condition inside the parentheses of a `while` loop be?
Q2084 hard code error
What error will occur when the `Worker` thread attempts to call `wait()`?
java
class SharedResource {
    public void doWork() {
        // This thread does not own the monitor for 'this'
        try {
            this.wait(); 
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

public class Main {
    public static void main(String[] args) {
        SharedResource resource = new SharedResource();
        Thread worker = new Thread(resource::doWork);
        worker.start();
    }
}
Q2085 medium
What is the default initial value for elements of a newly created `double[]` array in Java?
Q2086 hard
Consider the behavior of the `this` keyword within a lambda expression versus an anonymous inner class. Which statement is correct?
Q2087 hard
Consider an interface `I` with a `default` method `void perform()`. A class `C` implements `I` and also extends a class `A` which has a concrete method `void perform()`. If `C` does not explicitly override `perform()`, which `perform()` method is invoked when calling `new C().perform()`?
Q2088 medium code output
What is the output of this code?
java
class MyJob implements Runnable {
    private String message;
    public MyJob(String message) {
        this.message = message;
    }
    @Override
    public void run() {
        System.out.println("Running: " + message);
    }
}

public class Main {
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyJob("Job A"));
        Thread t2 = new Thread(new MyJob("Job B"));
        t1.start();
        t2.start();
        System.out.println("Main thread done scheduling.");
    }
}
Q2089 easy
Can the return type of an overriding method be different from the overridden method in the superclass?
Q2090 medium code error
What error occurs when compiling this Java code?
java
public class LoopTest {
    public static void main(String[] args) {
        final int value = 0;
        do {
            System.out.println("Value: " + value);
            value++;
        } while (false);
    }
}
Q2091 easy
Which of the following statements about `Serializable` interface is true?
Q2092 hard code error
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 FileReaderError5 {
    public static void main(String[] args) {
        File file = new File("test_fr5.txt");
        try {
            file.createNewFile();
            try (FileReader fr = new FileReader(file)) {
                char[] buffer = null; // Null buffer
                fr.read(buffer); // Calling read with a null buffer
                System.out.println("Read successfully");
            }
        } catch (IOException e) {
            System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        } catch (NullPointerException e) {
            System.err.println(e.getClass().getSimpleName());
        } finally {
            file.delete();
        }
    }
}
Q2093 easy code output
What is the output of this code?
java
abstract class Vehicle {
    abstract String getType();
}

class Car extends Vehicle {
    @Override
    String getType() {
        return "Sedan";
    }
}

public class Test {
    public static void main(String[] args) {
        Vehicle v = new Car();
        System.out.println(v.getType());
    }
}
Q2094 medium code output
What is the output of this code?
java
String data = "apple,banana,cherry";
String result1 = data.replace(",", "-");
String result2 = data.replaceAll("a", "X");
System.out.println(result1 + " " + result2);
Q2095 easy code output
What will this Java code print?
java
public class MyClass {
    public static void main(String[] args) {
        String item = "Pen";
        switch (item) {
            case "Book": System.out.print("Reading");
            case "Pen": System.out.print("Writing");
            case "Pencil": System.out.print("Drawing"); break;
            default: System.out.print("Other activity");
        }
    }
}
Q2096 easy code error
What kind of error will this Java code produce?
java
public class LoopError9 {
    public static void main(String[] args) {
        int x = 5;
        while (x) { // Error: int cannot be converted to boolean
            System.out.println("Value: " + x);
            x--;
        }
    }
}
Q2097 medium
What does "fail-fast" behavior of an `Iterator` primarily mean in the context of Java collections?
Q2098 medium
Compared to the `synchronized` keyword, what is a key advantage of using `ReentrantLock` for synchronization?
Q2099 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> queue = new LinkedList<>();
        queue.offer("Apple");
        queue.offer("Banana");
        System.out.println(queue.poll());
        System.out.println(queue.peek());
    }
}
Q2100 medium
Is `BufferedReader` inherently thread-safe?
← Prev 103104105106107 Next → Page 105 of 200 · 3994 questions