☕ Java MCQ Questions – Page 11

Questions 201–220 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q201 easy code output
What 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();
    }
}
Q202 easy code error
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));
    }
}
Q203 medium code output
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());
    }
}
Q204 hard code output
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);
    }
}
Q205 hard
A class declares a `final` instance variable. Which initialization strategy for this variable is *invalid* across all its possible constructors and initialization blocks?
Q206 easy
Is a semicolon required after the 'while' part of a do-while loop's condition?
Q207 hard
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?
Q208 easy code error
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
    }
}
Q209 medium
If `threadA` calls `threadB.join()` without a timeout, what state does `threadA` enter until `threadB` finishes its execution?
Q210 medium code output
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"));
    }
}
Q211 easy code error
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
    }
}
Q212 hard code error
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");
    }
}
Q213 easy code error
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]);
    }
}
Q214 hard code output
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());
    }
}
Q215 hard code error
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());
    }
}
Q216 hard
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)`)?
Q217 hard code error
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());
    }
}
Q218 easy
Which rule applies to the access modifier of an overriding method in a subclass compared to the overridden method in the superclass?
Q219 hard code error
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);
    }
}
Q220 medium
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?
← Prev 910111213 Next → Page 11 of 200 · 3994 questions