☕ Java MCQ Questions – Page 187

Questions 3721–3740 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3721 easy code output
What does this code print?
java
import java.util.ArrayDeque;
import java.util.Queue;

public class Main {
    public static void main(String[] args) {
        Queue<Double> values = new ArrayDeque<>();
        values.offer(1.1);
        values.offer(2.2);
        values.poll();
        values.offer(3.3);
        System.out.println(values);
    }
}
Q3722 hard code output
What is the output of this code?
java
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

public class Test {
    public static void main(String[] args) throws IOException {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        bw.write("Hello");
        String contentBeforeClose = sw.toString();
        bw.close();
        String contentAfterClose = sw.toString();
        System.out.println("Before: " + contentBeforeClose.length());
        System.out.println("After: " + contentAfterClose.length());
    }
}
Q3723 medium code error
What compilation error will this Java code produce?
java
public class SwitchError {
    public static void main(String[] args) {
        int day = 4;
        switch (day) {
            case 1: {
                System.out.println("Workday");
                break 10; // Incorrect usage of break with value in a statement
            }
            default:
                System.out.println("Other day");
        }
    }
}
Q3724 easy code error
What is the error in this Java code?
java
class Animal {
    public void eat() {
        System.out.println("Animal eats");
    }
}

class Dog extends Animal {
    public void bark() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Dog();
        myAnimal.bark(); // Line 14
    }
}
Q3725 easy code error
What error will this Java code produce during compilation?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][] matrix = {{1, 2}, {3, 4}};
        System.out.println(matrix[0,1]);
    }
}
Q3726 easy code output
What is the output of this Java code?
java
public class TypeCast {
    public static void main(String[] args) {
        int num = 130;
        byte b = (byte) num;
        System.out.println(b);
    }
}
Q3727 easy code output
What is the output of this Java code snippet?
java
public class Main {
    public static void main(String[] args) {
        int x = 5;
        int y = 10;
        if (x < 10) {
            if (y > 5) {
                System.out.println("Both conditions met");
            }
        }
    }
}
Q3728 easy code output
What is the output of this Java code?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        Iterator<String> it = fruits.iterator();
        while (it.hasNext()) {
            String fruit = it.next();
            if (fruit.equals("Banana")) {
                it.remove();
            }
        }
        System.out.println(fruits);
    }
}
Q3729 medium
Which requirement must `case` labels satisfy in a Java `switch` statement or expression?
Q3730 medium
In a traditional Java `for` loop, what happens if any of the three parts (initialization, condition, or update) are omitted?
Q3731 easy code error
What kind of error will occur when compiling this Java code?
java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        BufferedWriter bw;
        try {
            // bw is not guaranteed to be initialized here before use
            bw.write("Hello");
            bw = new BufferedWriter(new FileWriter("output.txt"));
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q3732 medium code output
What is printed by this Java code?
java
class LoginFailedException extends Exception {
    public LoginFailedException(String message, Throwable cause) {
        super(message, cause);
    }
}

public class Main {
    public static void main(String[] args) {
        try {
            try {
                Integer.parseInt("abc"); // Throws NumberFormatException
            } catch (NumberFormatException e) {
                throw new LoginFailedException("Login service unavailable", e);
            }
        } catch (LoginFailedException e) {
            System.out.println("Root Cause: " + e.getCause().getClass().getSimpleName());
        }
    }
}
Q3733 medium code error
What error will occur when running this Java code snippet?
java
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Map<String, String> config = new HashMap<>();
        config.put("setting1", "value1");
        config.put("setting2", "value2");
        config.put("setting3", "value3");

        Iterator<Map.Entry<String, String>> iterator = config.entrySet().iterator();
        
        if (iterator.hasNext()) {
            iterator.next(); // Move to the first element
            iterator.remove(); // Remove the first element
            iterator.remove(); // Attempt to remove again without calling next()
        }
        System.out.println(config.size());
    }
}
Q3734 hard code error
What is the output of this code?
java
import java.io.File;
import java.io.IOException;

public class MkdirParentAsFile {
    public static void main(String[] args) {
        File parentFile = new File("parent_as_file.txt");
        File childDir = new File(parentFile, "child_dir");
        try {
            parentFile.createNewFile(); // Create a regular file at parent path
            boolean success = childDir.mkdir(); // Attempt to create a directory with a file as its parent
            System.out.println("mkdir success: " + success);
        } catch (IOException e) {
            System.out.println("Error: " + e.getClass().getSimpleName() + ": " + e.getMessage());
        } finally {
            parentFile.delete(); // Clean up
        }
    }
}
Q3735 easy code error
Identify the compile-time error in the provided Java snippet related to method overloading.
java
import java.io.IOException;

class Worker {
    void performTask() {
        System.out.println("Task performed.");
    }

    void performTask() throws IOException {
        System.out.println("Task with potential IO error performed.");
    }
}
Q3736 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 writer = new FileWriter("output.txt")) {
            writer.write(65);
            writer.write(97);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q3737 hard code error
What compilation error will occur in the `calculateValue` method?
java
class Test {
    public int calculateValue() {
        int i = 0;
        do {
            i++;
            // No explicit return statement within the loop
        } while (true); // An infinite loop
        // The compiler expects a reachable return statement here
    }
}
Q3738 medium
What does the `mark()` method in `BufferedReader` allow you to do?
Q3739 hard code error
What is the error encountered when running this Java code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class FileReaderError4 {
    public static void main(String[] args) {
        File file = new File("test_fr4.txt");
        try {
            file.createNewFile();
            try (FileReader fr = new FileReader(file)) {
                // FileReader typically does not support mark/reset
                if (!fr.markSupported()) {
                    fr.mark(10); // Attempt to mark when not supported
                }
                System.out.println("Mark set");
            }
        } catch (IOException e) {
            System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        } finally {
            file.delete();
        }
    }
}
Q3740 hard code error
What is the error in the execution of this Java code snippet?
java
public class BuilderError {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("World");
        sb.replace(10, 5, "Hello");
        System.out.println(sb);
    }
}
← Prev 185186187188189 Next → Page 187 of 200 · 3994 questions