import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args) {
// Assume test.txt exists and contains a single character 'H'
try (FileReader reader = new FileReader("test.txt")) {
int charCode = reader.read();
System.out.print((char) charCode);
} catch (IOException e) {
System.out.print("Error");
}
}
}
✅ Correct Answer: A) H
The `read()` method returns the ASCII value of the character. Casting it to `char` prints the character itself. The 'H' has ASCII value 72.
Q382hard
In the context of `enum` types, what is the implicit access modifier for an enum constructor, and what are its implications?
✅ Correct Answer: C) Private; ensures that enum instances can only be created by the enum definition itself.
Enum constructors are implicitly `private`. This ensures that new enum instances cannot be created arbitrarily from outside the enum declaration, maintaining the fixed set of enum constants defined within the enum.
Q383easycode error
What compilation error will the following Java code produce?
java
import java.util.function.BiFunction;
@FunctionalInterface
interface MyFunction {
String apply(int a, int b);
}
public class Main {
public static void main(String[] args) {
MyFunction func = (x, y, z) -> String.valueOf(x + y + z);
System.out.println(func.apply(1, 2));
}
}
✅ Correct Answer: B) incompatible types: incompatible parameter types in lambda expression
The `MyFunction` interface's abstract method `apply` expects two `int` parameters, but the lambda expression `(x, y, z)` provides three parameters, leading to an incompatible parameter types error.
Q384medium
What is a potential issue if `equals()` is overridden for a custom class, but `hashCode()` is NOT, and objects of this class are stored in a `HashSet`?
✅ Correct Answer: C) The `HashSet` might store duplicate objects, violating its contract.
If `equals()` is overridden but `hashCode()` is not, two 'equal' objects might produce different hash codes, leading `HashSet` to place them in different buckets and thus store them as duplicates.
Q385easy
Which method of the `java.io.File` class is used to check if the file or directory denoted by the abstract pathname actually exists?
✅ Correct Answer: C) `exists()`
The `exists()` method is a boolean method that returns true if the file or directory referred to by the `File` object exists, and false otherwise.
Q386hardcode error
What error occurs when attempting to compile the following Java code?
java
public class IfScopeError {
public static void main(String[] args) {
boolean isValid = false;
if (isValid) {
String statusMessage = "Data valid";
System.out.println(statusMessage);
} else {
// statusMessage is not in scope here
}
System.out.println("Final status: " + statusMessage); // Use outside scope
}
}
✅ Correct Answer: B) Compilation error: cannot find symbol `statusMessage`
The variable `statusMessage` is declared within the `if` block's scope. Once that block is exited, `statusMessage` is no longer accessible. Attempting to use it outside its declared scope results in a compile-time error, as the compiler cannot find the symbol.
Q387hardcode error
What compile-time error occurs when attempting to compile this Java code?
java
class Animal {
public static void makeSound() {
System.out.println("Generic animal sound");
}
}
class Dog extends Animal {
public static void specificSound() {
super.makeSound();
System.out.println("Bark!");
}
}
✅ Correct Answer: C) Error: non-static variable super cannot be referenced from a static context
The `super` keyword is used to access members of the superclass in an instance context. It refers to the superclass portion of the current object. In a `static` method, there is no object instance, and therefore `super` cannot be used, leading to a compile-time error.
Q388easycode output
What is the output of this Java code?
java
class Dog {
String name;
public Dog(String name) {
this.name = name;
}
public void bark() {
System.out.print(name + " says Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Buddy");
myDog.bark();
}
}
✅ Correct Answer: A) Buddy says Woof!
An object of type Dog is created with the name 'Buddy' via the constructor. The bark() method is then called on this object, which prints the dog's name followed by ' says Woof!'.
Q389easycode error
What is the error in this Java code?
java
public class FileWriterError1 {
public static void main(String[] args) {
FileWriter writer = new FileWriter("output.txt");
writer.write("Hello, World!");
writer.close();
}
}
✅ Correct Answer: A) The `FileWriter` constructor and `write`/`close` methods can throw `IOException`, which must be caught or declared.
The `FileWriter` constructor, `write()`, and `close()` methods declare that they can throw an `IOException`. This is a checked exception, so it must either be caught using a `try-catch` block or declared in the method signature using `throws IOException`.
Q390medium
What is the primary consequence of a `while` loop whose controlling boolean expression always evaluates to `true` and contains no `break` statement?
✅ Correct Answer: C) It will result in an infinite loop.
If the condition of a `while` loop always remains `true` and there is no `break` statement to exit it, the loop will run indefinitely, leading to an infinite loop.
Q391medium
Which method of the `Iterator` interface is used to determine if there are more elements available for iteration?
✅ Correct Answer: C) `hasNext()`
The `hasNext()` method is the standard way to check if there is a next element in the iteration sequence.
The `String::toUpperCase` method reference refers to the instance method `toUpperCase` of an arbitrary `String` object. The `apply` method's argument becomes the target object for the `toUpperCase` call. Both 'hello' and 'world' are converted to uppercase.
Q393hardcode output
What will be the most likely output of this Java code snippet? (Assume the program runs on a multi-core processor and is not terminated early.)
java
public class RaceConditionDemo {
private static int counter = 0;
public static void main(String[] args) throws InterruptedException {
Runnable incrementTask = () -> {
for (int i = 0; i < 1000; i++) {
counter++;
}
};
Thread t1 = new Thread(incrementTask);
Thread t2 = new Thread(incrementTask);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Final counter: " + counter);
}
}
✅ Correct Answer: C) Final counter: (A value less than 2000 but greater than 0)
This code demonstrates a race condition. The `counter++` operation is not atomic. When multiple threads try to read, increment, and write back the `counter` concurrently without synchronization, updates can be lost, resulting in a value less than the expected 2000.
Q394easycode output
What is the output of this Java code?
java
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
Function<String, Integer> stringLength = s -> s.length();
System.out.println(stringLength.apply("Java"));
}
}
✅ Correct Answer: A) 4
The Function `stringLength` takes a String and returns its length. The string 'Java' has a length of 4, so `apply("Java")` returns 4.
Q395hard
In a `HashMap`, where is a `null` key stored internally, and how many `null` keys are permitted?
✅ Correct Answer: A) In the first bucket (index 0) of the internal array; only one `null` key is permitted.
`HashMap` allows exactly one `null` key. Its hash is treated as 0, which means it is consistently placed in the bucket at index 0 of the internal table array.
Q396mediumcode error
What is the compilation error in the following code?
java
class Base {
public static void greet() {
System.out.println("Hello from Base!");
}
}
class Derived extends Base {
@Override
public static void greet() {
System.out.println("Hello from Derived!");
}
}
✅ Correct Answer: A) Error: Method does not override method from its superclass
Static methods cannot be overridden, they are 'hidden'. The `@Override` annotation is used to ensure a method properly overrides a superclass method. Since static methods don't participate in polymorphism, the `@Override` annotation on a static method attempting to hide a superclass static method will result in a compilation error.
Q397hard
An immutable class is generally considered thread-safe by default. However, what is a common pitfall that can inadvertently compromise the thread safety of an otherwise immutable object?
✅ Correct Answer: B) Returning a direct reference to a mutable internal collection (e.g., `ArrayList`) from a getter method.
If an immutable class returns a direct reference to an internal mutable object, external code can modify that object's state, thus compromising the immutability and thread safety of the 'immutable' class.
Q398mediumcode error
What will be the compilation error in this code?
java
class Printer {
public void print(String text) {
System.out.println("Printing: " + text);
}
}
class ConsolePrinter extends Printer {
@Override
public static void print(String text) {
System.out.println("Console Print: " + text);
}
}
✅ Correct Answer: A) Error: Method does not override method from its superclass
A non-static method cannot be overridden by a static method. If a superclass has a non-static method, a subclass cannot declare a static method with the same signature and expect it to be an override. The '@Override' annotation correctly causes a compile-time error.
Q399easycode error
What is the error in this Java code?
java
abstract class Vehicle {
abstract void start();
void stop() {
System.out.println("Vehicle stopped.");
}
}
class Car extends Vehicle { // Line 8
// No start() method implemented
}
public class Main {
public static void main(String[] args) {
// Car c = new Car(); // If uncommented, would show the error
}
}
✅ Correct Answer: B) Compile-time error: Car is not abstract and does not override abstract method start() in Vehicle
A concrete class that extends an abstract class must implement all inherited abstract methods, or it must itself be declared abstract. Since `Car` is not abstract, it must implement `start()`.
Q400hardcode error
What compilation error will occur when compiling this Java code?
java
public class LabeledIfBreak {
public static void main(String[] args) {
int counter = 0;
outerIf:
if (true) {
for (int i = 0; i < 3; i++) {
if (i == 1) {
break outerIf;
}
counter++;
}
}
System.out.println("Counter: " + counter);
}
}
✅ Correct Answer: A) `label outerIf not a loop or switch statement`
A labeled `break` can only target an enclosing loop or switch statement. `outerIf` is a label on an `if` statement, which is not a valid target for `break`.