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
CSC-111 Intro to Java Programming Exam #3 Chapters 5 and 6 Closed Book / Closed Notes MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) How many times will the following code print ʺWelcome to Javaʺ? 1) int count = 0; while (count < 10) { System.out.println(ʺWelcome to Javaʺ); count++; } A) 8 B) 9 C) 10 D) 11 E) 0 2) How many times will the following code print ʺWelcome to Javaʺ? 2) int count = 0; while (count++ < 10) { System.out.println(ʺWelcome to Javaʺ); } A) 8 B) 9 C) 10 D) 11 E) 0 3) How many times will the following code print ʺWelcome to Javaʺ? 3) int count = 0; do { System.out.println(ʺWelcome to Javaʺ); count++; } while (count < 10); A) 8 B) 9 C) 10 D) 11 E) 0 4) How many times will the following code print ʺWelcome to Javaʺ? 4) int count = 0; do { System.out.println(ʺWelcome to Javaʺ); } while (count++ < 10); A) 8 B) 9 C) 10 D) 11 5) Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100? A: double sum = 0; for (int i = 1; i <= 99; i++) { sum = i / (i + 1); } System.out.println(ʺSum is ʺ + sum); B: 1 E) 0 5) double sum = 0; for (int i = 1; i < 99; i++) { sum += i / (i + 1); } System.out.println(ʺSum is ʺ + sum); C: double sum = 0; for (int i = 1; i <= 99; i++) { sum += 1.0 * i / (i + 1); } System.out.println(ʺSum is ʺ + sum); D: double sum = 0; for (int i = 1; i <= 99; i++) { sum += i / (i + 1.0); } System.out.println(ʺSum is ʺ + sum); E: double sum = 0; for (int i = 1; i < 99; i++) { sum += i / (i + 1.0); } System.out.println(ʺSum is ʺ + sum); A) B B) CDE C) BCD D) CD E) ABCD 6) 6) The following loop displays ________. for (int i = 1; i <= 10; i++) { System.out.print(i + ʺ ʺ); i++; } A) 1 3 5 7 9 B) 2 4 6 8 10 C) 1 2 3 4 5 6 7 8 9 10 D) 1 2 3 4 5 6 7 8 9 E) 1 2 3 4 5 7) What is the output for y? 7) int y = 0; for (int i = 0; i<10; ++i) { y += i; } System.out.println(y); A) 13 B) 12 C) 10 D) 11 2 E) 45 8) Analyze the following fragment: 8) double sum = 0; double d = 0; while (d != 10.0) { d += 0.1; sum += sum + d; } A) The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers. B) The program does not compile because sum and d are declared double, but assigned with integer value 0. C) After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9. D) The program never stops because d is always 0.1 inside the loop. 9) How many times is the println statement executed? 9) for (int i = 0; i < 10; i++) for (int j = 0; j < i; j++) System.out.println(i * j) A) 45 B) 20 C) 100 D) 10 10) 10) What is the printout after the following loop terminates? int number = 25; int i; boolean isPrime = true; for (i = 2; i < number; i++) { if (number % i == 0) { isPrime = false; break; } } System.out.println(ʺi is ʺ + i + ʺ isPrime is ʺ + isPrime); B) i is 6 isPrime is false D) i is 5 isPrime is true A) i is 5 isPrime is false C) i is 6 isPrime is true 11) What is the number of iterations in the following loop? 11) for (int i = 1; i < n; i++) { // iteration } A) n - 1 B) 2 * n C) n 3 D) n + 1 12) Suppose the input for number is 9. What is the output from running the following program? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print(ʺEnter an integer: ʺ); int number = input.nextInt(); int i; boolean isPrime = true; for (i = 2; i < number && isPrime; i++) { if (number % i == 0) { isPrime = false; } } System.out.println(ʺi is ʺ + i); if (isPrime) System.out.println(number + ʺ is primeʺ); else System.out.println(number + ʺ is not primeʺ); } } A) i is 3 followed by 9 is prime C) i is 4 followed by 9 is not prime B) i is 3 followed by 9 is not prime D) i is 4 followed by 9 is prime 4 12) 13) Analyze the following code: 13) import java.util.Scanner; public class Test { public static void main(String[] args) { int sum = 0; for (int i = 0; i < 100000; i++) { Scanner input = new Scanner(System.in); sum += input.nextInt(); } } } A) The program compiles, but does not run because there is not prompting message for entering the input. B) The program does not compile because the Scanner input = new Scanner(System.in); statement is inside the loop. C) The program compiles and runs, but it is not efficient and unnecessary to execute the Scanner input = new Scanner(System.in); statement inside the loop. You should move the statement before the loop. D) The program compiles, but does not run because the Scanner input = new Scanner(System.in); statement is inside the loop. 14) You can always convert a for loop to a while loop. A) true B) false 14) 15) You can always convert a while loop to a for loop. A) true B) false 15) 16) A break statement can be used only in a loop. A) true 16) B) false 17) Which of the loop statements always have their body executed at least once? A) The do-while loop B) The for loop C) The while loop 17) 18) Analyze the following code. 18) int x = 1; while (0 < x) && (x < 100) System.out.println(x++); A) The loop runs forever. B) The numbers 2 to 100 are displayed. C) The numbers 1 to 99 are displayed. D) The code does not compile because (0 < x) && (x < 100) is not enclosed in a pair of parentheses. E) The code does not compile because the loop body is not in the braces. 5 19) What is the value of balance after the following code is executed? 19) int balance = 10; while (balance >= 1) { if (balance < 9) break; balance = balance - 9; } A) -1 B) 0 C) 1 D) 2 20) What is the output of the following fragment? 20) int i = 1; int j = 1; while (i < 5) { i++; j = j * 2; } System.out.println(j); A) 8 B) 64 C) 4 D) 32 E) 16 21) Which of the following expression yields an integer between 0 and 100, inclusive? A) (int)(Math.random() * 100 + 1) B) (int)(Math.random() * 101) C) (int)(Math.random() * 100) + 1 D) (int)(Math.random() * 100) 21) 22) Suppose your method does not return any value, which of the following keywords can be used as a return type? A) void B) public C) int D) double E) None of the above 22) 23) The signature of a method consists of ________. A) return type, method name, and parameter list B) method name and parameter list C) method name D) parameter list 23) 24) Arguments to methods always appear within ________. A) parentheses B) quotation marks C) curly braces D) brackets 24) 6 25) Does the return statement in the following method cause compile errors? 25) public static void main(String[] args) { int max = 0; if (max != 0) System.out.println(max); else return; } A) Yes B) No 26) You should fill in the blank in the following code with ________. 26) public class Test { public static void main(String[] args) { System.out.print(ʺThe grade is ʺ + getGrade(78.5)); System.out.print(ʺ\nThe grade is ʺ + getGrade(59.5)); } public static ________ getGrade(double score) { if (score >= 90.0) return ʹAʹ; else if (score >= 80.0) return ʹBʹ; else if (score >= 70.0) return ʹCʹ; else if (score >= 60.0) return ʹDʹ; else return ʹFʹ; } } A) double B) char D) boolean C) int E) void 27) Given the following method 27) static void nPrint(String message, int n) { while (n > 0) { System.out.print(message); n--; } } What is the printout of the call nPrint(ʹaʹ, 4)? A) aaaa B) invalid call C) aaaaa 7 D) aaa 28) Given the following method 28) static void nPrint(String message, int n) { while (n > 0) { System.out.print(message); n--; } } What is k after invoking nPrint(ʺA messageʺ, k)? int k = 2; nPrint(ʺA messageʺ, k); A) 0 B) 1 C) 2 D) 3 29) Analyze the following code: 29) class Test { public static void main(String[] args) { System.out.println(xmethod(5)); } public static int xmethod(int n, long t) { System.out.println(ʺintʺ); return n; } public static long xmethod(long n) { System.out.println(ʺlongʺ); return n; } } A) The program runs fine but displays things other than 5. B) The program displays int followed by 5. C) The program displays long followed by 5. D) The program does not compile because the compiler cannot distinguish which xmethod to invoke. 8 30) 30) Analyze the following code. public class Test { public static void main(String[] args) { System.out.println(max(1, 2)); } public static double max(int num1, double num2) { System.out.println(ʺmax(int, double) is invokedʺ); if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { System.out.println(ʺmax(double, int) is invokedʺ); if (num1 > num2) return num1; else return num2; } } A) The program runs and prints 2 followed by ʺmax(double, int)ʺ is invoked. B) The program runs and prints 2 followed by ʺmax(int, double)ʺ is invoked. C) The program runs and prints ʺmax(int, double) is invokedʺ followed by 2. D) The program cannot compile because you cannot have the print statement in a non-void method. E) The program cannot compile because the compiler cannot determine which max method should be invoked. 9 31) Analyze the following code. 31) public class Test { public static void main(String[] args) { System.out.println(m(2)); } public static int m(int num) { return num; } public static void m(int num) { System.out.println(num); } } A) The program runs and prints 2 once. B) The program runs and prints 2 twice. C) The program has a compile error because the two methods m have the same signature. D) The program has a compile error because the second m method is defined, but not invoked in the main method. 32) What is k after the following block executes? 32) { int k = 2; nPrint(ʺA messageʺ, k); } System.out.println(k); A) 0 B) 1 C) 2 D) k is not defined outside the block. So, the program has a compile error 33) ________ is to implement one method in the structure chart at a time from the top to the bottom. A) Stepwise refinement B) Bottom-up approach C) Top-down approach D) Bottom-up and top-down approach 33) 34) A method can be declared with no parameters. A) true 34) B) false 35) A method can be defined inside a method in Java. A) true 35) B) false 36) You must have a return statement in a non-void method. A) true B) false 10 36) 37) 37) Analyze the following code: class Test { public static void main(String[] args) { System.out.println(xMethod((double)5)); } public static int xMethod(int n) { System.out.println(ʺintʺ); return n; } public static long xMethod(long n) { System.out.println(ʺlongʺ); return n; } } A) The program does not compile. B) The program displays long followed by 5. C) The program displays int followed by 5. D) The program runs fine but displays things other than 5. 38) Variables defined inside a method are called ________. A) parameters B) local variables C) global variables 38) D) arguments 39) Java allows you to declare methods with the same name in a class. This is called ________. A) method redeclaration B) method overloading C) method duplication D) method overriding 39) 40) Methods can be declared in any order in a class. A) true 40) B) false 41) When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as ________. A) pass by value B) method invocation C) pass by reference D) pass by name 41) 42) Which of the following should be defined as a void method? A) Write a method that returns a random integer from 1 to 100. B) Write a method that prints integers from 1 to 100. C) Write a method that checks whether current second is an integer from 1 to 100. D) Write a method that converts an uppercase letter to lowercase. 42) 43) Does the method call in the following method cause compile errors? 43) public static void main(String[] args) { Math.pow(2, 4); } A) Yes B) No 11 44) All Java applications must have a method ________. A) public static void main(String[] args) B) public static Main(String[] args) C) public static Main(String args[]) D) public void main(String[] args) E) public static main(String[] args) 44) 45) ________ is a simple but incomplete version of a method. A) A non-main method B) A method developed using top-down approach C) A stub D) A main method 45) 12 Answer Key Testname: CSC111EXAM3 1) C 2) C 3) C 4) D 5) D 6) A 7) E 8) A 9) A 10) A 11) A 12) C 13) C 14) A 15) A 16) B 17) A 18) D 19) C 20) E 21) B 22) A 23) B 24) A 25) B 26) B 27) B 28) C 29) C 30) E 31) C 32) D 33) C 34) A 35) B 36) A 37) A 38) B 39) B 40) A 41) A 42) B 43) B 44) A 45) C 13