Name:_______________________ Covers Chapters 3 and 4 CSCI 1301 Introduction to Programming Armstrong Atlantic State University Instructor: Y. Daniel Liang Part I: Multiple Choice Questions. 1. Analyze the following code: // Enter an integer String numString = JOptionPane.showInputDialog(null, "Enter a number:", "Exam Input", JOptionPane.QUESTION_MESSAGE); int number = Integer.parseInt(numString); if (number <= 0) System.out.println(number); a. b. c. d. e. 2. The if statement is wrong, because it does not have the else clause; System.out.println(number); must be placed inside braces; If number is zero, number is displayed; If number is positive, number is displayed. number entered from the input cannot be negative. What is the output of the following code: (Please indent the statement correctly first. This will help you read the program.) int x = 9; int y = 8; int z = 7; if (x > 9) if (y > 8) System.out.println("x > 9 and y > 8"); else if (z >= 7) System.out.println("x <= 9 and z >= 7"); else System.out.println("x <= 9 and z < 7"); a. b. c. d. 3. x > 9 and y > 8; x <= 9 and z >= 7; x <= 9 and z < 7; None of the above. Analyze the following code: String numString = JOptionPane.showInputDialog(null, "Enter a number:", "Exam Input", JOptionPane.QUESTION_MESSAGE); int number = Integer.parseInt(numString); 1 if (number <= 0) System.out.println(number); System.out.println(number); a. b. c. d. e. 4. number number number number All of is always printed out at least once; is printed out twice if number is zero; is printed out twice if number is negative; is printed out once if number is positive. the above. What is y after the following switch statement? int x = 0; int y = 0; switch (x + 1) { case 0: y = 0; case 1: y = 1; default: y = -1 } a. b. c. d. 5. 1 -1 0 None of the above Analyze the following code. int x = 0; int y = ((x<100) && (x>0)) ? 1: -1; a. b. c. d. The code has a syntax error because & must be &&. y becomes 1 after the code is executed. y becomes -1 after the code is executed. None of the above. 6. Which a. b. c. d. e. of (1 (x (x a, a, 7. Which of the following expression is equivalent to (x > 1). a. b. c. d. e. 8. the following is not a valid boolean expression. < x < 100) = 1) || (x != 1) =< 5) && (x>=5) b, and c are all correct b, and c are all wrong x >= 1 !(x <= 1) !(x = 1) !(x < 1) None of the above Analyze the following two code fragments. (i) int x = 5; if (0 < x) && (x < 100) 2 System.out.println("x is between 1 and 100"); (ii) int x = 5; if (0 < x && x < 100) System.out.println("x is between 1 and 100"); a. b. c. d. e. 9. The first fragment has a syntax error on the second line. The second fragment has a syntax error on the second line. Both fragments produce the same output. Both fragments compile, but produce different result. None of the above. Analyze the following fragment. double x = 0; double d = 1; switch (d + 4) { case 5: x++; case 6: --x; } a. The required break keyword is missing in the switch statement. b. The required default keyword is missing in the switch statement c. The switch control variable cannot be double d. a, b, and c are all correct. e. a, b, and c are all incorrect. 10. Analyze the following code. int x = 0; ; a. b. c. d. if (x > 0) { System.out.println("x"); } The symbol x is always printed. The value of variable x is always printed. Nothing is printed because x > 0 is false. None of the above. 11. Which least once. a. b. c. d. 12. of the loop statements always have their body executed at The while loop The do-while loop The for loop None of the above Analyze the following code. int x = 1; while (0 < x) & (x < 100) System.out.println(x++); 3 a. b. c. d. e. 13. The The the The not The The loop runs for ever. code does not compile because the loop body is not in braces. code does not compile because (0 < x) & (x < 100) is enclosed in a pair of parentheses. number 1 to 99 are displayed. number 2 to 100 are displayed. Analyze the following code. double sum = 0; for (double d = 0; d < 10; sum += sum + d) d += 0.1; } a. b. c. d. 14. The program has a syntax error because the adjustment statement is incorrect in the for loop. The program has a syntax error because the control variable in the for loop cannot be of the double type. The program compiles but does not stop because d would always be less than 10. The program compiles and runs fine. What is y after the following for loop statement is executed? int y = 0; for (int i = 0; i < 10; ++i) y += 1; } a. b. 15. { 9 10 c. d. e. { 11 12 None of the above. What balance after the following code is executed? int balance = 10; while (balance >= 1) { if (balance < 9) continue; balance = balance - 9; } a. b. c. d. e. -1 0 1 2 The loop does not end 16. What is the value of balance after the following code is executed? 4 int balance = 10; while (balance >= 1) { if (balance < 9) break; balance = balance - 9; } a. b. c. d. e. 17. –1 0 1 2 None of the above What is x after evaluating x = (2 > 3) ? 2 : 3; a. b. c. d. 2 3 4 None of the above 18. Analyze the following code: boolean even = ((231 % 2) == 0); if (even = true) System.out.println("It is even!"); else System.out.println("It is odd!"); a. b. c. d. e. The program The program The program The program None of the has a syntax error has a runtime error displays “It is odd!” displays “It is even!” above 19. Analyze the following code: 5 (A) if (income <= 10000) tax = income * 0.1; else if (income <= 20000) tax = 1000 + (income – 10000) * 0.15; else tax = 1000 + 1500 + (income – 20000) * 0.18; (B) if (income <= 20000) tax = 1000 + (income – 10000) * 0.15; else if (income <= 10000) tax = income * 0.1; else if (income <= 20000) tax = 1000 + (income – 10000) * 0.15; else tax = 1000 + 1500 + (income – 20000) * 0.18; (C) if (income <= 10000) tax = income * 0.1; else if (income > 10000 && income <= 20000) tax = 1000 + (income – 10000) * 0.15; else tax = 1000 + 1500 + (income – 20000) * 0.18; a. b. c. d. A and B are equivalent. A and C are equivalent. B and C are equivalent. A, B and C are equivalent. Part II. (1 pts) Correctly indent the following statement. if (i > 0) if (j > 0) x = 0; else if (k > 0) y = 0; else z = 0; Part III. (5 pts) Identify and fix errors in the following code: public class Test { public static void main(string[] args) { for (int i = 0; i < 10; i++); { if (j > i) then j++ else j--; } } } 6 Part IV. (5 pts) Show the printout of the following code: public class Test { public static void main(String[] args) { int i = 1; while (i <= 4) { int num = 1; for (int j = 1; j <= i; j++) { System.out.print(num + "bb"); num *= 3; } System.out.println(); i++; } } } Part V: (5 pts each): 1. Write a program that prints numbers from 1 to 50, but for numbers that are multiples of 5, print HiFive, and for numbers that are divisible by both 2 and 3, print Georgia. 7 2. Write a loop that computes 1 2 3 98 99 ... 2 3 4 99 100 8 3. Write a loop that displays the following pattern. 12345678987654321 234567898765432 3456789876543 45678987654 567898765 6789876 78987 898 9 9 Key for Exam2 Part I: Multiple Choice Questions. 1. c 2. d 3. e 4. b 5. c 6. e 7. b 8. a 9. c 10. a 11. b 12. c 13. d 14. b 15. e 16. c 17. b 18. d 19. b Part II. (1 pts) Correctly indent the following statement. if (i > 0) if (j > 0) x = 0; else if (k > 0) y = 0; else z = 0; Part III. (5 pts) Identify and fix errors in the following code: public class Test { public static void main(string[] args) // should be String { for (int i = 0; i < 10; i++); // should remove ; { if (j > i) then // should remove then // j not defined j++ // miss ; else j--; } } } Part IV. (5 pts) Show the printout of the following code: 10 1bb 1bb3bb 1bb3bb9bb 1bb3bb9bb27bb Part V: (a) import javax.swing.JOptionPane; public class Test { /**Main method*/ public static void main(String[] args) { // Prompt the user to enter sales amount String doubleString = JOptionPane.showInputDialog( "Enter sales amount:"); // Convert string to double double salesAmount = Integer.parseInt(doubleString); // Compute sales commission double salesCommission = 0; if (salesAmount <= 5000) salesCommission = salesAmount * 0.08; else if (salesAmount <= 10000) salesCommission = 5000 * 0.08 + (salesAmount - 5000) * 0.1; else salesCommission = 5000 * 0.08 + 10000 * 0.1 + (salesAmount - 10000) * 0.12; // Display results System.out.println("Sales commission is " + salesCommission + " for sales amount of " + salesAmount); } } (b) import javax.swing.JOptionPane; public class Test { /**Main method*/ public static void main(String[] args) { double sum = 0; for (int i = 1; i <= 99; i++) sum += sum + (i * 1.0 / (i + 1)); // Display results System.out.println("Sum is " + sum); } } 11 (c) public class Test { /**Main method*/ public static void main(String[] args) { for (int row = 1; row <= 9; row++) { // Print spaces for (int column = 1; column < row; column++) System.out.print(" "); for (int column = row; column <= 9; column++) System.out.print(column); for (int column = 8; column >= row; column--) System.out.print(column); System.out.println(); } } } 12