Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Name:_______________________ Covers Chapters 1, 2, 3 (75 mins) CSCI 1301 Introduction to Programming Armstrong Atlantic State University Instructor: Y. Daniel Liang Circle your choices on this paper Part I: Multiple Choice Questions: (1 pt each) (Multiple answers allowed for this test) 1. The binary value 10011 is __________ in hex. a. b. c. d. e. 13 10 12 14 1A # 2. Hexadecimal number A1 is binary number ____________. a. b. c. d. e. 11001001 10010100 11100001 01100001 10100001 # 3. Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to ________. a. b. c. d. A1 B 66 Illegal expression # 4. The expression (int)(76.0252175 * 100) / 100 evaluates to _________. a. b. c. d. 76 76.02 76.0252175 76.03 # 5. a. b. c. d. To add a value 1 to variable x, you write x += x x := x 1; = 1 + x; 1; = x + 1; 1 e. 1 + x = x; # 6. To declare a constant MAX_LENGTH inside a method with value 99.98, you write a. b. c. d. # 7. final float MAX_LENGTH = 99.98; double MAX_LENGTH = 99.98; final MAX_LENGTH = 99.98; final double MAX_LENGTH = 99.98; what is y displayed in the following code? public class Test { public static void main(String[] args) { int x = 1; int y = x++ + x; System.out.println("y is " + y); } } a. b. c. d. # 8. a. b. y y y y is is is is 2. 1. 3. 4. Is 'a' larger than 'A'. true false # 9. The Unicode of 'a' is 97. What is the Unicode for 'c'? a. b. c. d. 98 99 97 96 # 10. Which of the Boolean expressions below is incorrect? a. b. c. d. e. (-10 < x < 0) !(x > 0) && (x > 0) (true) && (3 => 4) (x > 0) || (x < 0) (x != 0) || (x = 0) 2 # 11. Suppose x=10 and y=10 what is x after evaluating the expression (y > 10) && (x++ > 10). a. b. c. 9 11 10 # 12. Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative? a. b. c. d. ((x < 100) && (x (1 > x > 100) || 1 < x < 100 && x ((x < 100) && (x > 1)) && (x < 0) (x < 0) < 0 > 1)) || (x < 0) # 13. Analyze the following code: Code 1: boolean even; if (number % 2 == 0) even = true; else even = false; Code 2: boolean even = (number % 2 == 0); a. b. c. d. # 14. Code Both Both Code 1 has syntax errors. Code 1 and Code 2 are correct, but Code 2 is better. Code 1 and Code 2 have syntax errors. 2 has syntax errors. Assume x is 0. What is the output of the following statement? if (x > 0) print("x is greater than 0"); else if (x < 0) print("x is less than 0"); else print("x equals 0"); a. x equals 0 3 b. c. d. x is less than 0 x is greater than 0 None # 15. Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x++ > 10). a. b. c. # 16. 9 10 11 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. # 17. The The The The program program program program has a runtime error displays "It is even!" displays "It is odd!" has a syntax error What is the printout of the following switch statement? char ch = 'b'; switch (ch) { case 'a': System.out.print(ch); case 'b': System.out.print(ch); case 'c': System.out.print(ch); case 'd': System.out.print(ch); } a. b. c. d. e. bbb b bcd bb abcd # 18. Analyze the following code. int x = 0; 4 int y = ((x < 100) & (x > 0)) ? 1: -1; a. b. c. 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. # 19. Suppose x=0 and y=0 what is x after evaluating the expression (y > 0) && (1 > x++). a. b. c. -1 1 0 # 20. Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if (number % 2 == 0) even = true; else even = false; Code 2: even = (number % 2 == 0) ? true: false; Code 3: even = number % 2 == 0; a. Code 2 has a syntax error, in the conditional expression. b. Code 3 has a syntax error, c. All three are correct, but d. All three are correct, but e. All three are correct, but # 21. Which of the positive. a. if (radius b. if (radius c. if (radius d. if (radius because you cannot have true and false literals because you attempt to assign number to even. Code 2 is preferred. Code 3 is preferred. Code 1 is preferred. following code displays the area of a circle if the radius is != 0) System.out.println(radius * radius * Math.PI); >= 0) System.out.println(radius * radius * Math.PI); > 0) System.out.println(radius * radius * Math.PI); <= 0) System.out.println(radius * radius * Math.PI); # 22. What is 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0? a. There is no guarantee that 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0 is true. b. false c. true # 5 23. The statement System.out.printf("%10s", 123456) outputs ___________. (Note: * represents a space) a. b. c. d. ****123456 123456**** 23456***** 12345***** Part II: Show the output of the following code: (Write your output on the right side) (5 pts) public class Test { public static void main(String[] args) { int x1, x2, i, j, k, y, z; float f; x1 = 1; x2 = 1; y = 5 + x1--; z = 5 + ++x2; i = 6 % 4; j = 1; j += j + 3; k = 25 / 2; f = (float)((2 / 5) * k); System.out.println("x1 is " + x1); System.out.println("x2 is " + x2); System.out.println("i is " + i); System.out.println("j is " + j); System.out.println("k is " + k); System.out.println("y is " + y); System.out.println("z is " + z); System.out.println("f is " + f); } } Part III: A. (5 pts) Write a complete program named Exam1.java. The program reads three double numbers from the keyboard and displays the average of these three numbers. 6 B. (5 pts) Write a program that prompts the user to enter an integer and checks whether the number is divisible by both 5 and 6, or neither of them, or just one of them. Here are some sample outputs for inputs 10, 30, and 23. 10 is divisible by 5 or 6, but not both 30 is divisible by both 5 and 6 23 is not divisible by either 5 or 6 7 Key Part I: Multiple Choice Questions. 1. The binary value 10011 is __________ in hex. a. 13 b. 10 c. 12 d. 14 e. 1A Key:a # 2. Hexadecimal number A1 is binary number ____________. a. 11001001 b. 10010100 c. 11100001 d. 01100001 e. 10100001 Key:e # 3. Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to ________. a. A1 b. B c. 66 d. Illegal expression Key:a # 4. The expression (int)(76.0252175 * 100) / 100 evaluates to _________. a. 76 b. 76.02 c. 76.0252175 d. 76.03 Key:a # 5. To add a value 1 to variable x, you write a. b. c. d. x += 1; x = 1 + x; x := 1; x = x + 1; 8 e. 1 + x = x; Key:abd # 6. a. b. c. d. Key:d To declare a constant MAX_LENGTH inside a method with value 99.98, you write final float MAX_LENGTH = 99.98; double MAX_LENGTH = 99.98; final MAX_LENGTH = 99.98; final double MAX_LENGTH = 99.98; # 7. what is y displayed in the following code? public class Test { public static void main(String[] args) { int x = 1; int y = x++ + x; System.out.println("y is " + y); } } a. y is 2. b. y is 1. c. y is 3. d. y is 4. Key:c # 8. Is 'a' larger than 'A'. a. true b. false Key:a # 9. a. b. c. d. Key:b # 10. The Unicode of 'a' is 97. What is the Unicode for 'c'? 98 99 97 96 Which of the Boolean expressions below is incorrect? 9 a. (-10 < x < 0) b. !(x > 0) && (x > 0) c. (true) && (3 => 4) d. (x > 0) || (x < 0) e. (x != 0) || (x = 0) Key:ace # 11. Suppose x=10 and y=10 what is x after evaluating the expression (y > 10) && (x++ > 10). a. 9 b. 11 c. 10 Key:c # 12. Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative? a. b. c. d. Key:d # 13. ((x < 100) && (x > 1)) && (x < 0) (1 > x > 100) || (x < 0) 1 < x < 100 && x < 0 ((x < 100) && (x > 1)) || (x < 0) Analyze the following code: Code 1: boolean even; if (number % 2 == 0) even = true; else even = false; Code 2: boolean even = (number % 2 == 0); a. Code 1 has syntax errors. b. Both Code 1 and Code 2 are correct, but Code 2 is better. c. Both Code 1 and Code 2 have syntax errors. d. Code 2 has syntax errors. Key:b 10 # 14. Assume x is 0. What is the output of the following statement? if (x > 0) print("x is greater than 0"); else if (x < 0) print("x is less than 0"); else print("x equals 0"); a. x equals 0 b. x is less than 0 c. x is greater than 0 d. None Key:a # 15. Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x++ > 10). a. 9 b. 10 c. 11 Key:b # 16. 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. The program has a runtime error b. The program displays "It is even!" c. The program displays "It is odd!" d. The program has a syntax error Key:b # 17. What is the printout of the following switch statement? char ch = 'b'; switch (ch) { case 'a': 11 System.out.print(ch); case 'b': System.out.print(ch); case 'c': System.out.print(ch); case 'd': System.out.print(ch); } a. bbb b. b c. bcd d. bb e. abcd Key:a # 18. Analyze the following code. int x = 0; int y = ((x < 100) & (x > 0)) ? 1: -1; a. The code has a syntax error because & must be &&. b. y becomes -1 after the code is executed. c. y becomes 1 after the code is executed. Key:b # 19. Suppose x=0 and y=0 what is x after evaluating the expression (y > 0) && (1 > x++). a. -1 b. 1 c. 0 Key:c # 20. Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if (number % 2 == 0) even = true; else even = false; Code 2: even = (number % 2 == 0) ? true: false; 12 Code 3: even = number % 2 == 0; a. Code 2 has a syntax error, because you cannot have true and false literals in the conditional expression. b. Code 3 has a syntax error, because you attempt to assign number to even. c. All three are correct, but Code 2 is preferred. d. All three are correct, but Code 3 is preferred. e. All three are correct, but Code 1 is preferred. Key:d # 21. Which of the following code displays the area of a circle if the radius is positive. a. if (radius != 0) System.out.println(radius * radius * Math.PI); b. if (radius >= 0) System.out.println(radius * radius * Math.PI); c. if (radius > 0) System.out.println(radius * radius * Math.PI); d. if (radius <= 0) System.out.println(radius * radius * Math.PI); Key:c # 22. What is 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0? a. There is no guarantee that 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0 is true. b. false c. true Key:a # 23. The statement System.out.printf("%10s", 123456) outputs ___________. (Note: * represents a space) a. b. c. d. Key:a ****123456 123456**** 23456***** 12345***** Part II: Show the output of the following code: (5 pts) public class Test { public static void main(String[] args) { int x1, x2, i, j, k, y, z; float f; x1 = 1; x2 = 1; y = 5 + x1--; z = 5 + ++x2; i = 6 % 4; j = 1; j += j + 3; 13 k = 25 / 2; f = (float)((2 / 5) * k); System.out.println("x1 is " + x1); System.out.println("x2 is " + x2); System.out.println("i is " + i); System.out.println("j is " + j); System.out.println("k is " + k); System.out.println("y is " + y); System.out.println("z is " + z); System.out.println("f is " + f); } } x1 is 0 x2 is 2 i is 2 j is 5 k is 12 y is 6 z is 7 f is 0.0 Part III: B. (5 pts) Write a complete program named Exam1.java. The program reads three double numbers from the keyboard and displays the average of these three numbers. import java.util.Scanner; public class Test { /**Main method*/ public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter an integer System.out.print("Enter number1:"); int number1 = input.nextInt(); System.out.print("Enter number2:"); int number2 = input.nextInt(); System.out.print("Enter number3:"); int number3 = input.nextInt(); // Display results System.out.println("The average is " + (number1 + number2 + number3) / 3); } } 14 B. (5 pts) Write a program that prompts the user to enter an integer and checks whether the number is divisible by both 5 and 6, or neither of them, or just one of them. Here are some sample outputs for inputs 10, 30, and 23. 10 is divisible by 5 or 6, but not both 30 is divisible by both 5 and 6 23 is not divisible by either 5 or 6 import java.util.Scanner; public class Exercise3_12 { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter an integer System.out.print("Enter an integer: "); int number = input.nextInt(); if (number % 5 == 0 && number % 6 == 0) System.out.println(number + " is divisible by both 5 and 6"); else if (number % 5 == 0 ^ number % 6 == 0) System.out.println(number + " is divisible by both 5 and 6, but not both"); else System.out.println(number + " is not divisible by either 5 or 6"); } } 15