☕ Java MCQ Questions – Page 180

Questions 3581–3600 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3581 easy
How do you declare and initialize an integer array named `numbers` with a capacity for 5 elements in Java?
Q3582 easy code output
What does this code print to the console?
java
import java.io.*;

class WrapperHolder implements Serializable {
    private Integer count;
    private Boolean isActive;
    private Character initial;

    public WrapperHolder(Integer c, Boolean a, Character i) {
        this.count = c;
        this.isActive = a;
        this.initial = i;
    }

    public String toString() {
        return "Count: " + count + ", Active: " + isActive + ", Initial: " + initial;
    }
}

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        WrapperHolder wh = new WrapperHolder(123, true, 'X');
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(wh);
        oos.close();

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        WrapperHolder deserializedWh = (WrapperHolder) ois.readObject();
        ois.close();

        System.out.println(deserializedWh);
    }
}
Q3583 hard code error
What compilation error will occur when compiling this Java code?
java
public class LabeledBlockBreak {
    public static void main(String[] args) {
        myBlock: {
            System.out.println("Before break");
            break myBlock;
            System.out.println("After break");
        }
        System.out.println("Exited main block");
    }
}
Q3584 medium code error
What compilation error will occur in the `main` method?
java
public class MySpecificCheckedException extends Exception {
    public MySpecificCheckedException(String message) {
        super(message);
    }
}

public class AnotherCheckedException extends Exception {
    public AnotherCheckedException(String message) {
        super(message);
    }
}

public class Worker {
    public void doWork() throws MySpecificCheckedException {
        System.out.println("Doing work...");
        throw new MySpecificCheckedException("Work failed!");
    }

    public static void main(String[] args) {
        Worker worker = new Worker();
        try {
            worker.doWork();
        } catch (AnotherCheckedException e) {
            System.out.println("Caught another exception: " + e.getMessage());
        }
    }
}
Q3585 medium code error
What is the compilation error in the provided Java code?
java
interface Processor {
    void process(Object data);
}

class StringProcessor implements Processor {
    @Override
    public void process(String data) { // ERROR: signature mismatch
        System.out.println("Processing String: " + data);
    }
}

public class Main {
    public static void main(String[] args) {
        // StringProcessor sp = new StringProcessor();
        // sp.process("hello");
    }
}
Q3586 hard code output
What does this code print?
java
import java.util.Arrays;
import java.util.stream.IntStream;

public class ArrayStreamFlatMap {
    public static void main(String[] args) {
        int[][] matrix = {{1, 2}, {3}, {4, 5, 6}};
        int sum = Arrays.stream(matrix)
                        .flatMapToInt(row -> Arrays.stream(row))
                        .sum();
        System.out.println(sum);
    }
}
Q3587 medium code error
What error occurs when running this code, assuming 'temp_file.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 tempFile = new File("temp_file.txt");
        try {
            tempFile.createNewFile();
            FileReader reader = new FileReader(tempFile);
            reader.close(); // Close the reader
            int data = reader.read(); // Attempt to read from a closed stream
            System.out.println("Read data: " + data);
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        } finally {
            tempFile.delete();
        }
    }
}
Q3588 hard code error
What is the runtime error when executing this Java code snippet?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<String> set = new TreeSet<>();
        set.add("apple");
        set.add("banana");
        set.add(null);
        System.out.println(set.size());
    }
}
Q3589 hard code output
What is the output of this code? (Assuming Java 17+)
java
public class SwitchTest {
    public static void main(String[] args) {
        Object obj = "Hello Java";
        String description = switch (obj) {
            case Integer i -> "An Integer: " + i;
            case String s -> "A String: " + s.length();
            case null -> "It's null";
            default -> "Unknown Type";
        };
        System.out.println(description);
    }
}
Q3590 easy
Which Java construct is typically used to achieve 100% abstraction?
Q3591 easy code output
What does this Java code print?
java
public class Main {
    public static void main(String[] args) {
        int num1 = 5;
        int num2 = 3;
        int sum = num1 + num2;
        System.out.println(sum);
    }
}
Q3592 easy
Which statement correctly describes the use of `continue` with respect to a loop's condition check?
Q3593 easy
Which operator is used to increment the value of a variable by 1 in Java?
Q3594 easy code output
What does this code print?
java
public class Main {
    public static void main(String[] args) {
        boolean isSunny = true;
        boolean isWeekend = false;
        System.out.println(!isSunny || isWeekend);
    }
}
Q3595 medium code output
What is a possible output of this code snippet?
java
public class Main {
    public static void main(String[] args) {
        Runnable lambdaTask = () -> {
            System.out.println("Lambda running on: " + Thread.currentThread().getName());
        };
        Thread t = new Thread(lambdaTask);
        t.start();
        System.out.println("Main thread finishes.");
    }
}
Q3596 medium code error
What type of error will occur when compiling the following Java code?
java
public class Main {
    public static void main(String[] args) {
        float f = 3.14;
        System.out.println(f);
    }
}
Q3597 easy
When is 'Narrowing Type Casting' typically used in Java?
Q3598 hard code error
What compile-time error occurs when attempting to compile this Java code?
java
class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

class AdvancedCalculator extends Calculator {
    @Override
    public Integer add(int a, int b) {
        return a + b;
    }
}
Q3599 medium code error
What error occurs when running this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer(-10);
        System.out.println(sb.capacity());
    }
}
Q3600 easy code error
What compilation error will occur in the following Java code?
java
interface Movable {
    void move();
}

abstract class Car implements Movable {
    // No implementation for move()
}
← Prev 178179180181182 Next → Page 180 of 200 · 3994 questions