☕ Java MCQ Questions – Page 14

Questions 261–280 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q261 easy code error
What error will occur when attempting to write to a non-existent directory '/nonexistent_dir/file.ser'?
java
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.File;

public class Main {
    public static void main(String[] args) {
        String filePath = "/nonexistent_dir/file.ser"; // Path to a directory that likely doesn't exist
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
            oos.writeObject("Hello");
        } catch (IOException e) {
            System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        }
    }
}
Q262 easy code error
What is the compilation error in the following Java code snippet?
java
public class LoopError {
    public static void main(String[] args) {
        int k = 0;
        do {
            System.out.println(k);
            k++;
        } while (k < 2);
        break;
    }
}
Q263 hard code error
What is the error in this Java code snippet?
java
public class BuilderError {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        char ch = null;
        sb.append(ch);
        System.out.println(sb);
    }
}
Q264 medium
A method reference `ClassName::instanceMethod` (e.g., `String::toUpperCase`) refers to an instance method of an arbitrary object of a particular type. How does this differ from `object::instanceMethod` (e.g., `myString::toUpperCase`)?
Q265 medium
Which two mechanisms are primarily used to achieve abstraction in Java?
Q266 easy
If a thread calls `object.wait()` without a timeout, what state does it enter until `notify()` or `notifyAll()` is called?
Q267 easy
Which of the following situations will most likely lead to an infinite `for` loop?
Q268 hard
Consider the Java snippet: `byte b = 10; char c = 'a'; int result = b + c;`. What is the value of `result`?
Q269 easy
What is the primary benefit of using inheritance in Java?
Q270 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("Hello");
        char[] arr = null;
        sb.insert(2, arr);
        System.out.println(sb);
    }
}
Q271 hard code error
What is the output of this code?
java
import java.io.File;

public class GetParentOfRoot {
    public static void main(String[] args) {
        File root = new File(File.separator); // Represents the root directory
        File parent = root.getParentFile();
        if (parent == null) {
            System.out.println("Parent is null.");
        } else {
            System.out.println("Parent path: " + parent.getAbsolutePath());
        }
    }
}
Q272 easy code error
What error will occur when attempting to deserialize an object from 'empty.ser' which was created as an empty file?
java
import java.io.*;

public class Main {
    public static void main(String[] args) {
        String filename = "empty.ser";
        try (FileOutputStream fos = new FileOutputStream(filename)) {
            // File is created but nothing is written to it
        } catch (IOException e) {
            System.err.println("Error creating file: " + e.getMessage());
        }

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
            Object obj = ois.readObject(); // Attempt to read from an empty stream
            System.out.println("Object deserialized.");
        } catch (IOException | ClassNotFoundException e) {
            System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        }
    }
}
Q273 medium code error
What error occurs when running this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("abc");
        String sub = sb.substring(0, 4);
        System.out.println(sub);
    }
}
Q274 hard
What is the outcome of attempting to cast a `null` reference to any non-primitive reference type, for example, `(String) null`?
Q275 easy code error
What will happen when this Java code is compiled?
java
public class MyClass {
    public static void main(String[] args) {
        int static = 10;
        System.out.println(static);
    }
}
Q276 hard code error
What is the error in this Java code snippet?
java
public class BuilderError {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Test");
        char[] chars = {'a', 'b', 'c', 'd', 'e'};
        sb.append(1, 2, chars);
        System.out.println(sb);
    }
}
Q277 medium
What is the key difference in focus between encapsulation and abstraction in object-oriented programming?
Q278 easy
Is `java.lang.StringBuilder` thread-safe?
Q279 medium code error
What kind of error will occur when compiling the following Java code, assuming 'some_file.txt' may or may not exist?
java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TestClass {
    public static void main(String[] args) {
        FileReader fr = new FileReader("some_file.txt");
        BufferedReader br = new BufferedReader(fr);
        try {
            br.readLine();
        } catch (IOException e) {
            System.err.println("IO Error");
        } finally {
            // Closing logic omitted
        }
    }
}
Q280 easy code error
What error occurs when `wait()` is called on an object without holding the object's lock?
java
public class Main {
    public static void main(String[] args) {
        Object lock = new Object();
        try {
            lock.wait(); // Calling wait() outside a synchronized block
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
← Prev 1213141516 Next → Page 14 of 200 · 3994 questions