☕ Java MCQ Questions – Page 139

Questions 2761–2780 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2761 hard
In which scenario would an anonymous inner class typically be preferred over a lambda expression for implementing an abstract type in Java?
Q2762 easy code error
What compile-time error will this code produce?
java
class MyRunnable implements Runnable {
    public int run() {
        System.out.println("Running task");
        return 0;
    }
}

public class Main {
    public static void main(String[] args) {
        MyRunnable r = new MyRunnable();
        new Thread(r).start();
    }
}
Q2763 hard code error
What error will be thrown by `thread1` when `thread2` interrupts it while it's attempting to acquire the lock?
java
import java.util.concurrent.locks.ReentrantLock;

public class InterruptibleLock {
    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            try {
                lock.lockInterruptibly(); // Attempts to acquire lock, but can be interrupted
                System.out.println("Thread 1 acquired lock.");
            } catch (InterruptedException e) {
                System.out.println("Thread 1 was interrupted while acquiring lock.");
                // No re-interrupt, just prints message
            }
        });

        lock.lock(); // Main thread acquires the lock first
        thread1.start();
        Thread.sleep(100); // Give thread1 time to try and acquire lock
        thread1.interrupt(); // Interrupt thread1
        lock.unlock(); // Main thread releases the lock
    }
}
Q2764 easy code output
What is the output of this Java code?
java
public class TypeCast {
    public static void main(String[] args) {
        int x = 10;
        x += 5.5;
        System.out.println(x);
    }
}
Q2765 hard
What is the amortized time complexity for adding `N` elements to an `ArrayList` starting from an empty list (`new ArrayList<>()`)?
Q2766 easy code output
What does this code print?
java
public class Main {
    public static void main(String[] args) {
        try {
            int x = 5 / 0;
            System.out.print("Try ");
        } catch (ArithmeticException e) {
            System.out.print("Catch ");
        } finally {
            System.out.print("Finally ");
        }
        System.out.print("End ");
    }
}
Q2767 hard
In Java, when overriding an instance method, which statement correctly describes the behavior regarding return types and polymorphism?
Q2768 hard code error
What is the error encountered when running this Java code?
java
public class LoopBoundsError {
    public static void main(String[] args) {
        String[] names = {"Alice", "Bob", "Charlie"};
        for (int i = 0; i <= names.length; i++) {
            System.out.println(names[i]);
        }
    }
}
Q2769 medium code error
What error will occur when compiling and running this Java code?
java
import java.util.HashSet;
import java.util.Set;

public class HashSetError1 {
    public static void main(String[] args) {
        Set<String> names = new HashSet<>();
        names.add("Alice");
        names.add("Bob");
        String first = names.get(0); // ERROR line
        System.out.println(first);
    }
}
Q2770 medium code output
What does this code print?
java
public class Main {
    private static int counter = 0;

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

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

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

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

        System.out.println(counter);
    }
}
Q2771 hard code output
What is the output of this Java code, demonstrating strong immutability and encapsulation with `Collections.unmodifiableList()`?
java
import java.util.ArrayList; import java.util.Collections; import java.util.List;

class ImmutableSettings {
    private final List<String> options;
    public ImmutableSettings(List<String> inputOptions) {
        this.options = Collections.unmodifiableList(new ArrayList<>(inputOptions));
    }
    public List<String> getOptions() { return options; }
}

public class Main {
    public static void main(String[] args) {
        ImmutableSettings settings = new ImmutableSettings(new ArrayList<>(List.of("A", "B")));
        System.out.println("Initial: " + settings.getOptions());
        try {
            settings.getOptions().add("C");
        } catch (UnsupportedOperationException e) {
            System.out.println("Caught: " + e.getClass().getSimpleName());
        }
    }
}
Q2772 easy code error
What is the output or error of the following Java code?
java
public class ExceptionTest {
    public static void main(String[] args) {
        try {
            Integer.parseInt("abc");
        } catch (NumberFormatException e) {
            System.out.println("Caught NFE");
        } finally {
            throw new RuntimeException("Finally Exception");
        }
    }
}
Q2773 easy
Can a `while` loop execute zero times?
Q2774 medium code error
What is the compile-time error in the following Java code?
java
class MyLogger {
    private String loggerName;

    public MyLogger() {
        this.loggerName = "default";
    }

    public void initialize(String name) {
        this(name); // Invalid usage: this() can only be in a constructor
        System.out.println("Logger initialized to: " + loggerName);
    }

    public MyLogger(String name) {
        this.loggerName = name;
    }

    public static void main(String[] args) {
        MyLogger log = new MyLogger();
        log.initialize("app_log");
    }
}
Q2775 medium
When using `System.arraycopy(source, srcPos, destination, destPos, length)` to copy elements from one array to another, what does `srcPos` represent?
Q2776 medium
In Java, what is the main function of the `throws` keyword in a method signature?
Q2777 hard code error
What is the compile-time error in this Java code?
java
import java.io.IOException;
import java.sql.SQLException;
public class RethrowProblem {
    public void processData() {
        try {
            if (true) throw new IOException("Data read error");
        } catch (IOException e) {
            System.err.println("Caught IOException: " + e.getMessage());
            throw new SQLException("Database error wrapping IO", e);
        }
    }
    public static void main(String[] args) {
        // new RethrowProblem().processData();
    }
}
Q2778 easy
Which method is used to retrieve an element at a specific index from an `ArrayList`?
Q2779 medium
Which statement about null keys and null values in a `java.util.TreeMap` is true when using natural ordering for keys?
Q2780 medium code error
Identify the compilation error in the following Java code.
java
public class CompoundAssignment {
    public static void main(String[] args) {
        byte b = 10;
        b = b + 5; // Potential issue here
        System.out.println(b);
    }
}
← Prev 137138139140141 Next → Page 139 of 200 · 3994 questions