☕ Java MCQ Questions – Page 199

Questions 3961–3980 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3961 medium
Which of the following statements about `ArrayList` and thread safety is true?
Q3962 hard code error
What error occurs when executing the following Java code?
java
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("One");
        stringList.add("Two");

        Integer[] intArray = new Integer[stringList.size()];
        stringList.toArray(intArray); // Attempt to put String elements into an Integer array
        for (Integer num : intArray) {
            System.out.println(num);
        }
    }
}
Q3963 easy code output
What does this code print?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("First");
        sb.append("Second").append("Third");
        System.out.print(sb);
    }
}
Q3964 easy
Which method is primarily used to begin the execution of a newly created `Thread` object?
Q3965 hard code output
What is the output of this code?
java
class Base {
    Number getValue() {
        return Integer.valueOf(10);
    }
}

class Derived extends Base {
    @Override
    Integer getValue() {
        return Integer.valueOf(20);
    }
}

public class Main {
    public static void main(String[] args) {
        Base obj = new Derived();
        System.out.println(obj.getValue());
    }
}
Q3966 hard code error
What is the compile-time error in this Java code?
java
public class MissingRun implements Runnable {
    public void execute() {
        System.out.println("Executing a task.");
    }

    public static void main(String[] args) {
        // new Thread(new MissingRun()).start(); // Line 8
    }
}
Q3967 medium
Which statement best describes how immutability contributes to application security?
Q3968 hard code error
What is the runtime error when executing this Java code snippet?
java
import java.util.TreeSet;
import java.io.*;

class MyObject implements Serializable {
    int value;
    MyObject(int value) { this.value = value; }
}

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        TreeSet<MyObject> set = new TreeSet<>((o1, o2) -> o1.value - o2.value);
        set.add(new MyObject(10));
        
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(set);
        oos.close();
        System.out.println("Serialization successful.");
    }
}
Q3969 hard code error
What kind of error will occur when compiling or running the following Java code?
java
public class SBError {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Test"); // Length 4
        char c = sb.charAt(sb.length()); // Access index 4
        System.out.println(c);
    }
}
Q3970 easy
Which of the following is the correct way to declare an integer variable named `score` in Java?
Q3971 medium code error
What is the compile-time error in this Java code?
java
import java.util.function.Supplier;

public class LambdaError {
    public static void main(String[] args) {
        Supplier<String> greeting = (name) -> "Hello, " + name;
        System.out.println(greeting.get());
    }
}
Q3972 medium
Which of the following Stream API terminal operations is designed to return an `Optional`?
Q3973 easy code output
What does this code print?
java
public class MyClass {
    public static void main(String[] args) {
        char grade = 'B';
        switch (grade) {
            case 'A': System.out.print("Excellent"); break;
            case 'B': System.out.print("Good"); break;
            case 'C': System.out.print("Fair"); break;
            default: System.out.print("Invalid");
        }
    }
}
Q3974 easy code output
What is the output of this Java program?
java
public class Main {
    public static void main(String[] args) {
        int result = 5 + 2 * 3;
        System.out.println(result);
    }
}
Q3975 easy
What is the primary purpose of the `FileReader` class in Java?
Q3976 medium code error
What compilation error will occur in the `main` method's lambda expression?
java
import java.util.function.Consumer;

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

public class ServiceRunner {
    public void executeTask(Consumer<String> task) {
        try {
            task.accept("data");
        } catch (Exception e) {
            System.out.println("Caught task exception: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        ServiceRunner runner = new ServiceRunner();
        runner.executeTask(s -> {
            System.out.println("Processing " + s);
            throw new CustomServiceException("Task failed for " + s);
        });
    }
}
Q3977 medium code error
What is the compilation error in the following Java code?
java
public class FinalVariable {
    public static void main(String[] args) {
        final int MAX_VALUE = 100;
        MAX_VALUE = 200;
        System.out.println(MAX_VALUE);
    }
}
Q3978 easy code output
What is the output of this code?
java
public class DataTypeTest {
    public static void main(String[] args) {
        int myNumber = 123;
        System.out.println(myNumber);
    }
}
Q3979 medium code error
What is the compile-time error for the `Car` class?
java
abstract class Vehicle {
    public abstract void start();
    public void stop() {
        System.out.println("Vehicle stopped.");
    }
}

class Car extends Vehicle {
    // Missing implementation for start()
}

public class Main {
    public static void main(String[] args) {
        // Code won't reach here due to compile error
    }
}
Q3980 medium code output
What does this code print?
java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamTest {
    public static void main(String[] args) {
        List<List<String>> nestedLists = Arrays.asList(
            Arrays.asList("a", "b"),
            Arrays.asList("c", "d", "e"),
            Arrays.asList("f")
        );
        String result = nestedLists.stream()
                                   .flatMap(List::stream)
                                   .map(String::toUpperCase)
                                   .collect(Collectors.joining(" "));
        System.out.println(result);
    }
}
← Prev 197198199200 Next → Page 199 of 200 · 3994 questions