Chapter 2.2: Syntax Rules
Multiple Choice · Compilation
Question 1: What Does javac Produce?
javac HelloWorld.java — what happens when this command completes without errors?
B: javac translates source code not into the processor's machine language but into JVM bytecode and writes it to the .class file. Running the program is a separate command: java HelloWorld. The same .class file runs on the JVM of every platform, so there is no need to recompile for each operating system.
Spot the Bug · Compilation
Question 2: Compilation Errors
The code is saved to the file hello.java and compiled with javac hello.java. Find the errors in the code. Does the compiler report them all at once? What does the corrected program print?
public class Hello { public static void main(String[] args) { int number = 5 system.out.println("Number: " + Number); } }
Show answer
- Line 3:
;is missing →error: ';' expected. This is a syntax error; on the first attempt javac reports only this one, because name and type checking is not reached until the code can be parsed. ;is added, three more errors appear:class Hello is public, should be declared in a file named Hello.java(the file name must be exactly the same as the class name),package system does not exist(Systemis written with a capital letter) andcannot find symbol … variable Number(number≠Number).- Once the file is saved as
Hello.javaand the three fixes are made, the output is:Number: 5
Multiple Choice · Compilation
Question 3: Bytecode Portability
Compiled on Windows with javac, the file Hello.class is copied to Linux and run with java Hello. What happens?
B: javac translates source code to bytecode specific to the JVM, not to the processor. The JVM on every platform interprets this bytecode or compiles it to machine code with JIT: “write once, run anywhere”. A and C confuse bytecode with machine code.
Output Question · Data Types
Question 4: Overflow
What does the program print? l1 and l2 — why do they differ?
int a = Integer.MAX_VALUE; a++; System.out.println(a); byte b = (byte) (100 + 100); System.out.println(b); short s = 32767; s += 2; System.out.println(s); long l1 = Integer.MAX_VALUE + 1; long l2 = Integer.MAX_VALUE + 1L; System.out.println(l1 + " " + l2);
Show answer
-2147483648 -56 -32767 -2147483648 2147483648
- A value that exceeds the limit does not cause an error; it wraps around to the other end of the range: 2147483647 + 1 → −2147483648.
- 200 does not fit the byte range (−128 … 127): 200 − 256 = −56. For short, 32767 + 2 is 32769 − 65536 = −32767 (
s += 2does the conversion itself). Integer.MAX_VALUE + 1is computed between two ints and first overflows; only the overflowed result is assigned to the long variable.1Lmakes the operation long from the start.
Multiple Choice · Data Types
Question 5: Which Declaration Compiles?
Which of the following declarations compiles?
D: char → int widening is automatic; code becomes 65. 3.14 is a double constant; it becomes a float only when written as 3.14f. 8000000000 exceeds the int range; a long constant needs the L suffix. A boolean takes only true or false, so 0 and 1 cannot be used.
Multiple Choice · Variables
Question 6: Unassigned String
For a local variable, we write String s; System.out.println(s);. What happens?
C: Java does not allow a local variable to be read before it is assigned; the error is caught at compile time. String s = null; would print null, String s = ""; would print an empty line: the empty string, null and unassigned are three different things.
Multiple Choice · Identifiers
Question 7: Valid Identifiers
In which line are all of the names valid identifiers?
A: An identifier starts with a letter, _ or $; it may contain letters, digits, _ and $. Class is not a keyword because it is written with a capital C. 2age starts with a digit, final is a keyword, full-name contains -, the subtraction operator.
Multiple Choice · Identifiers
Question 8: Is It a Keyword?
In the declaration int ___ = 3;, which of the following can be used as the variable name?
C: String is a class name, not a keyword: int String = 3; compiles (but it confuses readers and should not be used). goto is a reserved but unused keyword, double is a type keyword, null is a literal value; none of these three can be a name.
Multiple Choice · Conversion
Question 9: Integer Division
double avg = (3 + 4) / 2; — afterwards, the variable avg has what value?
D: (3 + 4) / 2 is between two ints: integer division gives 3 and the fraction is dropped. The result is widened to double during assignment: 3.0. Since int → double widening is automatic, the code compiles. For 3.5, write (3 + 4) / 2.0.
Step-by-Step Trace · Conversion
Question 10: Tracing Assignment and Conversion
Follow the values of the variables after each line. What does the program print?
Two int variables.
x / y is two ints: integer division gives 3. The result widens to double on assignment → 3.0.
(double) applies only to x (the precedence of a unary cast is high): 7.0 / 2 = 3.5.
Compound assignment: x = x + (y * 3) = 7 + 6 = 13. The right-hand side is evaluated first.
13 % 4 = 1.
(int) 3.99 → 3, (int) -2.5 → −2: the fraction is dropped, not rounded. 3 + (−2) = 1.
double values are written with .0.
int x = 7; int y = 2; double d = x / y; double e = (double) x / y; x += y * 3; x %= 4; int z = (int) 3.99 + (int) -2.5; System.out.println(d + " " + e + " " + x + " " + z);
Output Question · Conversion
Question 11: char and int
What does each line print? ('A' = 65, 'a' = 97, '0' = 48)
char c = 'A'; System.out.println(c + 2); System.out.println((char) (c + 2)); System.out.println("" + c + 2); System.out.println('a' - 'A'); char d = 'x'; d++; System.out.println(d); System.out.println((int) '0' + 1);
Show answer
67 C A2 32 y 49
c + 2: when a char enters an operation it is promoted to int → 67. For a character,(char)is needed → C."" + c + 2: evaluated left to right, a String forms first, then it is all concatenation: "A2".- The difference between the codes of uppercase and lowercase letters is 32.
d++works on a char and keeps its type: x → y. (int) '0' + 1= 48 + 1 = 49 (the code of the digit character, not the digit itself).
Output Question · Conversion
Question 12: char and Code Value
Which six lines does the program print?
char c = 'a';
c += 2;
System.out.println(c);
System.out.println('a' + 2);
System.out.println((char) ('A' + 32));
System.out.println('9' - '0' + 1);
System.out.println("" + 'x' + 1);
System.out.println('x' + 1 + "");Show answer
c 99 a 10 x1 121
c += 2: the compound assignment converts the result back tocharagain: 97 + 2 = 99 →'c'.'a' + 2:char + intyieldsint: 99.'A' + 32= 97,(char)turns it into'a'. The difference between the codes of uppercase and lowercase letters is 32.'9' - '0'= 57 − 48 = 9, plus 1: 10."" + 'x' + 1is string concatenation from left to right:"x1";'x' + 1 + ""adds the numbers first: 120 + 1 = 121.
Multiple Choice · Decimal Precision
Question 13: 0.1 + 0.2 == 0.3
System.out.println(0.1 + 0.2 == 0.3); — which statement about this line is true?
A: 0.1 repeats infinitely in binary as 0.000110011…₂; it is rounded to fit the 52-bit fraction. The sum is 0.30000000000000004. The code compiles (C is wrong); the constant 0.1 in Java is a double (D is wrong). For comparison use Math.abs(a - b) < 1e-9.
Output Question · Operators
Question 14: Operator Precedence
What does each line print? Show the order of evaluation.
int a = 7, b = 2;
System.out.println(a / b * b + a % b);
System.out.println(1 + 2 + "3" + 4 * 5);
System.out.println(-17 / 5 + " " + -17 % 5);
System.out.println(10 - 4 - 3 + " " + 20 / 4 * 2);
System.out.println(a > b && b > 3 || a == 7);Show answer
7 3320 -3 -2 3 10 true
7 / 2 * 2 + 7 % 2= 3 * 2 + 1 = 7:/,*,%have equal precedence and go left to right.4 * 5first: 20. Then left to right: 1 + 2 = 3, "3" + "3" = "33", "33" + 20 = "3320".- Integer division truncates toward zero: −17 / 5 = −3; the remainder has the sign of the dividend: −2.
10 - 4 - 3goes from the left: 3.20 / 4 * 2goes from the left: 10.&&binds tighter than||: (true && false) || true = true.
Coding Question · Operators
Question 15: Converting Seconds to Hours
Read a whole number of seconds from the user and print its equivalent in the form H:M:S. Write the code with / and % (1 hour = 3600 seconds, 1 minute = 60 seconds). What does it print for 7510 and 3599?
Show answer
Scanner input = new Scanner(System.in); int seconds = input.nextInt(); int hours = seconds / 3600; int minutes = seconds % 3600 / 60; int remaining = seconds % 60; System.out.println(hours + ":" + minutes + ":" + remaining);
- 7510: 7510 / 3600 = 2, 7510 % 3600 = 310, 310 / 60 = 5, 7510 % 60 = 10 →
2:5:10. - 3599 →
0:59:59. %and/have equal precedence and are evaluated left to right:seconds % 3600 / 60=(seconds % 3600) / 60.- For two-digit output use
System.out.printf("%d:%02d:%02d%n", hours, minutes, remaining);→2:05:10.
Step-by-Step Trace · Operators
Question 16: Tracing Compound Assignment
Postfix increment and compound assignments in sequence. Trace the variables after each line. What does the program print?
Two int variables.
The right-hand side is computed with the old values: 5 + 6 = 11, then written to x.
Postfix x++: z is assigned the old value 11, then x becomes 12.
y -= 4 → y = 6 − 4 = 2.
x *= y → x = 12 · 2 = 24.
x /= 4 → 24 / 4 = 6 (integer division; any remainder would be dropped). Five lines were printed.
int x = 5; int y = 6; x = x + y; System.out.println("x: " + x + " y: " + y); int z = x++; System.out.println("x: " + x + " z: " + z); y -= 4; System.out.println("x: " + x + " y: " + y); x *= y; System.out.println("x: " + x + " y: " + y); x /= 4; System.out.println("x: " + x + " y: " + y);
Step-by-Step Trace · Operators
Question 17: Tracing Increment and Decrement
Prefix and postfix operators in the same expression. Operands are evaluated from left to right. What does the program print?
i starts at 3.
Left operand i++: the old value 3 enters the expression, then i becomes 4.
Right operand ++i: first i becomes 5, then the new value 5 enters the expression. j = 3 + 5 = 8.
i--: the value 5 enters the expression, then i becomes 4.
--i: first i becomes 3, then the value 3 enters the expression. k = 5 − 3 = 2.
The two increments and two decrements cancel out: i is 3 again.
int i = 3; int j = i++ + ++i; int k = i-- - --i; System.out.println(i + " " + j + " " + k);
Multiple Choice · Operators
Question 18: Postfix and Compound Assignment
What does the program print?
int a = 5; boolean b = a == a++; char c = 'A'; c += 2; System.out.println(b + " " + a + " " + c);
C: == evaluates its operands from left to right: the left side is 5, and the a++ on the right also yields the old value 5, then a becomes 6; 5 == 5 → true. c += 2 is a compound assignment, so it converts the result to char itself: 'A' + 2 → 'C'. c = c + 2; would not compile: an int result cannot be assigned to a char.
Multiple Choice · Operators
Question 19: Short-Circuit
What does the program print?
int s = 0; boolean p = (s > 0) && (++s > 0); boolean q = (s == 0) || (++s > 0); boolean r = (s == 0) && (++s > 0); System.out.print(s + " " + p); System.out.println(" " + q + " " + r);
C: p: the left side is false, so && never evaluates its right side, and s stays 0. q: the left side is true, so || does not look at the right. r: the left side is true and the result is not yet determined; the right side is evaluated, ++s makes s 1 and 1 > 0 is true. Of the three ++s, only one ran.
Multiple Choice · Operators
Question 20: && and ||
int x = 7; is declared. What does System.out.println(x > 5 && x < 7 || x == 7); print?
A: && has higher precedence than ||: (7 > 5 && 7 < 7) = false, then false || (7 == 7) = true. The result of comparisons is boolean, not a number (D). Parentheses improve readability but are not required (C).
Coding Question · Operators
Question 21: Leap Year Expression
int year is given; write the line that decides whether the year is a leap year with a single boolean expression. Rule: years divisible by 4 but not by 100, and years divisible by 400, are leap years. What is the result for 1900, 2000, 2024 and 2023?
Show answer
boolean leap =
(year % 4 == 0 && year % 100 != 0)
|| year % 400 == 0;| year | % 4 | % 100 | % 400 | leap |
|---|---|---|---|---|
| 1900 | 0 | 0 | 300 | false |
| 2000 | 0 | 0 | 0 | true |
| 2024 | 0 | 24 | 24 | true |
| 2023 | 3 | 23 | 23 | false |
&& binds tighter than ||, so the parentheses are not required, but they improve readability. Divisibility, like an even-number test, is also checked with % … == 0.
Multiple Choice · Control Flow
Question 22: Semicolon and Braces
Do not be fooled by indentation. What does the program print?
int x = 7; if (x > 10); { System.out.print("A"); } if (x > 10) System.out.print("B"); System.out.print("C");
A: The body of the first if is just ; (an empty statement); the block in braces is independent of the condition and always runs: A. Only the first statement is attached to the second if (it has no braces): B is skipped, C is always printed. Indentation means nothing to Java.
Output Question · Control Flow
Question 23: Fall-Through in switch
On every iteration the loop enters switch. What does the program print? How many points are added in each round?
int score = 0; for (int k = 1; k <= 3; k++) { switch (k) { case 1: score += 1; case 2: score += 10; break; default: score += 100; } } System.out.println(score);
Show answer
Output: 121
| k | cases that run | added | score |
|---|---|---|---|
| 1 | case 1 → (no break) case 2 | 1 + 10 | 11 |
| 2 | case 2 → break | 10 | 21 |
| 3 | default | 100 | 121 |
break exits not the loop but only the switch; the loop continues.