☕ Java MCQ Questions – Page 165

Questions 3281–3300 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3281 medium
If a concrete (non-abstract) class implements an interface in Java, what is the consequence?
Q3282 medium
In a traditional Java `switch` statement, what is the consequence if a `break` statement is omitted from a `case` block?
Q3283 hard code error
What compilation error will occur when compiling the following Java code?
java
import java.util.function.Function;

public class NestedLambdaTypeMismatch {
    public static void main(String[] args) {
        Function<Integer, Function<String, String>> f = (i) -> (s) -> s.length();
    }
}
Q3284 medium
Under what condition might calling the `remove()` method of a `java.util.Iterator` result in an `UnsupportedOperationException`?
Q3285 medium code output
What will be printed to the console when this code runs?
java
public class ExitPreventsFinally {
    public static void main(String[] args) {
        try {
            System.out.println("In try block");
            System.exit(0);
        } catch (Exception e) {
            System.out.println("In catch block");
        } finally {
            System.out.println("In finally block");
        }
        System.out.println("After try-catch-finally");
    }
}
Q3286 hard code error
What error occurs when executing the following Java code?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        for (String fruit : list) {
            if ("Banana".equals(fruit)) {
                list.remove(fruit);
            }
        }
        System.out.println(list);
    }
}
Q3287 medium code output
What does this code print?
java
import jakarta.validation.*;
import jakarta.validation.constraints.Min;
import java.util.*;

public class Test {
    static class ProductItem { @Min(1) public int quantity; public ProductItem(int q) { this.quantity = q; } }
    static class ShoppingCart {
        @Valid public List<ProductItem> items;
        public ShoppingCart(List<ProductItem> i) { this.items = i; }
    }
    public static void main(String[] args) {
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        ShoppingCart cart = new ShoppingCart(Arrays.asList(new ProductItem(0))); // quantity < 1
        Set<ConstraintViolation<ShoppingCart>> violations = validator.validate(cart);
        System.out.println("Violations: " + violations.size());
    }
}
Q3288 hard code error
What compilation error will occur in this Java code?
java
class Test {
    public static void main(String[] args) {
        final int value;
        do {
            value = 10;
        } while (Math.random() < 0.5); // Condition might cause loop to run more than once
        System.out.println(value);
    }
}
Q3289 easy code output
What is the output of this code?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<String> letters = new ArrayList<>();
        letters.add("A");
        letters.add("B");
        Iterator<String> it = letters.iterator();
        System.out.print(it.next());
        System.out.print(it.next());
        try {
            System.out.print(it.next());
        } catch (Exception e) {
            System.out.print("-" + e.getClass().getSimpleName());
        }
    }
}
Q3290 hard
Why might attempting to serialize an instance of a non-static inner class lead to an unexpected `NotSerializableException` if its enclosing class is not `Serializable`?
Q3291 hard code output
What is the output of this code?
java
public class DataTypeChallenge {
    public static void main(String[] args) {
        char c1 = 'A'; // ASCII 65
        char c2 = 1;
        int result = c1 + c2;
        char c3 = (char) (c1 + c2);
        System.out.println(result + ", " + c3);
    }
}
Q3292 easy code output
What is the output of this code?
java
import java.util.function.Consumer;

public class LambdaTest {
    public static void main(String[] args) {
        Consumer<String> printer = s -> System.out.println(s + "!");
        printer.accept("Java");
    }
}
Q3293 medium
How does encapsulation contribute to creating immutable objects in Java?
Q3294 medium code output
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        String msg = "Hello from Runnable!";
        Runnable task = () -> {
            System.out.println(msg);
        };
        new Thread(task).start();
        System.out.println("Main thread is done.");
    }
}
Q3295 hard code error
What is the runtime error encountered when executing this Java code snippet?
java
import java.util.HashSet;
import java.util.Objects;

class KeyWithNullableIntField {
    Integer id;
    public KeyWithNullableIntField(Integer id) { this.id = id; }
    @Override
    public int hashCode() {
        return id.hashCode(); // Potential NPE
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        KeyWithNullableIntField that = (KeyWithNullableIntField) o;
        return Objects.equals(id, that.id);
    }
}

public class Main {
    public static void main(String[] args) {
        HashSet<KeyWithNullableIntField> set = new HashSet<>();
        set.add(new KeyWithNullableIntField(null)); // Object with null 'id' field added
    }
}
Q3296 easy
Which of these classes is designed to be mutable for efficient string manipulation?
Q3297 hard code output
What does this code print?
java
class A {}
class B extends A {}
class C extends A {}
public class Test {
    public static void main(String[] args) {
        A obj1 = new B();
        A obj2 = new A();
        B b1 = (B) obj1;
        try {
            C c1 = (C) obj2;
            System.out.println("Cast successful");
        } catch (ClassCastException e) {
            System.out.println("ClassCastException caught");
        }
        System.out.println("Program continues");
    }
}
Q3298 hard code error
What error will this Java code produce when compiled and run?
java
public class ArrayMistake {
    public static void main(String[] args) {
        int[] numbers = new int[3];
        numbers[0] = 1;
        numbers[1] = 2;
        // Attempt to re-initialize using initializer list after declaration
        numbers = {4, 5, 6};
        System.out.println(numbers[0]);
    }
}
Q3299 easy
Which access modifier is commonly used to achieve data hiding as part of encapsulation in Java?
Q3300 hard code error
What compile-time error will this Java code produce?
java
public class OperatorError4 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int c = (a + b)++; // Error line
        System.out.println(c);
    }
}
← Prev 163164165166167 Next → Page 165 of 200 · 3994 questions