Name:_____________________________________Date:___________________________Block:_________ Review for Test Lessons 9-12

advertisement
Name:_____________________________________Date:___________________________Block:_________
Review for Test Lessons 9-12
1. Show the basic skeleton of an if - else structure.
2. In the following code, assume that the portion designated with <#1> is a true statement.
What will be the output?
if( <#1> )
{
System.out.print(“Elvis”);
}
System.out.println(“ Presley”);
3. In problem 2, what would be the output if <#1> was a false statement?
4. If you wanted to compare the contents of two Strings, s1 and s2, which of the following would be appropriate to use?
a. s1.equals(s2)
b. s2.equals(s1)
c. s1.equalsIgnoreCase(s2)
d. All of the above
e. s1 = = s2
f. None of these
5. In a switch statement ( switch(???) ) what are two types of variables that may be used
in the area designated by ???
6. What is the purpose of default in a switch structure?
Notice in the following code that there are some missing break commands. Use this code to
answer problems 7 and 8.
int p = ??;
int x = 79;
switch(p)
{
case 1:
x = 20;
break;
case 2:
x = 2;
case 3:
x = 3;
default:
x ++;
}
7. If p = 2 what is the final value of x?
8. If p = 1 what is the final value of x?
For questions9 to 13, consider the following code:
int sum = 0;
for(int j = 0; j < 3; j++)
{
sum = sum + 2;
}
System.out.println(sum);
9. Identify the control expression.
10. Identify the step expression.
11. Identify the initializing expression.
12. How many times does the loop iterate (repeat)?
13. What is printed?
14. Write code that forms the basic skeleton of a while loop.
15. Write code that forms the basic skeleton of a do-while loop
16. What is the effect of break when encountered in a loop?
17. What is the effect of continue when encountered in a loop?
18. What is the major difference in a while and a do-while loop?
Download