☕ Java MCQ Questions – Page 91

Questions 1801–1820 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1801 hard code error
What is the compilation error in the following Java code snippet?
java
import java.util.function.Consumer;

public class Main {
    public static void main(String[] args) {
        Consumer<String> stringConsumer = (Integer i) -> {
            System.out.println("Consumed: " + i);
        };
        stringConsumer.accept("Hello");
    }
}
Q1802 easy
When is a constructor invoked in Java?
Q1803 easy code error
What error will occur when this code is executed?
java
public class Main {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Program");
        sb.deleteCharAt(7);
        System.out.println(sb);
    }
}
Q1804 easy code output
What is the output of this Java code?
java
public class ThreadPriorityDemo {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            System.out.println("Thread priority: " + Thread.currentThread().getPriority());
        });
        thread.start();
    }
}
Q1805 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 {
            System.out.println("No exception here.");
        } catch (java.io.IOException e) {
            System.out.println("Caught IOException");
        }
    }
}
Q1806 hard
Consider a class `MyClass<T>` with two constructors: `MyClass(T value)` and `MyClass()`. If you need to create a `Supplier<MyClass<String>>` using a method reference, which reference is valid?
Q1807 hard code error
What is the compile-time error in this Java code?
java
public class AnonymousNonEffectivelyFinal {
    public static void main(String[] args) {
        int data = 100;
        Runnable job = new Runnable() {
            @Override
            public void run() {
                // System.out.println("Data: " + data); // Line 7
            }
        };
        data = 200; // This makes 'data' not effectively final for 'job'
        new Thread(job).start();
    }
}
Q1808 easy
Can an `ArrayList` store duplicate elements?
Q1809 medium
What happens if you try to modify an `ArrayList` (e.g., add or remove elements) while iterating over it using a standard `for-each` loop?
Q1810 easy code output
What is the output of this Java code?
java
public class WhileLoopDemo {
    public static void main(String[] args) {
        int x = 0;
        int y = 5;
        while (x < 2 && y > 3) {
            System.out.print(x);
            x++;
            y--;
        }
    }
}
Q1811 medium
If you obtain an `Iterator` from a `LinkedList` and then call the `remove()` method on that `Iterator`, what does it remove?
Q1812 easy code error
What is the error in this Java code?
java
abstract class Shape {
    abstract void draw();
    void description() {
        System.out.println("This is a shape.");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape s = new Shape(); // Line 10
    }
}
Q1813 medium code error
Which compile-time error will this Java code produce?
java
public class Test {
    public static void main(String[] args) {
        myLoop for (int i = 0; i < 5; i++) {
            if (i == 2) {
                break myLoop;
            }
            System.out.print(i);
        }
    }
}
Q1814 easy code error
Which error will occur when compiling this Java code?
java
import java.io.FileReader;

public class FileReaderIssue {
    public static void main(String[] args) {
        FileReader reader = new FileReader("nonexistent.txt");
        System.out.println("File reader created.");
    }
}
Q1815 medium code output
What does this code print?
java
import java.util.stream.Stream;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

public class StreamTest {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        boolean allEven = numbers.stream()
                                 .filter(n -> n > 1)
                                 .allMatch(n -> n % 2 == 0);
        System.out.println(allEven);
    }
}
Q1816 hard
When processing data from an external resource (e.g., network stream) using a `while` loop that reads until EOF or an error, what crucial concern must be addressed if `try-with-resources` is *not* used, to prevent resource leaks in case of an unexpected exception within the loop?
Q1817 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 Part {
        @Min(5) public int value;
        public Part(int v) { this.value = v; }
    }
    static class Machine {
        @Valid public Part part; // @Valid here
        public Machine(Part p) { this.part = p; }
    }
    public static void main(String[] args) {
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Machine machine = new Machine(null); // part is null
        Set<ConstraintViolation<Machine>> violations = validator.validate(machine);
        System.out.println("Violations: " + violations.size());
    }
}
Q1818 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<Integer> numbers = new HashSet<>();
        numbers.add(10);
        numbers.add(20);
        System.out.println(numbers.get(0)); // This line will cause an error
    }
}
Q1819 medium code output
What does this code print?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<Integer> numbers = new LinkedList<>();
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);
        boolean contains20 = numbers.contains(20);
        numbers.remove(new Integer(10)); 
        System.out.println(contains20 + " " + numbers.size());
    }
}
Q1820 hard code error
What is the compilation error in the following Java code?
java
package com.example;

private class TopLevelPrivate {
    public void greet() {
        System.out.println("Hello from private class");
    }
}

public class Main {
    public static void main(String[] args) {
        // Code to test TopLevelPrivate would go here, but the class itself is the issue.
    }
}
← Prev 8990919293 Next → Page 91 of 200 · 3994 questions