
10 Powerful Java Interview Questions to Supercharge Your Success || Quiz -1
Java Interview Questions
Java Interview Questions Quiz
Question 1:
What is the output of the following code snippet?
public class Example {
public static void main(String[] args) {
int x = 5;
int y = 2;
double result = x / y;
System.out.println(result);
}
}
A) 2.5
B) 2.0
C) 2
D) Compilation Error
Question 2:
Which of the following access modifiers allows a method to be accessed from any class in the same package?
A) public
B) private
C) protected
D) default (no modifier)
Question 3:
What is the primary purpose of the break
statement in Java?
A) To exit a loop or switch statement
B) To jump to a specific label in the code
C) To continue to the next iteration of a loop
D) To terminate the program
Question 4:
Which data type is used to represent single characters in Java?
A) char
B) string
C) Character
D) int
Question 5:
What is the correct way to declare and initialize a constant in Java?
A) constant int MAX_VALUE = 100;
B) final int MAX_VALUE = 100;
C) static const int MAX_VALUE = 100;
D) const MAX_VALUE = 100;
Question 6:
Which of the following is not a valid Java identifier?
A) _variableName
B) 1stValue
C) $value
D) myValue
Question 7:
What is the purpose of the toString
method in Java?
A) To convert a string to an integer
B) To convert an object to its string representation
C) To convert a double to a float
D) To concatenate two strings
Question 8:
Which of the following Java statements is used to allocate memory for an object?
A) new
B) malloc
C) allocate
D) create
Question 9:
What is the result of the following code snippet?
String str1 = "Hello";
String str2 = new String("Hello");
boolean result = (str1 == str2);
System.out.println(result);
A) true
B) false
C) Compilation Error
D) Runtime Error
Question 10:
In Java, what is the purpose of the finally
block in a try-catch-finally construct?
A) To specify the exception type to catch
B) To execute code regardless of whether an exception is thrown or not
C) To handle exceptions
D) To terminate the program
Answers and Explanations: || Java Interview Questions
- Answer: B) 2.0
Explanation: In Java, when dividing two integers, the result is an integer unless one or both of the operands are explicitly cast to a floating-point type. - Answer: D) default (no modifier)
Explanation: The default access modifier allows a method to be accessed from any class in the same package. - Answer: A) To exit a loop or switch statement
Explanation: Thebreak
statement is used to exit a loop or switch statement prematurely. - Answer: A) char
Explanation: Thechar
data type is used to represent single characters in Java. - Answer: B)
final int MAX_VALUE = 100;
Explanation: To declare a constant in Java, you use thefinal
keyword. - Answer: B) 1stValue
Explanation: Variable names in Java cannot start with a digit. - Answer: B) To convert an object to its string representation
Explanation: ThetoString
method is used to provide a string representation of an object. - Answer: A)
new
Explanation: In Java, thenew
keyword is used to allocate memory for an object. - Answer: B) false
Explanation:str1
andstr2
are two different String objects, sostr1 == str2
evaluates to false. - Answer: B) To execute code regardless of whether an exception is thrown or not
Explanation: Thefinally
block is used to specify code that should be executed regardless of whether an exception is thrown or not in a try-catch block.
Post Comment