Which compile-time error is present in the array initialization below?
java
public class ArrayInitTest {
public static void main(String[] args) {
int[] numbers = {10, 20, 30.0, 40}; // 30.0 is a double literal
for (int num : numbers) {
System.out.println(num);
}
}
}
✅ Correct Answer: A) Error: incompatible types: possible lossy conversion from double to int
The array is declared to hold 'int' elements. The literal '30.0' is a 'double'. Java does not allow implicit narrowing conversions (like from 'double' to 'int') during array initialization, requiring an explicit cast if desired.
Q1562easycode error
What kind of error will occur when compiling this Java code?
java
import java.io.FileWriter;
public class Main {
public static void main(String[] args) {
// Missing import for BufferedWriter
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("output.txt"));
bw.write("Hello");
bw.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
}
✅ Correct Answer: A) Compile-time error: cannot find symbol BufferedWriter
The `BufferedWriter` class is part of the `java.io` package and needs to be explicitly imported (`import java.io.BufferedWriter;`) to be used. Without the import, the compiler cannot find the definition of `BufferedWriter`, leading to a 'cannot find symbol' error.
Q1563easy
Can a `java.util.LinkedList` store `null` elements?
✅ Correct Answer: C) Yes, it allows `null` elements.
`java.util.LinkedList`, like most Java collections, permits `null` elements to be stored within it.
Q1564mediumcode 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 rawList = new LinkedList();
rawList.add("String Data");
rawList.add(123);
String data = (String) rawList.get(1);
System.out.println(data);
}
}
✅ Correct Answer: C) ClassCastException
Using a raw `LinkedList` bypasses compile-time type checking. Although an `Integer` (123) can be added, attempting to cast it to `String` at runtime will result in a `ClassCastException`.
Q1565easycode error
Which error will be thrown when running the given Java code?
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
public class IteratorError {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(Arrays.asList("Alpha", "Beta"));
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String s = it.next();
if (s.equals("Alpha")) {
list.add("Gamma"); // Modifying the list directly
}
}
}
}
✅ Correct Answer: A) java.util.ConcurrentModificationException
Modifying the underlying collection directly (e.g., adding an element) while it is being iterated over by an Iterator will cause a `ConcurrentModificationException`.
Q1566easy
How do you convert a `StringBuilder` object back into a standard `String` object?
✅ Correct Answer: D) `toString()`
The `toString()` method returns a new `String` object that contains the character sequence currently represented by the `StringBuilder`.
Q1567easy
Does `FileWriter` primarily deal with writing characters or raw bytes to a file?
✅ Correct Answer: A) Characters
`FileWriter` operates on characters, converting them into bytes using the platform's default encoding for storage.
Q1568medium
What is the effect of declaring a method as `final` on polymorphism?
✅ Correct Answer: B) It prevents the method from being overridden in subclasses.
Declaring a method as `final` explicitly prevents any subclass from overriding that method. This ensures that the method's implementation remains consistent throughout the inheritance hierarchy.
Q1569mediumcode error
What is the error in this Java code snippet?
java
public class MyClass {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Programming");
sb.replace(12, 14, "Java");
System.out.println(sb);
}
}
✅ Correct Answer: D) A runtime error: StringIndexOutOfBoundsException.
The StringBuilder 'sb' has a length of 11. The 'replace' method expects the start index to be less than or equal to the length. An index of 12 is out of bounds, leading to a StringIndexOutOfBoundsException at runtime.
Q1570medium
Which statement is true regarding covariant return types in Java for method overriding?
✅ Correct Answer: B) The return type of the overriding method must be a subtype of the return type of the overridden method.
Since Java 5, covariant return types are allowed, meaning the overriding method's return type can be a subtype of the overridden method's return type.
Q1571mediumcode error
What kind of error will this code throw at runtime?
java
public class LoopTest {
public static void main(String[] args) {
String[] data = {"10", "20", "invalid", "40"};
int i = 0;
while (i < data.length) {
int num = Integer.parseInt(data[i]);
System.out.println("Parsed: " + num);
i++;
}
}
}
✅ Correct Answer: B) A java.lang.NumberFormatException.
The `Integer.parseInt()` method attempts to convert a string to an integer. When it encounters the string "invalid", it cannot parse it into a number, leading to a `NumberFormatException` at runtime.
Q1572easycode output
What is the output of this code?
java
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Integer> scores = new ArrayList<>();
scores.add(90);
scores.add(85);
scores.add(92);
System.out.println(scores);
}
}
✅ Correct Answer: C) [90, 85, 92]
When an ArrayList is printed directly using `System.out.println()`, its `toString()` method is invoked, which typically returns a string representation like `[element1, element2, ..., elementN]`.
Q1573easycode output
What is the output of this code?
java
public class LambdaTest {
public static void main(String[] args) {
String prefix = "Value: ";
Runnable r = () -> System.out.println(prefix + 10);
r.run();
}
}
✅ Correct Answer: A) Value: 10
Lambdas can capture effectively final local variables. The `prefix` variable is effectively final, so the lambda correctly concatenates it with 10 and prints the result.
Q1574hard
How does the internal buffer of `BufferedReader` primarily enhance the efficiency of reading operations from an underlying `Reader`?
✅ Correct Answer: B) By reducing the number of direct I/O calls to the underlying stream.
The `BufferedReader`'s internal buffer reads larger chunks of data from the underlying stream at once, significantly reducing the number of potentially slow physical I/O operations.
Q1575easy
Can the `run()` method of the `Runnable` interface declare checked exceptions (e.g., `throws IOException`)?
✅ Correct Answer: B) No, the `run()` method signature (`public void run()`) does not allow checked exceptions to be declared.
The `run()` method signature `public void run()` in the `Runnable` interface does not include any `throws` clause, meaning implementations cannot declare checked exceptions. Any checked exceptions must be handled internally within the `run()` method.
Q1576medium
What is the primary performance drawback of `StringBuffer` compared to `StringBuilder` in a single-threaded environment?
✅ Correct Answer: B) Slower execution due to the overhead of synchronization.
`StringBuffer`'s methods are synchronized to ensure thread safety, which introduces a performance overhead. In a single-threaded environment where thread safety is not required, `StringBuilder` is faster because it lacks this synchronization.
equals() is case-sensitive, while equalsIgnoreCase() ignores case. compareTo() returns a negative integer if the calling string is lexicographically less than the argument string, and a positive integer if greater.
Q1578medium
How does `StringBuffer` ensure thread safety when multiple threads attempt to modify its contents concurrently?
✅ Correct Answer: A) It uses internal synchronization mechanisms, making its methods synchronized.
`StringBuffer` achieves thread safety by synchronizing its methods, which means only one thread can access an instance's methods at a time, preventing data corruption in multi-threaded environments.
Q1579medium
Which statement accurately describes the behavior of `File.canWrite()` on a Unix-like system when a `File` object represents a file for which the user has read permissions but not write permissions?
✅ Correct Answer: B) `canWrite()` will return `false`.
`canWrite()` specifically checks if the application is permitted to modify the file denoted by the abstract pathname. If the user lacks write permissions, it will correctly return `false`.
Q1580hard
Given the following Java classes:
java
class Base {
public void test(Object obj) { System.out.println("Base Object"); }
}
class Derived extends Base {
public void test(String str) { System.out.println("Derived String"); }
}
What will be printed when the following code is executed?
`Derived d = new Derived();
d.test("hello");`
✅ Correct Answer: B) Derived String
When resolving overloaded methods in an inheritance hierarchy, the compiler searches for the most specific method. `Derived.test(String)` is more specific for a `String` argument than `Base.test(Object)`, so it is chosen.