What compile-time error will occur when compiling this Java code snippet?
java
public class Test {
public static void main(String[] args) {
int[][][] threeDArray = new int[2][][];
threeDArray[0] = new int[3][];
int[] oneD = new int[]{1, 2, 3};
threeDArray[0][0] = oneD;
System.out.println(threeDArray[0][0][0]);
}
}
✅ Correct Answer: A) java: error: incompatible types: int[] cannot be converted to int[][]
The variable `threeDArray[0][0]` expects an `int[][]` type (as it's part of a `int[][][]` array), but an `int[]` (oneD) is being assigned to it. This is a type mismatch that the Java compiler detects immediately.
Q822medium
What is the fundamental difference between `FileReader` and `FileInputStream`?
✅ Correct Answer: B) `FileReader` reads character streams, while `FileInputStream` reads raw byte streams.
`FileReader` is designed for reading characters and inherently handles character encoding, whereas `FileInputStream` reads raw bytes without any character encoding interpretation.
Q823easycode output
What is the output of this code?
java
public class Main {
public static void main(String[] args) {
int[][] data = {{10, 20, 30}, {40, 50}};
System.out.println(data.length);
}
}
✅ Correct Answer: A) 2
`data.length` returns the number of rows (or inner arrays) in the 2D array. In this case, there are 2 rows, so the output is `2`.
Q824medium
What is the primary advantage of using a `try-with-resources` statement when working with `FileWriter`?
✅ Correct Answer: C) It ensures that the `FileWriter` is automatically closed upon exiting the `try` block, even if exceptions occur.
`try-with-resources` guarantees that any resource declared within the `try` block that implements `AutoCloseable` will have its `close()` method invoked automatically, ensuring proper resource management.
Q825mediumcode output
What does this Java code print to the console?
java
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Integer> numbers = new HashSet<>();
numbers.add(10);
numbers.add(20);
numbers.add(10);
numbers.add(30);
System.out.println(numbers.size());
}
}
✅ Correct Answer: B) 3
HashSet stores only unique elements. When 10 is added for the second time, it's ignored because an equal element already exists in the set. Thus, the set contains 10, 20, and 30.
Q826medium
Which of the following best describes the condition required for an `if` statement's block of code to execute in Java?
✅ Correct Answer: C) The condition must evaluate to a boolean `true`.
In Java, an `if` statement's condition must always resolve to a primitive `boolean` value (`true` or `false`). The `if` block executes only if this condition is `true`.
Q827medium
Under what condition does the Java compiler automatically provide a default no-argument constructor for a class?
✅ Correct Answer: A) If the class does not define any constructors (neither no-arg nor parameterized).
The compiler only provides a default constructor if no constructors (either no-arg or parameterized) are explicitly defined in the class. Once any constructor is defined, the default is no longer provided.
Q828hardcode error
What will be the result of compiling this Java code?
java
abstract class Shape {
abstract void draw();
void fillColor() {
System.out.println("Filling color");
}
}
public class DrawingApp {
public static void main(String[] args) {
Shape s = new Shape(); // Problematic line
s.fillColor();
}
}
✅ Correct Answer: A) Compile-time error: Shape is abstract; cannot be instantiated.
Abstract classes cannot be instantiated directly because they may contain abstract methods that have no implementation. To create an object, you must instantiate a concrete subclass that provides implementations for all abstract methods.
Q829medium
Which of the following best describes the difference between `list.remove(Object o)` and `list.remove(int index)` for an `ArrayList`?
✅ Correct Answer: A) `remove(Object o)` removes the first occurrence of the specified object, while `remove(int index)` removes the element at the specified position.
`remove(Object o)` searches for and removes the first element that is equal to the specified object. `remove(int index)` removes the element located at the given zero-based index.
Q830easycode output
What is the output of this Java code?
java
public class TypeCast {
public static void main(String[] args) {
int myInt = 100;
double myDouble = myInt;
System.out.println(myDouble);
}
}
✅ Correct Answer: A) 100.0
This is an example of widening (implicit) casting. An int is automatically converted to a double, adding a decimal point representation.
Q831easycode output
What does this Java code print?
java
class Chain {
Chain() {
this(10);
System.out.println("No-arg constructor");
}
Chain(int value) {
System.out.println("Parameterized constructor with value: " + value);
}
}
public class Main {
public static void main(String[] args) {
Chain c = new Chain();
}
}
✅ Correct Answer: A) Parameterized constructor with value: 10
No-arg constructor
The `this(10)` call in the no-argument constructor initiates constructor chaining, executing the parameterized constructor first. After the chained constructor completes, the rest of the no-argument constructor's body is executed.
Q832easy
Method overloading is an example of which type of polymorphism in Java?
✅ Correct Answer: C) Compile-time polymorphism.
Method overloading is resolved at compile time, meaning the compiler decides which overloaded method to invoke based on the method signature at compilation. This is also known as static polymorphism.
Q833hard
In a `try-with-resources` statement, if an exception is thrown both by the `try` block's execution and by the `close()` method of one of the resources, which exception is propagated and how is the other handled?
✅ Correct Answer: B) The exception from the `try` block is propagated, and the exception from `close()` is added as a suppressed exception.
In `try-with-resources`, if both the `try` block and a resource's `close()` method throw an exception, the exception from the `try` block is propagated. The exception from the `close()` method is added to the primary exception as a 'suppressed' exception.
Q834easycode output
What is the output of this code?
java
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Boolean> status = new HashMap<>();
status.put("TaskA", true);
status.put("TaskB", false);
System.out.println(status.containsKey("TaskA") + ", " + status.containsKey("TaskC"));
}
}
✅ Correct Answer: B) true, false
The `containsKey()` method checks if the map contains a mapping for the specified key. 'TaskA' exists, but 'TaskC' does not.
Q835easycode error
What error occurs when `Thread.sleep()` is called with a negative argument?
java
public class Main {
public static void main(String[] args) {
try {
Thread.sleep(-100); // Negative sleep time
} catch (InterruptedException e) {
System.out.println("Interrupted!");
}
}
}
✅ Correct Answer: A) java.lang.IllegalArgumentException
The `Thread.sleep()` method throws an `IllegalArgumentException` if the value of the timeout argument is negative.
Q836easy
Which keyword is used to immediately terminate a `for` loop and continue program execution at the statement immediately following the loop?
✅ Correct Answer: C) `break`
The `break` statement forces an immediate exit from the current loop, transferring control to the statement following the loop.
Q837medium
What is the primary purpose of using a labeled `break` statement in Java?
✅ Correct Answer: B) To terminate a specific outer loop from within an inner loop.
A labeled `break` statement is used to terminate an outer loop, or any labeled statement, from within an inner block. This allows for breaking out of multiple levels of nested loops.
Q838hardcode error
What compile-time error will this Java code produce?
java
public class OperatorError10 {
public static void main(String[] args) {
final int counter = 10;
counter++; // Error line
System.out.println(counter);
}
}
✅ Correct Answer: B) cannot assign a value to final variable counter
The `final` keyword in Java makes a variable's value immutable after initialization. The increment operator `++` attempts to reassign the variable (e.g., `counter = counter + 1`), which violates the `final` constraint, leading to a compile-time error.
Q839easycode error
What will be the result of executing the following Java code snippet?
java
import java.util.Queue;
import java.util.LinkedList;
public class QueueError {
public static void main(String[] args) {
Queue<Integer> numbers = new LinkedList<>();
System.out.println(numbers.element());
}
}
✅ Correct Answer: C) NoSuchElementException
The `element()` method retrieves, but does not remove, the head of the queue. If the queue is empty, it throws a `NoSuchElementException`.
Q840mediumcode output
What is the output of this code?
java
String raw = " Hello World ";
String trimmed = raw.trim();
System.out.println(raw.length() + " " + trimmed.length());
✅ Correct Answer: A) 17 11
The length() method returns the number of characters in the string. The trim() method returns a new string with leading and trailing whitespace removed, without modifying the original string.