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
}
}
✅ Correct Answer: A) Compilation error: The method setTemperature(double) is not visible.
The 'setTemperature' method is declared as private within the TemperatureSensor class. Private methods cannot be accessed or called from outside the class, leading to a compilation error when Monitor tries to invoke it.
Q3842hard
What happens internally when `list.remove(Object o)` is called on a `java.util.LinkedList` to remove a non-null element?
✅ Correct Answer: B) The list iterates from the beginning to find the first occurrence of the object, requiring O(N) time in the worst case, and then updates pointers.
To remove a specific `Object o` from a `LinkedList`, the list must traverse its nodes from the beginning (or end, if optimized for location) until it finds an element that equals `o`. This search operation takes O(N) time in the worst case, after which pointer manipulation is O(1).
Q3843mediumcode 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);
}
}
}
✅ Correct Answer: A) Compile-time error
All elements in an array initializer list must be compatible with the array's declared type. Including a `double` literal (3.0) in an `int` array initialization causes a compile-time type mismatch error.
Q3844hard
After invoking `BufferedWriter.flush()`, what is the state of the `BufferedWriter`'s internal character buffer?
✅ Correct Answer: A) The buffer is cleared (emptied), but the underlying character array used for buffering remains allocated and its size is unchanged.
Invoking `flush()` writes all accumulated characters from the internal buffer to the underlying `Writer`. After this operation, the buffer is effectively emptied, allowing new characters to be written, but the allocated `char[]` array itself is not deallocated or resized.
Q3845mediumcode 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]);
}
}
}
✅ Correct Answer: B) Runtime error: java.lang.ArrayIndexOutOfBoundsException.
The loop condition `i <= numbers.length` allows `i` to reach `numbers.length` (which is 3 in this case). Arrays are 0-indexed, so `numbers[3]` is an invalid access, leading to a `java.lang.ArrayIndexOutOfBoundsException` at runtime.
Q3846easycode 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);
}
}
✅ Correct Answer: A) Pass
The ternary operator `? :` evaluates the condition `score > 60`. Since 75 is greater than 60, the condition is `true`, and the value "Pass" is assigned to `result`.
Q3847easycode 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"));
}
}
✅ Correct Answer: C) A compile-time error because interfaces cannot be instantiated directly.
Map is an interface and cannot be instantiated directly. An implementing class like HashMap or TreeMap must be used to create an object.
Q3848easy
Which keyword is used to explicitly throw an exception in Java?
✅ Correct Answer: C) throw
The `throw` keyword is used to explicitly throw an instance of an exception, which can then be caught by a `catch` block.
Q3849easycode 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();
}
}
✅ Correct Answer: C) NoSuchElementException
After removing 'first' and '123', the queue becomes empty. The third call to `rawQueue.remove()` on an empty queue will throw a `NoSuchElementException`.
Q3850hardcode 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
}
}
✅ Correct Answer: A) Multiple non-overriding abstract methods found in interface InvalidFunctionalInterface
A functional interface must have exactly one abstract method. While `toString()` is a public method of `Object` and does not count towards the abstract method count, `execute()` and `anotherMethod()` are both abstract and not part of `Object`, resulting in 'Multiple non-overriding abstract methods found' compilation error because it violates the `@FunctionalInterface` contract.
Q3851easycode 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());
}
}
✅ Correct Answer: C) 3
The `size()` method returns the number of elements in the ArrayList. After adding three elements, the size is 3.
Q3852easycode 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);
}
}
✅ Correct Answer: A) 19.99
A `double` variable `price` is initialized with the floating-point value 19.99. Printing `price` directly outputs this exact numerical value.
Q3853mediumcode 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");
}
}
✅ Correct Answer: C) java.lang.RuntimeException: Comparator failed
When `map.put(2, "Two")` is called, `TreeMap` uses its `Comparator` to determine the key's position. The `brokenComp`'s `compare` method explicitly throws a `RuntimeException`. This `RuntimeException` will propagate up and terminate the program.
Q3854hard
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"))`)
✅ Correct Answer: B) The Turkish dotted capital 'I' (`'İ'`).
In the Turkish locale, the lowercase Latin letter 'i' (`i`) converts to the uppercase Latin letter 'I' with dot above (`İ`), not the standard ASCII 'I'. This demonstrates locale-dependent character conversions for `toUpperCase()`.
Q3855medium
Which underlying data structure does `TreeSet` internally use to store its elements?
✅ Correct Answer: D) A Red-Black tree
`TreeSet` is implemented using a Red-Black tree, which is a self-balancing binary search tree. This structure ensures `log(n)` time complexity for basic operations.
Q3856hardcode 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);
}
}
✅ Correct Answer: A) Compile-time error: Cannot assign a value to final variable 'x'.
A `final` instance variable can only be assigned a value once. In this code, `x` is assigned in the instance initializer block (`x = 5;`) and then again in the constructor (`x = 10;`), which is not allowed and causes a compile-time error.
Q3857hardcode 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");
}
}
✅ Correct Answer: A) error: Car is not abstract and does not override abstract method stop() in Vehicle
A concrete class that extends an abstract class must implement all inherited abstract methods, or else it must declare itself as abstract. In this case, 'Car' is not abstract but fails to implement 'stop()'.
Q3858easycode 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.");
}
}
✅ Correct Answer: A) Inside riskyCall
Error: / by zero
Main execution continues.
The `riskyCall` method prints 'Inside riskyCall' and then attempts a division by zero, which throws an `ArithmeticException`. The `catch` block in `main` handles this exception, printing its message. The line 'After division' in `riskyCall` is never reached, and 'Main execution continues.' is printed after the catch block.
Q3859easy
By default, if a file already exists, what does `FileWriter` do when you open it with `new FileWriter("filename.txt")`?
✅ Correct Answer: B) Overwrites the existing content of the file.
By default, `FileWriter` truncates an existing file to zero length before writing, effectively overwriting its content.
Q3860easycode 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);
}
}
✅ Correct Answer: A) 3000
Similar to `byte` arithmetic, operations on `short` variables are promoted to `int`. The sum `1000 + 2000` is `3000`, which is then stored in the `int` variable `result` and printed.