☕ Java MCQ Questions – Page 94

Questions 1861–1880 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1861 hard
Which of the following scenarios is `java.util.LinkedList` generally NOT optimized for, compared to `java.util.ArrayList`?
Q1862 hard code output
What is the output of this code?
java
import java.util.Arrays;

public class ArrayEqualsDeepEquals {
    public static void main(String[] args) {
        int[][] arr1 = {{1, 2}, {3, 4}};
        int[][] arr2 = {{1, 2}, {3, 4}};
        int[][] arr3 = arr1.clone();
        
        System.out.println(Arrays.equals(arr1, arr2));
        System.out.println(Arrays.deepEquals(arr1, arr2));
        System.out.println(Arrays.equals(arr1, arr3));
        System.out.println(Arrays.deepEquals(arr1, arr3));
    }
}
Q1863 medium code output
What is the output of this code?
java
import java.io.File;

public class DirectoryCreation {
    public static void main(String[] args) {
        File parent = new File("parent_dir");
        File nested = new File(parent, "child_dir/grandchild_dir");
        boolean createdNested = nested.mkdirs();
        boolean createdParentAgain = parent.mkdir(); // Attempt to create parent again
        
        // Cleanup (delete innermost first)
        new File(parent, "child_dir/grandchild_dir").delete();
        new File(parent, "child_dir").delete();
        parent.delete();
        
        System.out.println(createdNested + ", " + createdParentAgain);
    }
}
Q1864 hard
What is the key difference and primary use case distinction between `java.util.function.BinaryOperator<T>` and `java.util.function.BiFunction<T, U, R>` when `T` and `U` are the same type as `R`?
Q1865 hard code output
Consider the following Java code. What will be printed?
java
String s1 = "Programming";
String s2 = s1.substring(3);
String s3 = "gramming";
String s4 = s1.substring(0, 7) + s1.substring(7);

System.out.println(s2 == s3);
System.out.println(s3 == s4);
System.out.println(s1 == s4);
Q1866 medium code output
What is the output of this Java code?
java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamTest {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("apple", "banana", "apricot", "grape");
        String result = words.stream()
                             .filter(s -> s.startsWith("a"))
                             .map(String::toUpperCase)
                             .collect(Collectors.joining(", "));
        System.out.println(result);
    }
}
Q1867 medium code output
What does this code print?
java
import jakarta.validation.*;
import jakarta.validation.constraints.NotNull;
import java.util.Set;

public class Test {
    static class Contact { @NotNull public String email; public Contact(String email) { this.email = email; } }
    static class UserProfile {
        @NotNull public String username; @Valid public Contact contact;
        public UserProfile(String u, Contact c) { this.username = u; this.contact = c; }
    }
    public static void main(String[] args) {
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        UserProfile profile = new UserProfile("tester", new Contact("test@example.com"));
        Set<ConstraintViolation<UserProfile>> violations = validator.validate(profile);
        System.out.println("Violations: " + violations.size());
    }
}
Q1868 medium code error
What will be the output of the following Java code?
java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        try {
            FileWriter fw = new FileWriter("data.txt");
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("First line");
            fw.close(); // Close underlying stream directly
            bw.write("Second line");
            bw.close();
        } catch (IOException e) {
            System.out.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        }
    }
}
Q1869 hard code error
What error occurs when attempting to compile the following Java code?
java
public class UnreachableElseCompiler {
    public static void main(String[] args) {
        final boolean DEBUG_MODE = true;
        if (DEBUG_MODE) {
            System.out.println("Debugging enabled.");
        } else { // Unreachable 'else' block due to final constant `DEBUG_MODE`
            System.out.println("Debugging disabled.");
        }
    }
}
Q1870 easy code output
What does this Java code print?
java
public class OverloadDemo2 {
    void greet() {
        System.out.println("Hello!");
    }
    void greet(String name) {
        System.out.println("Hello, " + name + "!");
    }
    public static void main(String[] args) {
        OverloadDemo2 obj = new OverloadDemo2();
        obj.greet("Alice");
    }
}
Q1871 hard code output
What is the output of this code?
java
import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest {
    private final CountDownLatch latch = new CountDownLatch(3);

    public void worker(String name) {
        try {
            System.out.println(name + " is starting.");
            Thread.sleep((long) (Math.random() * 100)); // Simulate work
            System.out.println(name + " is done.");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            latch.countDown();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        CountDownLatchTest test = new CountDownLatchTest();
        new Thread(() -> test.worker("Worker-1")).start();
        new Thread(() -> test.worker("Worker-2")).start();
        new Thread(() -> test.worker("Worker-3")).start();

        System.out.println("Main thread waiting.");
        latch.await(); // Main thread waits until count is zero
        System.out.println("Main thread proceeding.");
    }
}
Q1872 hard
Consider constructor overloading in Java. If a class has constructors `C(int a)` and `C(Integer b)`, and a third constructor `C(long c)`, which constructor will be invoked by `new C(5)`?
Q1873 medium
What is the average time complexity for appending an element to the end of an `ArrayList`?
Q1874 easy code error
What is the runtime error in the following Java code snippet?
java
import java.util.ArrayList;

public class Question6 {
    public static void main(String[] args) {
        ArrayList<Double> prices = new ArrayList<>();
        prices.remove(0);
        System.out.println(prices);
    }
}
Q1875 easy code error
What type of error will occur when compiling this Java code?
java
public class BadSyncSyntax {
    private final Object lock = new Object();

    public void faultyMethod() {
        synchronized lock { // Incorrect syntax for synchronized block
            System.out.println("Attempting synchronization.");
        }
    }

    public static void main(String[] args) {
        new BadSyncSyntax().faultyMethod();
    }
}
Q1876 medium
Which Java access modifier is most commonly used to enforce data hiding as a core principle of encapsulation?
Q1877 hard
A thread attempts to enter a `synchronized` block, but the intrinsic lock (monitor) is currently held by another thread. While it's in this `BLOCKED` state, its `interrupt()` method is called by a third thread. What is the immediate effect on the `BLOCKED` thread's state and interrupt status?
Q1878 hard code error
What error occurs when attempting to compile the following Java code?
java
public class FinalVariableConditional {
    public static void main(String[] args) {
        final int threshold = 100;
        int current = 50;
        if (current < threshold) {
            threshold = 120; // Reassignment of a final variable
        }
        System.out.println("Threshold: " + threshold);
    }
}
Q1879 medium code output
What is the output of this code?
java
class MyTask implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " is running.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyTask task = new MyTask();
        Thread t = new Thread(task);
        t.run(); // Calling run() directly
        System.out.println("End of main.");
    }
}
Q1880 medium code error
Which compile-time error will occur when `SubClass` attempts to extend `ImmutableClass`?
java
final class ImmutableClass {
    private int value;
    public ImmutableClass(int value) {
        this.value = value;
    }
}

class SubClass extends ImmutableClass {
    public SubClass(int value) {
        super(value);
    }
}

public class Main {
    public static void main(String[] args) {
        new SubClass(5);
    }
}
← Prev 9293949596 Next → Page 94 of 200 · 3994 questions