Browse By Unit
Milo Chang
Milo Chang
Once you recognize the frequent symbols used in Java, you'll be a pro at understanding what's happening in the code!
for (int i=0; i<5; i++)
{
System.out.print(i);
}
What is printed as a result of executing this code?
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?
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?
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:
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
<< Hide Menu
Milo Chang
Milo Chang
Once you recognize the frequent symbols used in Java, you'll be a pro at understanding what's happening in the code!
for (int i=0; i<5; i++)
{
System.out.print(i);
}
What is printed as a result of executing this code?
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?
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?
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:
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
© 2024 Fiveable Inc. All rights reserved.