☕ Java MCQ Questions – Page 121

Questions 2401–2420 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2401 easy
Which of these primitive types can be implicitly converted to a `double`?
Q2402 medium code output
What is the output of this code?
java
class Item {
    String description;
    int quantity;
    boolean inStock;
}

public class Main {
    public static void main(String[] args) {
        Item item = new Item();
        System.out.println("Description: " + item.description);
        System.out.println("Quantity: " + item.quantity);
        System.out.println("In Stock: " + item.inStock);
    }
}
Q2403 hard code error
What kind of error will occur when compiling or running the following Java code?
java
public class SBError {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("JavaProgramming"); // Length 15
        String replacement = "Awesome";
        // Attempt to replace from index 10 to 5 (start > end)
        sb.replace(10, 5, replacement);
        System.out.println(sb);
    }
}
Q2404 medium code error
What is the error in the following Java code?
java
class Product {
    private final String sku;
    private String name;

    public Product(String sku, String name) {
        this.sku = sku;
        this.name = name;
    }

    public void renameProduct(String newName) {
        this.sku = newName; // Attempt to modify final field
    }

    public String getName() { return name; }
    public String getSku() { return sku; }
}

public class Inventory {
    public static void main(String[] args) {
        Product p = new Product("P123", "Laptop");
        p.renameProduct("Desktop");
    }
}
Q2405 easy code output
What is the output of this Java code snippet?
java
public class OverloadDemo6 {
    void execute(long l) {
        System.out.println("Executing long: " + l);
    }
    void execute(float f) {
        System.out.println("Executing float: " + f);
    }
    public static void main(String[] args) {
        OverloadDemo6 obj = new OverloadDemo6();
        obj.execute(30);
    }
}
Q2406 easy code output
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        int value = 0;
        try {
            value = 10;
        } finally {
            value = 20;
        }
        System.out.println(value);
    }
}
Q2407 easy code error
What kind of error will occur when executing the following Java code?
java
public class MyNotifier {
    private final Object lock = new Object();

    public void signalWaitingThread() {
        // Calling notify() without holding the monitor for 'lock'
        lock.notify();
    }

    public static void main(String[] args) {
        MyNotifier notifier = new MyNotifier();
        new Thread(notifier::signalWaitingThread).start();
    }
}
Q2408 medium
What is the valid range for a thread's priority in Java, and what is its default value?
Q2409 medium
Given the following Java code snippet: java String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s1 == s3); What will be the output?
Q2410 medium code output
What is the content of 'auto_closed.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;

public class TryWithResourcesTest {
    public static void main(String[] args) {
        try (FileWriter writer = new FileWriter("auto_closed.txt")) {
            writer.write("This is automatically closed.");
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
        // No explicit close() is called here.
    }
}
Q2411 hard code error
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Arrays;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<Double> values = new LinkedList<>(Arrays.asList(1.1, 2.2, 3.3));
        values.remove(values.size()); // Invalid index for remove
        System.out.println(values);
    }
}
Q2412 easy
When creating a thread by extending the `Thread` class, which of the following is a mandatory step?
Q2413 hard code error
What is the error encountered when running this Java code?
java
public class PrimitiveArrayCastError {
    public static void main(String[] args) {
        Object obj = new Integer[]{1, 2, 3};
        int[] intArray = (int[]) obj;
        System.out.println(intArray[0]);
    }
}
Q2414 medium code output
What does this Java code print to the console?
java
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class Test {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        Consumer<String> addToList = names::add;
        Consumer<String> printName = System.out::println;

        addToList.accept("Alice");
        printName.accept("Bob");
        System.out.println(names.size());
    }
}
Q2415 medium code output
What is the output of this Java code?
java
class DaemonTask implements Runnable {
    public void run() {
        try {
            System.out.println("Daemon thread started.");
            Thread.sleep(1000); // Simulate long running task
            System.out.println("Daemon thread finished.");
        } catch (InterruptedException e) {
            System.out.println("Daemon interrupted.");
        }
    }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Thread daemonThread = new Thread(new DaemonTask());
        daemonThread.setDaemon(true);
        daemonThread.start();
        Thread.sleep(50); // Give daemon a chance to start
        System.out.println("Main thread exiting.");
    }
}
Q2416 medium code output
What will this Java code print?
java
public class StringBufferTest {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Test");
        String s = "Java";
        sb.append(s).insert(sb.length(), "!");
        System.out.print(sb.length() + " " + sb.capacity());
    }
}
Q2417 easy code error
What error will occur when compiling the following Java code?
java
public class Main {
    public static void main(String[] args) {
        int x = 7;
        if (5 < x < 10) {
            System.out.println("X is between 5 and 10.");
        } else {
            System.out.println("X is not between 5 and 10.");
        }
    }
}
Q2418 easy
How do you create a new single-dimensional integer array of size 5 named `scores`?
Q2419 easy
What does a `Thread` object represent in Java?
Q2420 hard
Considering memory footprint and garbage collection overhead, how does `java.util.LinkedList` compare to `java.util.ArrayList` for storing a large number of objects?
← Prev 119120121122123 Next → Page 121 of 200 · 3994 questions