1Z0-811 Java Language Fundamentals 1 — Questions and Answers
Question 1: What will be the output of the following code snippet? <br> int a = 10; <br> int b = 5; <br> System.out.println(a % b);
- 2
- 0 (Correct answer)
- 5
- 10
Correct answer: 0
The '%' operator in Java is the modulo operator, which calculates the remainder of a division. In the given code, `a` is 10 and `b` is 5. When 10 is divided by 5, the result is 2 with a remainder of 0. Therefore, `a % b` evaluates to 0.
Question 2: Which of the following is true about the "switch" statement in Java?
- switch statements can only be used with integer values
- Each case block must end with a break statement
- switch statements can be used with String values starting from Java SE 7 (Correct answer)
- switch statements can only have int and char types as case labels
Correct answer: switch statements can be used with String values starting from Java SE 7
Prior to Java SE 7, `switch` statements could only evaluate expressions of type `byte`, `short`, `char`, or `int`, or their corresponding wrapper classes, and enums. However, with Java SE 7, the capability was extended to allow `String` objects as the expression in a `switch` statement. This enhancement provides more flexibility and cleaner code for string-based conditional logic.
Question 3: What is the result of the following code? int x = 5; <br> x += 3; <br> System.out.println(x);
- 5
- 8 (Correct answer)
- 3
- 15
Correct answer: 8
The code initializes an integer variable `x` with the value 5. The line `x += 3;` is a shorthand assignment operator equivalent to `x = x + 3;`. This operation adds 3 to the current value of `x`, so `x` becomes `5 + 3`, which is 8. Finally, `System.out.println(x)` prints the updated value of `x`, which is 8.
Question 4: How does Java handle integer division when both operands are integers?
- It performs floating-point division
- It performs integer division and truncates the result (Correct answer)
- It throws an ArithmeticException
- It performs multiplication
Correct answer: It performs integer division and truncates the result
In Java, when both operands of a division operation are integers, the division performed is integer division. This means that any fractional part of the result is discarded or truncated, effectively rounding towards zero. For example, `7 / 3` would result in `2`, not `2.33` or `3`.
Question 5: Which of the following statements about Java methods is true?
- Methods in Java can be declared without a return type
- Methods can only have one return statement
- A method can be overloaded with different parameter types (Correct answer)
- Methods must always include at least one parameter
Correct answer: A method can be overloaded with different parameter types
Method overloading in Java allows a class to have multiple methods with the same name, provided they have different parameter lists. This difference can be in the number of parameters, the data types of the parameters, or the order of the parameter types. The return type alone is not sufficient to overload a method, but different parameter types enable distinct method signatures.
What will be the output of the following code snippet?
int a = 10;
int b = 5;
System.out.println(a % b);