(1) Primitive Types
(2) Using Objects
(3) Boolean Expressions and if Statements
(4) Iteration
(5) Writing Classes
100

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.

100

How do you concatenate two String objects in Java?

Using the + operator or the .concat() method.

100

What operator is used to perform logical AND in Java?

&&

100

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

100

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.

200

What is the result of the expression 5 / 2 when both operands are integers?

2

200

What method can you use to check if a String starts with a specific substring?

.startsWith()

200

What is the result of the following expression if x = 5 and y = 10?

x >= 5 || y < 5


true

200

Which type of loop is most appropriate when you know the number of iterations in advance?

for loop

200

What is a getter method in Java?

A getter method is used to retrieve the value of an instance variable.

300

What is the difference between primitive and reference data types?

Primitive data types store values directly, while reference types store addresses to objects.

300

What does the indexOf(String str) method return if the substring is not found in a String?

-1

300

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)

300

What will the following code output if x = 0?

do {
    System.out.println(x);
    x++;
} while (x < 3);


0, 1, 2

300

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.

400

What keyword is used to declare a variable that cannot be changed once assigned?

final

400

What is the difference between == and .equals() when comparing String objects?

== compares memory references, while .equals() compares the actual content of the strings.

400

What is the result of the following expression if x = 4 and y = 8?
(x < y && y > 10) || (x == 4)

true

400

How do you exit a loop early in Java?

break statement

400

What is the difference between an instance method and a static method in Java?

  • An instance method operates on an instance of the class and can access instance variables.
  • A static method belongs to the class itself and can be called without creating an instance. It can only access static variables.
500

What primitive type would you use to store a single character, and how many bytes of memory does it use?

char and 2 bytes

500

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".

500

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.

500

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.

500

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.