☕ Java MCQ Questions – Page 156

Questions 3101–3120 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3101 medium code error
Consider the following code snippet with a custom class `BuggyPerson`. What error will occur when running this code?
java
import java.util.HashSet;
import java.util.Set;

class BuggyPerson {
    String name;

    public BuggyPerson(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BuggyPerson that = (BuggyPerson) o;
        return name.equals(that.name); // Potentially ERROR here if name is null
    }

    @Override
    public int hashCode() {
        return name.hashCode(); // Potentially ERROR here if name is null
    }
}

public class HashSetError8 {
    public static void main(String[] args) {
        Set<BuggyPerson> people = new HashSet<>();
        people.add(new BuggyPerson("Alice"));
        BuggyPerson pNull = new BuggyPerson(null); // Create person with null name
        people.add(pNull); // ERROR line
    }
}
Q3102 easy code error
What compile-time error will this code produce?
java
public class Main {
    public static void main(String[] args) {
        Runnable myRunnable = new Runnable(); // Attempt to instantiate an interface
        Thread thread = new Thread(myRunnable);
        thread.start();
    }
}
Q3103 medium code output
What does this code print?
java
public class StringBufferTest {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Java");
        sb.insert(4, " is fun");
        sb.insert(0, "Learning ");
        System.out.print(sb);
    }
}
Q3104 easy code output
What does this code print?
java
public class DataTypeTest {
    public static void main(String[] args) {
        double price = 99.99;
        System.out.println(price);
    }
}
Q3105 medium code output
What is the output of this code?
java
class Book {
    String title;
    String author;

    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book [Title: " + title + ", Author: " + author + "]";
    }
}

public class Main {
    public static void main(String[] args) {
        Book book = new Book("The Lord of the Rings", "J.R.R. Tolkien");
        System.out.println(book);
    }
}
Q3106 medium
Which of the following is a defining characteristic of an immutable object in Java?
Q3107 easy code output
What does this Java code print to the console?
java
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(50, "Fifty");
        treeMap.put(10, "Ten");
        treeMap.put(70, "Seventy");
        System.out.println(treeMap.lastKey());
    }
}
Q3108 hard
Which statement accurately describes the use of covariant return types in Java method overriding?
Q3109 medium code output
What does this code print?
java
import jakarta.validation.*;
import jakarta.validation.constraints.*;
import java.util.Set;

public class Test {
    static class PersonInfo {
        @NotNull public String name; @Min(18) public Integer age;
        public PersonInfo(String n, Integer a) { this.name = n; this.age = a; }
    }
    static class Application {
        @Valid public PersonInfo info;
        public Application(PersonInfo i) { this.info = i; }
    }
    public static void main(String[] args) {
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Application app = new Application(new PersonInfo(null, 15));
        Set<ConstraintViolation<Application>> violations = validator.validate(app);
        System.out.println("Violations: " + violations.size());
    }
}
Q3110 medium code error
What is the compilation error in the following Java code (Java 10+)?
java
public class VarKeyword {
    public static void main(String[] args) {
        var count;
        count = 10;
        System.out.println(count);
    }
}
Q3111 easy code error
What is the compilation or runtime error in the following Java code?
java
public class Main {
    public static void main(String[] args) {
        String data = "banana";
        String modifiedData = data.replace('a', "x");
        System.out.println(modifiedData);
    }
}
Q3112 hard
What is the specific behavior regarding a traditional `for` loop's iteration variable (e.g., `int i`) being 'effectively final' when referenced inside a lambda expression or anonymous inner class in Java 8 and later?
Q3113 hard
Which statement correctly describes the behavior of `System.arraycopy` when used to copy a multi-dimensional array?
Q3114 easy code error
What exception will be thrown when executing the following Java code?
java
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class MyClass {
    public static void main(String[] args) {
        Set<String> fruits = new HashSet<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        for (String fruit : fruits) {
            if (fruit.equals("Banana")) {
                fruits.remove("Banana"); // This line causes a runtime error
            }
        }
        System.out.println(fruits.size());
    }
}
Q3115 hard
A developer is building a cross-platform application that needs to read text files reliably across different operating systems. They choose to use `FileReader` for its simplicity. Which of the following statements most accurately describes a significant pitfall of using `FileReader` in this scenario?
Q3116 hard code error
What error occurs when attempting to compile the following Java code?
java
public class ReturnUnreachableCode {
    public static void process(int value) {
        if (value > 0) {
            if (value < 10) {
                System.out.println("Value is small positive.");
                return; // Exits the method
                int unreachableVar = 100; // This line is unreachable
                System.out.println(unreachableVar);
            }
        }
        System.out.println("Value is not small positive or not positive.");
    }

    public static void main(String[] args) {
        process(5);
    }
}
Q3117 easy
A thread in the RUNNABLE state might transition to the BLOCKED state under which circumstance?
Q3118 easy code output
What is the output of this code?
java
class Printer {
    boolean printed = false;
    public synchronized void printA() {
        System.out.print("A");
        printed = true;
        notify();
    }
    public synchronized void printB() throws InterruptedException {
        while(!printed) {
            wait();
        }
        System.out.print("B");
    }
}

public class MyProgram {
    public static void main(String[] args) throws InterruptedException {
        Printer p = new Printer();
        Thread t1 = new Thread(() -> { try { p.printB(); } catch(InterruptedException e) {} });
        Thread t2 = new Thread(() -> { try { p.printA(); } catch(InterruptedException e) {} });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}
Q3119 medium code error
What compilation error will this Java code produce?
java
public class UnaryOperatorError {
    public static void main(String[] args) {
        String text = "Hello";
        int len = -text;
        System.out.println(len);
    }
}
Q3120 medium
What is the primary characteristic of how elements are stored in a `TreeSet`?
← Prev 154155156157158 Next → Page 156 of 200 · 3994 questions