☕ Java MCQ Questions – Page 101

Questions 2001–2020 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2001 easy code error
What kind of error will occur when this Java code is executed?
java
public class Test {
    public static void main(String[] args) {
        char[] chars = {'J', 'a', 'v', 'a'};
        StringBuilder sb = new StringBuilder(chars);
        System.out.println(sb);
    }
}
Q2002 hard code error
What is the error in the following Java code?
java
class Super {
    final void display() {
        System.out.println("Super display");
    }
}

class Sub extends Super {
    void display() { // Attempting to override final method
        System.out.println("Sub display");
    }
}
Q2003 easy
When you perform an operation like `str.concat("suffix")` on a String `str`, what is the outcome?
Q2004 easy code error
What is wrong with this Java code?
java
import java.util.function.Supplier;

public class Test {
    public static void main(String[] args) {
        Supplier<String> supplier = () -> 123;
    }
}
Q2005 easy code output
What is the output of this Java code snippet?
java
public class ArrayTest {
    public static void main(String[] args) {
        String[] fruits = {"Apple", "Banana"};
        for (String fruit : fruits) {
            System.out.print(fruit + ",");
        }
    }
}
Q2006 medium
Which `StringBuffer` method is used to append the string representation of an argument to the sequence?
Q2007 medium code output
What is the output of this code?
java
import java.util.PriorityQueue;
import java.util.Queue;

public class PriorityQueueDuplicates {
    public static void main(String[] args) {
        Queue<String> pq = new PriorityQueue<>();
        pq.offer("cat");
        pq.offer("dog");
        pq.offer("cat");
        System.out.print(pq.poll());
        System.out.print(pq.poll());
        System.out.print(pq.size());
    }
}
Q2008 hard
What is the most significant difference when choosing between `String.replace(CharSequence target, CharSequence replacement)` and `String.replaceAll(String regex, String replacement)` for replacing all occurrences of a literal substring?
Q2009 medium code output
What is the output of this Java code?
java
class Car {
    String color;
    Car(String c) {
        this.color = c;
    }
}
public class Main {
    public static void main(String[] args) {
        Car car1 = new Car("Red");
        Car car2 = new Car("Blue");
        System.out.println(car1.color + " " + car2.color);
    }
}
Q2010 hard code output
What is the output of this Java code?
java
String s1 = "apple";
String s2 = "Apple";
String s3 = "banana";

System.out.println(s1.compareTo(s2));
System.out.println(s1.compareToIgnoreCase(s2));
System.out.println(s3.compareTo(s1));
Q2011 medium code output
What is the output of this code?
java
import java.util.HashMap;

class MyKey {
    int id;
    public MyKey(int id) { this.id = id; }
}

public class Test {
    public static void main(String[] args) {
        HashMap<MyKey, String> map = new HashMap<>();
        MyKey k1 = new MyKey(1);
        MyKey k2 = new MyKey(1); // Different object, same 'id'
        map.put(k1, "First Value");
        System.out.println(map.get(k2));
    }
}
Q2012 hard code error
What compile-time error occurs when attempting to compile this Java code?
java
final class Immutable {
    public String name;
    public Immutable(String name) {
        this.name = name;
    }
}

class SubImmutable extends Immutable {
    public SubImmutable(String name) {
        super(name);
    }
}
Q2013 easy
Which method is used to add an element to the tail of a Queue and throws an exception if the operation fails (e.g., if the queue is full)?
Q2014 medium code error
What error occurs when compiling this Java code?
java
public class LoopTest {
    public static void main(String[] args) {
        int data;
        do {
            System.out.println("Processing...");
        } while (data < 10);
    }
}
Q2015 hard code error
What runtime error will this Java code snippet throw?
java
public class Test {
    public static void main(String[] args) {
        Object[][] matrix = new String[2][2];
        matrix[0][0] = "Java";
        matrix[1][0] = new Integer(100);
        System.out.println(matrix[1][0]);
    }
}
Q2016 hard code error
What is the compilation error in the following Java code snippet?
java
import java.util.function.Function;

class Processor {
    public String process(Integer i) {
        return "Processed: " + i;
    }
}

public class Main {
    public static void main(String[] args) {
        Function<Integer, String> func = Processor::process;
        System.out.println(func.apply(10));
    }
}
Q2017 medium code output
What is the output of this code?
java
public class StringBufferTest {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append("Hello");
        sb.ensureCapacity(30);
        sb.append("World");
        System.out.print(sb.capacity() + " " + sb.length());
    }
}
Q2018 hard code output
What is the output of this code?
java
import java.util.HashSet;
import java.util.Objects;

class AlwaysTrueEqualsItem {
    int id;
    public AlwaysTrueEqualsItem(int id) { this.id = id; }
    @Override public boolean equals(Object o) { return true; } // Always returns true
    @Override public int hashCode() { return Objects.hash(id); }
}

public class Main {
    public static void main(String[] args) {
        HashSet<AlwaysTrueEqualsItem> set = new HashSet<>();
        set.add(new AlwaysTrueEqualsItem(1));
        set.add(new AlwaysTrueEqualsItem(2));
        System.out.println(set.size());
    }
}
Q2019 medium code error
What is the output of this Java code?
java
public class ExceptionFlow4 {
    public static void main(String[] args) {
        System.out.println(testMethod());
    }

    public static String testMethod() {
        try {
            System.out.println("Try block");
            throw new RuntimeException("Test exception");
        } catch (RuntimeException e) {
            System.out.println("Catch block");
            return "Returned from catch";
        } finally {
            System.out.println("Finally block");
        }
    }
}
Q2020 medium code error
Consider the following Java code. What will be the compilation error?
java
public class UninitializedVariable {
    public static void main(String[] args) {
        int x;
        int y = x + 10;
        System.out.println(y);
    }
}
← Prev 99100101102103 Next → Page 101 of 200 · 3994 questions