EXAM QUESTIONS AND ANSWERS
ALREADY PASSED
What are the main primitive data types in Java, and what are they used for?
✔✔ The main primitive data types in Java are `int` (for integers), `double` (for floating-point
numbers), `boolean` (for true/false values), `char` (for single characters), `byte`, `short`, `long`,
and `float`. These are used to store basic data values directly in memory.
Consider the following code segment:
```java
int x = 5;
int y = 2;
double result = x / y;
```
What is the value of `result`, and why?
1
, ✔✔ The value of `result` will be `2.0`. Since both `x` and `y` are integers, the division `x / y`
performs integer division, resulting in 2. The result is then implicitly cast to a double, but the
value remains 2.0.
How do you declare a constant in Java, and why is it useful?
✔✔ You declare a constant in Java using the `final` keyword, like so: `final int
CONSTANT_NAME = 10;`. Constants are useful because they make your code more readable
and maintainable, ensuring that the value cannot be changed once assigned.
What is the difference between `float` and `double` in Java?
✔✔ `float` is a 32-bit floating-point data type, while `double` is a 64-bit floating-point data type.
`double` provides more precision and is generally used when more accurate decimal values are
required.
Consider the following code segment:
```java
boolean isValid = (3 + 4 > 5) && (10 % 3 == 1);
2