OA Exams

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

Question 01

What is the purpose of the static keyword in Java?

A. It allows a method to be overridden.

B. It allows a variable to be shared across all instances of a class.

C. It restricts access to a variable.

D. It makes a method abstract.

Answer: B

Explanation: The static keyword allows a variable to be shared across all instances of a class.

Question 02

Which of the following is a method of the String class?

A. split()

B. join()

C. merge()

D. concatenate()

Answer: A

Explanation: The split() method is a valid method of the String class used to divide a string into substrings.

Question 03

How do you declare a single-dimensional array in Java?

A. int arr[] = new int[10];

B. int arr = new int[10];

C. int arr(10);

D. int[] arr;

Answer: A

Explanation: The correct way to declare and initialize a single-dimensional array is int arr[] = new int[10];.

Question 04

Which of the following is a valid way to define a method in Java?

A. public void myMethod;

B. void myMethod() {}

C. public myMethod() {}

D. myMethod() {}

Answer: B

Explanation: The correct way to define a method is void myMethod() {}.

Question 05

What is the output of System.out.println(10 % 3);?

A. 3

B. 1

C. 0

D. 10

Answer: B

Explanation: The modulus operator % returns the remainder of the division, which is 1 in this case.

Question 06

Which of the following types can be used in a switch statement?

A. int

B. String

C. char

D. All of the above

Answer: D

Explanation: You can use int, String, char, and other types that are compatible with switch statements.

Question 07

How do you handle exceptions in Java?

A. using try-catch blocks

B. using if-else statements

C. using switch-case statements

D. using assertions

Answer: A

Explanation: Exceptions are handled in Java using try-catch blocks.

Question 08

What is an ArrayList in Java?

A. A fixed-size array

B. A resizable array implementation of the List interface

C. A data structure for sorting

D. A class for primitive data types

Answer: B

Explanation: An ArrayList is a resizable array implementation of the List interface.

Question 09

Which of the following is not a Java access modifier?

A. public

B. private

C. package

D. protected

Answer: C

Explanation: package is not an access modifier in Java; the correct term is package-private, which is the default access level.

Question 10

How do you create a thread in Java?

A. By extending the Thread class

B. By implementing the Runnable interface

C. Both A and B

D. By using the ThreadPoolExecutor class

Answer: C

Explanation: You can create a thread by either extending the Thread class or implementing the Runnable interface.

Question 11

What is the output of System.out.println(3 * 2 + 1);?

A. 7

B. 8

C. 6

D. 9

Answer: A

Explanation: The expression evaluates to 7 due to operator precedence.

Question 12

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

A. final int CONSTANT;

B. const int CONSTANT = 100;

C. static final int CONSTANT = 100;

D. Both A and C

Answer: D

Explanation: Both final int CONSTANT; and static final int CONSTANT = 100; are valid ways to declare constants.

Question 13

How do you check if a String is empty in Java?

A. str.isEmpty();

B. str.length() == 0;

C. str.equals(“”);

D. All of the above

Answer: D

Explanation: All methods can be used to check if a string is empty.

Question 14

Which exception is thrown when you divide by zero in Java?

A. ArithmeticException

B. NullPointerException

C. ClassCastException

D. IndexOutOfBoundsException

Answer: A

Explanation: An ArithmeticException is thrown when dividing by zero.

Question 15

What is the purpose of the default keyword in an interface?

A. To indicate a method is not implemented

B. To provide a default implementation of a method

C. To prevent method overriding

D. To declare constants

Answer: B

Explanation: The default keyword provides a default implementation for a method in an interface.

Question 16

Which of the following collections allows duplicate elements?

A. Set

B. List

C. Map

D. All of the above

Answer: B

Explanation: A List allows duplicate elements, while a Set does not.

Question 17

What will the following code output? System.out.println(“Hello”.charAt(1));?

A. H

B. e

C. l

D. o

Answer: B

Explanation: The charAt(1) method returns the character at index 1, which is 'e'.

Question 18

How do you create an immutable class?

A. By using private fields and no setter methods

B. By using final methods

C. By using static methods

D. Both A and B

Answer: A

Explanation: An immutable class is typically created using private fields and no setter methods.

Question 19

Which of the following is not a type of loop in Java?

A. for

B. while

C. do-while

D. repeat-until

Answer: D

Explanation: repeat-until is not a loop type in Java.

Question 20

What is the purpose of the try block?

A. To define an exception handler

B. To execute code that may throw an exception

C. To catch exceptions

D. To throw exceptions

Answer: B

Explanation: The try block is used to execute code that may throw an exception.

Complete the Captcha to view next question set.

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