☕ Java MCQ Questions – Page 118

Questions 2341–2360 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2341 hard code error
What is the output of this code?
java
public class Main {
    private static final Object lock = new Object();

    public static void main(String[] args) {
        Thread notifier = new Thread(() -> {
            System.out.println("Notifier thread attempting to notify...");
            lock.notify(); // Calling notify() without owning the monitor
            System.out.println("Notifier thread finished.");
        });
        notifier.start();
    }
}
Q2342 easy code error
What will be the result of attempting to compile this Java code?
java
class Point {
    private final int x;
    public Point(int x) { this.x = x; }
    public int getX() { return x; }
}
public class ImmutableFieldAccess {
    public static void main(String[] args) {
        Point p = new Point(5);
        p.x = 10;
        System.out.println(p.getX());
    }
}
Q2343 hard code output
What is the output of this code?
java
class MyGenericException extends Exception { public MyGenericException(String m, Throwable c) { super(m, c); } public MyGenericException(String m) { super(m); } }
class MySpecificException extends MyGenericException { public MySpecificException(String m) { super(m); } }
public class Main {
    public static void process() throws MyGenericException {
        try { throw new MySpecificException("Specific issue occurred"); }
        catch (MySpecificException e) { throw new MyGenericException("Generic error wrapper", e); }
    }
    public static void main(String[] args) {
        try { process(); }
        catch (MyGenericException e) {
            System.out.println("Caught: " + e.getMessage());
            System.out.println("Cause: " + e.getCause().getMessage());
        }
    }
}
Q2344 easy
In which scenario would `java.util.LinkedList` typically be preferred over `java.util.ArrayList`?
Q2345 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) {
        StringBuilder sb = new StringBuilder("abcdef");
        sb.setLength("3");
        System.out.println(sb);
    }
}
Q2346 easy code error
What is the result of running this Java code?
java
public class StringError {
    public static void main(String[] args) {
        final String name = "Alice";
        name = "Bob";
        System.out.println(name);
    }
}
Q2347 medium code output
What is the output of this code?
java
public class ThreadStateDemo3 {
    private static final Object lock = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("T1 holding lock.");
                try { Thread.sleep(200); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
            }
        });
        Thread t2 = new Thread(() -> {
            System.out.println("T2 trying to acquire lock.");
            synchronized (lock) { System.out.println("T2 acquired lock."); }
        });
        t1.start();
        Thread.sleep(50); // Ensure t1 gets the lock first
        t2.start();
        Thread.sleep(50); // Give t2 time to try and get the lock
        System.out.println("T2 state: " + t2.getState());
    }
}
Q2348 easy code output
What is the output of this code?
java
class Printer {
    void print(String s) {
        System.out.println("Printing String: " + s);
    }
}

class LaserPrinter extends Printer {
    void print(int i) {
        System.out.println("Printing Integer: " + i);
    }
}

public class Main {
    public static void main(String[] args) {
        LaserPrinter lp = new LaserPrinter();
        lp.print("Document");
    }
}
Q2349 hard
When an `ArrayList` instance is serialized, how does it manage to correctly reconstruct its state, given that its internal element array (`elementData`) is marked `transient`?
Q2350 easy code error
What is the expected error when compiling and running the following Java code?
java
import java.util.LinkedList;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<Integer> stack = new LinkedList<>();
        stack.pop(); // Tries to remove and return the head of the list (stack behavior)
    }
}
Q2351 easy code output
What is the output of this Java code?
java
public class StringMethodTest {
    public static void main(String[] args) {
        String original = "Programming";
        String changed = original.toUpperCase();
        System.out.println(original);
    }
}
Q2352 hard
When using `FileWriter` to write text, how should one ensure platform-independent newline characters are written, and does `FileWriter` handle this translation automatically?
Q2353 hard code output
What will be the output of this code?
java
public class MultiArrayQ9 {
    public static void main(String[] args) {
        Object[][] mixed = new Object[2][2];
        mixed[0][0] = 5; // Autoboxes to Integer
        mixed[0][1] = "Java";
        mixed[1][0] = new Double(3.14);
        int val = (Integer)mixed[0][0];
        try {
            String s = (String)mixed[1][0];
            System.out.println(s);
        } catch (ClassCastException e) {
            System.out.println("CCE caught");
        }
        System.out.println(val);
    }
}
Q2354 hard code output
What is the output of this code?
java
abstract class Greeter { abstract String getMsg(); }
public class Outer {
    private String lang = "English";
    public static void main(String[] args) {
        Greeter g = new Greeter() {
            @Override String getMsg() {
                return "Hello " + lang; // Problematic line
            }
        };
        // System.out.println(g.getMsg()); // This line would cause the compile error
    }
}
Q2355 easy
Which of the following is a valid way to declare and initialize a `float` variable in Java?
Q2356 medium
An interface contains one abstract method and several `default` methods. Is it considered a functional interface?
Q2357 easy
Which operator is used for simple assignment of a value to a variable in Java?
Q2358 medium
Consider a class `Car` and a subclass `SportsCar`. If you declare `Car myCar = new SportsCar();`, what type of polymorphism is primarily demonstrated by `myCar` when its methods are invoked?
Q2359 easy
What is the primary benefit of using `BufferedReader` in Java?
Q2360 medium code error
Consider the following Java code. What type of error will occur at compile time?
java
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        File someFile = new File("data.txt");
        BufferedWriter writer = new BufferedWriter(someFile);
        try {
            writer.write("Info");
            writer.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
← Prev 116117118119120 Next → Page 118 of 200 · 3994 questions