☕ Java MCQ Questions – Page 143
Questions 2841–2860 of 3994 total — Java interview practice
▶ Practice All Java QuestionsIn a traditional Java `for` loop (`for (initializer; condition; update)`), which combination of the three parts can be *legally omitted* without necessarily causing a compile-time error, potentially leading to an infinite loop?
Consider the following Java code. What will be printed to the console?
java
public class Employee {
private String name;
private int id;
public Employee(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name.toUpperCase(); // Returns name in uppercase
}
public static void main(String[] args) {
Employee emp = new Employee("john doe", 101);
System.out.println(emp.getName());
}
}
Consider the following declarations:
`String s1 = "hello";`
`String s2 = new String("hello");`
`String s3 = "hello";`
Which statement correctly describes the outcome of `s1 == s3` and `s1 == s2`?
What is the result of executing this Java code snippet?
java
import java.util.ArrayList;
import java.util.List;
public class GenericsCasting {
public static void main(String[] args) {
List<Integer> intList = new ArrayList<>();
intList.add(10);
List rawList = intList;
rawList.add("Hello");
List<String> stringList = (List<String>) rawList;
String s = stringList.get(0);
System.out.println(s);
}
}
What is the output of this code?
java
class ProcessingException extends RuntimeException {
private int errorCode;
public ProcessingException(String message, int code) {
super(message);
this.errorCode = code;
}
public int getErrorCode() {
return errorCode;
}
}
public class Main {
public static void processData(boolean fail) {
if (fail) {
throw new ProcessingException("Failed to process!", 101);
}
System.out.println("Data processed successfully.");
}
public static void main(String[] args) {
try {
processData(true);
} catch (ProcessingException e) {
System.out.println("Error: " + e.getMessage() + " [Code: " + e.getErrorCode() + "]");
}
}
}
What runtime error will this Java code produce?
java
public class OperatorError7 {
public static void main(String[] args) {
Integer i = null;
int j = 10;
int result = i + j; // Error line
System.out.println(result);
}
}
What error will this Java `TreeMap` code snippet generate?
java
import java.util.TreeMap;
class Key implements Comparable<Key> {
int value;
public Key(int v) { this.value = v; }
@Override
public int compareTo(Key other) {
if (this.value > other.value) return 1;
return 0; // This is problematic
}
}
public class Main {
public static void main(String[] args) {
TreeMap<Key, String> map = new TreeMap<>();
map.put(new Key(1), "A");
map.put(new Key(2), "B");
map.put(new Key(3), "C"); // This might cause an issue
}
}
What is a key advantage of using immutable objects, especially in multi-threaded environments?
What is the output of this code?
java
abstract class LivingBeing {
public LivingBeing() {
System.out.println("A living being is created.");
}
abstract void breath();
}
class Human extends LivingBeing {
public Human() {
// super() is implicitly called here
System.out.println("A human is created.");
}
@Override
void breath() {
System.out.println("Breathing air.");
}
}
public class Test {
public static void main(String[] args) {
Human h = new Human();
}
}
What is the output of this Java code snippet?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
StringWriter sw = new StringWriter();
BufferedWriter bw = new BufferedWriter(sw);
bw.write("Hello");
bw.flush();
bw.write("World");
bw.close();
System.out.print(sw.toString());
}
}
Which of the following is the correct way to declare and initialize a 3x4 two-dimensional integer array in Java?
Why are `break` and `continue` statements not directly applicable or typically used within the functional operations of the Java 8 Streams API (e.g., `filter`, `map`, `forEach`)?
What will be the compilation error in this code?
java
class Vehicle {
public final void start() {
System.out.println("Vehicle started.");
}
}
class Car extends Vehicle {
@Override
public void start() {
System.out.println("Car started.");
}
}
What is the compilation error in the following Java code snippet?
java
public class LoopError {
public static void main(String[] args) {
int i = 0;
do {
System.out.println("Iteration " + i);
i++;
} while (i < 3)
}
}
What does this code print?
java
abstract class Logger {
public static abstract void log(String message);
}
class ConsoleLogger extends Logger {
// Cannot override static method
// public static void log(String message) { System.out.println("LOG: " + message); }
}
public class StaticAbstractTest {
public static void main(String[] args) {
// Code would be here if no error
}
}
In Java, arrays are covariant. Which of the following statements correctly describes a potential runtime issue stemming from array covariance?
What is the output of this Java code?
java
public class LoopTest {
public static void main(String[] args) {
for (int i = 5; i > 2; i--) {
System.out.print(i);
}
}
}
Which of the following classes is a common concrete implementation of the `Queue` interface in Java?
What is the compilation or runtime error in the following Java code?
java
public class Main {
public static void main(String[] args) {
String text = "Hello World";
String sub = text.substring(0, 12);
System.out.println(sub);
}
}
What is the output of this code?
java
class ValidationException extends Exception {
private final int errorCode;
public ValidationException(String message, int errorCode) {
super(message + " [Code: " + errorCode + "]");
this.errorCode = errorCode;
}
public int getErrorCode() { return errorCode; }
}
public class Main {
public static void validateInput(int value) throws ValidationException {
if (value < 0 || value > 100) { throw new ValidationException("Input value out of range", 101); }
System.out.println("Input valid: " + value);
}
public static void main(String[] args) {
try { validateInput(150); }
catch (ValidationException e) {
System.out.println("Error: " + e.getMessage());
System.out.println("Details: Code " + e.getErrorCode());
}
}
}