Java Mcq Series -5 Hard Level

Here Java Mcq Hard Level

1. What is the result of the following Java code?

public class Main { public static void main(String[] args) { String str = "Java"; str.concat(" is awesome!"); System.out.println(str); } }

Answer: B) “Java”

  • Explanation: In Java, strings are immutable. The concat method returns a new string but does not modify the original string. Therefore, str remains “Java.”

2. What is the purpose of the transient keyword in Java when applied to an instance variable?

  • A) It specifies that the variable is constant.
  • B) It marks the variable as non-accessible.
  • C) It indicates that the variable should not be serialized.
  • D) It prevents the variable from being modified.

Answer: C) It indicates that the variable should not be serialized.

  • Explanation: When the transient keyword is applied to an instance variable, it indicates that the variable should not be included in the object’s serialized form.

3. What is a lambda expression in Java used for?

  • A) To create anonymous classes
  • B) To declare variables
  • C) To define constants
  • D) To define inline implementations of functional interfaces

Answer: D) To define inline implementations of functional interfaces.

  • Explanation: Lambda expressions in Java are used to create inline implementations of functional interfaces, allowing for concise and expressive code.

4. What is the output of the following code snippet?

java

public class Main { public static void main(String[] args) { String str1 = "Java"; String str2 = "Java"; System.out.println(str1 == str2); } }

Answer: A) true

  • Explanation: In this code, both str1 and str2 refer to the same string literal in the string pool, so str1 == str2 evaluates to true.

5. Which data structure in Java provides a LIFO (Last-In-First-Out) order of elements?

  • A) Queue
  • B) LinkedList
  • C) Stack
  • D) ArrayList

Answer: C) Stack

  • Explanation: A Stack in Java follows the LIFO order, where the last element added is the first one to be removed.

6. What is the purpose of the volatile keyword in Java when applied to a variable?

  • A) It specifies that the variable is constant.
  • B) It ensures that the variable is visible to all threads and prevents caching.
  • C) It prevents the variable from being modified.
  • D) It makes the variable a global variable.

Answer: B) It ensures that the variable is visible to all threads and prevents caching.

  • Explanation: The volatile keyword in Java is used to declare variables that are shared among multiple threads, ensuring visibility and preventing thread-local caching.

7. What is the result of the following code?

java

public class Main { public static void main(String[] args) { int x = 5; x = x++ + ++x; System.out.println(x); } }

Answer: D) 12

  • Explanation: The expression x++ first uses the current value of x (5) and then increments x. The expression ++x increments x first and then uses the updated value. So, the calculation is 5 + 7, resulting in 12.

8. In Java, what is a checked exception?

  • A) An exception that is checked at compile time
  • B) An exception that is checked at runtime
  • C) An exception that is never checked
  • D) An exception that cannot be caught

Answer: A) An exception that is checked at compile time.

  • Explanation: Checked exceptions in Java are exceptions that are checked at compile time, and the code must handle them or declare them using the throws keyword.

9. What is the purpose of the assert statement in Java?

  • A) To declare variables
  • B) To create loops
  • C) To handle exceptions
  • D) To perform assertions and check conditions

Answer: D) To perform assertions and check conditions.

  • Explanation: The assert statement in Java is used to perform assertions and check conditions, allowing you to catch programming errors during testing.

10. Which of the following Java collections does not allow duplicate elements?

diff

- A) ArrayList

- B) HashSet

- C) LinkedList

- D) TreeMap

Answer: B) HashSet

  • Explanation: A HashSet in Java does not allow duplicate elements and stores elements in an unordered manner.

11. In Java, what is the purpose of the super keyword when used in a constructor?

diff

- A) It calls the constructor of the current class.

- B) It calls a method in the superclass.

- C) It initializes instance variables.

- D) It calls the constructor of the superclass.

Answer: D) It calls the constructor of the superclass.

  • Explanation: In a constructor, the super keyword is used to call the constructor of the superclass.

12. What is the output of the following code snippet?

java

public class Main { public static void main(String[] args) { int x = 5; int y = 10; int z = x > y ? x : y; System.out.println(z); } }

Answer: B) 10

  • Explanation: This code uses the ternary conditional operator (? :) to assign the value of x to z if x is greater than y, otherwise, it assigns the value of y to z. Since x is not greater than y, z is assigned the value of y, which is 10.

13. Which Java keyword is used to define a constant?

diff

- A) static

- B) final

- C) constant

- D) const

Answer: B) final

  • Explanation: In Java, the final keyword is used to define constants.

14. What is the purpose of the try-with-resources statement in Java?

css

- A) To declare variables

- B) To create loops

- C) To handle exceptions and automatically close resources

- D) To define inner classes

Answer: C) To handle exceptions and automatically close resources.

  • Explanation: The try-with-resources statement in Java is used for handling exceptions and automatically closing resources, such as files or streams, when they are no longer needed.

15. What is the output of the following code snippet?

java

public class Main { public static void main(String[] args) { int result = 0; for (int i = 0; i < 5; i++) { result += i; } System.out.println(result); } }

Answer: A) 10

  • Explanation: This code calculates the sum of integers from 0 to 4 and assigns it to the result variable. The sum is 0 + 1 + 2 + 3 + 4, which equals 10.

16. What is the purpose of the default case in a switch statement in Java?

css

- A) To specify the first case

- B) To specify the last case

- C) To provide a default action when none of the other cases match

- D) To indicate that the switch statement has no cases

Answer: C) To provide a default action when none of the other cases match.

  • Explanation: The default case in a switch statement in Java is used to provide a default action to be executed when none of the other cases match the value being switched.

17. What is the result of the following code?

java

public class Main { public static void main(String[] args) { int x = 10; int y = x++ + ++x; System.out.println(y); } }

Answer: C) 23

  • Explanation: The expression x++ first uses the current value of x (10) and then increments x. The expression ++x increments x first and then uses the updated value. So, the calculation is 10 + 12, resulting in 22.

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

sql

- A) Threads are always executed sequentially.

- B) A thread can be in only one state at a time.

- C) Threads cannot communicate with each other.

- D) Java supports both user-level and kernel-level threads.

Answer: B) A thread can be in only one state at a time.

  • Explanation: In Java, a thread can be in only one state at any given time, such as running, blocked, or waiting.

19. What is the output of the following code?

java

public class Main { public static void main(String[] args) { int x = 5; System.out.println(++x); } }

Answer: B) 6

  • Explanation: The ++x expression increments the value of x by 1 and then returns the updated value, which is 6.

20. What is a Java annotation used for?

css

- A) To declare variables - B) To create loops - C) To provide metadata and information about code elements - D) To define inner classes

Answer: C) To provide metadata and information about code elements.

  • Explanation: Java annotations are used to provide metadata and information about code elements, such as classes, methods, and fields, to aid in various tasks like documentation, code analysis, and runtime processing.

Leave a Comment

error: Content is protected !!