☕ Java MCQ Questions – Page 11
Questions 201–220 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is the output of this code?
java
class A {
A() {
System.out.println("Class A constructor");
}
}
class B extends A {
B() {
// super() is implicitly called here
System.out.println("Class B constructor");
}
}
public class Main {
public static void main(String[] args) {
B obj = new B();
}
}
What is the error in this Java code?
java
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<int, String> myMap = new HashMap<>();
myMap.put(1, "One");
System.out.println(myMap.get(1));
}
}
What is the output of the interrupted status and the final state of `t`?
java
public class ThreadStateDemo7 {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Inside catch block, interrupted status: " + Thread.currentThread().isInterrupted());
}
});
t.start();
Thread.sleep(100); // Ensure t is sleeping
t.interrupt();
Thread.sleep(100); // Give t time to process interruption
System.out.println("Final state of t: " + t.getState());
}
}
What is the output of this code?
java
interface MyTransformer {
String transform(String s);
default String enhance(String s) {
return "Enhanced: " + s.toUpperCase();
}
}
public class LambdaDefaultMethods {
public static void main(String[] args) {
MyTransformer upperCaseLambda = s -> s.toUpperCase();
MyTransformer concatLambda = s -> s + "!!!";
String res1 = upperCaseLambda.enhance("hello");
String res2 = concatLambda.transform("world");
System.out.println(res1);
System.out.println(res2);
}
}
A class declares a `final` instance variable. Which initialization strategy for this variable is *invalid* across all its possible constructors and initialization blocks?
Is a semicolon required after the 'while' part of a do-while loop's condition?
Consider the following Java interface definitions:
java
interface A { void foo(); }
interface B extends A { default void bar() {} }
interface C { void baz(); }
interface D extends A, C {}
Which of the following interfaces is *not* a valid functional interface?
What is the error in this Java code?
java
class Animal {}
class Dog extends Animal {}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
String name = (String) myDog; // Line 7
}
}
If `threadA` calls `threadB.join()` without a timeout, what state does `threadA` enter until `threadB` finishes its execution?
What is the output of this code?
java
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("fruit", "apple");
map.put("color", "red");
map.put("fruit", "banana");
System.out.println(map.get("fruit"));
}
}
What is the expected error when compiling and running the following Java code?
java
import java.util.LinkedList;
public class MyClass {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.remove(); // Tries to remove the first element
}
}
What is the output of this code?
java
public class Main {
public static void main(String[] args) {
System.out.println("Before sleep");
Thread.sleep(100); // Unhandled InterruptedException
System.out.println("After sleep");
}
}
What type of error will occur when compiling or running this Java code snippet?
java
public class ArrayTest {
public static void main(String[] args) {
Object[] objects = new String[5];
objects[0] = Integer.valueOf(10);
System.out.println(objects[0]);
}
}
Assume a file named 'short.txt' exists and contains the text "XYZ". What does this code print?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
public class FileReaderPartialRead2 {
public static void main(String[] args) throws IOException {
// Assume 'short.txt' has content: "XYZ"
char[] buffer = new char[5];
StringBuilder sb = new StringBuilder();
try (FileReader reader = new FileReader(new File("short.txt"))) {
int charsRead = reader.read(buffer);
sb.append("First read: ").append(charsRead).append(" chars. Buffer: ")
.append(new String(buffer, 0, charsRead)).append("\n");
charsRead = reader.read(buffer);
sb.append("Second read: ").append(charsRead).append(" chars.");
}
System.out.println(sb.toString());
}
}
What is the result of running this Java code?
java
import java.util.Comparator;
import java.util.TreeMap;
public class TreeMapError10 {
static class CustomComparator implements Comparator<Integer> {
@Override
public int compare(Integer i1, Integer i2) {
if (i1 == null || i2 == null) {
throw new NullPointerException("Keys cannot be null");
}
if (i1 == 5 || i2 == 5) {
throw new UnsupportedOperationException("Comparison with 5 is not supported");
}
return i1.compareTo(i2);
}
}
public static void main(String[] args) {
TreeMap<Integer, String> map = new TreeMap<>(new CustomComparator());
map.put(1, "One");
map.put(10, "Ten");
map.put(5, "Five");
System.out.println(map.size());
}
}
Regarding 'dead code elimination' by the Java compiler, what is true about an `if` block whose condition is a compile-time constant `false` (e.g., `if (false)` or `if (final boolean DEBUG = false; DEBUG)`)?
What compile-time error occurs in the `Book` class?
java
class Product {
public Product getProduct() {
return new Product();
}
}
class Shelf {}
class Book extends Product {
@Override
public Shelf getProduct() { // Line 9
return new Shelf();
}
}
public class Store {
public static void main(String[] args) {
Product p = new Book();
System.out.println(p.getProduct());
}
}
Which rule applies to the access modifier of an overriding method in a subclass compared to the overridden method in the superclass?
What error occurs when executing the following Java code?
java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> words = new ArrayList<>();
words.add("Java");
words.add(null);
words.add("Programming");
String firstWord = words.get(0).toLowerCase();
String secondWord = words.get(1).toUpperCase(); // This will cause an error
System.out.println(firstWord + " " + secondWord);
}
}
In the worst-case scenario, if all keys inserted into a `HashMap` produce the same hash code, what is the time complexity for `get()` and `put()` operations?