☕ Java MCQ Questions – Page 46

Questions 901–920 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q901 medium code error
What is the error in this Java code?
java
// No import java.util.ArrayList;

public class MyClass {
    public static void main(String[] args) {
        List<String> colors = new ArrayList<>(); // ArrayList not recognized
        colors.add("Red");
        System.out.println(colors);
    }
}
Q902 hard code output
What is the output of this code?
java
public class FloatPrecision {
    public static void main(String[] args) {
        final double value1 = 0.1 * 3;
        final double value2 = 0.3;

        System.out.println(value1 == value2);
        System.out.println(String.format("%.17f", value1));
        System.out.println(String.format("%.17f", value2));
    }
}
Q903 hard code error
What is the compile-time error in this Java code?
java
interface MyInterface {
    private abstract void setup();
    void execute();
}
Q904 easy code error
What error will this code produce?
java
public class LoopError {
    public static void main(String[] args) {
        for (int i = 0, j = 0; i < 5, j < 5; i++, j++) {
            System.out.println(i + " " + j);
        }
    }
}
Q905 hard
If a `try-catch` block is placed inside a `while` loop's body, and upon catching an exception, a `continue` statement is executed, what is the immediate next step in the loop's execution flow?
Q906 medium
Which of the following is NOT a valid way to declare and initialize an array of integers in Java?
Q907 easy
Are `BufferedReader` and `BufferedWriter` primarily used for character-based or byte-based I/O?
Q908 easy code output
What does this Java program print?
java
class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}
public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        int result = calc.add(7, 8);
        System.out.print("Result: " + result);
    }
}
Q909 easy code output
What is the output of this code?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<String> ts = new TreeSet<>();
        ts.add("x");
        ts.add("y");
        ts.add("z");
        for (String s : ts) {
            System.out.print(s);
        }
    }
}
Q910 easy code error
What error will occur when executing the provided Java code?
java
import java.util.ArrayList;
import java.util.Iterator;

public class IteratorError {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        Iterator<String> it = names.iterator();
        if (!names.isEmpty()) { // Condition is false as names is empty
            it.next();
        } else {
            System.out.println("List is empty.");
        }
        it.remove(); // This line will cause an error
    }
}
Q911 medium code output
What does this code print?
java
public class PassImmutable {
    public static void modifyString(String s) {
        s = s.concat(" World");
        System.out.println("Inside method: " + s);
    }

    public static void main(String[] args) {
        String myString = "Hello";
        modifyString(myString);
        System.out.println("Outside method: " + myString);
    }
}
Q912 hard code error
Consider the provided Java code. What type of error is expected during its execution?
java
import java.io.*;

public class FileWriterError5 {
    public static void main(String[] args) {
        FileWriter fw = null;
        try {
            // Assume '/path/to/nonexistent/directory/file.txt' is an invalid path 
            // where the parent directories do not exist and cannot be created by FileWriter.
            fw = new FileWriter("/nonexistent/dir/file.txt"); 
            fw.write("Hello");
        } catch (IOException e) {
            System.out.println("Caught IOException during creation: " + e.getMessage());
        } finally {
            try {
                if (fw != null) {
                    fw.close(); 
                }
            } catch (IOException e) {
                System.out.println("Caught IOException during close: " + e.getMessage());
            }
        }
    }
}
Q913 hard code error
What error will this Java code produce when compiled and run?
java
public class MissingArrayDimension {
    public static void main(String[] args) {
        // Attempt to create an array without specifying its size
        int[] nums = new int[]; 
        System.out.println(nums.length);
    }
}
Q914 hard code error
What is the output of this code?
java
import java.io.File;

public class NullParentConstructor {
    public static void main(String[] args) {
        String parentPath = null;
        try {
            File file = new File(parentPath, "child.txt"); // Passing null as parent
            System.out.println("File path: " + file.getAbsolutePath());
        } catch (NullPointerException e) {
            System.out.println("Caught NullPointerException: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("Caught other exception: " + e.getClass().getSimpleName());
        }
    }
}
Q915 medium code output
What does this Java code print to the console?
java
public class StringTest {
    public static void main(String[] args) {
        String original = "Programming is fun";
        String modified = original.replace('g', 'G');
        String finalStr = modified.replace("fun", "exciting");
        System.out.println(finalStr);
    }
}
Q916 medium
When does the `isAlive()` method of a `Thread` object return `true`?
Q917 hard
When designing a custom checked exception intended for remote communication and serialization across different JVM versions, what is the most critical consideration to ensure consistent deserialization and avoid `InvalidClassException`?
Q918 easy
What happens if you try to add an element that already exists in a `HashSet`?
Q919 hard code output
What is the output of this code?
java
public class ContinueWhile {
    public static void main(String[] args) {
        int count = 0;
        int i = 0;
        while (i < 5) {
            i++; 
            if (i == 3) {
                continue;
            }
            count++; 
            if (i == 4) {
                i++; 
            }
        }
        System.out.println(count + ", " + i);
    }
}
Q920 hard code output
Assume a file named 'data.txt' exists and contains the text "ABCDEFGHI". What does this code print?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class FileReaderPartialRead {
    public static void main(String[] args) throws IOException {
        // Assume 'data.txt' has content: "ABCDEFGHI"
        char[] buffer = new char[5]; 
        String result = "";
        try (FileReader reader = new FileReader(new File("data.txt"))) {
            reader.skip(2); 
            int charsRead = reader.read(buffer, 1, 3); 
            result += "Chars Read: " + charsRead + "\n";
            result += "Buffer Content: " + new String(buffer) + "\n"; 
            charsRead = reader.read(buffer, 0, 5); 
            result += "Chars Read 2: " + charsRead + "\n";
            result += "Buffer Content 2: " + new String(buffer);
        }
        System.out.println(result);
    }
}
← Prev 4445464748 Next → Page 46 of 200 · 3994 questions