☕ Java MCQ Questions – Page 37
Questions 721–740 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat type of error will occur when compiling the following Java code?
java
public class Main {
public static void main(String[] args) {
short s = 5;
s = s + 10;
System.out.println(s);
}
}
What is the error in the following Java code?
java
class MessageService {
public void sendMessage() {
System.out.println("Sending default message.");
}
}
class EmailService extends MessageService {
@Override
public static void sendMessage() { // Attempting to override instance method as static
System.out.println("Sending email message.");
}
}
What is the output of this code?
java
public class DataTypeChallenge {
public static void main(String[] args) {
double d = 0.0;
int i = 0;
try {
System.out.println(1.0 / d);
System.out.println(1 / i);
} catch (ArithmeticException e) {
System.out.println("Integer Division by Zero");
}
}
}
What is the error in this code?
java
import java.util.TreeMap;
public class Test {
public static void main(String[] args) {
TreeMap<String, String> map = new TreeMap<>();
map.put("key1", "value1");
map.put(null, "value2");
System.out.println(map.size());
}
}
What is the output of this code?
java
class SharedCounter {
public int count = 0;
}
class Incrementer implements Runnable {
private SharedCounter shared;
public Incrementer(SharedCounter shared) {
this.shared = shared;
}
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
shared.count++;
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
SharedCounter counter = new SharedCounter();
Thread t1 = new Thread(new Incrementer(counter));
Thread t2 = new Thread(new Incrementer(counter));
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Final count: " + counter.count);
}
}
Since which Java version can the `switch` statement accept `String` as an expression?
What is the output of this code?
java
interface Greeter {
void greet(String name);
}
public class LambdaTest {
public static void main(String[] args) {
Greeter simpleGreeter = (name) -> {
String message = "Hello, " + name + "!";
System.out.println(message);
};
simpleGreeter.greet("World");
}
}
Consider a `try` block that can throw `IOException` and `NullPointerException`. If the `catch` blocks are ordered `catch (Exception e)` followed by `catch (IOException e)`, what will happen if an `IOException` occurs?
What is the primary role of the `serialVersionUID` in a serializable class?
What type of expression must be provided inside the parentheses of an `if` statement in Java?
Why can't `static` methods be overridden in Java?
What exception will be thrown when executing this Java code snippet?
java
import java.util.ArrayDeque;
import java.util.Deque;
public class QueueError {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.push("First");
deque.push(null); // ArrayDeque does not permit nulls
}
}
What is the output of this code?
java
import java.io.*;
class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
private transient String computedField;
public Employee(String name, int age) {
this.name = name;
this.age = age;
this.computedField = name + "_" + age;
}
public String toString() {
return "Name: " + name + ", Age: " + age + ", Computed: " + computedField;
}
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
ois.defaultReadObject();
this.computedField = name + "_reconstructed_" + age;
}
}
public class SerializationTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Employee emp = new Employee("Alice", 30);
System.out.println("Original: " + emp);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(emp);
oos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Employee deserializedEmp = (Employee) ois.readObject();
ois.close();
System.out.println("Deserialized: " + deserializedEmp);
}
}
What happens if a do-while loop's condition never evaluates to false?
What is the type and value of `result` after the following Java code executes?
java
int x = 5;
short y = 10;
Object result = +y;
What is the compilation error in the `LocalCustomException` class constructor?
java
public class Main {
public void someMethod() {
String data = "initial"; // This variable is not final or effectively final
class LocalCustomException extends Exception {
public LocalCustomException(String msg) {
super(msg + data); // Accessing non-final 'data' - This line causes the compilation error
}
}
try { new LocalCustomException("Error:"); } catch (LocalCustomException e) {}
}
public static void main(String[] args) {
new Main().someMethod();
}
}
What is the output of this code?
java
import java.io.*;
class ExternalizableClass implements Externalizable {
private static final long serialVersionUID = 1L;
private String field1;
private int field2;
private transient String field3 = "Should not appear";
public ExternalizableClass() {
System.out.println("ExternalizableClass constructor called.");
}
public ExternalizableClass(String f1, int f2, String f3) {
this.field1 = f1;
this.field2 = f2;
this.field3 = f3;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(field1);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("readExternal called.");
this.field1 = (String) in.readObject();
}
public String toString() {
return "F1: " + field1 + ", F2: " + field2 + ", F3: " + field3;
}
}
public class SerializationTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ExternalizableClass obj = new ExternalizableClass("Alpha", 123, "Beta");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
ExternalizableClass deserializedObj = (ExternalizableClass) ois.readObject();
ois.close();
System.out.println(deserializedObj);
}
}
What compile-time error will occur when compiling this Java code?
java
public class MyClass {
public static void main(String[] args) {
char grade = 'A';
switch (grade) {
case 'A':
System.out.println("Excellent");
break;
case 70000: // 70000 is beyond char's max value (65535)
System.out.println("Invalid Grade");
break;
default:
System.out.println("Pass");
}
}
}
What is the output of this Java code?
java
public class Main {
public static void main(String[] args) {
int age = 25;
System.out.println(age);
}
}
What compilation error will occur in the `DataProcessor` class?
java
public class InvalidInputException extends Exception {
public InvalidInputException(String message) {
super(message);
}
}
public class DataProcessor {
private int data;
public DataProcessor(int value) {
if (value < 0) {
throw new InvalidInputException("Input must be non-negative.");
}
this.data = value;
}
public int getData() {
return data;
}
}
public class Main {
public static void main(String[] args) {
DataProcessor processor = new DataProcessor(-10);
System.out.println(processor.getData());
}
}