☕ Java MCQ Questions – Page 193

Questions 3841–3860 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3841 medium code error
What is the error in the following Java code?
java
class TemperatureSensor {
    private double temperature;

    public TemperatureSensor(double temp) {
        this.temperature = temp;
    }

    // Private setter
    private void setTemperature(double newTemp) {
        this.temperature = newTemp;
    }

    public double getTemperature() {
        return temperature;
    }
}

public class Monitor {
    public static void main(String[] args) {
        TemperatureSensor sensor = new TemperatureSensor(25.5);
        sensor.setTemperature(26.0); // Attempt to call private method
    }
}
Q3842 hard
What happens internally when `list.remove(Object o)` is called on a `java.util.LinkedList` to remove a non-null element?
Q3843 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) {
        int[] arr = {1, 2, 3.0};
        for (int val : arr) {
            System.out.println(val);
        }
    }
}
Q3844 hard
After invoking `BufferedWriter.flush()`, what is the state of the `BufferedWriter`'s internal character buffer?
Q3845 medium code error
What is the primary issue with this Java code snippet when executed?
java
public class LoopError {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30};
        for (int i = 0; i <= numbers.length; i++) {
            System.out.println(numbers[i]);
        }
    }
}
Q3846 easy code output
What does this code print?
java
public class Main {
    public static void main(String[] args) {
        int score = 75;
        String result = (score > 60) ? "Pass" : "Fail";
        System.out.println(result);
    }
}
Q3847 easy code error
What is the error in this Java code?
java
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Map<String, String> myMap = new Map<>();
        myMap.put("key", "value");
        System.out.println(myMap.get("key"));
    }
}
Q3848 easy
Which keyword is used to explicitly throw an exception in Java?
Q3849 easy code error
What exception will occur during the execution of this Java code snippet?
java
import java.util.Queue;
import java.util.LinkedList;

public class QueueError {
    public static void main(String[] args) {
        Queue rawQueue = new LinkedList();
        rawQueue.add("first");
        rawQueue.add(123);
        String item1 = (String) rawQueue.remove();
        Integer item2 = (Integer) rawQueue.remove();
        String item3 = (String) rawQueue.remove();
    }
}
Q3850 hard code error
What is the compilation error in the following Java code snippet?
java
@FunctionalInterface
interface InvalidFunctionalInterface {
    void execute();
    String toString(); // Object method, ignored
    void anotherMethod(); // Second abstract method
}

public class Main {
    public static void main(String[] args) {
        // The error occurs in the interface definition itself
    }
}
Q3851 easy code output
What is the output of this code?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);
        System.out.println(numbers.size());
    }
}
Q3852 easy code output
What does the following Java program print?
java
public class Main {
    public static void main(String[] args) {
        double price = 19.99;
        System.out.println(price);
    }
}
Q3853 medium code error
This code uses a `Comparator` that always throws an exception. What is the expected runtime error?
java
import java.util.Comparator;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Comparator<Integer> brokenComp = (a, b) -> {
            throw new RuntimeException("Comparator failed");
        };
        TreeMap<Integer, String> map = new TreeMap<>(brokenComp);
        map.put(1, "One");
        map.put(2, "Two");
    }
}
Q3854 hard
In the Turkish locale (`tr-TR`), what is the expected result of `new Locale("tr", "TR").toUpper("i")`? (Assume `String.toUpperCase(Locale.forLanguageTag("tr-TR"))`)
Q3855 medium
Which underlying data structure does `TreeSet` internally use to store its elements?
Q3856 hard code error
What will be the result of compiling this Java code?
java
public class Example {
    private final int x;
    {
        x = 5;
    }
    public Example() {
        x = 10; // Problematic line
        System.out.println("Constructor: " + x);
    }
    public static void main(String[] args) {
        Example ex = new Example();
        System.out.println("Main: " + ex.x);
    }
}
Q3857 hard code error
What is the compile-time error in this Java code?
java
abstract class Vehicle {
    public abstract void start();
    public abstract void stop();
}

class Car extends Vehicle {
    @Override
    public void start() {
        System.out.println("Car started");
    }
}
Q3858 easy code output
What is the output of this Java code?
java
public class MyClass {
    public static void riskyCall() {
        System.out.println("Inside riskyCall");
        int result = 10 / 0; // ArithmeticException
        System.out.println("After division");
    }

    public static void main(String[] args) {
        try {
            riskyCall();
        } catch (ArithmeticException e) {
            System.out.println("Error: " + e.getMessage());
        }
        System.out.println("Main execution continues.");
    }
}
Q3859 easy
By default, if a file already exists, what does `FileWriter` do when you open it with `new FileWriter("filename.txt")`?
Q3860 easy code output
What does this code print?
java
public class DataTypeTest {
    public static void main(String[] args) {
        short s1 = 1000;
        short s2 = 2000;
        int result = s1 + s2;
        System.out.println(result);
    }
}
← Prev 191192193194195 Next → Page 193 of 200 · 3994 questions