☕ Java MCQ Questions – Page 82
Questions 1621–1640 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat does this code print?
java
import java.util.HashMap;
import java.util.Map;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
Map<String, String> mutableMap = new HashMap<>();
mutableMap.put("key1", "value1");
mutableMap.put("key2", "value2");
Map<String, String> unmodifiableMap = Collections.unmodifiableMap(mutableMap);
mutableMap.put("key3", "value3");
// unmodifiableMap.put("key4", "value4"); // Would throw UnsupportedOperationException
System.out.println(unmodifiableMap.size());
}
}
A developer attempts to move a file from one mounted filesystem (`/mnt/drive1/file.txt`) to another (`/mnt/drive2/file.txt`) using `new File("/mnt/drive1/file.txt").renameTo(new File("/mnt/drive2/file.txt"))`. What is the most likely outcome?
What is the key difference between calling `flush()` and `close()` on a `BufferedWriter`?
Which scenario involving an `if` statement with compile-time constant boolean expressions will most likely result in a compile-time error regarding unreachable code?
What is the compilation error in the `Main` class?
java
abstract class AbstractCustomException extends Exception {
public AbstractCustomException(String message) {
super(message);
}
}
public class Main {
public static void main(String[] args) {
// Attempting to instantiate an abstract class
AbstractCustomException exc = new AbstractCustomException("Test"); // This line causes the compilation error
}
}
What is the compile-time error in this Java code?
java
final abstract class CannotBeExtended {
public abstract void doSomething();
public void commonTask() {
System.out.println("Performing common task.");
}
}
What is the runtime error when executing this Java code snippet?
java
import java.util.TreeSet;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> numbers = new TreeSet<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
for (Integer num : numbers) {
if (num == 2) {
numbers.add(4);
}
}
System.out.println(numbers.size());
}
}
What does this code print?
java
import java.util.TreeMap;
import java.util.Comparator;
public class TreeMapComparatorExample {
public static void main(String[] args) {
TreeMap<String, Integer> treeMap = new TreeMap<>(Comparator.reverseOrder());
treeMap.put("apple", 10);
treeMap.put("banana", 5);
treeMap.put("cherry", 15);
System.out.println(treeMap.firstKey());
}
}
What is the primary difference in behavior when calling `thread.start()` versus calling `thread.run()` directly on a `Thread` object?
Where are String literals primarily stored to promote reusability and save memory?
What is the output of this code?
java
public class MultiArrayQ1 {
public static void main(String[] args) {
int[][] matrix = new int[3][];
matrix[1] = new int[2];
System.out.println(matrix[0]);
System.out.println(matrix[1][0]);
}
}
What will happen when this Java code is compiled and executed?
java
class MyClass {
public void checkValue(int value) {
if (value < 0) {
throws new IllegalArgumentException("Negative value");
}
}
}
What is the error in this Java code?
java
class Parent {
public void show() {
System.out.println("Parent show");
}
}
class Child extends Parent {
private void show() { // Line 7
System.out.println("Child show");
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
p.show();
}
}
What is the result of attempting to compile this code?
java
public class Main {
public static void main(String[] args) {
int counter = 0;
while(true) {
if (counter > 5) {
break;
}
System.out.println(counter);
// Missing increment, but the question is about compilation
// The actual problem is a logical error leading to infinite loop, BUT
// this is testing 'break' rules, not general logic.
}
// This code won't compile based on previous examples.
// Let's make it a compile error regarding 'break' usage itself.
// Revised snippet for compile error:
int val = 10;
switch (val) {
case 10:
System.out.println("Ten");
case 20:
break anotherCase;
default:
System.out.println("Other");
}
}
}
What is the compilation error in this code?
java
class Dog {
String breed = "Labrador";
}
public class Main {
public static void main(String[] args) {
Dog fluffy;
fluffy = Dog();
System.out.println(fluffy.breed);
}
}
What is the content of 'numeric_chars.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;
public class WriteIntTest {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("numeric_chars.txt")) {
writer.write(65);
writer.write(97);
writer.write(10);
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
What is the output of this code, assuming System.getProperty("line.separator") returns "\n"?
java
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;
public class Test {
public static void main(String[] args) throws IOException {
StringWriter sw = new StringWriter();
BufferedWriter bw = new BufferedWriter(sw);
String unicodeChar = "\u20AC"; // Euro sign
bw.write("Price: ");
bw.write(unicodeChar);
bw.newLine();
bw.write("Total: 100");
bw.close();
System.out.println(sw.toString().replace("\n", "\\n").replace("\r", "\\r"));
}
}
What is the compilation error in the following Java code?
java
class BaseCalculator {
public void calculate() {
System.out.println("Calculating in BaseCalculator.");
}
}
class AdvancedCalculator extends BaseCalculator {
@Override
private void calculate() { // Attempting to reduce visibility
System.out.println("Calculating in AdvancedCalculator.");
}
}
What is the output of this code?
java
String text = "Programming in Java";
String sub1 = text.substring(0, 11);
String sub2 = text.substring(15);
System.out.println(sub1 + " " + sub2);
What is the output of the following Java code?
java
class Lamp {
boolean isOn = false;
public void turnOn() {
isOn = true;
}
public void turnOff() {
isOn = false;
}
}
public class Main {
public static void main(String[] args) {
Lamp livingRoomLamp = new Lamp();
livingRoomLamp.turnOn();
System.out.print("Lamp is on: " + livingRoomLamp.isOn);
}
}