☕ Java MCQ Questions – Page 20

Questions 381–400 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q381 easy code output
What is the output of this code?
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 'H'
        try (FileReader reader = new FileReader("test.txt")) {
            int charCode = reader.read();
            System.out.print((char) charCode);
        } catch (IOException e) {
            System.out.print("Error");
        }
    }
}
Q382 hard
In the context of `enum` types, what is the implicit access modifier for an enum constructor, and what are its implications?
Q383 easy code error
What compilation error will the following Java code produce?
java
import java.util.function.BiFunction;

@FunctionalInterface
interface MyFunction {
    String apply(int a, int b);
}

public class Main {
    public static void main(String[] args) {
        MyFunction func = (x, y, z) -> String.valueOf(x + y + z);
        System.out.println(func.apply(1, 2));
    }
}
Q384 medium
What is a potential issue if `equals()` is overridden for a custom class, but `hashCode()` is NOT, and objects of this class are stored in a `HashSet`?
Q385 easy
Which method of the `java.io.File` class is used to check if the file or directory denoted by the abstract pathname actually exists?
Q386 hard code error
What error occurs when attempting to compile the following Java code?
java
public class IfScopeError {
    public static void main(String[] args) {
        boolean isValid = false;
        if (isValid) {
            String statusMessage = "Data valid";
            System.out.println(statusMessage);
        } else {
            // statusMessage is not in scope here
        }
        System.out.println("Final status: " + statusMessage); // Use outside scope
    }
}
Q387 hard code error
What compile-time error occurs when attempting to compile this Java code?
java
class Animal {
    public static void makeSound() {
        System.out.println("Generic animal sound");
    }
}

class Dog extends Animal {
    public static void specificSound() {
        super.makeSound();
        System.out.println("Bark!");
    }
}
Q388 easy code output
What is the output of this Java code?
java
class Dog {
    String name;
    public Dog(String name) {
        this.name = name;
    }
    public void bark() {
        System.out.print(name + " says Woof!");
    }
}
public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog("Buddy");
        myDog.bark();
    }
}
Q389 easy code error
What is the error in this Java code?
java
public class FileWriterError1 {
    public static void main(String[] args) {
        FileWriter writer = new FileWriter("output.txt");
        writer.write("Hello, World!");
        writer.close();
    }
}
Q390 medium
What is the primary consequence of a `while` loop whose controlling boolean expression always evaluates to `true` and contains no `break` statement?
Q391 medium
Which method of the `Iterator` interface is used to determine if there are more elements available for iteration?
Q392 medium code output
What does this Java code print?
java
import java.util.function.Function;

public class MethodRefTest {
    public static void main(String[] args) {
        Function<String, String> transformer = String::toUpperCase;
        String s1 = transformer.apply("hello");
        String s2 = transformer.apply("world");
        System.out.println(s1 + " " + s2);
    }
}
Q393 hard code output
What will be the most likely output of this Java code snippet? (Assume the program runs on a multi-core processor and is not terminated early.)
java
public class RaceConditionDemo {
    private static int counter = 0;

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

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

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

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

        System.out.println("Final counter: " + counter);
    }
}
Q394 easy code output
What is the output of this Java code?
java
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<String, Integer> stringLength = s -> s.length();
        System.out.println(stringLength.apply("Java"));
    }
}
Q395 hard
In a `HashMap`, where is a `null` key stored internally, and how many `null` keys are permitted?
Q396 medium code error
What is the compilation error in the following code?
java
class Base {
    public static void greet() {
        System.out.println("Hello from Base!");
    }
}

class Derived extends Base {
    @Override
    public static void greet() {
        System.out.println("Hello from Derived!");
    }
}
Q397 hard
An immutable class is generally considered thread-safe by default. However, what is a common pitfall that can inadvertently compromise the thread safety of an otherwise immutable object?
Q398 medium code error
What will be the compilation error in this code?
java
class Printer {
    public void print(String text) {
        System.out.println("Printing: " + text);
    }
}

class ConsolePrinter extends Printer {
    @Override
    public static void print(String text) {
        System.out.println("Console Print: " + text);
    }
}
Q399 easy code error
What is the error in this Java code?
java
abstract class Vehicle {
    abstract void start();
    void stop() {
        System.out.println("Vehicle stopped.");
    }
}

class Car extends Vehicle { // Line 8
    // No start() method implemented
}

public class Main {
    public static void main(String[] args) {
        // Car c = new Car(); // If uncommented, would show the error
    }
}
Q400 hard code error
What compilation error will occur when compiling this Java code?
java
public class LabeledIfBreak {
    public static void main(String[] args) {
        int counter = 0;
        outerIf:
        if (true) {
            for (int i = 0; i < 3; i++) {
                if (i == 1) {
                    break outerIf;
                }
                counter++;
            }
        }
        System.out.println("Counter: " + counter);
    }
}
← Prev 1819202122 Next → Page 20 of 200 · 3994 questions