Create New Post

Java MCQs - 15

  1. Consider the following code:
 public class Main {
    public static void main(String[] args) {
        String str = "Hello";
        StringBuilder sb = new StringBuilder(str);
        sb.append(" World");
        System.out.println(sb);
    }
}

What is the output of the above code?

A) Hello
B) World
C) Hello World
D) Compilation Error

Answer: C) Hello World

Explanation: The StringBuilder class allows mutable strings, and append() method appends the specified string to the end of the current string.

  1. Which of the following statements about Java interfaces is true?

A) Interfaces can contain implementation of methods.
B) An interface can extend multiple classes using the extends keyword.
C) Interfaces can have constructors.
D) Interface methods must be marked as static.

Answer: A) Interfaces can contain implementation of methods.

Explanation: Since Java 8, interfaces can contain default and static methods with implementation.

  1. Consider the following code:
 public class Main {
    public static void main(String[] args) {
        int x = 5;
        int y = x++ * 2 + ++x;
        System.out.println(y);
    }
}

What is the output of the above code?

A) 12
B) 15
C) 17
D) Compilation Error

Answer: C) 17

Explanation: x++ * 2 evaluates to 5 * 2, and ++x evaluates to 7. Therefore, y = 10 + 7 = 17.

  1. Which of the following statements about Java threads is true?

A) Threads are always executed sequentially.
B) Java threads always have the same priority.
C) Threads cannot be created in Java.
D) Threads share the same memory space.

Answer: D) Threads share the same memory space.

Explanation: Threads within the same process share the same memory space.

  1. Consider the following code:
 public class Main {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        System.out.println(str1 == str2);
    }
}

What is the output of the above code?

A) true
B) false
C) Compilation Error
D) Runtime Error

Answer: A) true

Explanation: Both str1 and str2 refer to the same string literal in the string pool, so == returns true.

  1. Which of the following statements about Java exceptions is true?

A) All exceptions in Java are checked exceptions.
B) Checked exceptions are subclasses of RuntimeException.
C) Unchecked exceptions must be caught using a try-catch block.
D) The throws keyword is used to handle exceptions.

Answer: B) Checked exceptions are subclasses of RuntimeException.

Explanation: Checked exceptions are subclasses of Exception, not RuntimeException.

  1. Consider the following code:
public class Main {
    public static void main(String[] args) {
        int x = 10;
        int y = 20;
        System.out.println("x + y = " + (x + y));
    }
}

What is the output of the above code?

A) x + y = 30
B) x + y = 1020
C) Compilation Error
D) Runtime Error

Answer: A) x + y = 30

Explanation: The expression (x + y) is evaluated before concatenating with the string.

  1. What is the purpose of the continue statement in Java?

A) To terminate the execution of a loop.
B) To skip the current iteration of a loop and continue with the next iteration.
C) To break out of a loop completely.
D) To restart the loop from the beginning.

Answer: B) To skip the current iteration of a loop and continue with the next iteration.

  1. Consider the following code:
 public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        for (int i = 0; i <= arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

What is the output of the above code?

A) 1 2 3
B) Compilation Error
C) Runtime Error
D) 1 2 3 ArrayIndexOutOfBoundsException

Answer: C) Runtime Error

Explanation: The loop tries to access arr[arr.length], which causes an ArrayIndexOutOfBoundsException.

  1. Which of the following statements about Java I/O streams is true?

A) Java I/O streams are only used for network communication.
B) Java I/O streams can be classified into three types: byte streams, character streams, and object streams.
C) Java I/O streams support only character-based input/output.
D) Java I/O streams are used only for reading data and not for writing.

Answer: B) Java I/O streams can be classified into three types: byte streams, character streams, and object streams.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

63296