21
Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control Structures I DO NOT WRITE ON THIS TEST This test includes program segments, which are not complete programs. Answer such questions with the assumption that the program segment is part of a correct program. Objective 1 - Types of Control Structures 01. Which of the following are the three general types of control structures? (A) conditional sequence, branching, selection (B) simple sequence, selection, repetition (C) selection, Repetition, repetition (D) simple sequence, branching, decision making 02. Selection is also called (A) conditional branching or decision making. (B) conditional sequence or Repetition. (C) conditional branching or Repetition. (D) decision making or conditional repetition 03. Which of the following are types of selection control structures? I. One-Way selection II. Two-Way selection III. Multiple-Way selection (A) I only (B) II only (C) I and II only (D) II and III only (E) I, II and III 04. A repetition control structure (A) always repeats without any condition. (B) requires a conditional statement. (C) can only be used in combination with a selection control structure. (D) is required for repeating program segments 500, or more, times.

Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Embed Size (px)

Citation preview

Page 1: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07

Exposure Java Multiple Choice Test-A Chapter 5 Control Structures I

DO NOT WRITE ON THIS TEST

This test includes program segments, which are not complete programs. Answer such questions with the assumption that the program segment is part of a correct program.

Objective 1 - Types of Control Structures 01. Which of the following are the three general types of control structures? (A) conditional sequence, branching, selection (B) simple sequence, selection, repetition (C) selection, Repetition, repetition (D) simple sequence, branching, decision making 02. Selection is also called (A) conditional branching or decision making. (B) conditional sequence or Repetition. (C) conditional branching or Repetition. (D) decision making or conditional repetition 03. Which of the following are types of selection control structures? I. One-Way selection II. Two-Way selection III. Multiple-Way selection (A) I only (B) II only (C) I and II only (D) II and III only (E) I, II and III 04. A repetition control structure (A) always repeats without any condition. (B) requires a conditional statement. (C) can only be used in combination with a selection control structure. (D) is required for repeating program segments 500, or more, times.

Page 2: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 2 Updated 02-03-07

Objective 2 - Relational Operators 05. A conditional statement is (A) a program expression that evaluates to true or false. (B) a program statement that requires input from the keyboard. (C) any program statement with a binary operator. (D) any program statement with an unary operator. 06. Which of the following sentences can be translated into a conditional statement? (A) Tomorrow is the start of the second semester. (B) If you are a national merit finalist, you will receive a scholarship. (C) Your SAT score is 1250. (D) Go straight to jail; do not pass go; do not collect any money. 07. Which of the following is the relational operator meaning “not equal to”? (A) == (B) = (C) != (D) >= (E) < 08. Which of the following is not a relational operator? (A) == (B) = (C) != (D) >= (E) <

Page 3: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 3 Updated 02-03-07

Objective 3 - One-Way Selection 09. Which Java keyword or keywords are used for one-way selection? (A) if only (B) if ... then only (C) if ... else only (D) if ... then ... else 10. Which is true about the indented program statements in the following program segment? double savings = some double value ; if (savings > 10000.0) System.out.println("We have enough money to go skiing."); System.out.println("Let's start packing right away."); System.out.println("Where do we want to go skiing?"); (A) Only the indented statements are executed whenever the if condition is true. (B) The second indented statement is syntactically incorrect without using braces. (C) Indented program statements do not alter program logic. (D) The first statement following the if condition must be indented. 11. What is the output of the following program segment? double bonus = 500.0; double sales = 200000.0; if (sales >= 300000.0) bonus += 250.0; System.out.println("Bonus: " + bonus); System.out.println("The End"); (A) Bonus: 50.0 The End (B) Bonus: 500.0 The End (C) Bonus: 750.0 The End (D) No output

Page 4: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 4 Updated 02-03-07

12. What is the output of the following program segment? double bonus = 500.0; double sales = 200000.0; if (sales >= 300000.0) System.out.println("Bonus: " + bonus); bonus += 250.0; (A) Bonus: 250.0 (B) Bonus: 500.0 (C) Bonus: 750.0 (D) No output 13. What is the output of the following program segment? int k; k = 2500; if (k < 3000) System.out.println(“k = “ + k); System.out.println(“k = “ + k); (A) 2500 2500 (B) k = 2500 k = 2500 (C) k = 2500 (D) No output

Page 5: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 5 Updated 02-03-07

14. What is the output of the following program segment? int k; k = 4000; if (k < 3000) System.out.println(“k = “ + k); System.out.println(“k = “ + k); (A) 4000 4000 (B) k = 4000 k = 4000 (C) k = 4000 (D) No output 15. What is the output of the following program if 2500 is entered at the keyboard? Scanner input = new Scanner(System.in); int k = input.nextInt(); if (k < 3000) { System.out.println(“k = “ + k); System.out.println(“k = “ + k); } (A) k = 3000 k = 2500 (B) k = 2500 k = 2500 (C) k = 2500 (D) No output

Page 6: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 6 Updated 02-03-07

16. What is the output of the following program if 4000 is entered at the keyboard? Scanner input = new Scanner(System.in); int k = input.nextInt(); if (k < 3000) { System.out.println(“k = “ + k); System.out.println(“k = “ + k); } (A) 4000 4000 (B) k = 4000 k = 4000 (C) k = 4000 (D) No output

Page 7: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 7 Updated 02-03-07

Objective 4 - Two-Way Selection 17. Which of the following Java keywords are used for two-way selection? (A) if only (B) if ... then only (C) if ... else only (D) if ... then ... else 18. What do one-way selection and two-selection have in common? Both control structures (A) uses parentheses around the conditional statement. (B) use the keyword if. (C) use braces { } to control multiple program statements. (D) do all of the above.

Page 8: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 8 Updated 02-03-07

Objective 5 - Multiple-Way Selection 19. Which of the following Java keywords are used for multiple-way selection? (A) switch (B) case (C) break (D) default (E) All of the above 20. What is the value of num at the conclusion of the following program segment?

char qwerty = 'B'; int num = 100; switch(qwerty) { case 'A': num ++; break; case 'B': num += 2; break; case 'C': num += 3; break; case 'D': num += 4; } (A) 100 (B) 102 (C) 105 (D) 109

21. What is the value of num at the conclusion of the following program segment?

char qwerty = 'B'; int num = 100; switch(qwerty) { case 'A': num ++; break; case 'B': num += 2; case 'C': num += 3; break; case 'D': num += 4; } (A) 100 (B) 102 (C) 105 (D) 109

Page 9: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 9 Updated 02-03-07

(E) Error message 22. What is the output of the following program, after 5 is entered at the keyboard? Scanner input = new Scanner(System.in); int dayNum = input.nextInt(); switch (dayNum) { case 1 : System.out.println(”Sunday”); case 2 : System.out.println(”Monday”); case 3 : System.out.println(”Tuesday”); case 4 : System.out.println(”Wednesday”); case 5 : System.out.println(”Thursday”); case 6 : System.out.println(”Friday”); case 7 : System.out.println(”Saturday”); default : System.out.println(”Wrong Input”); } (A) Thursday (B) Wrong Input (C) Thursday Friday Saturday Wrong Input (D) Error message (E) No Output

Page 10: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 10 Updated 02-03-07

23. What is the output of the following program, after 5 is entered at the command line prompt? Scanner input = new Scanner(System.in); int dayNum = input.nextInt(); switch (dayNum) { case 1 : System.out.println(”Sunday”); break; case 2 : System.out.println(”Monday”); break; case 3 : System.out.println(”Tuesday”); break; case 4 : System.out.println(”Wednesday”); break; case 5 : System.out.println(”Thursday”); break; case 6 : System.out.println(”Friday”); break; case 7 : System.out.println(”Saturday”); break; default : System.out.println(”Wrong Input”); } (A) Thursday (B) Wrong Input (C) Thursday Friday Saturday Wrong Input (D) Error message (E) No Output 24. Refer to the program in the previous question. What is the output of the following program, after 8 is entered at the keyboard? (A) Thursday (B) Wrong Input (C) Thursday Friday Saturday Wrong Input (D) Error message (E) No Output

Page 11: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 11 Updated 02-03-07

25. What is the output of the following program, after 8 is entered at the keyboard? Scanner input = new Scanner(System.in); int dayNum = input.nextInt(); switch (dayNum) { case 1 : System.out.println(”Sunday”); break; case 2 : System.out.println(”Monday”); break; case 3 : System.out.println(”Tuesday”); break; case 4 : System.out.println(”Wednesday”); break; case 5 : System.out.println(”Thursday”); break; case 6 : System.out.println(”Friday”); break; case 7 : System.out.println(”Saturday”); break; } (A) Thursday (B) Wrong Input (C) Thursday Friday Saturday Wrong Input (D) Error message (E) No Output

Page 12: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 12 Updated 02-03-07

Objective 5 - Different Kinds of Repetition 26. The for loop structure uses what kind of Repetition? (A) pre-condition (B) post-condition (C) fixed 27. The while loop structure uses what kind of Repetition? (A) pre-condition (B) post-condition (C) fixed 28. The do...while loop structure uses what kind of Repetition? (A) pre-condition (B) post-condition (C) fixed 29. What do selection control structures and repetition control structures have in common? (A) Both structures require user input. (B) Both structures generate output. (C) Both structures require a conditional statement. (D) Both structures require a user-created function.

Page 13: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 13 Updated 02-03-07

Objective 6 - Fixed Repetition 30. What is the output of the following program segment? for (int k = 1; k <= 5; k++) System.out.print(k); (A) 12345 (B) 1 2 3 4 5 (C) 1 2 3 4 5 (D) 54321 (E) 5 4 3 2 1 31. What is the output of the following program segment? int x,y; y = 0; for (x = 1; x <= 5; x++) y++; y++; System.out.println(“y = “ + y); (A) y = 5 (B) y = 6 (C) y = 10 (D) y = 11 (E) y = 12

Page 14: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 14 Updated 02-03-07

32. What is the output of the following program segment? int x,y; y = 0; for (x = 1; x <= 5; x++) { y++; y++; } System.out.println(“y = “ + y); (A) y = 5 (B) y = 6 (C) y = 10 (D) y = 11 (E) y = 12 33. What is the output of the following program segment? for (int k = 5; k > 0; k--) System.out.println(“What is OOP?”); (A) What is OOP? What is OOP? What is OOP? What is OOP? What is OOP? What is OOP? (B) What is OOP? What is OOP? What is OOP? What is OOP? What is OOP? (C) What is OOP? What is OOP? What is OOP? What is OOP? (D) What is OOP? (E) No output

Page 15: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 15 Updated 02-03-07

34. What is the output of the following program segment? for (int k = 0; k <= 10; k+=3) System.out.println(“What is OOP?”); (A) What is OOP? What is OOP? What is OOP? What is OOP? What is OOP? What is OOP? (B) What is OOP? What is OOP? What is OOP? What is OOP? What is OOP? (C) What is OOP? What is OOP? What is OOP? What is OOP? (D) What is OOP? (E) No output

Page 16: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 16 Updated 02-03-07

35. What is the output of the following program segment?

for (int k = 0; k <= 10; k+=2); System.out.println(“What is OOP?”); (A) What is OOP? What is OOP? What is OOP? What is OOP? What is OOP? What is OOP? (B) What is OOP? What is OOP? What is OOP? What is OOP? What is OOP? (C) What is OOP? What is OOP? What is OOP? What is OOP? (D) What is OOP? (E) No output 36. What is the output of the following program segment? for (int k = 1; k < 20; k+=3) System.out.print(k + “ “); (A) 1 4 7 10 13 16 19 22 (B) 3 6 9 12 15 18 21 (C) 1 4 7 10 13 16 19 (D) 3 6 9 12 15 18

Page 17: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 17 Updated 02-03-07

37. What is the output of the following program segment? for (int k = 1; k < 20; k+=k) System.out.print(k + “ “); (A) 1 2 4 8 16 (B) 2 4 6 8 10 12 14 16 18 20 (C) 2 4 6 8 10 12 14 16 18 (D) 1 3 7 11 16 21 (E) 1 2 4 8 16 32 38. What is the LAST number output from the following program segment? int j,k; for (j=1; j<30; j*=3) { k = 2 * j; System.out.println(j); } (A) 27 (B) 29 (C) 30 (D) 54 39. What is the output of the following program segment? int j,k; k = 8; for (j=1; j<k; j++) { System.out.print(j+k); k--; } (A) 9999 (D) 99999999 (B) 99999 (E) 9 9 (C) 9 9 9 9 9 9 9

Page 18: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 18 Updated 02-03-07

40. What is the output of the following program segment? int sum = 0; for (int k = 1; k < 9; k+=2) sum += k; System.out.println("sum: " + sum); (A) sum: 8 (B) sum: 9 (C) sum: 16 (D) sum: 25

Page 19: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 19 Updated 02-03-07

Objective 7 - Pre-Condition Repetition 41. What is the output of the following program segment? int j; j=25; while (j>2) { System.out.print(j + “ “); j/=2; } (A) 25 12 6 3 (B) 25 13 7 4 2 (C) 25 12 6 3 1 (D) 12 6 3 1 (E) 12 6 3 42. What is the output of the following program segment? int j; j=25; while (j>2) { j/=2; System.out.print(j + “ “); } (A) 25 12 6 3 (B) 25 13 7 4 2 (C) 25 12 6 3 1 (D) 12 6 3 1 (E) 12 6 3

Page 20: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 20 Updated 02-03-07

43. What is the FIRST number output from the following program segment? int j; j=5; while (j>-2) { j-=3; System.out.println(j); } (A) 5 (B) 2 (C) -1 (D) -2 (E) -3 44. What is the output of the following program segment? int j; j=1; while (j<=10) j++; System.out.print(j); (A) 12345678910 (B) 1 2 3 4 5 6 7 8 9 10 (C) 1 2 3 4 5 6 7 8 9 10 (D) 10 (E) 11

Page 21: Exposure Java Multiple Choice Test-A Chapter 5 Control ... · Exposure Java, Version 2006 Chapter 5 Test-A Page 1 Updated 02-03-07 Exposure Java Multiple Choice Test-A Chapter 5 Control

Exposure Java, Version 2006 Chapter 5 Test-A Page 21 Updated 02-03-07

Objective 8 - Post-Condition Repetition 45. What is the output of the following program segment? int j=1; do { System.out.print(j + “ “); j++; } while (j <= 11); (A) 1 2 3 4 5 6 7 8 9 10 (B) 1 2 3 4 5 6 7 8 9 10 11 (C) 2 3 4 5 6 7 8 9 10 (D) 2 3 4 5 6 7 8 9 10 11 46. What is the output of the following program segment? int j=1; do { j++; System.out.print(j + “ “); } while (j < 11); (A) 1 2 3 4 5 6 7 8 9 10 (B) 1 2 3 4 5 6 7 8 9 10 11 (C) 2 3 4 5 6 7 8 9 10 (D) 2 3 4 5 6 7 8 9 10 11