☕ Java MCQ Questions – Page 75

Questions 1481–1500 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1481 medium code error
What kind of error will occur when running this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][] matrix = new int[2][];
        matrix[0][0] = 10;
        System.out.println(matrix[0][0]);
    }
}
Q1482 easy code output
What does this Java code print to the console?
java
public class OverloadDemo10 {
    void log(String message) {
        System.out.println("Fixed argument: " + message);
    }
    void log(String... messages) {
        System.out.println("Varargs: " + messages.length + " messages");
    }
    public static void main(String[] args) {
        OverloadDemo10 obj = new OverloadDemo10();
        obj.log("Important info");
    }
}
Q1483 medium
To define a custom checked exception in Java, which class should it typically extend?
Q1484 medium
Which pair of methods can a `Serializable` class implement to provide custom serialization and deserialization logic?
Q1485 hard code error
What compile-time error is expected from this Java code?
java
import java.io.IOException;

class DataProcessor {
    DataProcessor() {
        // Simulating an operation that might throw IOException
        boolean errorCondition = true;
        if (errorCondition) {
            throw new IOException("Failed to initialize");
        }
    }
}
Q1486 hard code output
What is the output of this Java code?
java
import java.util.Comparator;
import java.util.TreeMap;

class CustomStringComparator implements Comparator<String> {
    @Override
    public int compare(String s1, String s2) {
        if (s1 == null && s2 == null) return 0;
        if (s1 == null) return -1; // Nulls come first
        if (s2 == null) return 1;  // Non-nulls come after null
        return s1.compareTo(s2);
    }
}

public class Test {
    public static void main(String[] args) {
        TreeMap<String, Integer> map = new TreeMap<>(new CustomStringComparator());
        map.put("banana", 2);
        map.put("apple", 1);
        map.put(null, 0); // This is allowed by the comparator
        map.put("cherry", 3);
        System.out.println(map.firstEntry());
    }
}
Q1487 medium code error
What error will this Java code produce when executed?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        Iterator<String> it = fruits.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        String extra = it.next(); // Attempt to retrieve an element when none left
    }
}
Q1488 medium
For processing an input and performing an action without returning any result, which built-in functional interface should be used?
Q1489 medium
What are the three main optional parts of a traditional Java `for` loop header, separated by semicolons?
Q1490 hard code error
What error occurs when attempting to compile the following Java code?
java
public class AssignmentInIf {
    public static void main(String[] args) {
        int x = 10;
        if (x = 20) { // Assignment of int to boolean context
            System.out.println("X is 20.");
        } else {
            System.out.println("X is not 20.");
        }
    }
}
Q1491 medium code error
Which exception will be thrown when executing this Java code?
java
public class StringError6 {
    public static void main(String[] args) {
        String prefix = null;
        String full = prefix.concat("world");
        System.out.println(full);
    }
}
Q1492 medium code error
What is the compilation error in the following Java code?
java
public class ScopeIssue {
    public static void main(String[] args) {
        if (true) {
            String message = "Hello";
        }
        System.out.println(message);
    }
}
Q1493 medium
Which pair of classes from `java.io` package are primarily used to perform object serialization and deserialization?
Q1494 easy code output
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        try {
            int result = 10 / 0;
            System.out.print("Try block executed. ");
        } catch (ArithmeticException e) {
            System.out.print("Catch block executed. ");
        } finally {
            System.out.print("Finally block executed.");
        }
    }
}
Q1495 medium
What is a 'race condition' in the context of multithreading, and how does synchronization help prevent it?
Q1496 easy
What principle in Object-Oriented Programming allows an object to hide its internal state and require all interaction to be performed through its public methods?
Q1497 medium
Which type of method reference is used to refer to a constructor, such as when creating a new `ArrayList` object?
Q1498 hard code output
What is the output of this Java code?
java
StringBuilder sb = new StringBuilder("Hello");
String s1 = sb.toString();
sb.append(" World");
String s2 = sb.toString();

System.out.println(s1.equals(s2));
System.out.println(s1);
System.out.println(s2);
Q1499 easy
Where is the condition checked in a `while` loop?
Q1500 easy code output
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        int score = 75;
        if (score >= 90) {
            System.out.println("Grade A");
        } else if (score >= 70) {
            System.out.println("Grade B");
        } else {
            System.out.println("Grade C");
        }
    }
}
← Prev 7374757677 Next → Page 75 of 200 · 3994 questions