☕ Java MCQ Questions – Page 80

Questions 1581–1600 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1581 medium code error
What error occurs when running this code, assuming 'data_to_skip.txt' is successfully created and closed?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class MyClass {
    public static void main(String[] args) {
        File file = new File("data_to_skip.txt");
        try {
            file.createNewFile();
            FileReader reader = new FileReader(file);
            reader.close(); // Close the reader
            reader.skip(10); // Attempt to skip on a closed stream
            System.out.println("Skipped 10 characters.");
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        } finally {
            file.delete();
        }
    }
}
Q1582 easy code output
What does this code print?
java
public class Main {
    public static void main(String[] args) {
        int[][] numbers = {{10, 20}, {30, 40}};
        int sum = 0;
        for (int[] row : numbers) {
            for (int num : row) {
                sum += num;
            }
        }
        System.out.println(sum);
    }
}
Q1583 easy code error
What type of error will occur when compiling or running this Java code snippet?
java
public class ArrayTest {
    public static void main(String[] args) {
        int[] values = {10, 20, 30};
        System.out.println(values[3]);
    }
}
Q1584 easy
Which of the following combinations is valid for a `try` block in Java?
Q1585 medium
Which of the following 2D array declarations and initializations is syntactically invalid in Java?
Q1586 easy
Which annotation is typically used to explicitly mark an interface as a functional interface, although it's not strictly mandatory?
Q1587 easy code error
What is the compilation error in the following Java code snippet?
java
public class LoopError {
    public static void main(String[] args) {
        int val = 0;
        do {
            System.out.println("Value: " + val++);
        } while (val);
    }
}
Q1588 medium code error
What is the compilation error in the provided Java code?
java
abstract class Vehicle {
    abstract void start();
    abstract void stop();
}

public class Car extends Vehicle {
    void start() {
        System.out.println("Car starts");
    }
}
Q1589 medium
When an unlabeled `break` statement is encountered within the innermost loop of a nested loop structure (e.g., `for (outer) { for (inner) { break; } }`), which loop does it terminate?
Q1590 medium code output
What is the output of this code?
java
class InvalidInputException extends Exception {
    public InvalidInputException(String message) {
        super(message);
    }
}

public class Main {
    public static void validate(int num) throws InvalidInputException {
        if (num == 0) {
            throw new InvalidInputException("Input cannot be zero.");
        }
        System.out.println("Validation successful.");
    }

    public static void main(String[] args) {
        try {
            validate(10);
            validate(0);
        } catch (InvalidInputException e) {
            System.out.println("Caught in main: " + e.getMessage());
        }
    }
}
Q1591 hard code error
What kind of error will occur when executing the following Java code?
java
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.Queue;

public class QError4 {
    public static void main(String[] args) {
        Queue<String> clq = new ConcurrentLinkedQueue<>();
        clq.add("first");
        clq.add(null);
        System.out.println(clq.poll());
    }
}
Q1592 medium
What is the primary benefit of using primitive functional interfaces (e.g., `IntPredicate`, `LongFunction`, `DoubleConsumer`) over their generic counterparts (e.g., `Predicate<Integer>`, `Function<Long, R>`, `Consumer<Double>`)?
Q1593 easy
Which keyword marks the beginning of the loop body in a do-while loop?
Q1594 easy code output
What does this Java code print?
java
class IDGenerator {
    static int nextId = 1;
    int id;

    IDGenerator() {
        id = nextId++;
        System.out.println("Assigned ID: " + id);
    }
}
public class Main {
    public static void main(String[] args) {
        IDGenerator gen1 = new IDGenerator();
        IDGenerator gen2 = new IDGenerator();
        IDGenerator gen3 = new IDGenerator();
    }
}
Q1595 medium code output
What does this code print?
java
import java.util.HashMap;

public class Test {
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "ValueA");
        map.putIfAbsent(1, "ValueB"); // Key 1 already exists
        map.putIfAbsent(2, "ValueC"); // Key 2 does not exist
        System.out.println(map.get(1) + ", " + map.get(2));
    }
}
Q1596 medium
What is the primary difference in performance characteristics between `ArrayList` and `LinkedList` in Java for adding/removing elements in the middle of the list?
Q1597 easy
How many bits does the `int` primitive data type occupy in Java?
Q1598 medium
What is the return type of the `reduce()` operation when provided with an identity and an accumulator (e.g., `stream.reduce(0, (a, b) -> a + b)`)?
Q1599 easy code error
What is the error in this Java code?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterError9 {
    public static void main(String[] args) throws IOException {
        FileWriter writer = new FileWriter("mydata.txt");
        writer.write("First line.\n");
        writer.close();
        // Attempting to write after closing the writer
        writer.write("Second line.");
    }
}
Q1600 easy code error
What kind of error will occur when executing the following Java code?
java
public class Sleeper {
    public static void main(String[] args) {
        Thread.sleep(1000); // Attempt to sleep without handling InterruptedException
        System.out.println("Slept for 1 second.");
    }
}
← Prev 7879808182 Next → Page 80 of 200 · 3994 questions