OA Exams
What does the break statement do in a switch-case structure?
A. Terminates the switch statement
B. Exits the current loop
C. Breaks the program
D. Skips the next iteration
Answer: A
Explanation: The break statement terminates the switch-case structure, preventing fall-through to the next case.
Which of the following is a valid way to compare two strings for equality?
A. str1.equals(str2)
B. str1 == str2
C. str1.compareTo(str2) == 0
D. Both A and C
Answer: D
Explanation: Both str1.equals(str2) and str1.compareTo(str2) == 0 are valid methods for comparing strings for equality.
What will be the output of System.out.println(5 * 2 + 3);?
A. 8
B. 13
C. 10
D. 5
Answer: B
Explanation: The expression evaluates to 10 (5 * 2) + 3 = 13.
How can you declare a method that takes no arguments?
A. public void methodName() { … }
B. public void methodName(int x) { … }
C. void methodName;
D. methodName() { … }
Explanation: The correct syntax to declare a method that takes no arguments is public void methodName() { ... }.
What is the result of the following operation: int[] arr = {1, 2, 3}; arr[1] = 5; System.out.println(arr[1]);?
A. 1
B. 2
C. 3
Explanation: The second element (index 1) of the array is updated to 5, so the output is 5
Which of the following statements correctly creates a new object of a class named Car?
A. Car myCar = new Car();
B. Car myCar = Car();
C. new Car myCar();
D. Car myCar;
Explanation: The correct syntax to create a new object is Car myCar = new Car();.
How do you declare a method that returns an integer value?
A. int methodName() { … }
B. void methodName() { … }
C. methodName() int { … }
D. public int methodName() { … }
Explanation: The correct syntax to declare a method that returns an integer is public int methodName() { ... }.
What is the purpose of the import statement?
A. To create a new class
B. To include other classes and packages
C. To define a method
D. To declare a variable
Explanation: The import statement is used to include classes and packages from other libraries.
How do you create a multi-dimensional array in Java?
A. int[][] arr = new int[3][4];
B. int arr[][] = new int[3][4];
C. Both A and B
D. int[3][4] arr;
Answer: C
Explanation: Both int[][] arr = new int[3][4]; and int arr[][] = new int[3][4]; are valid ways to declare a multi-dimensional array.
What will happen if you try to access an index outside the bounds of an array?
A. It returns null
B. It crashes the program
C. It throws an ArrayIndexOutOfBoundsException
D. It returns 0
Explanation: Accessing an index that is out of bounds will throw an ArrayIndexOutOfBoundsException.
What does the continue statement do in a loop?
A. Exits the loop
B. Skips the current iteration and proceeds to the next
C. Terminates the program
D. Causes an error
Explanation: The continue statement skips the current iteration of the loop and continues with the next iteration.
What will be the output of System.out.println(“Hello”.length());?
A. 5
B. 6
C. Error
D. 0
Explanation: The length() method returns the number of characters in the string, which is 5 for "Hello".
How do you convert a string to an integer in Java?
A. Integer.valueOf(str);
B. Integer.parseInt(str);
D. str.toInt();
Explanation: Both Integer.valueOf(str); and Integer.parseInt(str); can convert a string to an integer.
Which of the following is the correct way to define an interface in Java?
A. public class MyInterface { … }
B. public interface MyInterface { … }
C. interface MyInterface { … }
D. Both B and C
Explanation: An interface can be defined using either public interface MyInterface { ... } or interface MyInterface { ... }.
What is the main purpose of the final keyword?
A. To define a variable that can change
B. To define a method that can be overridden
C. To define a constant value
D. To create an abstract class
Explanation: The final keyword is used to declare a constant value that cannot be modified.
How do you check if a string is empty in Java?
A. str.length() == 0
B. str.equals(“”)
C. str.isEmpty()
D. All of the above
Explanation: All options are valid ways to check if a string is empty in Java.
Which of the following statements is used to handle exceptions in Java?
A. try-catch
B. throw-catch
C. exception
D. finally
Explanation: The try-catch statement is used to handle exceptions in Java.
What is the output of the following code snippet? System.out.println(7 % 4);
A. 3
B. 1
C. 0
D. 7
Explanation: The % operator returns the remainder of the division, which is 3.
How do you define a constructor in Java?
A. It must have the same name as the class and no return type.
B. It must have a return type of void.
C. It can have any name.
D. It must be public.
Explanation: A constructor must have the same name as the class and does not have a return type.
Which of the following keywords is used to create a subclass in Java?
A. extends
B. implements
C. inherits
D. uses
Explanation: The extends keyword is used to create a subclass from a superclass.