☕ Java MCQ Questions – Page 190
Questions 3781–3800 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat 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()));
}
}
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());
}
}
}
Which of the following modifiers cannot be used with an abstract method in Java?
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;
}
}
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");
}
}
}
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.
}
}
Consider an interface `MyInterface` and a concrete class `MyConcreteClass` implementing `MyInterface`. Which of the following single-dimensional array initializations is valid at compile-time?
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);
}
}
What is the maximum theoretical *valid index value* for a single-dimensional Java array of any type, given standard JVM limitations for array length?
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);
}
}
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.");
}
}
}
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");
}
}
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);
}
}
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));
}
}
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?
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");
}
}
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);
}
}
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
}
}
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++;
}
}
}
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.");
}
}
}