☕ Java MCQ Questions – Page 38

Questions 741–760 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q741 easy code error
Which error will occur when compiling this Java code?
java
import java.io.FileReader;
import java.io.FileNotFoundException;

public class FileReaderIssue {
    public static void main(String[] args) throws FileNotFoundException {
        FileReader reader = new FileReader("existing.txt"); // Assume existing.txt exists
        int data = reader.read(); // Potential IOException
        System.out.println(data);
    }
}
Q742 hard
FileWriter is a character stream designed for convenience. Which statement accurately describes its default character encoding behavior?
Q743 hard
Consider an `ArrayList` named `originalList`. A `List` `subList` is created using `originalList.subList(0, 5)`. If `originalList.add(10)` is called after `subList` is created, what happens when you then try to perform an operation on `subList`?
Q744 hard code output
What does this code print?
java
public class ArrayFieldInitialization {
    static int[] staticArr;
    int[] instanceArr;

    public static void main(String[] args) {
        ArrayFieldInitialization obj = new ArrayFieldInitialization();
        
        System.out.println(staticArr == null);
        System.out.println(obj.instanceArr == null);
        
        int[] localArr = new int[0];
        System.out.println(localArr.length);
        
        staticArr = new int[]{1, 2, 3};
        System.out.println(staticArr[0]);
    }
}
Q745 medium
What is the primary difference in behavior when creating an `Optional` using `Optional.of(value)` versus `Optional.ofNullable(value)`?
Q746 medium
What is the primary benefit of using `BufferedReader` over a plain `FileReader` for reading text files?
Q747 hard code error
What is the runtime error when executing this Java code snippet?
java
import java.util.TreeSet;
import java.util.Collections;
import java.util.SortedSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<String> originalSet = new TreeSet<>();
        originalSet.add("Alpha");
        originalSet.add("Beta");

        SortedSet<String> unmodifiableSet = Collections.unmodifiableSortedSet(originalSet);
        unmodifiableSet.add("Gamma");
        System.out.println(unmodifiableSet.size());
    }
}
Q748 hard code output
What is the output of this code?
java
class Base {
    void foo() {
        System.out.println("Base foo");
    }
    Base() {
        foo(); // Call to an overridden method in constructor
    }
}

class Derived extends Base {
    String message = "Derived foo";
    @Override
    void foo() {
        System.out.println(message);
    }
}

public class Main {
    public static void main(String[] args) {
        new Derived();
    }
}
Q749 easy code output
What does this code print?
java
public class StringTest {
    public static void main(String[] args) {
        String s = "Sunshine";
        System.out.println(s.startsWith("Sun"));
    }
}
Q750 hard code output
What is the output of this code?
java
class Animal { public Animal getSelf() { return this; } }
class Dog extends Animal { @Override public Dog getSelf() { return this; } }
public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        Animal returnedAnimal = a.getSelf();
        System.out.println(returnedAnimal instanceof Dog);
        System.out.println(returnedAnimal instanceof Animal);
    }
}
Q751 easy code output
What does this Java code print?
java
public class StringReassignmentTest {
    public static void main(String[] args) {
        String message = "Start";
        message = message + " End";
        System.out.println(message);
    }
}
Q752 medium code error
What is the compile-time error in this Java code?
java
import java.util.function.Consumer;

public class LambdaError {
    public static void main(String[] args) {
        int counter = 0;
        Consumer<String> printer = s -> {
            System.out.println(s + counter);
            counter++; // Attempt to modify counter
        };
        printer.accept("Count: ");
    }
}
Q753 easy code output
What does this Java code print?
java
class Person {
    String name;
    Person(String n) {
        name = n;
        System.out.println("Hello, " + name + "!");
    }
}
public class Main {
    public static void main(String[] args) {
        Person p = new Person("Alice");
    }
}
Q754 medium
To create a jagged (or irregular) 2D array in Java where each row can have a different number of columns, which initialization step is essential after declaring the outer array?
Q755 hard code output
What does this code print?
java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

class Person {
    String name;
    int age;
    double height;

    public Person(String name, int age, double height) {
        this.name = name;
        this.age = age;
        this.height = height;
    }

    public String getName() { return name; }
    public int getAge() { return age; }
    public double getHeight() { return height; }

    @Override
    public String toString() {
        return name + " (" + age + ", " + height + ")";
    }
}

public class ComparatorChaining {
    public static void main(String[] args) {
        List<Person> people = new ArrayList<>(List.of(
            new Person("Alice", 30, 1.65),
            new Person("Bob", 25, 1.80),
            new Person("Charlie", 30, 1.70),
            new Person("David", 25, 1.75),
            new Person("Alice", 28, 1.60)
        ));

        Comparator<Person> byName = Comparator.comparing(Person::getName);
        Comparator<Person> byAge = Comparator.comparingInt(Person::getAge);
        Comparator<Person> byHeight = Comparator.comparingDouble(Person::getHeight);

        Comparator<Person> complexComparator = byName
            .thenComparing(byAge.reversed())
            .thenComparing(byHeight);

        List<Person> sortedPeople = people.stream()
            .sorted(complexComparator)
            .collect(Collectors.toList());

        sortedPeople.forEach(System.out::println);
    }
}
Q756 medium
Which statement best describes the purpose of the Java Virtual Machine's (JVM) Garbage Collector?
Q757 hard
When comparing the iterators provided by `ArrayList` and `CopyOnWriteArrayList`, which statement accurately describes a key difference in their fail-safe/fail-fast behavior and performance characteristics during concurrent modification?
Q758 hard
Given a class with a static initializer block, an instance initializer block, and a constructor, what is the correct order of execution when a *new* object of this class is created for the *first time* in the JVM?
Q759 medium
Which scenario is best suited for using an interface over an abstract class?
Q760 easy code error
What compile-time error will occur in the `MyClass` definition?
java
class MyClass {
    public void MyClass() {
        System.out.println("Hello");
    }
}
public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
    }
}
← Prev 3637383940 Next → Page 38 of 200 · 3994 questions