General Instructions Read and follow all directions carefully. CSCI 1226 A Test #1 Wednesday, 10 October, 2018 Name: Student #: When writing programs or program segments, use the conventions used in the text for identifiers and indentation. Print clearly so that the grader can tell that the conventions are being followed. You do not need to include comments unless I specifically ask for them. You do not need to include the class declaration unless I specifically ask for it. You do not need to include import commands unless I specifically ask for them. For any question that requires input, you can assume a Scanner named kbd has already been created. You can use Sop and Sopln for System.out.print and System.out.println. There are 86 points available. You have 75 minutes to complete it. Good luck. Question Score Out of 1 6 2 8 3 6 4 12 5 6 6 10 7 6 8 6 9 12 10 4 11 10 TOTAL 1 of 8
1. [6] Declare a local variable for each of the following items. a) The height of a cannon ball above the ground. b) The mass of an ounce of gold (31.1035 grams). c) The user s name. d) What number of students passed a test. e) Whether the user answered Yes to a question. 2. [8] Circle exactly four syntax errors in the following code. For each error indicate briefly (one or two words) how to fix the problem. (The program fragment is supposed to tell us the product of 23 and 711, just as it says.) System.out.print("\n\nSimple Product\n\n") int = n; n = 23 x 711; System.out.println("23 times 711 is " n); 3. [6] Input, output, or both? (Circle the correct answer.) a) keyboard? Input Output Both b) LED? Input Output Both c) microphone? Input Output Both d) mouse? Input Output Both e) speaker? Input Output Both f) touch-screen? Input Output Both 2 of 8
4. [12] Translate the following English conditions into Java logical expressions. a) a and b are both less than 10. b) answer starts with an A or an a c) either y or z (or both) is less than 10 d) m is equal to n e) word is not equal to Yes or No. f) x plus 10 is greater than or equal to y plus z. 5. [6] Show the output of the following code: height = 4; for (int line = 1; line <= height; ++line) { for (int dot = 1; dot <= line; ++dot) { System.out.print("."); 3 of 8
6. [10] For each of the bolded method calls below, show the first line of the method definition. (Do not try to write the whole definition! Do not include the class definition.) The variables below may be used in the method calls. double x,; int n; String name; a) x = evaluate(17.1); b) show(name); c) n = morph(true, x); 7. [6] Consider the following code fragment: for (int i = 1; i <= 3; ++i) { num = kbd.nextint(); while (num < 0) { num = kbd.nextint(); System.out.println(i + " --> " + num); What will the output be if the user types (all on one line): 5-10 -5 20-30 40 20-5 -1 4 of 8
8. [6] Write a program fragment that reads an integer grade and produces the message Good. if that grade is greater than 80. 9. [12] Write a program fragment to read and verify the course number of an undergraduate computer science course at Saint Mary s University. The course number is at least 1000 but less than 5000. The program fragment should continue reading input until the user enters a valid number. When an invalid number is entered the program should notify the user and ask for another course number. For example: Enter course #: 226 That's not valid! Enter course #: 5026 That's not valid! Enter course #: 1226 OK! The user typed the numbers. The computer typed everything else. 5 of 8
10. [4] Based on your knowledge of our style guidelines, say what each of the items on the left is an example of. i) FileManager ii) handleexception iii) iv) MAX_DISKS myname a) algorithm b) class c) comment d) constant e) method f) variable 11. [10] Multiple Choice. Circle the letter of the best available answer. CPU stands for a) Canadian Programmers Union. b) Central Processing Unit. c) Central Programming Unit. d) Corporate Processing Union. e) Corporate Programming Unit. If program is expecting an integer value and user types something different (a word or a double value), the program crashes with a(n) a) ArithmeticException. b) ArrayIndexOutOfBoundsException. c) InputMismatchException. d) NullPointerException. e) NumberFormatException. What is the output of the following code int age = 15; if (age < 18) { System.out.print( Child ); if (age >= 18 && age < 65) { System.out.print( Adult ); else { System.out.print( Senior ); a) Adult b) Child c) Senior d) ChildAdultSenior e) (none of the above) 6 of 8
The mixture of English and computer language used to write algorithms is called a) variable declaration b) machine language c) pseudo-code d) digital skeuomorphism e) polymorphism When we try to create a Scanner object, we got an error shown in the figure below: What is the most likely cause of this error? a) kb should be declared final b) kb should be named kbd c) Scanner should be imported from java.util package d) Scanner should be spelled with a small s (scanner) e) System.in should be replaced by System.out Bit is short for a) basic information type. b) basic instruction type. c) binary digit. d) boolean integer type. e) byte. An algorithm is a) a program for translating high-level code into machine language. b) a program that lets other programs run on a different kind of computer. c) a set of step-by-step instructions for solving a problem. d) any computer program. e) one step in a computer program. 7 of 8
The command to read a single word from the keyboard into a String variable is: a) kbd.next(); b) kbd.nextint(); c) kbd.nextline(); d) kbd.nextstring(); e) kbd.nextword(); If you need your computer to remember a value, you need to create a) a method. b) a String. c) a variable. d) an emulator. e) an import. Suppose we have created a method that prints the message Hello! The return type of the method would be: a) double. b) int. c) static. d) String. e) void. 8 of 8