☕ Java MCQ Questions – Page 55

Questions 1081–1100 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1081 medium code error
Which line will cause a compilation error in the following Java code snippet related to 'effectively final' variables?
java
import java.util.function.Consumer;

public class ClosureExample {
    public static void main(String[] args) {
        String status = "Pending";
        Consumer<String> printer = s -> {
            System.out.println(s + ": " + status);
        };
        status = "Completed"; // This line causes the error
        printer.accept("Task");
    }
}
Q1082 hard code error
What is the compilation error, if any, for the provided Java code snippet?
java
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

class Overloader {
    public void schedule(Callable c) {}
    public void schedule(Future f) {}

    public static void main(String[] args) {
        Overloader o = new Overloader();
        o.schedule(null); // Line X
    }
}
Q1083 hard
What is the primary difference between `Thread.currentThread().isInterrupted()` and `Thread.interrupted()`?
Q1084 hard code error
Which compile-time error will prevent this Java code from running?
java
public class ShortRangeTest {
    public static void main(String[] args) {
        short s = 32768; // 32768 is outside the range of short
        System.out.println(s);
    }
}
Q1085 easy code error
What kind of error will occur when compiling this Java code?
java
class Parent {
    private int value = 10;
}

class Child extends Parent {
    public void display() {
        System.out.println(value);
    }
}

public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.display();
    }
}
Q1086 hard code error
What is the compilation error in the following Java code regarding static final variable initialization?
java
public class StaticBlankFinal {
    public static final int MY_STATIC_FINAL;

    // Attempting to initialize a static final field in an instance constructor
    public StaticBlankFinal() {
        MY_STATIC_FINAL = 50; 
    }

    public static void main(String[] args) {
        System.out.println("Attempting to access: " + MY_STATIC_FINAL);
    }
}
Q1087 medium code error
What is the compile-time error in this Java code?
java
import java.util.function.Predicate;

public class LambdaError {
    private int threshold = 10;

    public void testPredicate() {
        Predicate<Integer> isAboveThreshold = num -> {
            threshold++; // Attempt to modify instance field
            return num > threshold;
        };
        System.out.println(isAboveThreshold.test(15));
    }

    public static void main(String[] args) {
        new LambdaError().testPredicate();
    }
}
Q1088 medium
For reference type casting in Java, when is the *safeness* of a cast primarily verified?
Q1089 hard code output
What is the output of this code?
java
import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
        HashSet<Float> set = new HashSet<>();
        float nan1 = Float.NaN;
        float nan2 = Float.NaN;
        float inf1 = Float.POSITIVE_INFINITY;
        float inf2 = Float.POSITIVE_INFINITY;

        set.add(nan1);
        set.add(nan2);
        set.add(inf1);
        set.add(inf2);

        System.out.println(set.size());
    }
}
Q1090 hard
Given `List<String> elements = Arrays.asList("one", null, "three");`, what is the resulting string from `String.join("-", elements);`?
Q1091 hard
Consider a superclass method `public void performAction() throws IOException`. Which of the following declarations for an overriding method in a subclass would result in a compile-time error?
Q1092 medium
For efficient reading of large text files using `FileReader`, it is most common and recommended to wrap it in which other class?
Q1093 easy
When you see `void myMethod() throws MyException {}`, what type of entity is `MyException`?
Q1094 medium code error
Which type of error will occur when compiling and running the following Java code?
java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
        writer.write("Hello");
        writer.close();
    }
}
Q1095 easy code error
What kind of error will occur when this Java code is executed?
java
public class Test {
    public static void main(String[] args) {
        String result = new StringBuilder("Start").append("End");
        System.out.println(result);
    }
}
Q1096 hard code error
What kind of runtime error or unexpected behavior will occur when running this Java code, primarily related to thread lifecycle?
java
public class DeadlockDemo {
    private static Object lockA = new Object();
    private static Object lockB = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (lockA) {
                System.out.println("Thread-1 acquired lockA");
                try { Thread.sleep(100); } catch (InterruptedException e) {} // Give Thread-2 a chance
                synchronized (lockB) {
                    System.out.println("Thread-1 acquired lockB");
                }
            }
        }).start();

        new Thread(() -> {
            synchronized (lockB) {
                System.out.println("Thread-2 acquired lockB");
                try { Thread.sleep(100); } catch (InterruptedException e) {} // Give Thread-1 a chance
                synchronized (lockA) {
                    System.out.println("Thread-2 acquired lockA");
                }
            }
        }).start();
    }
}
Q1097 medium code error
What is the error in this Java code snippet?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Code");
        char c = sb.charAt(4);
        System.out.println(c);
    }
}
Q1098 medium
Which of the following is an example of a stateful intermediate operation in the Java Stream API?
Q1099 medium code error
Which type of error or exception will be produced by the following Java code at runtime, if the FileWriter constructor fails to initialize 'writer' due to an IOException?
java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter("log.txt"));
            writer.write("Log entry 1");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            // Intentionally no writer.close() here
        }
        writer.write("Log entry 2"); // This line will be reached
    }
}
Q1100 medium
Which set of rules is crucial for creating a custom immutable class in Java?
← Prev 5354555657 Next → Page 55 of 200 · 3994 questions