☕ Java MCQ Questions – Page 194

Questions 3861–3880 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3861 medium code error
What error will occur when compiling the following Java code snippet?
java
public class Main {
    public static void main(String[] args) {
        Runnable r = new Runnable();
        System.out.println("Runnable created.");
    }
}
Q3862 medium code error
What is the compile-time error in the following Java code?
java
import java.io.IOException;

class DataProcessor {
    private String data;

    public DataProcessor(String filePath) { // Missing throws IOException
        // Simulate an operation that could throw IOException
        if (filePath == null || filePath.isEmpty()) {
            throw new IOException("File path cannot be empty");
        }
        this.data = filePath;
        System.out.println("DataProcessor created for: " + filePath);
    }
}

public class ConstructorError7 {
    public static void main(String[] args) {
        new DataProcessor("my_file.txt");
    }
}
Q3863 hard
What kind of copy is performed by `ArrayList.clone()` for the elements stored within the list?
Q3864 medium
Which method would you use to remove characters from a `StringBuffer` within a specified range (start and end index)?
Q3865 medium code output
What is the output of this Java code?
java
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        TreeSet<Integer> numbers = new TreeSet<>();
        List<Boolean> addResults = new ArrayList<>();
        addResults.add(numbers.add(1));
        addResults.add(numbers.add(3));
        addResults.add(numbers.add(1)); 
        addResults.add(numbers.add(2));
        System.out.println(addResults + " " + numbers);
    }
}
Q3866 medium code error
What is the result of attempting to run the following Java code?
java
class MyTask implements Runnable {
    public void run() {
        System.out.println("Task running");
    }
}
public class Main {
    public static void main(String[] args) {
        MyTask task = new MyTask();
        Thread thread = new Thread(task);
        thread.start();
        thread.start(); // This line
    }
}
Q3867 hard code output
What is the output of this code?
java
public class SwitchTest {
    public static void main(String[] args) {
        Integer num = null;
        String result = "";
        switch (num) {
            case 1:
                result = "One";
                break;
            case 2:
                result = "Two";
                break;
            default:
                result = "Other";
        }
        System.out.println(result);
    }
}
Q3868 medium code error
What is the error in the following Java code?
java
class Parent {
    private int secretCode = 1234;
}

class Child extends Parent {
    public void revealSecret() {
        // Attempt to access private field of superclass
        System.out.println("Secret: " + secretCode);
    }

    public static void main(String[] args) {
        Child c = new Child();
        c.revealSecret();
    }
}
Q3869 hard code output
What is the output of this Java code snippet?
java
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class ListFilesNullTest {
    public static void main(String[] args) throws IOException {
        Path tempDir = Files.createTempDirectory("list_test");
        Path tempFile = Files.createFile(tempDir.resolve("my_file.txt"));
        
        File fileObj = tempFile.toFile();
        File nonExistentObj = new File(tempDir.toFile(), "non_existent_dir");

        System.out.println("On file object: " + (fileObj.listFiles() == null));
        System.out.println("On non-existent object: " + (nonExistentObj.listFiles() == null));
        System.out.println("On parent directory: " + (tempDir.toFile().listFiles().length > 0));
        
        Files.delete(tempFile);
        Files.delete(tempDir);
    }
}
Q3870 hard code output
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        Integer a = 100;
        Integer b = 100;
        Integer c = 200;
        Integer d = 200;

        System.out.println(a == b);
        System.out.println(c == d);
    }
}
Q3871 easy code output
What will be the content of the file 'output.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {
    public static void main(String[] args) {
        try (FileWriter writer1 = new FileWriter("output.txt")) {
            writer1.write("Data A\n");
        } catch (IOException e) {}

        try (FileWriter writer2 = new FileWriter("output.txt", true)) {
            writer2.write("Data B");
        } catch (IOException e) {}
    }
}
Q3872 easy
What is the primary benefit of using `BufferedWriter` in Java?
Q3873 hard code error
What compilation error will occur when compiling the following Java code?
java
import java.util.function.Consumer;

public class LambdaWildcardInference {
    public static void processConsumer(Consumer<?> consumer) {
        // consumer.accept(new Object()); // Would not compile here
    }

    public static void main(String[] args) {
        processConsumer(s -> System.out.println(s.length()));
    }
}
Q3874 medium
To accumulate elements of a Stream into a `Set` while preserving distinct elements, which `Collectors` method would you typically use?
Q3875 medium code error
What error occurs when compiling this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb1 = new StringBuffer("apple");
        StringBuffer sb2 = new StringBuffer("banana");
        System.out.println(sb1.compareTo(sb2));
    }
}
Q3876 medium code error
What will be the compilation error in this code?
java
class Producer {
    public String produceItem() {
        return "Item A";
    }
}

class SpecialProducer extends Producer {
    @Override
    public Integer produceItem() {
        return 123;
    }
}
Q3877 easy code error
What is the error in this Java code?
java
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Map<String, Integer> myMap = new HashMap<>();
        myMap.put("number", "123");
        System.out.println(myMap.get("number"));
    }
}
Q3878 easy code error
What is the compilation or runtime error in the following Java code?
java
public class Main {
    public static void main(String[] args) {
        String prefix = null;
        String data = "program";
        boolean starts = data.startsWith(prefix);
        System.out.println(starts);
    }
}
Q3879 medium code error
What is the compile-time error in the following Java code?
java
class MyClass {
    private int value;

    public static MyClass(int value) { // Illegal static modifier
        this.value = value;
    }

    public static void main(String[] args) {
        MyClass obj = new MyClass(10);
    }
}
Q3880 hard
A `TreeSet` is created using a custom `Comparator` that is defined as an anonymous inner class and is NOT `Serializable`. If this `TreeSet` instance is then attempted to be serialized to a file, what is the most likely outcome?
← Prev 192193194195196 Next → Page 194 of 200 · 3994 questions