OA Exams

  • web.groovymark@gmail.com
  • December 26, 2024

Question 41

Which of the following is a valid way to create an immutable class?

  • A. By declaring all fields as final
  • B. By providing no setter methods
  • C. By making the class final
  • D. All of the above

Answer: D

Explanation: All the options contribute to creating an immutable class.

Question 42

How do you create a synchronized block in Java?

  • A. synchronized void myMethod() {}
  • B. synchronized (object) {}
  • C. synchronized {}
  • D. Both A and B

Answer: D

Explanation: Both synchronized void myMethod() {} and synchronized (object) {} create synchronized blocks.

Question 43

What does the instanceof keyword do in Java?

  • A. Checks if an object is an instance of a class
  • B. Compares two objects
  • C. Casts an object to a class
  • D. Creates a new instance

Answer: A

Explanation: The instanceof keyword checks if an object is an instance of a specified class.

Question 44

Which method is used to remove all elements from a List in Java?

  • A. clear()
  • B. removeAll()
  • C. delete()
  • D. empty()

Answer: A

Explanation: The clear() method is used to remove all elements from a List.

Question 45

What is the output of System.out.println(“Hello” + 5 + 2);?

  • A. 7
  • B. Hello7
  • C. Hello52
  • D. Error

Answer: C

Explanation: The expression evaluates to "Hello52" due to string concatenation.

Question 46

Which of the following is a valid way to declare a map in Java?

  • A. Map<int, String> map = new HashMap<>();
  • B. Map<String, String> map = new HashMap<>();
  • C. Map<String, int> map = new HashMap<>();
  • D. Map<String> map = new HashMap<String>();

Answer: B

Explanation: The correct syntax to declare a map is Map<String, String> map = new HashMap<>();.

Question 47

 What is the purpose of the synchronized keyword in Java?

  • A. To allow only one thread to access a method
  • B. To declare a method as final
  • C. To stop a thread
  • D. To create a new thread

Answer: A

Explanation: The synchronized keyword is used to ensure that only one thread can access a method at a time.

Question 48

How do you check if a List contains a specific element in Java?

  • A. list.includes(element);
  • B. list.contains(element);
  • C. list.has(element);
  • D. list.indexOf(element);

Answer: B

Explanation: The contains() method is used to check if a List contains a specific element.

Question 49

What will the following code output? System.out.println(1 + “2” + 3);?

  • A. 6
  • B. 123
  • C. 32
  • D. 12

Answer: B

Explanation: The expression evaluates to "123" due to string concatenation.

Question 50

Which of the following is true about Java exceptions?

  • A. All exceptions must be caught.
  • B. Exceptions can be ignored.
  • C. Only runtime exceptions must be caught.
  • D. All exceptions must be handled.

Answer: C

Explanation: Only runtime exceptions do not need to be explicitly caught or declared.

Complete the Captcha to view next question set.

Tags

Prev Post
WGU D286 Practice Exam Questions – Set 3 – Part 3
Next Post
WGU D286 Practice Exam Questions – Set 4 – Part 1