☕ Java MCQ Questions – Page 190

Questions 3781–3800 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3781 hard code output
What is the output of this Java code?
java
import java.util.TreeMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(10, "Ten");
        map.put(20, "Twenty");
        map.put(30, "Thirty");
        map.put(40, "Forty");

        Map.Entry<Integer, String> entry = map.floorEntry(25);      // <= 25
        Map.Entry<Integer, String> nextEntry = map.ceilingEntry(25); // >= 25
        Map.Entry<Integer, String> lowerEntry = map.lowerEntry(20);  // < 20
        Map.Entry<Integer, String> higherEntry = map.higherEntry(40);// > 40

        System.out.println(entry.getKey() + " " + nextEntry.getKey() + " " + (lowerEntry == null ? "null" : lowerEntry.getKey()) + " " + (higherEntry == null ? "null" : higherEntry.getKey()));
    }
}
Q3782 hard code error
What is the output of this code?
java
import java.io.File;
import java.io.IOException;

public class FileCreationError {
    public static void main(String[] args) {
        File file = new File("non_existent_dir/new_file.txt");
        try {
            boolean created = file.createNewFile();
            System.out.println("File created: " + created);
        } catch (IOException e) {
            System.out.println("Error: " + e.getClass().getSimpleName() + ": " + e.getMessage());
        }
    }
}
Q3783 easy
Which of the following modifiers cannot be used with an abstract method in Java?
Q3784 easy code error
What is the compile-time error in this Java code snippet?
java
public class ArrayTest {
    public static void main(String[] args) {
        int[] data = new int[5];
        data.length = 10;
    }
}
Q3785 easy code output
What is the output of this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        try {
            System.out.println("Inside try block");
            throw new RuntimeException("Test Exception");
        } catch (RuntimeException e) {
            System.out.println("Inside catch block: " + e.getMessage());
        } finally {
            System.out.println("Inside finally block");
        }
    }
}
Q3786 hard code error
What type of error will occur when compiling this Java code?
java
final class BaseImmutable {
    private final String name;
    public BaseImmutable(String name) { this.name = name; }
    public String getName() { return name; }
}

class ExtendedImmutable extends BaseImmutable { // Attempt to extend a final class
    private int counter;
    public ExtendedImmutable(String name, int counter) {
        super(name);
        this.counter = counter;
    }
}

public class FinalClassExtension {
    public static void main(String[] args) {
        // Code to instantiate these classes is not needed to trigger the error.
    }
}
Q3787 hard
Consider an interface `MyInterface` and a concrete class `MyConcreteClass` implementing `MyInterface`. Which of the following single-dimensional array initializations is valid at compile-time?
Q3788 easy code error
What is the result of compiling and executing this Java code?
java
public class FinalStringBuilderReassignment {
    public static void main(String[] args) {
        final StringBuilder sb = new StringBuilder("Hello");
        sb.append(" World");
        sb = new StringBuilder("Goodbye");
        System.out.println(sb);
    }
}
Q3789 hard
What is the maximum theoretical *valid index value* for a single-dimensional Java array of any type, given standard JVM limitations for array length?
Q3790 hard code output
What is the output of this code?
java
public class FinalFieldInit {
    final int value;
    static final String NAME = "Default";

    public FinalFieldInit(int v) {
        this.value = v;
    }

    public static void main(String[] args) {
        FinalFieldInit f1 = new FinalFieldInit(100);
        FinalFieldInit f2 = new FinalFieldInit(200); 
        System.out.println(f1.value + " " + NAME);
    }
}
Q3791 medium code error
What error will occur when compiling this Java code?
java
public class ConditionCheck {
    public static void main(String[] args) {
        String value = "false";
        if (value) {
            System.out.println("Value is considered true.");
        } else {
            System.out.println("Value is considered false.");
        }
    }
}
Q3792 hard code error
What compilation error will occur when compiling this Java code?
java
public class StaticBlockBreak {
    static {
        System.out.println("Static block start");
        if (true) {
            break;
        }
        System.out.println("Static block end");
    }
    public static void main(String[] args) {
        System.out.println("Main method called");
    }
}
Q3793 easy code error
What kind of error will occur when this Java code is executed?
java
public class Test {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello");
        char c = sb.charAt(5);
        System.out.println(c);
    }
}
Q3794 medium code output
What does this code print to the console?
java
public class StringTest {
    public static void main(String[] args) {
        String data = "Hello World";
        System.out.println(data.charAt(data.length() - 1));
    }
}
Q3795 hard
If an `IOException` occurs during a `FileWriter.write()` call (e.g., due to disk full or permissions issue), what is the typical and recommended state of the `FileWriter` instance regarding subsequent `write()` operations *without* re-instantiation?
Q3796 medium code output
What does this Java code print?
java
public class TryFinallyExample {
    public static void main(String[] args) {
        try {
            System.out.println("Try block executed");
        } finally {
            System.out.println("Finally block executed");
        }
        System.out.println("After try-finally");
    }
}
Q3797 medium code error
What compilation error will occur in this Java class attempting to reassign a static final field?
java
public class Constants {
    public static final String APP_VERSION = "1.0";

    static {
        APP_VERSION = "1.1"; // Attempt to reassign static final field
    }

    public static void main(String[] args) {
        System.out.println(APP_VERSION);
    }
}
Q3798 medium code error
What error occurs when compiling this Java code?
java
import java.io.FileNotFoundException;

public class Main {
    public static void processFile() throws FileNotFoundException {
        System.out.println("Processing file...");
        throw new FileNotFoundException("File 'data.txt' not found.");
    }

    public static void main(String[] args) {
        processFile(); // This call might throw FileNotFoundException
    }
}
Q3799 medium code error
What kind of error will occur when compiling this Java code?
java
public class LoopTest {
    public static void main(String[] args) {
        int value;
        while (value < 10) {
            System.out.println("Current value: " + value);
            value++;
        }
    }
}
Q3800 hard code output
What is the output of this code?
java
class GeneralCustomException extends Exception { public GeneralCustomException(String msg) { super(msg); } }
class SpecificCustomException extends GeneralCustomException { public SpecificCustomException(String msg) { super(msg); } }
public class Main {
    public static void triggerSpecific() throws SpecificCustomException { throw new SpecificCustomException("Specific issue."); }
    public static void main(String[] args) {
        try { triggerSpecific(); }
        catch (GeneralCustomException e) { 
            System.out.println("Caught GeneralCustomException: " + e.getMessage());
        } catch (SpecificCustomException e) { 
            System.out.println("Caught SpecificCustomException: " + e.getMessage());
        } finally {
            System.out.println("Finally block executed.");
        }
    }
}
← Prev 188189190191192 Next → Page 190 of 200 · 3994 questions