Identify the compile-time error in this inheritance scenario.
java
class SuperClass {
SuperClass(String name) {
System.out.println("SuperClass: " + name);
}
}
class SubClass extends SuperClass {
SubClass() {
// Implicit call to super() here
}
}
✅ Correct Answer: A) Error: `implicit super constructor SuperClass() is undefined. Must explicitly invoke another constructor`
If a superclass defines only parameterized constructors and no no-argument constructor, a subclass must explicitly call one of the superclass's parameterized constructors using `super()` from its own constructor.
Q3342medium
How do you correctly start a new thread of execution using a `Runnable` object named `myRunnable`?
✅ Correct Answer: C) `new Thread(myRunnable).start();`
To start a new thread using a `Runnable` object, you must first create a `Thread` instance by passing the `Runnable` to its constructor, then call the `start()` method on the `Thread` object. Calling `myRunnable.run()` directly executes the task in the current thread.
Q3343easy
To determine if a `File` object represents a directory rather than a regular file, which method should be used?
✅ Correct Answer: B) `isDirectory()`
The `isDirectory()` method returns true if the `File` object represents an existing directory, and false if it represents an existing file or does not exist.
Q3344medium
To define a custom unchecked exception in Java, which class should it typically extend?
✅ Correct Answer: B) java.lang.RuntimeException
Custom unchecked exceptions, which do not need to be explicitly declared or caught, should extend `java.lang.RuntimeException`. These are typically used for programming errors or unrecoverable conditions.
Q3345hard
Which of the following operations is `TreeMap` *not* inherently optimized for compared to `HashMap` or `LinkedHashMap`?
✅ Correct Answer: D) Constant-time average performance for `get` and `put` operations.
`TreeMap` operations like `get` and `put` have `O(log n)` time complexity due to its Red-Black Tree structure. `HashMap` provides `O(1)` average time complexity for these operations.
Q3346mediumcode output
What does this Java code print to the console?
java
import java.util.Arrays;
import java.util.List;
public class StreamFilterLambda {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
.filter(n -> n % 2 == 0)
.forEach(System.out::print);
}
}
✅ Correct Answer: A) 24
The `filter` operation uses a lambda `n -> n % 2 == 0` to select only even numbers (2 and 4) from the list. The `forEach` then prints these filtered numbers without newlines, resulting in '24'.
Q3347easy
Is it possible for a single `try` block to have multiple `catch` blocks associated with it?
✅ Correct Answer: B) Yes, a `try` block can have multiple `catch` blocks to handle different types of exceptions.
A `try` block can indeed have multiple `catch` blocks, allowing it to handle different specific exception types that might be thrown within the `try` block.
Q3348mediumcode error
What is the error in the following Java code? (Assume classes are in separate files in their respective packages)
java
// File: com/package1/DataHolder.java
package com.package1;
public class DataHolder {
int value = 10; // Default (package-private) access
}
// File: com/package2/MainApp.java
package com.package2;
import com.package1.DataHolder;
public class MainApp {
public static void main(String[] args) {
DataHolder holder = new DataHolder();
System.out.println(holder.value);
}
}
✅ Correct Answer: A) Compilation error: The field DataHolder.value is not visible.
The 'value' field in DataHolder has default (package-private) access. Since MainApp is in a different package (com.package2), it cannot access 'value' directly, resulting in a compilation error due to lack of visibility.
Q3349easycode error
What compile-time error will occur when compiling this Java code?
java
public class MyClass {
public static void main(String[] args) {
int day = 1;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 1:
System.out.println("Another Monday");
break;
default:
System.out.println("Other day");
}
}
}
✅ Correct Answer: C) Compile-time error: Duplicate case label
Each 'case' label within a 'switch' statement must represent a unique constant value. Duplicate 'case 1' labels will cause a compile-time error.
Q3350easycode output
What is the output of this code?
java
import java.util.function.Consumer;
public class LambdaTest {
public static void main(String[] args) {
Consumer<String> printer = s -> System.out.println(s.toUpperCase());
printer.accept("java");
}
}
✅ Correct Answer: A) JAVA
A `Consumer` lambda is defined to print its input string in uppercase. When `accept("java")` is called, it prints the uppercase version of 'java'.
Q3351medium
What is the primary purpose of an instance initializer block (`{}`) in a Java class?
✅ Correct Answer: B) To execute code before every constructor call for an object.
An instance initializer block runs every time an object of the class is created, and it executes before any constructor. It's useful for common initialization logic shared by multiple constructors.
Q3352medium
Which Java String method is specifically designed to replace all occurrences of a substring that matches a given regular expression?
✅ Correct Answer: C) The `replaceAll()` method
The `replaceAll()` method accepts a regular expression as its first argument and replaces all matching occurrences. The `replace()` method replaces all literal occurrences of a target sequence.
Q3353medium
What is the behavior of the `continue` statement when executed within a `for` loop in Java?
✅ Correct Answer: C) It skips the rest of the current iteration and proceeds to the next iteration (evaluating the update expression and condition)
The `continue` statement skips the remaining statements in the current iteration of a loop and proceeds to the next iteration by evaluating the update expression and then the condition.
Q3354mediumcode output
What is the output of the following code snippet?
java
public class StringBuilderTest {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("madam");
sb.reverse();
System.out.println(sb);
}
}
✅ Correct Answer: A) madam
The reverse() method reverses the sequence of characters. Since 'madam' is a palindrome, reversing it results in the same string.
Q3355medium
When does a `HashMap` typically perform a 'rehashing' operation?
✅ Correct Answer: B) When the number of entries exceeds the product of its current capacity and load factor.
Rehashing occurs when the HashMap determines it's becoming too full to maintain good performance, specifically when its size grows beyond its capacity multiplied by the load factor. This process rebuilds the internal array with a larger capacity.
Q3356hardcode error
Which compile-time error will this Java program produce?
java
public class CharNegativeTest {
public static void main(String[] args) {
char unicodeChar = -1; // char type is unsigned and cannot hold negative values
System.out.println(unicodeChar);
}
}
✅ Correct Answer: A) Error: incompatible types: possible lossy conversion from int to char
The 'char' data type in Java is unsigned and represents a 16-bit Unicode character, so it cannot hold negative values. The literal '-1' is an 'int', and assigning it directly to a 'char' variable without an explicit cast results in a compile-time error due to possible lossy conversion.
Q3357hard
Given the following Java class:
java
class Calculator {
void calculate(int a, double b) { System.out.println("int, double"); }
void calculate(double a, int b) { System.out.println("double, int"); }
}
What is the result of compiling and running the following code?
`new Calculator().calculate(10, 20);`
✅ Correct Answer: C) Compilation Error: Ambiguous method call
Both methods are applicable. For `calculate(10, 20)`, the first method requires `20` to widen to `double`, and the second requires `10` to widen to `double`. Neither is more specific than the other, resulting in an ambiguous call.
Q3358easy
What does 'SAM' stand for in the context of functional interfaces?
✅ Correct Answer: C) Single Abstract Method
SAM is an acronym for Single Abstract Method. A functional interface is often referred to as a SAM interface because it must have exactly one abstract method.
Q3359easycode output
What is the output of this code?
java
public class Main {
public static void main(String[] args) {
int[][] values = {{1, 2, 3}, {4, 5, 6}};
int count = 0;
for (int i = 0; i < values.length; i++) {
if (values[i][0] % 2 == 0) {
count++;
}
}
System.out.println(count);
}
}
✅ Correct Answer: A) 1
The loop iterates through each row. `values[i][0]` accesses the first element of each row. For row 0, `values[0][0]` is 1 (not even). For row 1, `values[1][0]` is 4 (even), so `count` becomes 1. The final output is 1.
Q3360mediumcode error
Identify the compilation error in the provided Java code, which attempts to modify an object's state incorrectly within an 'immutable' context.
java
class MyImmutableContainer {
private final StringBuilder content;
public MyImmutableContainer(StringBuilder content) {
this.content = content;
}
public void modifyContent(String newText) {
this.content = new StringBuilder(newText); // Attempt to reassign final field
}
}
✅ Correct Answer: A) Cannot assign a value to final variable 'content'
Even though StringBuilder is a mutable object, the 'content' field itself is declared as final. This means the reference 'content' cannot be reassigned to point to a new StringBuilder object after its initial assignment in the constructor.