Exposure Java Exercises - Munoz AHISD

advertisement

APCS Exposure Java Exercises 05.05-09 Date:

Name: KEY Period:

2.

1. What is the simplest control structure? one-way selection

Write the Java statement that will do this action: if (grade >= 70)

System.out.println("You passed");

If your grade is 70 or better, print "You passed!"

3.

4.

Why should indentation be used with control structures?

It makes the program more readable.

Explain the unusual output of program Java0507.java

.

The program is meant to execute multiple statements only if the condition is true.

5.

6.

What do you need to add to a program if you wish to control multiple statements with if .

Create block structure with opening and closing braces.

7.

What statement is used with if for two-way selection ? else

Write the Java statement that will do this action: If your grade is 70 or better, print "You passed!" otherwise print "You failed!"

if (grade >= 70)

System.out.println("You passed");

else

System.out.println("You failed");

8. How do you control multiple statements with one-way or two-way selection ?

Use braces

9. Does Multi-way Selection use a conditional statement ?

No

10. Explain how Multi-way Selection is accomplished in Java.

Multi-way selection is accomplished with the switch command which uses a selection variable with a value. The selection variable value is compared with a group of designated case values. When a match is found, the statements following the case value are executed.

11. What data types work with switch ? int, char and String

12. In a switch structure, what symbol separates the selection constant from the program statement that needs to be performed if a match is found.

A colon ( : )

Exposure Java 2012, APCS Edition Exercises 05.05-09 Page 1 06-22-12

13. What would be the output of program Java0511.java

if 9 were entered?

September

October

November

December

14. While program Java0511.java

compiles and executes, its output is strange.

How does program Java0512.java

fix the problem?

It uses the break keyword.

15. When is the default statement executed?

When no match is found.

16. What would be the output of program Java0512.java

if 9 were entered?

September

17. What would be the output of program Java0512.java

if 0 were entered?

This is not a valid month number.

18. How do you control multiple statements with switch ?

With switch, controlling multiple lines is done in the same way as controlling a single line because you can put as many programming statements as you wish between the case and break statements.

19. With switch , can multiple cases yield the same result?

Yes

20. What are 2 synonyms for Repetition ?

Iteration and looping

21. What loop structure is used with fixed iteration ? for

22. In fixed iteration , what does fixed mean?

The number of times that the body of the loop structure executed.

23. What would you need to change in program Java0515.java

if Joe wanted the message displayed 1000 times?

Add 960 additional output statements

24. What would you need to change in program Java0516.java

if Joe wanted the message displayed 1000 times?

Change the value in the loop condition

25. What does LCV stand for?

Loop Control Variable

Exposure Java 2012, APCS Edition Exercises 05.05-09 Page 2 06-22-12

Use this statement for questions 26-28:

for(j = 1; j <= 100; j++)

26. What does the j = 1 mean?

It initializes the loop control variable

27. What does the j <= 100 mean?

Continue to execute the loop as long as this statement is true.

28. What does the j++ mean?

Increment the loop control variable by 1 for each iteration.

29. How do you repeat multiple statements with the for loop structure?

Use braces

30. Must the loop counter in a for statement be an int ?

No

31. Give a real life situation where fixed iteration is not practical.

A loop that repeats until a password is entered

32. What kind of loop is the while loop?

It is a pre-condition loop

33. The while loop structure requires a statement inside the loop body that was not necessary with the for loop. What is the purpose of this statement?

To give an opportunity for the loop condition variable to change.

34. What is the only thing checked in a while loop?

Only the loop condition is checked.

35. Where must the loop counter be initialized when using a while loop?

Before the loop condition is used

36. What must happen inside a while loop?

The loop counter must change

37. Why is the do...while

loop called a post-condition loop?

The loop condition comes after the loop body.

38. Assume the condition for a while loop is already false , even before the loop begins.

How many times will the loop execute?

Zero

39. Assume the condition for a do…while

loop is already false , even before the loop begins.

Furthermore, assume nothing in the loop will ever make the condition change to

How many times will the loop execute?

Once true .

Exposure Java 2012, APCS Edition Exercises 05.05-09 Page 3 06-22-12

40. A for loop is not well suited for something like entering a password. Both the while loop and the do…while

loop would work in this case. If you were writing a password program, which loop structure would you chose? Explain the reasoning behind your answer.

I would chose the do…while loop since it is guaranteed to always execute at least once.

Even if someone knows the password and will get it correct on his/her first try, he/she still needs to enter it that one time.

41. Which loop structure would be good if you wanted to display 50 rectangles on the screen?

The for loop

42. Which selection control structures are covered on the AP Exam? if and if…else

43. Which repetition control structures are covered on the AP Exam? for and while

Exposure Java 2012, APCS Edition Exercises 05.05-09 Page 4 06-22-12

Download