☕ Java MCQ Questions – Page 183
Questions 3641–3660 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is the effect of declaring a class as `final` in Java?
What error will this Java code produce when compiled and run?
java
import java.util.ArrayList;
public class ArrayCovariance {
public static void main(String[] args) {
Object[] objects = new String[5];
objects[0] = "Hello";
// Attempt to store an Integer in an array originally typed as String[]
objects[1] = new Integer(123);
System.out.println(objects[0]);
}
}
What is the output of the following Java code?
java
public class MyClass {
public static void main(String[] args) {
String fruit = "Apple";
switch (fruit) {
case "Banana": System.out.print("Yellow"); break;
case "Apple": System.out.print("Red"); break;
case "Orange": System.out.print("Orange"); break;
default: System.out.print("Unknown color");
}
}
}
What is the result of executing this Java code snippet?
java
public class MultiArrayCasting {
public static void main(String[] args) {
String[][] stringMatrix = {{"A", "B"}, {"C", "D"}};
Object[][] objMatrix = stringMatrix;
Integer[][] intMatrix = (Integer[][]) objMatrix;
System.out.println(intMatrix[0][0]);
}
}
What error will be thrown when `main` attempts to execute the `synchronized` block?
java
public class SyncOnNull {
public static void main(String[] args) {
Object lockObject = null;
synchronized (lockObject) {
System.out.println("This line will not be reached.");
}
}
}
Which annotation is commonly used in Java to indicate that a method is intended to override a method in a superclass or implement an interface method?
What is the compile-time error in the following Java code?
java
class MyClass {
int value;
public MyClass() {
System.out.println("Default constructor");
this(0); // This should be the first statement
}
public MyClass(int value) {
this.value = value;
System.out.println("Parameterized constructor: " + value);
}
}
public class ConstructorError2 {
public static void main(String[] args) {
new MyClass();
}
}
What error does this Java code produce when executed?
java
public class Main {
public static void main(String[] args) {
String fruit = "apple";
System.out.println(fruit.charAt(-1));
}
}
What is an abstract method in Java?
What is the output of this code?
java
public class LabeledBreakWhile {
public static void main(String[] args) {
int i = 0;
int j = 0;
String result = "";
outerLoop: while (i < 3) {
result += "O" + i;
j = 0;
while (j < 3) {
result += "I" + j;
if (i == 1 && j == 1) {
break outerLoop;
}
j++;
}
i++;
}
System.out.println(result);
}
}
What is the compilation error in the provided Java code?
java
@FunctionalInterface
interface RunnableTask {
void run();
}
public class Main {
public static void main(String[] args) {
int counter = 0;
RunnableTask task = () -> {
counter++;
System.out.println("Counter: " + counter);
};
task.run();
}
}
Examine the following Java code. What will be the outcome when executed?
java
import java.io.*;
public class FileWriterError3 {
public static void main(String[] args) {
File file = new File("closed_test.txt");
try {
FileWriter fw = new FileWriter(file);
fw.write("First line.");
fw.close(); // Stream is closed
fw.write("Second line?"); // Attempt to write to a closed stream
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
} finally {
file.delete(); // Clean up
}
}
}
Which method checks if a string has a length of 0?
What is a primary benefit of using polymorphism in Java?
What does this Java code print?
java
class MyThread extends Thread {
public void run() {
System.out.print("Thread running: ");
System.out.println(getName());
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
MyThread t = new MyThread();
t.setName("MyWorker");
t.start();
t.join();
System.out.print("Main done.");
}
}
What is the output of this Java code snippet?
java
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<String, String> treeMap = new TreeMap<>();
treeMap.put("key1", "value1");
treeMap.put("key2", null);
System.out.println(treeMap.get("key2"));
}
}
What is printed to the console when this code executes, assuming 'nonexistent_dir/file.txt' is an invalid path that cannot be created on the system?
java
import java.io.FileWriter;
import java.io.IOException;
public class InvalidPathTest {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("nonexistent_dir/file.txt")) {
writer.write("This should not be written.");
} catch (IOException e) {
System.out.println("Caught an IOException: " + e.getMessage());
}
}
}
What error will this Java code produce?
java
public class ArrayError {
public static void main(String[] args) {
int[2][] matrix;
matrix = new int[2][3];
System.out.println(matrix[0][0]);
}
}
If a method declares a local variable with the same name as an instance variable of the class, how can the instance variable be accessed from within that method?
In a method designed to validate user input, if the input is found to be invalid, how should a custom `InvalidInputException` (a checked exception) typically be used to signal this error?