☕ Java MCQ Questions – Page 65

Questions 1281–1300 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1281 easy code error
What error will this code produce?
java
public class LoopError {
    public static void main(String[] args) {
        for (int i = 0; i < 1; i++) {
            System.out.println("Inside loop");
            return;
        }
        System.out.println("After loop");
    }
}
Q1282 medium
Which of the following best describes the concept of "abstraction" in object-oriented programming?
Q1283 hard code error
What error will this Java code produce when compiled and run?
java
public class PrimitiveWrapperArrayCast {
    public static void main(String[] args) {
        int[] primitiveArr = {1, 2, 3};
        // Attempt a direct cast from an array of primitives to an array of wrappers
        Integer[] wrapperArr = (Integer[]) primitiveArr;
        System.out.println(wrapperArr[0]);
    }
}
Q1284 hard
If a `continue` statement is executed within a `try` block in a loop, what is the effect on any `finally` block associated with that `try` statement before the loop proceeds to its next iteration?
Q1285 easy code error
What is the outcome of attempting to compile this Java code?
java
public class Main {
    public static void main(String[] args) {
        outerLoop: for (int i = 0; i < 5; i++) {
            if (i == 2) {
                break nonexistentLabel;
            }
        }
    }
}
Q1286 easy code error
What kind of error will occur when compiling this Java code?
java
public class Main {
    public static void main(String[] args) {
        String status = "active";
        if (status) {
            System.out.println("Status is active");
        }
    }
}
Q1287 easy
Can a functional interface contain `static` methods?
Q1288 easy code output
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    public static void main(String[] args) {
        Queue<String> tasks = new LinkedList<>();
        tasks.offer("TaskA");
        tasks.offer("TaskB");
        tasks.poll();
        tasks.offer("TaskC");
        System.out.println(tasks.size());
    }
}
Q1289 medium code error
What error will occur when compiling the following Java code?
java
public class RunnableStartError {
    public static void main(String[] args) {
        Runnable task = () -> {
            System.out.println("Task running");
        };
        task.start(); // Runnable interface does not have a start() method
    }
}
Q1290 hard code error
When compiling this Java record-based code, what error will be encountered?
java
public record Product(String name, double price) {}

public class RecordModification {
    public static void main(String[] args) {
        Product laptop = new Product("Laptop", 1200.00);
        laptop.price = 1150.00; // Attempt to reassign a record component
        System.out.println(laptop.price());
    }
}
Q1291 medium code error
What exception will be thrown when running this Java code snippet?
java
import java.util.PriorityQueue;
import java.util.Queue;

public class QueueError {
    public static void main(String[] args) {
        Queue pq = new PriorityQueue(); // Raw type Queue
        pq.add("String element");
        pq.add(123); // Adding an Integer to a PriorityQueue that already contains a String
    }
}
Q1292 medium code error
What is the most likely outcome of running the following Java code, specifically due to the deprecated `stop()` method?
java
class StopperThread extends Thread {
    public void run() {
        System.out.println("StopperThread running...");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        System.out.println("StopperThread finished.");
    }
}

public class Main {
    public static void main(String[] args) {
        StopperThread t = new StopperThread();
        t.start();
        t.stop(); // This line
        System.out.println("Main thread exiting.");
    }
}
Q1293 easy code error
What is the error in the following Java code?
java
public class Main {
    public static void main(String[] args) {
        Object obj = "Hello";
        Integer num = (Integer) obj;
        System.out.println(num);
    }
}
Q1294 easy code error
What is the outcome of executing this Java code?
java
public class LoopError2 {
    public static void main(String[] args) {
        int counter = 0;
        while (counter < 5) {
            System.out.println("Counting...");
            // Missing counter++
        }
    }
}
Q1295 easy code output
What does this Java code print?
java
public class Main {
    public static void main(String[] args) {
        int age = 15;
        String category = "";
        if (age < 13) {
            category = "Child";
        } else if (age < 18) {
            category = "Teen";
        } else {
            category = "Adult";
        }
        System.out.println(category);
    }
}
Q1296 medium code error
What is the problem with the `run()` method implementation in the following `Runnable` class?
java
import java.io.IOException;

public class FaultyTask implements Runnable {
    @Override
    public void run() throws IOException {
        // Simulating an operation that might throw IOException
        throw new IOException("File not found");
    }
}

public class Main {
    public static void main(String[] args) {
        FaultyTask task = new FaultyTask();
    }
}
Q1297 hard
Consider the two ways to initialize an array: 1. `int[] arr1 = {1, 2, 3};` 2. `int[] arr2 = new int[]{1, 2, 3};` What is a key difference in their usage contexts?
Q1298 medium code output
What is the output of this code?
java
import java.util.TreeMap;

public class TreeMapExample {
    public static void main(String[] args) {
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(3, "C");
        treeMap.put(1, "A");
        treeMap.put(2, "B");
        System.out.println(treeMap);
    }
}
Q1299 hard
Consider nested `for` loops: `outerLoop: for (...) { innerLoop: for (...) { /* code */ } }`. What is the precise effect of executing `break outerLoop;` from within the innermost part of the `innerLoop`?
Q1300 medium code error
Which compile-time error will occur in the `main` method attempting to create an instance of `Singleton`?
java
class Singleton {
    private Singleton() {
        // Private constructor to prevent direct instantiation
    }

    public static Singleton getInstance() {
        return new Singleton();
    }
}

public class Main {
    public static void main(String[] args) {
        Singleton s = new Singleton(); 
    }
}
← Prev 6364656667 Next → Page 65 of 200 · 3994 questions