☕ Java MCQ Questions – Page 197

Questions 3921–3940 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3921 easy code output
What is the output of this code?
java
import java.util.function.Function;

public class LambdaTest {
    public static void main(String[] args) {
        Function<Integer, Integer> doubler = x -> x * 2;
        System.out.println(doubler.apply(4));
    }
}
Q3922 hard code output
Assume a file named 'data.txt' exists and contains the text "Hello World". What does this code print?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class FileReaderSkip {
    public static void main(String[] args) throws IOException {
        // Assume 'data.txt' has content: "Hello World"
        StringBuilder sb = new StringBuilder();
        try (FileReader reader = new FileReader(new File("data.txt"))) {
            reader.skip(5);
            int c = reader.read(); // Read ' ' (space)
            sb.append((char) c);
            reader.skip(Long.MAX_VALUE); // Skip past EOF
            c = reader.read(); // Should be -1
            sb.append(c); // Appends -1 as string "-1"
        }
        System.out.println(sb.toString());
    }
}
Q3923 medium
Why is calling the `close()` method on a `FileWriter` instance crucial after finishing writing operations?
Q3924 easy
Which of the following symbols is used as the 'arrow token' to separate parameters from the body of a lambda expression?
Q3925 easy code output
What does this Java code print?
java
public class StringPoolTest {
    public static void main(String[] args) {
        String s1 = "test";
        String s2 = "test";
        System.out.println(s1 == s2);
    }
}
Q3926 medium
`FileWriter` is a subclass of which abstract class in the `java.io` package, representing character output streams?
Q3927 easy
When defining a lambda expression with a single parameter, under what condition can you omit the parentheses around the parameter?
Q3928 hard
Consider the following Java code: `byte b = 10; b = b + 1;` Which statement about this code is true?
Q3929 easy code error
What is the compile-time error in this Java class attempting to use a 'native' method?
java
class HardwareInterface {
    public native void configurePort(int portNumber);

    public void configurePort(int portNumber) {
        System.out.println("Configuring port: " + portNumber);
    }
}
Q3930 easy
Can a Java class have multiple constructors?
Q3931 hard code output
What is the output of this code?
java
public class IntegerObjectEqualityWhile {
    public static void main(String[] args) {
        Integer i = 0;
        Integer limit = 150;
        int count = 0;
        while (i != limit) { 
            count++;
            i = i + 1; 
        }
        System.out.println("Final state: " + count + ", " + i.intValue());
    }
}
Q3932 hard
Consider a scenario where a `FileReader` instance is created and then immediately closed. If a subsequent attempt is made to read data from this same `FileReader` instance, what is the expected behavior?
Q3933 medium
In modern Java (Java 17 and later), what happens if the controlling expression of a `switch` statement or expression evaluates to `null`?
Q3934 medium
Which of the following is NOT a functional interface provided by Java's `java.util.function` package?
Q3935 hard code error
What does this code print?
java
import java.io.*;
import java.nio.charset.StandardCharsets;

public class EncodingErrorTest {
    public static void main(String[] args) {
        byte[] rawBytes = {(byte) 0xC3, (byte) 0x28}; // Invalid UTF-8 sequence
        try (ByteArrayInputStream bis = new ByteArrayInputStream(rawBytes);
             InputStreamReader isr = new InputStreamReader(bis, StandardCharsets.UTF_8);
             BufferedReader br = new BufferedReader(isr)) {
            System.out.println(br.readLine()); // Attempt to read invalid UTF-8
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
Q3936 easy code output
What will be printed when this Java code runs?
java
public class MyClass {
    public static void main(String[] args) {
        int value = 5;
        String message = "";
        switch (value) {
            default: message = "Default Message"; break;
            case 1: message = "Case 1"; break;
            case 2: message = "Case 2"; break;
        }
        System.out.print(message);
    }
}
Q3937 hard code error
What is the result of running this Java code?
java
import java.util.TreeMap;

public class TreeMapError1 {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(1, "One");
        map.put(null, "Null Value"); 
        System.out.println(map.size());
    }
}
Q3938 hard
Consider the code: java Integer[][] matrix = new Integer[2][]; System.out.println(matrix[0]); What will be printed to the console?
Q3939 easy code output
What does this code print?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<Integer> ts = new TreeSet<>();
        ts.add(1);
        ts.add(2);
        System.out.println(ts.isEmpty());
        ts.clear();
        System.out.println(ts.isEmpty());
    }
}
Q3940 easy
Which of the following is a common pre-defined functional interface found in the `java.util.function` package?
← Prev 195196197198199 Next → Page 197 of 200 · 3994 questions