☕ Java MCQ Questions – Page 188

Questions 3741–3760 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3741 easy code error
What is the compile-time error in this Java code snippet?
java
public class ArrayTest {
    public static void main(String[] args) {
        int[] items;
        items = new int[3];
        System.out.println(items[items.length]);
    }
}
Q3742 medium
How many `null` elements can a `HashSet` store?
Q3743 medium
What occurs if you call the `charAt(index)` method with an `index` that is less than 0 or greater than or equal to the String's `length()`?
Q3744 easy code error
What kind of error will occur when executing the following Java code?
java
public class Joiner {
    public static void main(String[] args) {
        Thread t = new Thread(() -> System.out.println("Child thread running."));
        t.start();
        t.join(); // Attempt to join without handling InterruptedException
        System.out.println("Child thread finished.");
    }
}
Q3745 medium
Consider a Java class `Product` with a field `public double price;`. Which statement best describes the encapsulation of the `price` field?
Q3746 easy code error
What kind of error will occur when compiling or running the following Java code snippet?
java
import java.io.File;
import java.io.IOException;

public class FileError1 {
    public static void main(String[] args) {
        File file = new File("nonExistentDir/myFile.txt");
        try {
            file.createNewFile();
            System.out.println("File created.");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
Q3747 medium
Which statement best describes the difference in locking behavior between a `synchronized` instance method and a `synchronized` block on `this` in Java?
Q3748 easy code error
What type of error will occur when compiling this Java code?
java
public class StaticSynchProblem {
    private boolean flag = false;

    public static void performStaticSync() {
        synchronized (this) { // Attempting to synchronize on 'this' in a static method
            System.out.println("Synchronized on 'this'.");
        }
    }

    public static void main(String[] args) {
        StaticSynchProblem.performStaticSync();
    }
}
Q3749 medium code output
What is the output of this Java code?
java
import java.util.function.Predicate;

public class MethodRefTest {
    public static void main(String[] args) {
        String name = "Java";
        Predicate<String> checker = name::startsWith;
        boolean result = checker.test("J");
        System.out.println(result);
    }
}
Q3750 hard
Consider a class `A` with a private constructor and a public static factory method `getInstance()`. What are the implications for its sub-classing?
Q3751 medium
When using an `ExecutorService`, which methods are commonly used to submit a `Runnable` task for asynchronous execution?
Q3752 medium
What is the average time complexity for adding an element at a specific index (not the end) in an `ArrayList`?
Q3753 hard
Which statement correctly characterizes the mutability of `StringBuffer` in a multi-threaded context?
Q3754 medium code error
What compilation error will occur in the following Java code?
java
public class FinalIncrement {
    public static void main(String[] args) {
        final int count = 5;
        count++;
        System.out.println(count);
    }
}
Q3755 easy code error
What is the error in this code?
java
import java.util.Comparator;
import java.util.TreeMap;

class MyStringComparator implements Comparator<String> {
    @Override
    public int compare(String s1, String s2) {
        // Intentionally causes a ClassCastException
        return ((Integer)(Object)s1).compareTo((Integer)(Object)s2);
    }
}

public class Test {
    public static void main(String[] args) {
        TreeMap<String, String> map = new TreeMap<>(new MyStringComparator());
        map.put("apple", "fruit");
    }
}
Q3756 medium code output
What is the output of this code?
java
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Test {
    public static void main(String[] args) {
        Set<Integer> uniqueNums = new HashSet<>();
        uniqueNums.add(3);
        uniqueNums.add(1);
        uniqueNums.add(2);
        uniqueNums.add(3); // Duplicate, ignored
        int sum = 0;
        Iterator<Integer> it = uniqueNums.iterator();
        while (it.hasNext()) {
            sum += it.next();
        }
        System.out.println(sum);
    }
}
Q3757 hard code error
What is the primary resource management error in this Java code?
java
import java.io.*;

public class ResourceLeakWhile {
    public static void main(String[] args) {
        BufferedReader reader = null;
        try {
            // Assume 'temp_data.txt' exists for this example
            reader = new BufferedReader(new FileReader("temp_data.txt"));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            System.err.println("File I/O Error: " + e.getMessage());
        }
        // Missing reader.close() call
    }
}
Q3758 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) {
        StringBuilder sb = new StringBuilder("123");
        sb.insert(4, "45");
        System.out.println(sb);
    }
}
Q3759 easy code output
What is the output of this code?
java
public class DataTypeTest {
    public static void main(String[] args) {
        char unicodeChar = '€'; 
        int intValue = unicodeChar;
        System.out.println(intValue);
    }
}
Q3760 easy code output
What does this Java code snippet print?
java
public class Settings {
    private boolean notificationsEnabled;

    public Settings() {
        this.notificationsEnabled = false; // Default value
    }

    public void enableNotifications() {
        this.notificationsEnabled = true;
    }

    public boolean areNotificationsEnabled() {
        return notificationsEnabled;
    }

    public static void main(String[] args) {
        Settings s = new Settings();
        System.out.println(s.areNotificationsEnabled());
    }
}
← Prev 186187188189190 Next → Page 188 of 200 · 3994 questions