-
web.groovymark@gmail.com
- December 26, 2024
Question 01
Which keyword is used to inherit a class in Java?
- A. implement
- B. extends
- C. inherit
- D. includes
Answer: B
Explanation: The extends keyword is used to inherit a class in Java.
Question 02
What will the following code output? System.out.println(5 + 10 + ” is the result.”);
- A. 15 is the result.
- B. 510 is the result.
- C. 5 + 10 is the result.
- D. Error
Answer: A
Explanation: The expression evaluates to "15 is the result." due to string concatenation.
Question 03
How do you declare a constant in Java?
- A. const int MY_CONSTANT = 10;
- B. final int MY_CONSTANT = 10;
- C. static int MY_CONSTANT = 10;
- D. constant int MY_CONSTANT = 10;
Answer: B
Explanation: You declare a constant in Java using the final keyword.
Question 04
Which of the following is not a valid data type in Java?
- A. int
- B. float
- C. string
- D. boolean
Answer: C
Explanation: string is not a valid data type; the correct type is String (with an uppercase 'S').
Question 05
What is the output of System.out.println(2 * 3 + 5);?
- A. 11
- B. 16
- C. 8
- D. 10
Answer: A
Explanation: The expression evaluates to 2 * 3 = 6, then 6 + 5 = 11.
Question 06
How can you make a method return multiple values in Java?
- A. By using an array
- B. By using a collection
- C. By creating a custom object
- D. All of the above
Answer: D
Explanation: You can return multiple values by using an array, a collection, or a custom object.
Question 07
What does the main method in Java look like?
- A. public static void main(String args[])
- B. public void main(String[] args)
- C. static void main(String args[])
- D. public static void main(String… args)
Answer: A
Explanation: The correct signature for the main method is public static void main(String[] args).
Question 08
What is the purpose of the break statement in loops?
- A. To exit the loop
- B. To skip to the next iteration
- C. To end the program
- D. To terminate the current method
Answer: A
Explanation: The break statement is used to exit a loop immediately.
Question 09
Which of the following correctly defines an abstract class in Java?
- A. abstract class MyClass { }
- B. class MyClass abstract { }
- C. abstract MyClass { }
- D. MyClass class abstract { }
Answer: A
Explanation: The correct syntax to define an abstract class is abstract class MyClass { }.
Question 10
Which statement is used to handle exceptions in Java?
- A. handle
- B. catch
- C. error
- D. throw
Answer: B
Explanation: The catch statement is used to handle exceptions in Java.
Question 11
How do you create a new thread in Java?
- A. Thread t = new Thread();
- B. new Thread();
- C. Thread t = new Thread(new Runnable());
- D. All of the above
Answer: D
Explanation: All the options represent ways to create a new thread in Java.
Question 12
What is the purpose of the static modifier in Java?
- A. To create a class variable
- B. To define an instance method
- C. To prevent a class from being instantiated
- D. Both A and C
Answer: D
Explanation: The static modifier can create class variables and can also indicate that a method or class cannot be instantiated.
Question 13
Which interface provides a way to handle input from the user in Java?
- A. Scanner
- B. Reader
- C. Writer
- D. InputStream
Answer: A
Explanation: The Scanner class provides methods to read input from the user.
Question 14
What is an instance variable?
- A. A variable shared among all instances of a class
- B. A variable defined within a method
- C. A variable specific to each instance of a class
- D. A constant variable
Answer: C
Explanation: An instance variable is specific to each instance of a class and maintains its own state.
Question 15
Which of the following statements is used to import a package in Java?
- A. package java.util.*;
- B. import java.util.*;
- C. using java.util.*;
- D. include java.util.*;
Answer: B
Explanation: The import statement is used to include a package in Java.
Question 16
What will the following code output? System.out.println(10 == 10);
- A. true
- B. false
- C. 10
- D. Error
Answer: A
Explanation: The expression evaluates to true since 10 is equal to 10.
Question 17
How can you create a copy of an array in Java?
- A. array.clone();
- B. Arrays.copyOf(array, length);
- C. Both A and B
- D. array.copy();
Answer: C
Explanation: Both methods array.clone() and Arrays.copyOf(array, length) can create a copy of an array.
Question 18
What is the output of the following code? System.out.println(“Hello” + ” World!”);
- A. HelloWorld!
- B. Hello World!
- C. Hello + World!
- D. Error
Answer: B
Explanation: The + operator concatenates the strings, resulting in "Hello World!".
Question 19
What does the return statement do in a method?
- A. Exits the method
- B. Sends a value back to the caller
- C. Both A and B
- D. Ends the program
Answer: C
Explanation: The return statement exits the method and sends a value back to the caller.
Question 20
Which of the following is not a valid access modifier in Java?
- A. public
- B. protected
- C. private
- D. hidden
Answer: D
Explanation: hidden is not a valid access modifier in Java.