OA Exams
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.
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.
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;
Explanation: The correct way to declare and initialize a single-dimensional array is int arr[] = new int[10];.
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() {}
Explanation: The correct way to define a method is void myMethod() {}.
What is the output of System.out.println(10 % 3);?
A. 3
B. 1
C. 0
D. 10
Explanation: The modulus operator % returns the remainder of the division, which is 1 in this case.
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.
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
Explanation: Exceptions are handled in Java using try-catch blocks.
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
Explanation: An ArrayList is a resizable array implementation of the List interface.
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.
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
Explanation: You can create a thread by either extending the Thread class or implementing the Runnable interface.
What is the output of System.out.println(3 * 2 + 1);?
A. 7
B. 8
C. 6
D. 9
Explanation: The expression evaluates to 7 due to operator precedence.
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
Explanation: Both final int CONSTANT; and static final int CONSTANT = 100; are valid ways to declare constants.
How do you check if a String is empty in Java?
A. str.isEmpty();
B. str.length() == 0;
C. str.equals(“”);
Explanation: All methods can be used to check if a string is empty.
Which exception is thrown when you divide by zero in Java?
A. ArithmeticException
B. NullPointerException
C. ClassCastException
D. IndexOutOfBoundsException
Explanation: An ArithmeticException is thrown when dividing by zero.
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
Explanation: The default keyword provides a default implementation for a method in an interface.
Which of the following collections allows duplicate elements?
A. Set
B. List
C. Map
Explanation: A List allows duplicate elements, while a Set does not.
What will the following code output? System.out.println(“Hello”.charAt(1));?
A. H
B. e
C. l
D. o
Explanation: The charAt(1) method returns the character at index 1, which is 'e'.
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
Explanation: An immutable class is typically created using private fields and no setter methods.
Which of the following is not a type of loop in Java?
A. for
B. while
C. do-while
D. repeat-until
Explanation: repeat-until is not a loop type in Java.
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
Explanation: The try block is used to execute code that may throw an exception.