public class StringBuilderTest {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Programming");
sb.delete(3, 7);
System.out.println(sb);
}
}
✅ Correct Answer: A) Promming
The delete(start, end) method removes characters from the specified start index (inclusive) to the end index (exclusive). In 'Programming', characters at index 3 ('g'), 4 ('r'), 5 ('a'), and 6 ('m') are removed, resulting in 'Promming'.
Q442medium
What does the `ready()` method of `BufferedReader` indicate?
✅ Correct Answer: C) Whether the next read operation will not block (i.e., characters are available in the buffer or can be readily supplied).
The `ready()` method returns `true` if the stream is ready to be read without blocking. This typically means there are characters available in the buffer or the underlying stream can immediately provide them.
Q443hardcode error
What happens when the following Java code is executed?
java
public class NullAutoboxingCheck {
public static void main(String[] args) {
Integer data = null;
if (data == 5) { // Autounboxing `null` to primitive `int` for comparison.
System.out.println("Data is 5.");
} else {
System.out.println("Data is not 5.");
}
}
}
✅ Correct Answer: C) Runtime error: `java.lang.NullPointerException`
When comparing an `Integer` object with a primitive `int` literal (like `data == 5`), Java attempts to unbox the `Integer` object to its primitive `int` value. If the `Integer` object is `null`, this unboxing operation results in a `NullPointerException` at runtime.
Q444easycode output
What does this Java code print?
java
import java.io.File;
public class FileTest {
public static void main(String[] args) {
File file = new File("nonExistentFileToDelete.txt");
boolean deleted = file.delete();
System.out.println(deleted + " " + file.exists());
}
}
✅ Correct Answer: A) false false
The `delete()` method returns `true` if the file or directory was successfully deleted, `false` otherwise. Since 'nonExistentFileToDelete.txt' does not exist, `delete()` returns `false`. Consequently, `exists()` also returns `false`.
Q445mediumcode output
What is the output of the following Java program?
java
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
boolean removed = fruits.remove("Apple");
System.out.println(removed + " " + fruits.size());
}
}
✅ Correct Answer: A) true 1
The `remove(Object)` method attempts to remove the first occurrence of the specified element and returns `true` if it was found and removed, `false` otherwise. "Apple" is found, so it returns `true` and the size becomes 1.
Q446medium
What is the generally accepted naming convention for custom exception classes in Java?
✅ Correct Answer: C) They should end with `Exception` (e.g., `DataNotFoundException`).
The standard Java convention for naming exception classes is to suffix them with `Exception` (e.g., `InvalidUserInputException`, `ServiceUnavailableException`), making their purpose immediately clear.
Q447easycode error
What compile-time error will this Java code produce concerning constructors?
java
class Book {
String title;
Book(String title) {
this.title = title;
}
Book(String title) {
this.title = title + " (Default Author)";
}
}
✅ Correct Answer: A) Compile-time error: Constructor 'Book(String)' is already defined in 'Book'.
Constructor overloading follows the same rules as method overloading: the signature (name and parameter list) must be unique. Here, both constructors have the exact same signature 'Book(String)', leading to a 'constructor already defined' compile-time error.
Q448hard
In a multi-package application, how does Java's default (package-private) access modifier primarily contribute to encapsulation?
✅ Correct Answer: C) It allows controlled access to implementation details among classes within the same logical component (package).
Package-private access provides a specific scope of encapsulation, allowing members to be accessed by other classes within the same package, which can be seen as a single logical unit. This promotes data hiding within that component while keeping it hidden from external packages.
Q449easycode output
What is the output of this code?
java
class StaticBlockCounter {
private static int count = 0;
private static final Object lock = new Object();
public void increment() {
synchronized(lock) {
count++;
System.out.print(count);
}
}
}
public class MyProgram {
public static void main(String[] args) throws InterruptedException {
StaticBlockCounter sbc1 = new StaticBlockCounter();
StaticBlockCounter sbc2 = new StaticBlockCounter();
Thread t1 = new Thread(() -> sbc1.increment());
Thread t2 = new Thread(() -> sbc2.increment());
t1.start();
t2.start();
t1.join();
t2.join();
}
}
✅ Correct Answer: C) Either '12' or '21'
The `synchronized(lock)` block uses a `static final` `Object` as the lock. This means all threads, regardless of the `StaticBlockCounter` instance they use, will synchronize on the *same* lock object. Therefore, the increments to `count` are atomic and ordered, resulting in '12' or '21'.
Q450easycode output
What does this Java code print?
java
class Vehicle {
Vehicle() {
System.out.println("No-arg Vehicle");
}
Vehicle(int wheels) {
System.out.println("Vehicle with " + wheels + " wheels");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v1 = new Vehicle();
Vehicle v2 = new Vehicle(4);
}
}
✅ Correct Answer: A) No-arg Vehicle
Vehicle with 4 wheels
The code demonstrates constructor overloading. `v1` calls the no-argument constructor, and `v2` calls the constructor with an `int` parameter, executing them in the order they are called in `main`.
Q451mediumcode output
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Queue;
public class QueuePeekPoll {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.add("First");
queue.add("Second");
System.out.println(queue.peek());
System.out.println(queue.poll());
System.out.println(queue.peek());
}
}
✅ Correct Answer: A) First
First
Second
`peek()` returns 'First'. `poll()` removes 'First' and returns it. Then `peek()` again returns the new head, 'Second'.
Q452mediumcode error
What kind of error will occur when executing this Java code?
java
import java.util.TreeSet;
class CustomComparable implements Comparable<CustomComparable> {
int value;
public CustomComparable(int value) { this.value = value; }
@Override
public int compareTo(CustomComparable o) { return Integer.compare(this.value, o.value); }
}
public class Main {
public static void main(String[] args) {
TreeSet set = new TreeSet(); // Raw type
set.add(new CustomComparable(10));
set.add(5); // Adding an Integer, which is not CustomComparable
System.out.println(set.size());
}
}
✅ Correct Answer: B) ClassCastException
When using a raw TreeSet, it attempts to sort elements using their natural ordering. After adding a CustomComparable object, adding an Integer will cause a ClassCastException because the TreeSet tries to compare a CustomComparable object with an Integer, which are incompatible types for comparison.
Q453easycode error
What compile-time error will occur in the `Child` class?
java
import java.io.IOException;
class Parent {
public void processData() {
System.out.println("Processing data");
}
}
class Child extends Parent {
@Override
public void processData() throws IOException {
System.out.println("Child processing data");
}
}
✅ Correct Answer: A) Error: `processData()` in `Child` cannot override `processData()` in `Parent`; overridden method does not throw `java.io.IOException`
An overriding method cannot declare checked exceptions that are new or broader than those declared by the overridden method. Since the parent method declares no exceptions, the child cannot declare `IOException`.
Q454hard
If a `try-finally` block is entirely contained within the `do` block of a `do-while` loop, and a `break` statement is executed within the `try` block, what is the precise execution sequence?
✅ Correct Answer: B) The `finally` block executes, and then the loop immediately terminates without checking the `while` condition.
A `finally` block is guaranteed to execute whenever control leaves a `try` block, regardless of how it leaves (normal completion, `return`, `break`, `continue`, or exception). After `finally` executes, the `break` takes effect, exiting the loop and skipping the `while` condition.
Q455easy
Which abstract class does `FileReader` directly extend in Java's character stream hierarchy?
✅ Correct Answer: C) Reader
`FileReader` extends `InputStreamReader`, which in turn extends the abstract `Reader` class, making it a character-oriented input stream.
Q456easycode error
What is the compile-time error in this Java code snippet?
java
public class ArrayTest {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int arraySize = numbers.length();
System.out.println(arraySize);
}
}
✅ Correct Answer: C) Compile-time error: cannot find symbol method length()
For Java arrays, `length` is a public final field, not a method. Therefore, it should be accessed as `numbers.length` instead of `numbers.length()`.
Q457easycode error
What error will occur when compiling the following Java code?
java
import java.util.HashSet;
public class MyClass {
public static void main(String[] args) {
HashSet<Integer> nums = new HashSet<>(10.5f); // This line will cause an error
nums.add(5);
System.out.println(nums.contains(5));
}
}
✅ Correct Answer: B) incompatible types: float cannot be converted to int
The `HashSet` constructor that accepts an initial capacity expects an `int` type. Providing a `float` literal (10.5f) results in a compile-time error due to incompatible types.
Q458easy
What is the correct general syntax for a do-while loop in Java?
✅ Correct Answer: A) do { // code } while (condition);
The correct syntax for a do-while loop begins with 'do', followed by the loop body in curly braces, and then 'while' with the condition in parentheses, terminated by a semicolon.
Q459easycode error
What is the expected error when executing this Java code?
java
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
public class IteratorError {
public static void main(String[] args) {
List<String> list = Arrays.asList("X", "Y", "Z"); // Returns a fixed-size list
Iterator<String> it = list.iterator();
it.next();
it.remove(); // Attempting to modify a fixed-size list
}
}
✅ Correct Answer: A) java.lang.UnsupportedOperationException
The `Arrays.asList()` method returns a fixed-size `List` backed by the original array. Its `remove()` operation is not supported, hence calling `iterator.remove()` on such a list throws an `UnsupportedOperationException`.
Q460medium
What is the scope of a member (variable or method) declared with no explicit access modifier (default/package-private) in Java?
✅ Correct Answer: B) Accessible only within the same package.
The default (or package-private) access modifier makes members accessible only from within the same package. They are not accessible from outside the package, even by subclasses in different packages.