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
CSC202 Exam 2 Name: _________________________ Questions 1 – 12 are worth 5 points each. 1. What is wrong with the following program? public class q1 { public static void main(String[] args) { int i = 1; while (i != 10) { System.out.println(i); i = i + 2; } } } Infinite loop. 2. What is wrong with the following program? public class q2 { public static void main(String[] args) { int i = 10; double j = 25.5; methodA(i, j); } public static void methodB(int a, double b) { System.out.println(a); System.out.println(b); } } cannot find symbol symbol : method methodA(int,double) 3. What is wrong with the following program? public class q3 { public static void main(String[] args) { int j; j = method1(); System.out.println(j); } public static double method1() { double x = 2.3; return x; } } possible loss of precision found required: int j = method1(); : double 4. When run, what will the following program output? public class q4 { public static void main(String[] args) { int i; for (i = 10; i <= 20; i += 2) { System.out.println(i); } } } 10 12 14 16 18 20 2 5. When run, what will the following program output? public class q5 { public static void main(String[] args) { int i = 0; while (i <= 10) { System.out.println(i); i = i + 5; } } } 0 5 10 6. When run, what will the following program output? public class q6 { public static void main(String[] args) { int i = 10; while (i > 100) { System.out.println(i); } System.out.println("Done"); } } Done 3 7. When run, what will the following program output? public class q7 { public static void main(String[] args) { int i, j; for (i = 1; i <= 3; i++) { for (j = 1; j <= 2; j++) { System.out.print("Y"); } System.out.println(); } } } YY YY YY 8. When run, what will the following program output? public class q8 { public static void main(String[] args) { int i; for (i = 1; i <= 3; i++) { methodA(); } } public static void methodA() { System.out.println("B"); } } B B B 4 9. When run, what will the following program output? public class q9 { public static void main(String[] args) { methodA(); methodB(); methodC(); } public static void methodA() { System.out.println("A"); methodB(); System.out.println("A"); } public static void methodB() { System.out.println("B"); methodC(); System.out.println("B"); } public static void methodC() { System.out.println("C"); } } A B C B A B C B C 5 10. When run, what will the following program output? public class q10 { public static void main(String[] args) { System.out.println(method1("Joe")); System.out.println(method1("Jane")); } public static int method1(String a) { if (a.equals("Joe")) return 1; else return 0; } } 1 0 11. When run, what will the following program output? public class q11 { public static void main(String[] args) { int s = 0; int i; for (i = 1; i <= 5; i++) { s += i; } System.out.println(s); } } 15 6 12. In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number: "); int number = keyboard.nextInt(); while (number < 0 || number > 100) { System.out.print("Enter another number: number = keyboard.nextInt(); } Any number between 0 and 100, inclusive. 7 "); 13. Assuming that the file named data.txt exists and contains a series of numbers (1 per line), write a program that will ask the user to input a number, and then print how many times that number is in the file. (20 points). import java.io.*; import java.util.Scanner; public class q13 { public static void main(String[] args) throws IOException { File myFile = new File("data.txt"); Scanner inputFile = new Scanner(myFile); Scanner keyboard = new Scanner(System.in); int count = 0; int choice; int number; System.out.print("Enter a number: "); choice = keyboard.nextInt(); while (inputFile.hasNext()) { number = inputFile.nextInt(); if (choice == number) { count++; } } System.out.println(choice + " appears " + count + " times."); } } 8 14. Write a program that will randomly print one of four possible messages. (20 points) import java.util.Random; public class q14 { public static void main(String[] args) { Random generator = new Random(); int choice = generator.nextInt(4); switch (choice) { case 0: System.out.println("message break; case 1: System.out.println("message break; case 2: System.out.println("message break; case 3: System.out.println("message break; } } } 9 1"); 2"); 3"); 4");