OA Exams
What is the purpose of the void keyword in a method declaration?
A. Indicates a return value
B. Indicates no return value
C. Indicates a static method
D. Indicates an abstract method
Answer: B
Explanation: The void keyword indicates that the method does not return a value.
What does the import statement do in Java?
A. Creates a new class
B. Includes a package or class for use
C. Declares variables
D. Defines methods
Explanation: The import statement allows the use of classes and packages in the current file.
What is a package in Java?
A. A group of related classes
B. A type of data structure
C. A method of storing data
D. A collection of methods
Answer: A
Explanation: A package is a namespace that organizes a set of related classes and interfaces.
Which of the following keywords is used to inherit a class in Java?
A. implements
B. extends
C. inherits
D. uses
Explanation: The extends keyword is used to indicate inheritance in Java.
What is the output of the following code snippet: System.out.println(10 / 3);?
A. 3
B. 3.3333
C. 3.0
D. 10/3
Explanation: Integer division results in the quotient without the remainder, so the output is 3.
How do you define a constant variable in Java?
A. const int MAX_VALUE = 100;
B. static int MAX_VALUE = 100;
C. final int MAX_VALUE = 100;
D. int MAX_VALUE = 100;
Answer: C
Explanation: The final keyword is used to declare a constant variable in Java.
Which method is used to compare two strings in a case-insensitive manner?
A. str1.equals(str2);
B. str1.compareTo(str2);
C. str1.equalsIgnoreCase(str2);
D. str1.compareToIgnoreCase(str2);
Explanation: The equalsIgnoreCase method is used to compare two strings while ignoring case.
What is the purpose of the this keyword in Java?
A. Refers to the current object
B. Declares a static variable
C. Calls a method
D. Creates a new instance
Explanation: The this keyword refers to the current object instance in a class.
How can you declare a method that does not return any value?
A. public void methodName() { … }
B. public methodName() { … }
C. public void methodName;
D. methodName() { … }
Explanation: The correct syntax to declare a method that does not return a value is public void methodName() { ... }.
What exception is thrown when trying to divide by zero in Java?
A. NullPointerException
B. ArithmeticException
C. ArrayIndexOutOfBoundsException
D. ClassCastException
Explanation: An ArithmeticException is thrown when attempting to divide by zero.
Which of the following statements is used to create a new instance of a class?
A. ClassName instanceName;
B. ClassName instanceName = new ClassName();
C. ClassName instanceName = ClassName();
D. new ClassName instanceName;
Explanation: The correct way to create a new instance of a class is by using ClassName instanceName = new ClassName();.
What does the length property of an array return?
A. The total memory allocated for the array
B. The number of elements in the array
C. The maximum value of the array
D. The minimum value of the array
Explanation: The length property returns the number of elements in the array.
How do you access the first element of an array named myArray?
A. myArray[1];
B. myArray[0];
C. myArray.first();
D. myArray.get(0);
Explanation: Array indices in Java are zero-based, so the first element is accessed with myArray[0];
Which of the following statements is used to read input from the keyboard in Java?
A. Scanner scnr = new Scanner(System.in);
B. Input scnr = new Input();
C. Read scnr = new Read();
D. Console scnr = new Console();
Explanation: The Scanner class is used to read input from the keyboard, instantiated with new Scanner(System.in);.
What is the result of the expression 5 == 5 in Java?
A. True
B. False
C. Error
D. Undefined
Explanation: The expression evaluates to true because both sides are equal.
Which of the following is NOT a valid way to create an array in Java?
A. int[] arr = new int[10];
B. int[] arr = {1, 2, 3};
C. int arr[] = new int[5];
D. int arr = new int[5];
Answer: D
Explanation: int arr = new int[5]; is invalid because it lacks array brackets.
How can you declare a two-dimensional array in Java?
A. int[][] arr = new int[5][5];
B. int arr[][] = new int[5][5];
C. int arr[5][5];
D. Both A and B
Explanation: Both int[][] arr = new int[5][5]; and int arr[][] = new int[5][5]; are valid declarations for a two-dimensional array.
What is the output of System.out.println(“Java”.charAt(0));?
A. J
B. a
C. Java
D. Error
Explanation: The charAt(0) method returns the character at index 0, which is 'J'.
Which of the following methods can be used to compare two strings lexicographically?
D. str1.contains(str2);
Explanation: The compareTo method compares two strings lexicographically.
What does the toUpperCase() method do in Java?
A. Converts a string to lowercase
B. Converts a string to uppercase
C. Trims whitespace from a string
D. Splits a string into an array
Explanation: The toUpperCase() method converts all characters in the string to uppercase.