☕ Java MCQ Questions – Page 125

Questions 2481–2500 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2481 hard code error
What error will this code produce at compile time?
java
public class InvalidForCondition {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 0; int j = 0; i < 5; i++, j++) {
            sum += i + j;
        }
        System.out.println(sum);
    }
}
Q2482 hard
When a variable is declared as `volatile` in Java, which crucial guarantee is provided regarding its value in a multi-threaded environment?
Q2483 easy
Can a Java class implement the `Runnable` interface and also extend another class simultaneously?
Q2484 hard code output
What does this code print?
java
import java.util.function.Predicate;

public class PredicateChaining {
    public static void main(String[] args) {
        Predicate<Integer> isEven = num -> num % 2 == 0;
        Predicate<Integer> isPositive = num -> num > 0;
        Predicate<Integer> isLessThanTen = num -> num < 10;

        Predicate<Integer> combinedPredicate = 
            isEven.and(isPositive).or(isLessThanTen.negate());

        System.out.println(combinedPredicate.test(4));
        System.out.println(combinedPredicate.test(15));
        System.out.println(combinedPredicate.test(-3));
        System.out.println(combinedPredicate.test(10));
        System.out.println(combinedPredicate.test(9));
    }
}
Q2485 medium code output
What is the output of this code?
java
String s1 = "HELLO";
String s2 = s1.toLowerCase();
String s3 = "hello";
System.out.println(s1 == s2);
System.out.println(s2 == s3);
System.out.println(s1.equals(s2));
System.out.println(s2.equals(s3));
Q2486 easy
What keyword is used to call another constructor within the same class from a constructor in Java?
Q2487 medium
Which of the following built-in functional interfaces is commonly used with lambda expressions to represent a boolean-valued function of one argument?
Q2488 hard code error
What kind of error will occur when executing the following Java code?
java
import java.util.PriorityQueue;
import java.util.Queue;

public class QError1 {
    public static void main(String[] args) {
        Queue<String> pq = new PriorityQueue<>();
        pq.add("apple");
        pq.add(null);
        System.out.println(pq.poll());
    }
}
Q2489 easy code output
What is the output of this Java code?
java
public class LoopTest {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            if (i % 2 == 0) {
                System.out.print(i);
            }
        }
    }
}
Q2490 medium
If the `indexOf()` method is used to search for a character or substring that does not exist within a given String, what value does it return?
Q2491 medium code output
What is the output of this code?
java
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
System.out.println(str1 == str3);
System.out.println(str1.equals(str3));
Q2492 easy code error
What error will this Java code produce when executed?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][] jaggedArray = new int[3][];
        System.out.println(jaggedArray[0][0]);
    }
}
Q2493 hard code output
What is the output of this code?
java
import java.lang.reflect.Constructor;
class Restricted {
    private String name;
    private Restricted(String name) {
        this.name = name;
        System.out.println("Restricted instance: " + name);
    }
}
public class Main {
    public static void main(String[] args) throws Exception {
        Constructor<Restricted> ctor = Restricted.class.getDeclaredConstructor(String.class);
        ctor.setAccessible(true);
        Restricted r1 = ctor.newInstance("First");
        Restricted r2 = ctor.newInstance("Second");
        System.out.println("Main method ends.");
    }
}
Q2494 easy code error
What is the error in the following Java code?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<Integer> numbers = new TreeSet<>();
        numbers.add(10);
        numbers.add("20");
    }
}
Q2495 medium code error
What error will occur when running this Java code snippet?
java
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Map<String, String> settings = new HashMap<>();
        settings.put("theme", "dark");
        settings.put("language", null); // Storing a null value
        
        String lang = settings.get("language");
        System.out.println(lang.toUpperCase()); // Attempt to call method on null
    }
}
Q2496 hard code output
Consider a custom `BadReader` class that extends `FileReader` and overrides `read()` to always throw an `IOException` and also overrides `close()` to always throw an `IOException`. Assume `data.txt` exists. What is the output of this code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class FileReaderTryWithResourcesException {
    static class BadReader extends FileReader {
        public BadReader(File file) throws IOException { super(file); }
        @Override
        public int read() throws IOException { throw new IOException("Read error!"); }
        @Override
        public void close() throws IOException { throw new IOException("Close error!"); }
    }

    public static void main(String[] args) {
        // Assume 'data.txt' exists
        try (BadReader reader = new BadReader(new File("data.txt"))) {
            reader.read();
        } catch (IOException e) {
            System.out.println("Caught: " + e.getMessage());
            Throwable[] suppressed = e.getSuppressed();
            if (suppressed.length > 0) {
                System.out.println("Suppressed: " + suppressed[0].getMessage());
            }
        }
    }
}
Q2497 easy
What is the default name given to the first user-created `Thread` if not specified explicitly?
Q2498 medium code output
What is the output of this code?
java
public class StringBufferTest {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Hello");
        sb.append(" World").append(123);
        System.out.print(sb.capacity());
    }
}
Q2499 easy
Encapsulation is often described as bundling data (attributes) and methods (behaviors) that operate on the data into a single unit. What is this unit called in Java?
Q2500 easy code output
What is the output of this Java code?
java
public class SleepExample {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(100);
                System.out.println("Thread woke up.");
            } catch (InterruptedException e) {
                System.out.println("Thread interrupted.");
            }
        });
        thread.start();
        System.out.println("Main finished.");
    }
}
← Prev 123124125126127 Next → Page 125 of 200 · 3994 questions