☕ Java MCQ Questions – Page 112
Questions 2221–2240 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat does this Java code print?
java
public class MyJob implements Runnable {
@Override
public void run() {
System.out.println("Job executed.");
}
public static void main(String[] args) {
MyJob job = new MyJob();
job.run();
System.out.println("Main thread finished.");
}
}
What is printed by this Java code, regarding mutable objects passed to constructors?
java
import java.util.Date;
class UserSession {
private Date loginTime;
public UserSession(Date loginTime) {
this.loginTime = loginTime;
}
public Date getLoginTime() {
return loginTime;
}
}
public class Main {
public static void main(String[] args) {
Date userLoginDate = new Date(1678886400000L); // March 15, 2023 00:00:00 GMT
UserSession session = new UserSession(userLoginDate);
System.out.println("Initial: " + session.getLoginTime().getTime());
userLoginDate.setTime(1678972800000L); // March 16, 2023 00:00:00 GMT
System.out.println("After modification: " + session.getLoginTime().getTime());
}
}
What error will occur when this code is executed?
java
public class Main {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("ABC");
char c = sb.charAt(3);
System.out.println(c);
}
}
What is wrong with this Java code?
java
interface MyInterface {
void method1();
void method2();
}
public class Test {
public static void main(String[] args) {
MyInterface mi = () -> System.out.println("Hello");
}
}
What is the output of this code?
java
public class OperatorChallenge {
public static void main(String[] args) {
double d1 = 0.0 / 0.0;
double d2 = 1.0 / 0.0;
System.out.println(d1 == d1);
System.out.println(d1 != d1);
System.out.println(d2 + 1 == d2);
}
}
Identify the runtime error in the following Java code snippet.
java
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorError {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Item1");
Iterator<String> it = list.iterator();
it.remove(); // Calling remove() before next()
}
}
Which compile-time error will be generated by the following Java code?
java
public class DataTypeLongError {
public static void main(String[] args) {
long bigNumber = 2147483648;
System.out.println(bigNumber);
}
}
What is the result of running this Java code?
java
public class Light {
private boolean isOn;
public Light() {
this.isOn = false;
}
public void toggle() {
this.isOn = !this.isOn;
}
public boolean getStatus() {
return isOn;
}
public static void main(String[] args) {
Light livingRoomLight = new Light();
livingRoomLight.toggle();
livingRoomLight.toggle();
livingRoomLight.toggle();
System.out.println(livingRoomLight.getStatus());
}
}
What is the result of running the following Java code?
java
import java.util.Queue;
import java.util.LinkedList;
public class QueueError {
public static void main(String[] args) {
Queue<String> tasks = new LinkedList<>();
String nextTask = tasks.poll();
System.out.println(nextTask.length());
}
}
Which error will occur when compiling this Java code?
java
// No import for FileReader
public class FileReaderIssue {
public static void main(String[] args) throws java.io.IOException {
FileReader reader = new FileReader("test.txt");
int data = reader.read();
reader.close();
}
}
Which compilation error will the following Java code produce related to array initialization?
java
public class ArrayDeclarationError {
public static void main(String[] args) {
// Illegal combination of explicit size and initializer list
int[] numbers = new int[3] {1, 2, 3};
System.out.println(numbers[0]);
}
}
What is the output of this Java code snippet?
java
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>();
nums.add(1);
nums.add(2);
nums.add(3);
System.out.println(nums.size());
nums.clear();
System.out.println(nums.isEmpty());
}
}
What does this code print?
java
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Main {
public static void modifyPoint(Point p) {
p.x = 100;
p.y = 200;
p = new Point(500, 600); // This reassigns the local parameter 'p'
}
public static void main(String[] args) {
Point myPoint = new Point(10, 20);
modifyPoint(myPoint);
System.out.println("x: " + myPoint.x + ", y: " + myPoint.y);
}
}
What does this code print?
java
import java.util.HashSet;
public class Test {
public static void main(String[] args) {
HashSet<Integer> numbers = new HashSet<>();
System.out.println(numbers.isEmpty());
numbers.add(100);
System.out.println(numbers.isEmpty());
}
}
What is the Java primitive data type used to store `true` or `false` values?
If a superclass method is declared with `protected` access, which of the following access modifiers would be invalid for an overriding method in a subclass?
What is the output of this code?
java
import java.util.HashSet;
class InconsistentKey {
int id;
public InconsistentKey(int id) { this.id = id; }
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
InconsistentKey that = (InconsistentKey) o;
return id == that.id;
}
@Override public int hashCode() { return System.identityHashCode(this); } // Inconsistent hash
}
public class Main {
public static void main(String[] args) {
HashSet<InconsistentKey> set = new HashSet<>();
set.add(new InconsistentKey(5));
set.add(new InconsistentKey(5));
System.out.println(set.size());
}
}
What error will this Java code produce?
java
public class ArrayError {
public static void main(String[] args) {
String[][] words = new Object[2][2];
System.out.println(words[0][0]);
}
}
What is the output of this Java code?
java
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileWriterNonExistentDir {
public static void main(String[] args) {
String filename = "nonexistent_dir/test.txt";
try (FileWriter fw = new FileWriter(filename)) {
fw.write("Hello");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
} finally {
try { Files.deleteIfExists(Path.of(filename)); } catch (IOException ignored) {}
try { Files.deleteIfExists(Path.of("nonexistent_dir")); } catch (IOException ignored) {}
}
}
}
What type of error will occur when compiling the following Java code?
java
public class Main {
public static void main(String[] args) {
boolean b = true;
int i = (int) b;
System.out.println(i);
}
}