-
web.groovymark@gmail.com
- December 26, 2024
Question 01
What is the default value of a char variable in Java?
- A. ‘0’
- B. ‘\u0000’
- C. ‘ ‘
- D. Error
Answer: B
Explanation: The default value of a char variable is '\u0000', which is the null character.
Question 02
Which of the following keywords is used to define an interface in Java?
- A. interface
- B. abstract
- C. class
- D. new
Answer: A
Explanation: The interface keyword is used to define an interface in Java.
Question 03
Which method in Java is used to start the execution of a thread?
- A. begin()
- B. run()
- C. start()
- D. execute()
Answer: C
Explanation: The start() method is used to begin the execution of a thread.
Question 04
How do you declare a multi-dimensional array in Java?
- A. int array[][];
- B. int[] array[];
- C. Both A and B
- D. int array{};
Answer: C
Explanation: Both int array[][]; and int[] array[]; are valid ways to declare a multi-dimensional array.
Question 05
What does the throw keyword do in Java?
- A. It handles exceptions.
- B. It creates a new exception.
- C. It throws an exception.
- D. It declares an exception.
Answer: C
Explanation: The throw keyword is used to explicitly throw an exception in Java.
Question 06
Which method is used to convert a string to lowercase in Java?
- A. toLower()
- B. toLowerCase()
- C. lowerCase()
- D. convertToLower()
Answer: B
Explanation: The toLowerCase() method is used to convert a string to lowercase.
Question 07
Which of the following is a valid declaration of a float variable?
- A. float f = 3.14;
- B. float f = 3.14f;
- C. float f = (float)3.14;
- D. Both B and C
Answer: D
Explanation: Both float f = 3.14f; and float f = (float)3.14; are valid ways to declare a float variable.
Question 08
What will happen if you try to access an array index that is out of bounds?
- A. It returns null.
- B. It returns 0.
- C. ArrayIndexOutOfBoundsException will be thrown.
- D. It returns an empty array.
Answer: C
Explanation: Accessing an array index that is out of bounds will result in an ArrayIndexOutOfBoundsException.
Question 09
How do you comment multiple lines in Java?
- A. // Comment
- B. /* Comment */
- C. # Comment
- D. Both A and B
Answer: B
Explanation: The /* Comment */ syntax is used for multi-line comments in Java.
Question 10
What is the output of System.out.println(10 != 5);?
- A. true
- B. false
- C. 10
- D. Error
Answer: A
Explanation: The expression evaluates to true since 10 is not equal to 5.
Question 11
Which of the following will correctly create an instance of the String class?
- A. String str = new String();
- B. String str = “Hello”;
- C. String str = String(“Hello”);
- D. Both A and B
Answer: D
Explanation: Both String str = new String(); and String str = "Hello"; are valid ways to create a String instance.
Question 12
What does the final keyword mean when applied to a method?
- A. The method cannot be overridden.
- B. The method cannot be called.
- C. The method is static.
- D. The method is abstract.
Answer: A
Explanation: A method declared with the final keyword cannot be overridden by subclasses.
Question 13
How do you declare a constant in Java?
- A. const int CONSTANT = 10;
- B. final int CONSTANT = 10;
- C. static int CONSTANT = 10;
- D. constant int CONSTANT = 10;
Answer: B
Explanation: You declare a constant in Java using the final keyword.
Question 14
Which of the following exceptions is thrown when attempting to convert a non-numeric string to a number?
- A. NullPointerException
- B. NumberFormatException
- C. ClassCastException
- D. ArithmeticException
Answer: B
Explanation: A NumberFormatException is thrown when attempting to convert a non-numeric string to a number.
Question 15
ow can you convert a string to an integer in Java?
- A. Integer.parseInt(string);
- B. string.toInteger();
- C. Integer.valueOf(string);
- D. Both A and C
Answer: D
Explanation: Both Integer.parseInt(string); and Integer.valueOf(string); can be used to convert a string to an integer.
Question 16
What is the primary purpose of the break statement?
- A. To exit a loop
- B. To skip an iteration
- C. To continue a loop
- D. To end the program
Answer: A
Explanation: The break statement is used to exit a loop immediately.
Question 17
How do you access the last element of an array named arr?
- A. arr[arr.length];
- B. arr[arr.length – 1];
- C. arr.last();
- D. arr.size();
Answer: B
Explanation: The last element of an array is accessed using arr[arr.length - 1];.
Question 18
hich keyword is used to indicate that a variable can be modified in a class?
- A. final
- B. static
- C. volatile
- D. none of the above
Answer: D
Explanation: By default, class variables can be modified unless specified with final.
Question 19
Which of the following statements is used to create a new object in Java?
- A. Object obj = new Object();
- B. Object obj;
- C. Object obj = Object();
- D. create Object obj;
Answer: A
Explanation: The correct syntax to create a new object is Object obj = new Object();.
Question 20
What does Math.pow(2, 3) return?
- A. 8
- B. 6
- C. 9
- D. 2
Answer: A
Explanation: Math.pow(2, 3) returns 8, which is 2 raised to the power of 3.