☕ Java MCQ Questions – Page 74

Questions 1461–1480 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1461 easy
By default, what character encoding does `FileReader` use to translate bytes read from the file into characters?
Q1462 hard
Which of the following `String` declarations would be invalid as a `case` label in a Java `switch` *statement*?
Q1463 easy
What does `HashSet` primarily offer compared to `ArrayList` when managing a collection of items where uniqueness and fast lookups are important?
Q1464 medium code error
What error will occur when compiling and running this Java code?
java
import java.util.HashSet;
import java.util.Set;

public class HashSetError7 {
    public static void main(String[] args) {
        Set rawSet = new HashSet(); // Raw type
        rawSet.add("String Data");
        rawSet.add(123); // Add an Integer

        for (Object obj : rawSet) {
            String s = (String) obj; // ERROR line if obj is Integer
            System.out.println(s.toUpperCase());
        }
    }
}
Q1465 medium
Consider a `TreeSet<Integer>` named `numbers` containing `{10, 20, 30, 40, 50}`. What would be the result of calling `numbers.headSet(30)`?
Q1466 hard
When `clone()` is invoked on a `String[]` array containing references to `String` objects, what is the outcome regarding the array's contents?
Q1467 hard code output
What is the output of this code?
java
class CheckedCustomException extends Exception { public CheckedCustomException(String m, Throwable c) { super(m, c); } }
class UncheckedCustomException extends RuntimeException { public UncheckedCustomException(String msg) { super(msg); } }
public class Main {
    public static void helperMethod() { throw new UncheckedCustomException("Helper's problem"); }
    public static void process() throws CheckedCustomException {
        try { helperMethod(); }
        catch (UncheckedCustomException e) { System.out.println("Caught Unchecked"); throw new CheckedCustomException("Wrapped as checked", e); }
    }
    public static void main(String[] args) {
        try { process(); }
        catch (CheckedCustomException e) {
            System.out.println("Caught Checked: " + e.getMessage());
            System.out.println("Cause: " + e.getCause().getMessage());
        }
    }
}
Q1468 medium
When an `if` statement's condition uses the logical AND operator (`&&`) in Java, what must be true for the entire condition to evaluate to `true`?
Q1469 easy
Can a functional interface contain `default` methods?
Q1470 medium
What is the outcome of calling the `get()` method on an `Optional` instance that does not contain a value (i.e., `Optional.empty()`)?
Q1471 easy
Which method is used to add characters or other data types to the end of a `StringBuilder` object?
Q1472 medium code error
What is the error in this Java code?
java
import java.util.ArrayList;
import java.util.List;

public class MyClass {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);

        // Access an element at an invalid index
        System.out.println(numbers.get(numbers.size()));
    }
}
Q1473 medium code error
What error occurs when compiling this Java code?
java
public class LoopTest {
    public static void main(String[] args) {
        int i = 0;
        do {
            System.out.println(i);
            break;
            i++;
        } while (i < 5);
    }
}
Q1474 hard code output
What does this code print to the console?
java
public class MultiArrayQ7 {
    static void modify(int[][] arr) {
        arr[0] = new int[]{10, 20};
        arr[1][0] = 50;
    }
    public static void main(String[] args) {
        int[][] matrix = {{1, 2}, {3, 4}};
        modify(matrix);
        System.out.println(matrix[0][0] + "," + matrix[1][0]);
    }
}
Q1475 medium code output
What does this code print?
java
import java.io.File;

public class FileCheck {
    public static void main(String[] args) {
        File file = new File("nonExistentFile.txt");
        System.out.println(file.exists() + ", " + file.isFile() + ", " + file.isDirectory());
    }
}
Q1476 medium code error
What error will occur when compiling this Java code?
java
public class ConditionCheck {
    public static void main(String[] args) {
        int x = 10;
        if (x = 20) {
            System.out.println("Value is 20");
        } else {
            System.out.println("Value is not 20");
        }
    }
}
Q1477 hard
Which statement accurately describes a key difference between `volatile` and `synchronized` keywords in Java?
Q1478 easy code error
What is the result of running this Java code?
java
public class StringError {
    public static void main(String[] args) {
        String text = "hello";
        System.out.println(text.charAt(-1));
    }
}
Q1479 hard
In a Java program with nested `do-while` loops, if an inner `do-while` loop contains a `break` statement with a label referring to the *outer* `do-while` loop, what is the precise effect?
Q1480 medium code output
What is the content of 'log.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;

public class AppendTest {
    public static void main(String[] args) {
        try {
            FileWriter writerA = new FileWriter("log.txt");
            writerA.write("Log entry 1.\n");
            writerA.close();
            FileWriter writerB = new FileWriter("log.txt", true); // Append mode
            writerB.write("Log entry 2.\n");
            writerB.close();
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
← Prev 7273747576 Next → Page 74 of 200 · 3994 questions