☕ Java MCQ Questions – Page 94
Questions 1861–1880 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhich of the following scenarios is `java.util.LinkedList` generally NOT optimized for, compared to `java.util.ArrayList`?
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));
}
}
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);
}
}
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`?
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);
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);
}
}
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());
}
}
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());
}
}
}
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.");
}
}
}
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");
}
}
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.");
}
}
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)`?
What is the average time complexity for appending an element to the end of an `ArrayList`?
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);
}
}
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();
}
}
Which Java access modifier is most commonly used to enforce data hiding as a core principle of encapsulation?
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?
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);
}
}
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.");
}
}
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);
}
}