☕ Java MCQ Questions – Page 98

Questions 1941–1960 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1941 medium
Which of the following Java 7+ constructs is the most recommended way to ensure a `BufferedWriter` is properly closed, even if exceptions occur?
Q1942 medium code error
What error will occur when compiling or running this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        int[] values = new int[3];
        // This is a re-initialization attempt with initializer list syntax
        values = new int[]{1, 2, 3, 4};
    }
}
Q1943 medium
What happens when you try to cast an `Integer` object to a `Long` object directly in Java (e.g., `Long l = (Long) someIntegerObject;`)?
Q1944 medium code error
What error will occur when compiling and running this Java code?
java
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class HashSetError2 {
    public static void main(String[] args) {
        Set<Integer> numbers = new HashSet<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);

        for (Integer num : numbers) {
            if (num == 2) {
                numbers.remove(num); // ERROR line
            }
        }
        System.out.println(numbers);
    }
}
Q1945 easy code error
What is the compilation or runtime error in the following Java code?
java
public class Main {
    public static void main(String[] args) {
        String s1 = null;
        String s2 = " World";
        String result = s1.concat(s2);
        System.out.println(result);
    }
}
Q1946 easy code output
What does this code print?
java
import java.util.HashSet;

public class Test {
    public static void main(String[] args) {
        HashSet<String> letters = new HashSet<>();
        letters.add("C");
        letters.add("A");
        letters.add("B");
        System.out.println(letters.contains("A") && letters.contains("B") && letters.contains("C"));
    }
}
Q1947 medium
What is the typical time complexity for adding or removing an element at the beginning or end of a `java.util.LinkedList`?
Q1948 hard
Which statement about the runtime overhead and object creation of lambda expressions is generally FALSE?
Q1949 easy code error
What compilation error will occur when compiling this Java code?
java
import java.io.BufferedReader;

public class MyClass {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader("Some text");
    }
}
Q1950 medium
Given an array `String[] names = {"Alice", "Bob", "Charlie"};`, what is the most concise way to iterate through and print each name?
Q1951 easy code error
What is the primary issue that prevents this Java code from compiling?
java
public class LoopError10 {
    public static void main(String[] args) {
        int count = 0;
        while count < 3 { // Missing parentheses around condition
            System.out.println("Current count: " + count);
            count++;
        }
    }
}
Q1952 hard
What is the final value of `a` and `b` after the following Java code executes? java int a = 5; int b = ++a * 2 + a--;
Q1953 hard code output
What is the output of this Java program?
java
public class MultiArrayQ8 {
    public static void main(String[] args) {
        int[][] table = new int[3][3];
        for (int i = 0; i < table.length; i++) {
            for (int j = 0; j < table[i].length; j++) {
                if (i == j) table[i][j] = i + j;
                else if (i > j) table[i][j] = i - j;
                else table[i][j] = i + j + 1;
            }
        }
        System.out.println(table[1][2]);
    }
}
Q1954 hard code error
What is the compilation error in the following Java code?
java
public class OuterClass {
    private int outerValue = 100;

    public static class StaticNestedClass {
        public void display() {
            // Attempt to access a non-static outer field from a static nested class
            System.out.println("Outer value: " + outerValue);
        }
    }
}
Q1955 medium code output
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Iterator;

public class Test {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("One");
        list.add("Two");
        list.add("Three");
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String s = it.next();
            if (s.equals("Two")) {
                it.remove();
            }
        }
        System.out.println(list.size());
    }
}
Q1956 hard code error
What is the compilation error in the following Java code?
java
class UserProfile {
    private String username = "guest";
    public String getUsername() {
        return username;
    }
}

public class AdminPanel {
    public static void main(String[] args) {
        UserProfile profile = new UserProfile();
        System.out.println(profile.username); // Direct access to private field
    }
}
Q1957 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<String> list = new LinkedList<>(Arrays.asList("A", "B", "C", "D"));
        for (String s : list) {
            if (s.equals("B")) {
                list.remove("B");
            }
        }
        System.out.println(list);
    }
}
Q1958 easy
In which of the following contexts can the `break` keyword be used in Java?
Q1959 easy code error
Which error will be reported when compiling the following Java program?
java
class Singleton {
    private Singleton() { }
}

public class TestSingleton {
    public static void main(String[] args) {
        Singleton instance = new Singleton();
    }
}
Q1960 easy
Which of the following is a valid way to overload a method named `process(int data)`?
← Prev 96979899100 Next → Page 98 of 200 · 3994 questions