OA Exams

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

Question 21

What is the output of System.out.println(“abc”.charAt(1));?

  • A. a
  • B. b
  • C. c
  • D. Error

Answer: B

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

Question 22

Which of the following statements is true regarding arrays in Java?

  • A. Arrays can hold primitive types only.
  • B. Arrays are fixed in size once created.
  • C. Arrays can be multi-dimensional.
  • D. Both B and C

Answer: D

Explanation: Arrays in Java are fixed in size and can also be multi-dimensional.

Question 23

What is the purpose of the super keyword?

  • A. To call a superclass constructor
  • B. To refer to the current object
  • C. To create a new object
  • D. To define a method

Answer: A

Explanation: The super keyword is used to call a constructor or method from a superclass.

Question 24

What does the default keyword indicate in a switch statement?

  • A. A value to be used if none of the cases match
  • B. A method that must be implemented
  • C. An access modifier
  • D. A keyword for creating constants

Answer: A

Explanation: The default keyword is used to specify a block of code that executes if no case matches in a switch statement.

Question 25

 What is the purpose of the throws keyword in method declarations?

  • A. To catch exceptions
  • B. To declare exceptions that may be thrown
  • C. To define a method as abstract
  • D. To indicate final variables

Answer: B

Explanation: The throws keyword is used in method declarations to indicate that a method may throw exceptions.

Question 26

How can you concatenate two strings in Java?

  • A. Using the concat() method
  • B. Using the + operator
  • C. Both A and B
  • D. Using StringBuilder

Answer: C

Explanation: You can concatenate strings using both the concat() method and the + operator.

Question 27

Which of the following is the correct syntax for an enhanced for loop?

  • A. for (Type var : collection) { … }
  • B. for (var : collection) { … }
  • C. for each (var : collection) { … }
  • D. for (Type var in collection) { … }

Answer: A

Explanation: The enhanced for loop syntax is for (Type var : collection) { ... }.

Question 28

What does the instanceof operator do in Java?

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

Answer: A

Explanation: The instanceof operator checks if an object is an instance of a specific class or subclass.

Question 29

How can you make a variable accessible across multiple classes?

  • A. By declaring it as private
  • B. By declaring it as public
  • C. By declaring it as static
  • D. By declaring it as final

Answer: B

Explanation: Declaring a variable as public allows it to be accessible from multiple classes.

Question 30

What is the purpose of a constructor overloading in Java?

  • A. To create multiple constructors with different parameter types
  • B. To allow creating an object without parameters
  • C. To improve performance
  • D. All of the above

Answer: D

Explanation: Constructor overloading allows multiple constructors with different parameter types, including default constructors without parameters.

Question 31

 Which of the following statements is used to declare a package in Java?

  • A. package com.example;
  • B. import com.example;
  • C. class com.example;
  • D. namespace com.example;

Answer: A

Explanation: The package statement is used to declare a package in Java.

Question 32

How do you throw an exception manually in Java?

  • A. throw new Exception(“Error message”);
  • B. throw Exception(“Error message”);
  • C. raise Exception(“Error message”);
  • D. new Exception(“Error message”);

Answer: A

Explanation: The correct syntax to throw an exception manually is throw new Exception("Error message");.

Question 33

What keyword is used to prevent a class from being subclassed?

  • A. final
  • B. static
  • C. private
  • D. protected

Answer: A

Explanation: The final keyword prevents a class from being subclassed.

Question 34

How do you retrieve the size of an ArrayList in Java?

  • A. arrayList.size();
  • B. arrayList.length;
  • C. arrayList.getSize();
  • D. arrayList.count();

Answer: A

Explanation: The size() method retrieves the number of elements in an ArrayList.

Question 35

 What will happen if you try to modify a string in Java?

  • A. The original string will change
  • B. A new string will be created
  • C. It will throw an error
  • D. Nothing will happen

Answer: B

Explanation: Strings in Java are immutable; modifying a string will create a new string instead of changing the original.

Question 36

Which of the following is a valid declaration of an enum in Java?

  • A. enum Colors { RED, GREEN, BLUE }
  • B. enum Colors[] { RED, GREEN, BLUE }
  • C. Colors { RED, GREEN, BLUE }
  • D. public Colors { RED, GREEN, BLUE }

Answer: A

Explanation: The correct way to declare an enum is enum Colors { RED, GREEN, BLUE }.

Question 37

How can you handle multiple exceptions in a single catch block?

  • A. catch (IOException | SQLException e) { … }
  • B. catch (IOException e) { … } catch (SQLException e) { … }
  • C. catch (IOException, SQLException e) { … }
  • D. Both A and B

Answer: D

Explanation: Both catch (IOException | SQLException e) { ... } and separate catch blocks handle multiple exceptions.

Question 38

What will be the output of System.out.println(10 + 5 + ” is the sum.”);?

  • A. 15 is the sum.
  • B. 10 + 5 is the sum.
  • C. 105 is the sum.
  • D. Error

Answer: A

Explanation: The expression evaluates to 15, resulting in "15 is the sum." due to string concatenation.

Question 39

Which method is used to convert a string to uppercase?

  • A. str.toUpperCase()
  • B. str.upper()
  • C. str.toUpper()
  • D. str.makeUpperCase()

Answer: A

Explanation: The toUpperCase() method converts a string to uppercase.

Question 40

How can you remove an element from an ArrayList?

  • A. arrayList.remove(index);
  • B. arrayList.delete(index);
  • C. arrayList.erase(index);
  • D. arrayList.clear(index);

Answer: A

Explanation: The remove(index) method is used to remove an element from an ArrayList.

Complete the Captcha to view next question set.

Tags

Prev Post
WGU D367 Practice Exam Questions – Set 5 – Part 1
Next Post
WGU D367 Practice Exam Questions – Set 5 – Part 2