CSCI 1226 Test #1 February 2017 General Instructions CSCI 1226 Sample Midterm Exam (A bit long since it combines parts of three earlier tests) Read and follow all directions carefully. 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. Note: not all spaces need to be filled in, but you will not need more spaces than are provided. There are 72 points available. You have 75 minutes to complete it. Good luck. 1. [12] Declare each of the following items. Use appropriate names, types, and styles. Create the most reasonable kind of item. Initialize the item if appropriate. a) the number of words entered by the user int numwords; b) the time taken by a race car for one circuit of the race track double laptime; c) whether the user is 65 or older boolean over65; d) the number of hours in a day (which is 24) public static final int HOURS_PER_DAY = 24; 2. [4] Show the output for the following program fragment. Put each character in a box by itself. int n = 42; System.out.print( The value is: ); System.out.println(n); System.out.print( The next value will be + n + 1); T h e v a l u e i s : 4 2 _ _ _ _ _ _ _ _ _ _ _ _ _ T h e n e x t v a l u e w i l l b e 4 2 1 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 1 of 9
3. [2] Show the output of the following program fragment. Put each character in a box by itself. System.out.print( Open file C:\temp\newfile.txt ); O p e n f i l e C : e m p _ _ _ _ _ _ _ _ _ e w f i l e. t x t _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 4. [10] Translate the following English conditions into Java logical expressions. a) a is equal to b a == b b) b is not equal to c b!= c c) m and n are both greater than 0 m > 0 && n > 0 d) one (or both) of x and y is less than 0 x < 0 y < 0 e) resp is not No (regardless of case)!resp.equalsignorecase( No ) f) s1 starts with the letters of s2 s1.startswith(s2) 5. [4] For each of the following method calls, say whether the method is void or valuereturning. For the value-returning methods, say what the returned data type is. a) kbd.nextint() // Note: kbd is a Scanner void value-returning: int b) System.out.println() // Note: System.out is a PrintStream void value-returning: void c) response.touppercase() // Note: response is a String void value-returning: String d) Math.round(avg) // Note: avg is a double void value-returning: long or int 2 of 9
6. [6] Write a code fragment that declares and reads a word (myword) and a double (mydbl), then pauses with the message Press Enter. Assume that a Scanner named kbd has already been created. String myword; double mydbl; myword = kbd.next(); mydbl = kbd.nextdouble(); kbd.nextline(); Sopln( Press enter ); kbd.nextline(); 7. [8] Write a conditional control to assign a value to the variable toll based on the value in the variable vehicleclass, based on the following chart. vehicleclass 1 2 3 or 4 (other) toll 3.00 4.00 5.00 10.00 if (vehicleclass == 1) { toll = 3.00; else if (vehicleclass == 2) { toll = 4.00; else if (vehicleclass == 3 vehicleclass == 4) { toll = 5.00; else { toll = 10.00; 3 of 9
8. [8] Write a loop (and its associated initializations) to sum a sequence of positive integers entered by the user. The loop stops when the user enters zero or a negative number. Assume that a Scanner named kbd has already been created. sum = 0; num = kbd.nextint(); while (num > 0) { sum += num; num = kbd.nextint(); kbd.nextline(); 9. [8] Write nested for loops to produce the following output: * ** *** **** for (int line = 1; line <= 4; ++line) { for (int star = 1; star <= line; ++star) { Sop( * ); Sopln(); 4 of 9
10. [10] Write the definition of printline(int numtimes, String part). It prints a line of numtimes copies of part. For example, printline(50, = ) produces: ================================================== (The next thing printed will start just below the first = sign.) private static void printline(int numtimes, String part) { for (int time = 1; time <= numtimes; ++time) { Sop(part); Sopln(); 11. [12] Multiple Choice: select the best available answer from the options shown. A byte is made up of a) 0 or 1 bits. b) 2 bits. c) 8 bits. d) 256 bits. e) as many bits as are required to represent the object the programmer created. An example of an input device is a) a monitor. b) a mouse. c) a printer. d) the CPU. e) (all of the above) 5 of 9
An imperative computer programming language is one that a) has us draw pictures to show the computer what to do. b) has us explain to the computer what things mean. c) has us specify which operations are to be carried out at the same time. d) has us tell the computer what to do. e) is only understood by the CPU of the same kind of computer. Based on the style guidelines for this course, numstudents would be the name of a a) class. b) constant. c) method. d) variable. e) (any of the above) The command to create a Scanner named kbd is a) Scanner kbd = new Scanner(); b) Scanner kbd = new Scanner(System) c) Scanner kbd = new Scanner(System.in); d) Scanner kbd = new Scanner(System.out); e) Scanner kbd = new Scanner(System.out.print); The correct header for the main method of a program is a) public void main() b) public void static main() c) public void main(string args) d) public static void main(string[] args) e) public void static main(string[] args) The difference between main memory and secondary memory is a) main memory is inside the computer and secondary memory is outside. b) main memory is on disks and tapes, but secondary memory is just on disks. c) main memory is used for programs that are running, and secondary memory is for long-term storage. d) secondary memory is faster than main memory. e) secondary memory holds less than main memory. 6 of 9
The output of the command System.out.print( 7 / 4 = + (7 / 4)) is a) 1 = 1 b) 1.75 = 1.75 c) 7 / 4 = 1 d) 7 / 4 = 1.75 e) 7 / 4 = (7 / 4) The value of the expression 2 + 8 / 2 + 3 is a) 2 b) 2.0 c) 8 d) 9 e) 9.0 Which of the following loops does NOT iterate exactly ten times? a) for (int i = 0; i < 10; i++) { b) for (int i = 0; i <= 10; i++) { c) for (int i = 1; i <= 10; i++) { d) for (int i = 10; i > 0; i--) { e) (all of the loops above iterate exactly ten times) A method body can contain a) assignment commands. b) input commands. c) method calls. d) output commands. e) (any of the above) A value-returning method is given information by a client program, and generates an answer. The answer should be a) kept private to the class. b) kept private to the method. c) printed for the user to see and returned to the client. d) printed for the user to see. e) returned to the client. 7 of 9
Methods in the class Math (such as round, random, max, sin, cos, ) are declared a) private static b) private static final c) private void d) public static e) public void Suppose we intend to create a toroman method, which converts an integer value to its Roman numeral equivalent. For example, the Roman numeral for 42 is XLII. (Note: the method builds the Roman numeral, but it does not print the Roman numeral.) Which of the following is the best stub for this method? a) public static int toroman() {return 0; b) public static int toroman(string s) {return 0; c) public static String toroman(int n) {return XLII ; d) public static void toroman(int n) { e) public static void toroman(int n) {System.out.print( XLII ); When we try to compile the following program public class MultiChoice { public static void main(string[] args) { dothis(); private void dothis() { System.out.println("Done!"); we get the error message MultiChoice.java:3: non-static method dothis() cannot be referenced from a static context dothis(); ^ 1 error The problem is a) the method dothis should have been declared public and static. b) the method dothis should have been declared public. c) the method dothis should have been declared static. d) the method main should have been declared private. e) the method main should not have been declared static. 8 of 9
(This page is for your rough calculations. You may tear it off the exam booklet.) 9 of 9