☕ Java MCQ Questions – Page 18

Questions 341–360 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q341 medium
How does the functionality of the `break` keyword differ when used within a `switch` statement compared to within a `for` loop?
Q342 hard
Which statement best describes the thread-safety of `java.util.LinkedList` and appropriate measures for concurrent use?
Q343 medium code output
What does this code print?
java
import java.util.HashMap;

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("key1", 10);
        System.out.println(map.getOrDefault("key1", 0) + ", " + map.getOrDefault("key2", 5));
    }
}
Q344 easy code output
What does this Java code print?
java
public class ArrayTest {
    public static void main(String[] args) {
        int[] numbers = {5, 10, 15};
        System.out.println(numbers[numbers.length - 1]);
    }
}
Q345 medium code output
What is the output of the following Java code?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        bw.write("PartOne");
        bw.flush();
        bw.write("PartTwo");
        bw.close();
        System.out.print(sw.toString());
    }
}
Q346 easy code error
What is the error in the following Java code snippet?
java
public class ArrayError {
    public static void main(String[] args) {
        boolean[] flags = new boolean[3];
        flags[0] = true;
        flags[1] = 1;
    }
}
Q347 medium code error
Which of the following describes the error encountered when compiling the provided Java code using varargs and arrays?
java
public class OverloadChecker {
    public void processArgs(int... numbers) {
        System.out.println("Varargs count: " + numbers.length);
    }

    public void processArgs(int[] data) { // This line causes the error
        System.out.println("Array length: " + data.length);
    }

    public static void main(String[] args) {
        OverloadChecker oc = new OverloadChecker();
        oc.processArgs(1, 2, 3);
    }
}
Q348 easy code error
What is the expected error when compiling and running the following Java code?
java
import java.util.LinkedList;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add(123); // Trying to add an Integer to a String list
    }
}
Q349 hard code output
What is the output of this code?
java
interface Loggable {
    private String formatMessage(String message) {
        return "[" + getClass().getSimpleName() + "] " + message;
    }
    default void log(String message) {
        System.out.println(formatMessage("Default log: " + message));
    }
}

class AppLogger implements Loggable {}

public class PrivateInterfaceTest {
    public static void main(String[] args) {
        new AppLogger().log("Initiating...");
    }
}
Q350 easy code output
What is the output of the following Java program?
java
public class Temperature {
    private double celsius;

    public Temperature(double initialCelsius) {
        this.celsius = initialCelsius;
    }

    public double getFahrenheit() {
        return (celsius * 9/5) + 32;
    }

    public static void main(String[] args) {
        Temperature temp = new Temperature(20.0);
        System.out.println(temp.getFahrenheit());
    }
}
Q351 hard code output
What does this code print?
java
String str = "  Java  World  ";
String result = str.trim().replace(" ", "").toLowerCase();

System.out.println(str);
System.out.println(result);
Q352 medium code error
What error occurs when compiling this Java code?
java
public class LoopTest {
    public static void main(String[] args) {
        int count = 5;
        do {
            System.out.println("Counting...");
            count--;
        } while (count);
    }
}
Q353 easy code error
What is the result of running this Java code?
java
public class StringError {
    public static void main(String[] args) {
        String word = "Java";
        System.out.println(word.charAt(4));
    }
}
Q354 easy
What is the purpose of curly braces `{}` around the statement(s) following an `if` or `else` keyword in Java?
Q355 medium code error
What exception will be thrown when executing this Java code snippet?
java
import java.util.LinkedList;
import java.util.Queue;
import java.util.Iterator;

public class QueueError {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();
        queue.add("A");
        queue.add("B");

        Iterator<String> iterator = queue.iterator();
        queue.add("C"); // Structural modification while iterating
        iterator.next();
    }
}
Q356 medium code error
What is the outcome when this Java code is executed?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<Integer> numbers = new LinkedList<>();
        numbers.add(10);
        numbers.add(20);
        int value = numbers.get(2);
        System.out.println(value);
    }
}
Q357 medium
Which statement is true regarding `private` methods and polymorphism in Java?
Q358 easy code output
What does this Java code print?
java
public class Test {
    public static void main(String[] args) {
        String[] greetings = {"Hello", "Hi", "Hey"};
        String lastGreeting = "";
        for (String greeting : greetings) {
            lastGreeting = greeting;
        }
        System.out.println(lastGreeting);
    }
}
Q359 hard code error
What error will this code produce at compile time?
java
public class EffectivelyFinalError {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                System.out.println(i);
            }).start();
            i = i + 0;
        }
    }
}
Q360 medium
What is the purpose of the `transient` keyword when applied to an instance variable in Java?
← Prev 1617181920 Next → Page 18 of 200 · 3994 questions