Variables, Output,
Errors & Division

Variables · Output · Errors · Division · Scanner

Task 1: Spot the Issue

Write the code, then try to compile

Your Goal

1. Write the code shown on the right in your editor.

2. Try to compile — what happens?

3. Fix any issues and identify the error type.

Example Code

Task 2: Compile & Run

Write the code, then compile and run it

Your Goal

1. Write the code shown on the right in your editor.

2. Compile, then run the program.

3. Observe what happens, identify the error type, and fix it.

Example Code

Task 3: Logic Error

It compiles and runs — but is the answer right?

Your Goal

1. Write code that calculates the average of 10 and 3.

2. Use only integer variables — observe what value you get.

3. The code compiles and runs, so no Syntax or Runtime Error. Is the result correct?

4. Identify the error type and fix it so the decimal portion is preserved.

Task 4: Print vs Println

Two ways to output text

Your Goal

1. Write code that prints the following in exactly this layout:

2. Hello World Java on one line.

3. end on the next line.

Task 5: Direct vs Quoted

When do you need double quotes?

Your Goal

1. Print the boolean value false.

2. Print the integer 5.

3. Print the String "5".

4. Explain why false and 5 do not need double quotes.

Task 6: Naming Rules

Which names are valid?

Your Goal

1. Identify whether each of these is a valid variable name:
myVar 2ndPlace class _temp my-var MAX_SIZE my var count2

2. For each invalid name, state which rule it breaks.

Task 7: Escape Sequences

Printing special characters

Your Goal

1. Write code that prints: He said "Hello"

2. Then print a file path: C:\Users\Name — the backslashes must appear in the output.

3. Then use a single output statement to print two lines:
Line1
Line2

Task 8: Uninitialized Variable

Can you use a variable without giving it a value?

Your Goal

1. Declare double b; without assigning a value.

2. Try to print b using System.out.println(b).

3. Compile and observe what happens.

4. Identify the error type and fix it.

Task 9: Integer Division

int vs double — truncation and promotion

Your Goal

1. Declare int c = 8 / 7 and print c. What value? Why?

2. Declare double d = 8 / 7 and print d. Why not 1.142...?

3. Declare double e = 8.0 / 7 and print e. Why is this different?

4. Explain integer truncation and automatic promotion in your own words.

Task 10: Mixed Arithmetic

What happens when int meets double?

Your Goal

1. Declare an int and a double variable with any values.

2. Compute intVar + doubleVar * 2 and print the result.

3. Now try to store that result into an int variable — what does the compiler say?

4. Explain which type wins when int and double mix.

Task 11: Constants & Input

Combining user input with constants

Your Goal

1. Write a program that asks the user to enter an integer amount.

2. Use a constant final double RATE = 0.15 for the rate.

3. Calculate a fee based on the amount and the rate, then print it.

4. Explain: what type is the result when an int multiplies a double?

Task 12: Final Constants

Can you change a final variable?

Your Goal

1. Declare a constant final int MAX_SCORE = 100.

2. Try to reassign MAX_SCORE to a different value.

3. Compile and observe what happens.

4. Explain what final means and why this error occurs.

01 / 13