☕ Java MCQ Questions – Page 103
Questions 2041–2060 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat kind of error will occur when compiling the following Java code?
java
import java.util.Comparator;
import java.util.PriorityQueue;
class MyData {}
public class QError10 {
public static void main(String[] args) {
Comparator<String> stringComparator = (s1, s2) -> s1.compareTo(s2);
PriorityQueue<MyData> pq = new PriorityQueue<>(stringComparator);
pq.add(new MyData());
System.out.println(pq.poll());
}
}
What kind of error will occur when compiling this Java code?
java
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Main {
public static void main(String[] args) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("output.txt"));
bw.write("Data to write.");
} catch (Exception e) {
e.printStackTrace();
}
// close() method can throw IOException and is outside a try-catch block
bw.close();
}
}
What will be the compilation error in the following Java code snippet?
java
public class ImmutableStringExample {
public static void main(String[] args) {
final String message = "Hello";
message = "World"; // Attempt to reassign final variable
System.out.println(message);
}
}
Identify the compile-time error in the given Java code.
java
public class DataTypeBooleanError {
public static void main(String[] args) {
boolean isValid = 1;
System.out.println(isValid);
}
}
What does this Java code print?
java
import java.util.function.BiFunction;
class MathUtils {
public static int multiply(int a, int b) {
return a * b;
}
}
public class MethodRefTest {
public static void main(String[] args) {
BiFunction<Integer, Integer, Integer> multiplier = MathUtils::multiply;
int result = multiplier.apply(5, 3);
System.out.println("Result: " + result);
}
}
Which exception is typically thrown by the `FileReader` constructor if the specified file does not exist or cannot be opened for reading?
What is the primary purpose of the `try` block in Java exception handling?
What is the output of this Java program?
java
public class TypeOrder {
void process(int num, String text) {
System.out.println("Int-String: " + num + " " + text);
}
void process(String text, int num) {
System.out.println("String-Int: " + text + " " + num);
}
public static void main(String[] args) {
TypeOrder to = new TypeOrder();
to.process(10, "Hello");
to.process("World", 20);
}
}
What is the typical average time complexity for adding an element to a `HashSet` (assuming a good hash function)?
What is the output of this code?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
StringWriter stringWriter = new StringWriter();
try (BufferedWriter writer = new BufferedWriter(stringWriter)) {
writer.append("First");
writer.append(' ');
writer.append("Second");
writer.flush();
System.out.print(stringWriter.toString());
} catch (IOException e) {
System.out.print("Error");
}
}
}
What is the output of this code?
java
public class MyClass {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Start");
sb.append("End").insert(5, "Middle");
System.out.print(sb);
}
}
What is the outcome of running this code?
java
public class LoopTest {
public static void main(String[] args) {
int[] numbers = {10, 20, 30};
int i = 0;
while (i <= numbers.length) {
System.out.println(numbers[i]);
i++;
}
}
}
What is the output of this code?
java
class Person {
protected String name = "Default";
}
class Student extends Person {
void displayName() {
System.out.println("Name: " + name);
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.displayName();
}
}
What is the error in the following Java code?
java
abstract class Gadget {
public abstract void activate();
public void status() { System.out.println("Gadget OK"); }
}
class Smartwatch extends Gadget { // Missing implementation of activate()
@Override
public void status() { System.out.println("Smartwatch Ready"); }
}
What is the output of this Java program?
java
public class WhileLoopDemo {
public static void main(String[] args) {
int k = 3;
while (k > 0) {
System.out.print(k);
k--;
}
}
}
What is the output of this Java code?
java
import java.util.TreeSet;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Set<String> set = new TreeSet<>();
set.add("hello");
set.add("world");
try {
set.add(null);
System.out.println(set);
} catch (NullPointerException e) {
System.out.println("NullPointerException caught!");
}
}
}
What error occurs when attempting to compile the following Java code?
java
public class ConditionalCompileError {
public static void main(String[] args) {
final boolean FLAG = true;
if (FLAG) {
System.out.println("Condition is true.");
} else { // This 'else' branch is unreachable.
System.out.println("Condition is false.");
}
}
}
Which error will occur when compiling this Java code?
java
import java.io.FileReader;
import java.io.FileNotFoundException;
public class FileReaderIssue {
public static void main(String[] args) throws FileNotFoundException {
FileReader reader = new FileReader("existing.txt"); // Assume existing.txt exists
// ... some operations ...
reader.close(); // Potential IOException, not declared or caught
}
}
An `ImmutableObject` class is `Serializable` and correctly enforces immutability through private final fields and no setters. When deserializing an `ImmutableObject`, which approach is most effective for ensuring that the deserialized instance remains truly immutable and adheres to its creation invariants?
What is the error in the following Java code?
java
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<String> names = new TreeSet<>();
names.add("Alice");
names.add("Bob");
names.add(null);
}
}