GSW CSCI 2510 Midterm Practice One Part I: Multiple Choice Questions 1. 24 % 5 is A. 2 B. 0 C. 4 D. 1 E. 3 2. What is the result of 45 / 4? A. 11 B. 12 C. 10 D. 11.25 3. The following code displays . double temperature = 50; if (temperature >= 100) System.out.println("too hot"); else if (temperature <= 40) System.out.println("too cold"); else System.out.println("just right"); A. B. C. D. too hot too cold too hot too cold just right just right 4. 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. B. C. D. E. Code 3 has a syntax error, because you attempt to assign number to even. Code 2 has a syntax error, because you cannot have literals in the conditional expression. All three are correct, but Code 1 is preferred. All three are correct, but Code 2 is preferred. All three are correct, but Code 3 is preferred. 1/6 5. What is the value in count after the following loop is executed? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 9); System.out.println(count); A. B. C. D. E. 11 0 9 8 10 6. What is the output for y? int y = 0; for (int i = 0; i<10; ++i) { y += i; } System.out.println(y); A. B. C. D. E. 45 12 13 10 11 7. bottom. A. B. C. D. is to implement one method in the structure chart at a time from the top to the Stepwise refinement Top-down approach Bottom-up and top-down approach Bottom-up approach 2/6 8. Analyze the following code: public class Test { public static void main(String[] args) { System.out.println(xMethod(5, 500L)); } public static int xMethod(int n, long l) { System.out.println("int, long"); return n; } public static long xMethod(long n, long l) { System.out.println("long, long"); return n; } } A. The program does not compile because the compiler cannot distinguish which xMethod to invoke. B. The program runs fine but displays things other than 5. C. The program displays long, long followed by 5. D. The program displays int, long followed by 5. 9. How many elements are in array double[] list = new double[5]? A. B. C. D. 5 4 6 0 10. If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is A. B. C. D. E. . 3.4 2.0 3.4 5.5 undefined 3/6 Part II. Short Answer Questions 1. How many times is the following loop body repeated? What is the printout of the loop? int i = 0; while (i < 10) { if ((i + 1) % 2 == 0) System.out.println(i); i++; } 2. Convert following if statement used to find interest rate based on year to a switch statement if (numOfYears == 7) annualInterestRate = 7.25; else if (numOfYears == 15) annualInterestRate = 8.50; else if (numOfYears == 30) annualInterestRate = 9.0; else { System.out.println("Wrong number of years"); System.exit(0); } 4/6 3. Suppose the input is 2 3 4 5 0. What is the output of the following code? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number, max; number = input.nextInt(); max = number; while (number != 0) { number = input.nextInt(); if (number > max) max = number; } System.out.println("max is " + max); System.out.println("number " + number); } } 4. Show the printout of the following code public class Test { public static void main(String[] args) { int number = 0; int[] numbers = new int[1]; m(number, numbers); System.out.println("number is " + number + " and numbers[0] is " + numbers[0]); } public static void m(int x, int[] y) { x = 3; y[0] = 3; } } 5/6 5. Suppose the input is 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4. There are 16 one digit integers separated by space. What is the output of the following code? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); double[][] m = new double[4][4]; for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) m[i][j] = input.nextDouble(); System.out.print(ttt(m)); } public static int ttt(double[][] m) { int sum = 0; for (int i = 0; i < m.length; i++) sum += m[i][i]; return sum; } } 6/6