What is the difference between a float and a double in Java?
A float has a precision of 7 decimal digits and is 32 bits
A double has a precision of 15-16 decimal digits and is 64 bits.
How do you concatenate two String objects in Java?
Using the + operator or the .concat() method.
What operator is used to perform logical AND in Java?
&&
What is the output of this while loop if x = 5 at the start?
while (x > 0) {
System.out.println(x);
x--;
}
5, 4, 3, 2, 1
How do you define a constructor in a Java class?
A constructor is defined with the same name as the class and without a return type.
What is the result of the expression 5 / 2 when both operands are integers?
2
What method can you use to check if a String starts with a specific substring?
.startsWith()
What is the result of the following expression if x = 5 and y = 10?
x >= 5 || y < 5
true
Which type of loop is most appropriate when you know the number of iterations in advance?
for loop
What is a getter method in Java?
A getter method is used to retrieve the value of an instance variable.
What is the difference between primitive and reference data types?
Primitive data types store values directly, while reference types store addresses to objects.
What does the indexOf(String str) method return if the substring is not found in a String?
-1
What does the ! (logical NOT) operator do in a Boolean expression?
It negates the value of the Boolean expression (flips true to false and vice versa)
What will the following code output if x = 0?
0, 1, 2
What is the purpose of a setter method in Java?
A setter method is used to set or modify the value of an instance variable.
What keyword is used to declare a variable that cannot be changed once assigned?
final
What is the difference between == and .equals() when comparing String objects?
== compares memory references, while .equals() compares the actual content of the strings.
What is the result of the following expression if x = 4 and y = 8?
(x < y && y > 10) || (x == 4)
true
How do you exit a loop early in Java?
break statement
What is the difference between an instance method and a static method in Java?
What primitive type would you use to store a single character, and how many bytes of memory does it use?
char and 2 bytes
What is the result of the following code snippet if str1 and str2 are both String objects containing the value "hello"?
String str1 = "hello";
String str2 = new String("hello");
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
The first output will be false because == compares memory references, and str1 and str2 are different objects.
The second output will be true because .equals() compares the content of the strings, and they are both "hello".
What is the result of using the = operator instead of == in a Boolean expression in Java?
Using = (the assignment operator) instead of == (the equality operator) in a Boolean expression will result in a compilation error when trying to compare two values, because = is used for assignment, not for comparison. If mistakenly used in an if statement, it could also lead to unintended behavior by assigning a value to a variable instead of performing a comparison.
In the following loop, what is the purpose of i++?
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
i++ increments the value of i by 1 after each iteration, controlling how many times the loop executes.
What does it mean when a method in a class is marked as void?
A method marked as void means that it does not return any value. It performs some action but does not provide a result to the caller.