public class MyRunner implements Runnable {
@Override
public void run() {
System.out.println("Runnable running.");
}
public static void main(String[] args) {
System.out.println("Main starting...");
Thread t = new Thread(new MyRunner());
t.start();
System.out.println("Main ending.");
}
}
✅ Correct Answer: A) Main starting...
Main ending.
Runnable running.
The `start()` method initiates the new thread, but its execution is asynchronous with the main thread. The main thread continues its execution immediately after `t.start()`. Therefore, 'Main starting...' and 'Main ending.' will likely print before 'Runnable running.', though the exact timing of 'Runnable running.' is not guaranteed to be strictly after 'Main ending.' in all cases, 'a' represents the most common/expected output given the very short task.
Q3222easycode output
What is the output of this Java code snippet?
java
public class TypeCast {
public static void main(String[] args) {
long bigNum = 50000L;
int smallNum = (int) bigNum;
System.out.println(smallNum);
}
}
✅ Correct Answer: A) 50000
This is a valid narrowing cast from long to int. Since 50000 is within the range of an int, the conversion happens successfully without data loss.
Q3223easycode output
What does this Java code print to the console?
java
public class TypeCast {
public static void main(String[] args) {
double myDouble = 9.78;
int myInt = (int) myDouble;
System.out.println(myInt);
}
}
✅ Correct Answer: A) 9
This is an example of narrowing (explicit) casting. The double value 9.78 is cast to an int, which truncates the decimal part.
Q3224hard
If `byte b = -5;`, what is the value of `~b` (bitwise NOT) when treated as an `int` after promotion?
✅ Correct Answer: A) 4
In Java's two's complement system, the bitwise NOT operator `~x` is equivalent to `(-x) - 1`. Therefore, `~(-5)` evaluates to `(-(-5)) - 1 = 5 - 1 = 4`.
Q3225mediumcode error
What is the result of attempting to compile and run the following Java code?
java
class JoinerThread extends Thread {
private Thread otherThread;
public JoinerThread(Thread other) {
this.otherThread = other;
}
public void run() {
System.out.println("Waiting for other thread...");
otherThread.join(); // This line
System.out.println("Other thread finished!");
}
}
public class Main {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
});
t1.start();
new JoinerThread(t1).start();
}
}
✅ Correct Answer: B) A compilation error: 'unreported exception InterruptedException; must be caught or declared to be thrown'.
The Thread.join() method can throw an InterruptedException, which is a checked exception. It must be caught or declared to be thrown by the enclosing method, leading to a compilation error if unhandled.
Q3226easycode output
What is the output of this code?
java
import java.util.HashSet;
public class Test {
public static void main(String[] args) {
HashSet<Integer> numbers = new HashSet<>();
numbers.add(10);
numbers.add(20);
numbers.add(10); // Duplicate
numbers.add(30);
System.out.println(numbers.size());
}
}
✅ Correct Answer: A) 3
HashSets do not allow duplicate elements. When '10' is added for the second time, it's ignored, so the size of the set remains 3 (10, 20, 30).
Q3227easycode output
What is the output of this code?
java
public class StringTest {
public static void main(String[] args) {
String s = "computer";
System.out.println(s.contains("put"));
}
}
✅ Correct Answer: A) true
The `contains(CharSequence s)` method returns `true` if and only if this string contains the specified sequence of character values. "put" is indeed present in "computer".
Q3228easycode error
Will this Java code compile and run successfully, and if so, what kind of behavior is it demonstrating regarding the `while` loop?
java
public class LoopError7 {
public static void main(String[] args) {
final int LIMIT = 0;
int count = 5;
while (count < LIMIT) { // Condition is always false
System.out.println("Count is: " + count);
count++;
}
System.out.println("Program finished.");
}
}
✅ Correct Answer: B) It will compile and run, but the `while` loop body will never execute.
The `while` loop condition `count < LIMIT` (which is `5 < 0`) is `false` from the beginning. The loop body will never execute, but since `count` and `LIMIT` are variables, the compiler does not issue an unreachable code error as it's not a compile-time constant `false`.
Q3229easy
Which keyword is used to explicitly raise an exception instance?
✅ Correct Answer: C) `throw`
The `throw` keyword is specifically used to explicitly raise, or 'throw', an instance of an exception or error, initiating the exception handling mechanism.
Q3230medium
Consider `class Animal {} class Dog extends Animal {}`. If you have `Animal a = new Dog();`, and you want to call a `bark()` method specific to the `Dog` class, what type of casting is required?
✅ Correct Answer: D) Explicit narrowing cast.
To access methods specific to a subclass from a superclass reference, you must perform a downcast (narrowing conversion). This requires an explicit cast, e.g., `((Dog)a).bark();`.
Q3231easy
Which operator is used to invert the value of a boolean expression in Java?
✅ Correct Answer: C) !
The logical NOT operator (!) inverts the boolean value of its operand. If the operand is true, it returns false, and vice versa.
Q3232easy
What exception is thrown if you try to access an element at an invalid index (e.g., a negative index or an index greater than or equal to the array's length) in a Java array?
Attempting to access an array element using an index that is out of the valid range (0 to length-1) results in an `ArrayIndexOutOfBoundsException`.
Q3233medium
What is the primary characteristic of a Java array concerning its size after initialization?
✅ Correct Answer: B) Its size is fixed and cannot be changed after creation.
Once a Java array is created, its size is fixed and cannot be changed. To effectively 'resize' an array, a new array must be created and elements copied over.
Q3234hard
Consider a method `void processStrings(String... values)`. If this method is invoked as `processStrings(null);`, what is the state of the `values` parameter inside the method body?
✅ Correct Answer: C) `values` will be a single-element array containing `null` (`new String[]{null}`).
When a single `null` argument is passed to a varargs method, it is treated as an array of length one whose single element is `null`. Passing `(String[]) null` would cause `values` itself to be `null` and lead to a `NullPointerException` upon access, but `processStrings(null)` implies the former.
Q3235easycode error
What error will occur when compiling the following Java code?
java
public class Main {
public static void main(String[] args) {
int num = 10;
if (num instanceof Integer) {
System.out.println("num is an Integer.");
} else {
System.out.println("num is not an Integer.");
}
}
}
✅ Correct Answer: D) Compilation Error: `incompatible types: int cannot be converted to java.lang.Object`
The `instanceof` operator can only be used to check if an object is an instance of a particular class or interface. It cannot be used with primitive types like `int`, leading to an 'incompatible types' compilation error.
Q3236hard
When constructing a `FileReader` with a file path that points to a non-existent file, which specific type of `IOException` is most likely to be thrown?
✅ Correct Answer: B) `FileNotFoundException`
If the file specified by the pathname does not exist, or if it is a directory rather than a regular file, the FileReader constructor will throw a FileNotFoundException, which is a subclass of IOException.
Q3237easy
Is the `Runnable` interface considered a functional interface in Java 8 and later?
✅ Correct Answer: B) Yes, because it has exactly one abstract method.
A functional interface is an interface that has exactly one abstract method. `Runnable` fits this definition with its single `run()` method, making it eligible for use with lambda expressions.
Q3238hardcode error
What is the compilation error in the following Java code snippet?
java
class NonFunctionalTarget {
void doIt() { System.out.println("Doing it."); }
}
public class Main {
public static void main(String[] args) {
NonFunctionalTarget target = () -> System.out.println("Lambda action");
System.out.println("Attempting to assign lambda...");
}
}
✅ Correct Answer: A) Incompatible types: lambda expression cannot be converted to NonFunctionalTarget
A lambda expression can only be assigned to a functional interface type. `NonFunctionalTarget` is a concrete class and not a functional interface, thus the lambda expression cannot be converted or assigned to it, leading to an 'incompatible types' error.
Q3239easycode error
What kind of error will occur when running this Java code?
java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
BufferedWriter bw = null;
try {
// This condition ensures bw remains null for typical execution
if (false) {
bw = new BufferedWriter(new FileWriter("output.txt"));
}
bw.write("Attempt to write to a null buffer.");
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
System.err.println("Caught NPE: " + e.getMessage());
}
}
}
✅ Correct Answer: A) Runtime error: NullPointerException
The `BufferedWriter` object `bw` is initialized to `null`. The condition `if (false)` ensures that `bw` is never assigned a new instance. Therefore, attempting to call the `write()` method on a `null` object (`bw.write(...)`) results in a `NullPointerException` at runtime.
Q3240easy
What is the primary risk associated with narrowing type casting in Java?
✅ Correct Answer: B) Loss of data or precision
Narrowing type casting can lead to loss of data or precision because the target data type may not be able to hold the full range of values from the source data type.