☕ Java MCQ Questions – Page 61

Questions 1201–1220 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1201 easy code error
What compilation error will occur in the following Java code?
java
abstract class Widget {
    abstract void display();

    public static void createWidget() {
        Widget w = new Widget();
    }
}
Q1202 easy code error
What error will this Java code produce during compilation?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][] matrix = new int[2][2];
        int[] singleDimArray = {1, 2};
        matrix = singleDimArray;
    }
}
Q1203 medium code output
What does this code print?
java
import java.util.HashSet;
import java.util.Set;

class Product {
    int id;
    String name;
    Product(int id, String name) { this.id = id; this.name = name; }
    @Override public int hashCode() { return id; }
    @Override public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Product product = (Product) o;
        return id == product.id;
    }
}

public class Main {
    public static void main(String[] args) {
        Set<Product> products = new HashSet<>();
        products.add(new Product(101, "Laptop"));
        products.add(new Product(101, "Monitor")); 
        System.out.println(products.size());
    }
}
Q1204 medium
Why is it considered best practice to always close a `FileReader` instance after you are finished reading from it?
Q1205 easy
What is the key difference between a `while` loop and a `do-while` loop?
Q1206 hard
What is the correct way to declare variables with the same name in different `case` blocks of a single `switch` *statement* without causing a compile-time error?
Q1207 hard
Consider a thread that calls `Object.wait()` without holding the monitor for that object. What is the immediate consequence?
Q1208 medium code error
What error occurs when running this code, assuming 'test_input.txt' exists?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class MyClass {
    public static void main(String[] args) {
        File file = new File("test_input.txt");
        try {
            file.createNewFile();
            try (FileReader reader = new FileReader(file)) {
                char[] buffer = null; // Null buffer
                reader.read(buffer, 0, 5); // This call will throw NPE
                System.out.println("Read complete.");
            }
        } catch (IOException e) {
            System.err.println("Caught IOException: " + e.getMessage());
        } catch (NullPointerException e) {
            System.err.println("Caught NullPointerException: " + e.getMessage());
        } finally {
            file.delete();
        }
    }
}
Q1209 hard
Given `String[] sArr = new String[1];`. Which of the following `instanceof` expressions correctly describes `sArr`'s runtime type or implemented interfaces?
Q1210 medium code output
What is the output of this Java Stream API code?
java
import java.util.stream.IntStream;

public class StreamTest {
    public static void main(String[] args) {
        int sum = IntStream.rangeClosed(1, 5)
                           .filter(n -> n % 2 == 0)
                           .map(n -> n * 2)
                           .sum();
        System.out.println(sum);
    }
}
Q1211 hard
What is the worst-case time complexity for `put()` and `get()` operations in a `HashMap` if all keys consistently produce the same `hashCode()` and the table capacity is below the `MIN_TREEIFY_CAPACITY` threshold (64)?
Q1212 easy code error
What compile-time error will occur in the `Child` class?
java
class Parent {
    public void printValue() {
        System.out.println("Parent value");
    }
}

class Child extends Parent {
    @Override
    public int printValue() {
        System.out.println("Child value");
        return 1;
    }
}
Q1213 hard
Given two single-dimensional arrays, `int[] arr1 = {1, 2, 3};` and `int[] arr2 = {1, 2, 3};`. Which statement correctly describes the outcome of their comparison methods?
Q1214 medium code output
What does this code print?
java
import jakarta.validation.*;
import jakarta.validation.constraints.Min;
import java.util.Set;

public class Test {
    static class Item { @Min(1) public int value; public Item(int v) { this.value = v; } }
    static class Container {
        @Valid public Item[] items; // @Valid on an array
        public Container(Item[] i) { this.items = i; }
    }
    public static void main(String[] args) {
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Container c = new Container(new Item[]{new Item(5), new Item(0)}); // Second item invalid
        Set<ConstraintViolation<Container>> violations = validator.validate(c);
        System.out.println("Violations: " + violations.size());
    }
}
Q1215 hard code error
What is the compilation error in this Java code?
java
class Test {
    public static void main(String[] args) {
        int x = 0;
        do {
            System.out.println("Executing...");
            return;
        } while (x < 1);
        int y = 10;
        System.out.println(y);
    }
}
Q1216 easy code error
What error will occur when compiling the following Java code?
java
import java.util.HashSet;
import java.util.Set;

public class MyClass {
    public static void main(String[] args) {
        Set<String> items; // Not initialized
        items.add("Laptop"); // This line will cause an error
        System.out.println(items.size());
    }
}
Q1217 easy
What happens when you use the `reverse()` method on a `StringBuffer` object?
Q1218 easy code output
What is the result of compiling and running this Java code?
java
public class OverloadDemo7 {
    int getValue(int x) {
        return x * 2;
    }
    double getValue(int x) { // This line causes a compile-time error
        return (double)x / 2;
    }
    public static void main(String[] args) {
        OverloadDemo7 obj = new OverloadDemo7();
        System.out.println(obj.getValue(7));
    }
}
Q1219 medium code error
What is the error in this Java code snippet?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuilder sb;
        sb.append("Hello");
        System.out.println(sb);
    }
}
Q1220 hard code error
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        Thread t = new Thread(() -> System.out.println("Worker thread running."));
        t.start();
        try {
            t.join(-100); // Negative timeout
        } catch (InterruptedException e) {
            System.out.println("Interrupted.");
        }
    }
}
← Prev 5960616263 Next → Page 61 of 200 · 3994 questions