Once you recognize the frequent symbols used in Java, you'll be a pro at understanding what's happening in the code!
- == compares the value on the left and the value on the right
- % gives you the remainder when the value on the left is divided by the right
- ! means "not", so != means "not equal to"
- i++ is the same as i += 1 or i = i+1
- i-- is the same as i -=1 or i = i-1
- i= 2* is the same as i = i2*
for (int i=0; i<5; i++)
{
System.out.print(i);
}
What is printed as a result of executing this code?
- The value of i starts off at 0 when the for loop begins, so when i is printed, the computer prints "0"
- Since the loop increments by i++, the value of i is now 1, and the computer prints "1"
- i increases in value until i equals 4 and the computer prints "4"
- After that, i increases in value to 5, but 5 is not less than 5, so the computer exits the for loop and does not print anything else
- Since the code uses "print", all of the numbers are printed on the same line
Your output is:
01234
for (int i=0; i<5; i+=1)
{
if (i%2 == 0)
{
System.out.print(i);
}
else if (i!=3)
{
System.out.println(i*2);
}
else
{
System.out.print("Three");
}
}
What is printed as a result of executing this code?
- The value of i starts off at 0 when the for loop begins
- Since 0%2 equals 0 (since the remainder when 0 is divided by 2 is 0), the first if statement is true and the code inside executes, printing "0"
- The value of i then increases by 1 to become 1
- Since 1%2 is not equal to 0, the computer checks the else-if statement
- 1 is not equal to 3, so the computer prints 1*2, or "2"
- BE CAREFUL!!! Since the code says "println", the computer prints the "2" next to the "0", then starts a new line!
- If you continue tracing the code, when i becomes 2, the computer prints "2"
- When i equals 3, it fails the if statement and the else-if statement, which means the code in the else statement runs, printing "Three"
- Continue tracing the code for i equals 4 and the computer prints "4"
Your output is:
02
2Three4
int count = 1;
while (count<5)
{
count*=2;
}
System.out.println(count);
What is printed as a result of executing this code?
- When the code begins, count equals 1
- Since 1 is less than 5, the computer enters the while loop and count is doubled, making count equal to 2
- Since 2 is still less than 5, the computer re-enters the while loop and count is doubled again, making count equal to 4
- Since 4 is still less than 5, the computer re-enters the while loop and count is doubled and now equals 8
- Now that 8 is not less than 5, the computer moves on and prints count, or "8"
Your output is:
8
public int addNums (int x, int y)
{
return x+y;
}
What does the method return with after the method call addNums (5,10)?
The method returns the integer value 15. (Since 5+10 = 15.)
for (int y=5; y>0; y--)
{
for (int x=0; x<5; x++)
{
System.out.print(y);
}
System.out.println();
}
What is the output when you execute this code?
First of all, don't panic when you see this snippet, it's easy to solve if you follow it step-by-step and write every step down. 🙌
Start when y equals 5:
- Now that you're on the inside of the double for loop, just focus on the inner for loop
- The inner for loop prints y, which is currently 5, five times
- Now the the inner for loop is finished, the computer exits that loop and moves on to "System.out.println()", which causes the computer to skip a line
- Since there's nothing inside the parentheses, nothing is printed
The value of y then decreases by 1, so y now equals 4. Repeat the same process as above for y equals 4, then 3, then 2, then 1. The code stops running after that because y ends up being 0 and does not meet the condition that y is greater than 0, causing the computer to exit all of the loops.
Your output is:
55555
44444
33333
22222
11111
- As you trace the code, write down what variables the computer has stored and what is being printed (and how it is printed)
- Understand the common symbols used in Java
- Pay careful attention to what is happening to the variables (and write down any changes) because a wrong value will lead you to the wrong answer
- Pay attention to "print" vs. "println" because they lead to very different outputs