Computer Science S-111a: Intensive Introduction to Computer Science Using Java Handout #11 Your Name Teaching Fellow J a v a Quiz (Unit 3, Test 0 Practice) Multiple-choice questions are worth 2 points if correct on the first try, but only 1 point if right on the second try. Each multiple-choice question has only one correct answer (we think). 1 st 2 nd 1. The program fragment int n, sum = 0, count = 0; do n = in.nextint(); sum += n; count++ ; while (n!= -1); System.out.println( sum / count ); is applied to the keyboard input 3 5-1 What will be the output? A. 4 B. 4.0 C. 2.3 D. 3 E. 2 Summer, 2006 Dr. H. H. Leitner
Computer Science S-111a: Intensive Introduction to Computer Science Using Java Handout #11 2. Which one of the following is a syntactically valid boolean expression whose value is true? A. ((7 < 3)! False) B. ((5 < 4) && (3 * 3 > 8)) C. ((3 < 5) = false) D. (5 >= 5 > 3) E. (1!= 2 5 == 6) 3. Assuming variable n has been properly declared as an int, the statement switch (n) case 1: System.out.println("The number is 1" ); case 2: System.out.println("The number is 2" ); break; default: System.out.println("The number is not 1 or 2"); A. Contains a syntax error, and therefore won t compile. B. Can never print the message "The number is 2" C. Is equivalent to: if (n==1) System.out.println( "The number is 1" ); else if (n==2) System.out.println( "The number is 2" ); else System.out.println("The number is not 1 or 2"); D. Will always print "The number is 2" when n equals 1 E. Will cause an execution error whenever n has a value less than 1. Summer, 2006 Dr. H. H. Leitner
Computer Science S-111a: Intensive Introduction to Computer Science Using Java Page 3 4. The expression (int) (Math.random() * 9-3) produces integer values in the range A. 0 to 6, inclusive B. 0 to 5, inclusive C. 3 to 11, inclusive D. -3 to 5, inclusive E. none of the above 5. Suppose the following code fragment is executed, String s = in.nextline(); char ch = s.charat(s.length() 3); and the input that the user types on the keyboard in response to this code consists of S-111a ROCKS The character variable ch now contains: A. a newline character B. 'S' C. 'K' D. 'C' E. 'O'
Computer Science S-111a: Intensive Introduction to Computer Science Using Java Page 4 6. The following complete program is designed to compute the number of decimal digits, ndigits, in an integer x (where x 0). class Prob6 public static void main (String [] args) int power = 1, ndigits, x; System.out.print("Input a value greater than 0: "); x = in.nextint(); while (x > power) power = * 10; ndigits ++; System.out.println("Number of digits is " + ndigits ); Find and correct three small, distinct errors in this code so that it will work as intended, even if x is a power of 10. There might be more than 3 errors, but you need to fix just 3 of them. 7. Consider the creation of a Time class, the values of which represent a time object containing an hour and a minute within a single day. In this problem you will work with the following methods: a constructor method for creating Time objects; an addminutes method to increment a Time object, returning a new Time object a tostring method to output a Time object; and a priorto method to compare two Time objects (to determine which is earlier ) For example, a method that uses this class might contain the following: Time dawn = new Time(6, 35); // creates the AM value 6:35 Time dusk = dawn.addminutes(60*12); // creates the PM value 18:35 Time m = new Time (0, 0); // creates the midnight value System.out.println ( dawn ); System.out.println ( dusk ); System.out.println ( m ); if (m.priorto(dusk)) System.out.println("Hello");
Computer Science S-111a: Intensive Introduction to Computer Science Using Java Page 5 This code will print 6:35 AM 6:35 PM Midnight Hello Your task is to replace each of the five??????????s with correct Java code. Note that tostring() should return time values in an intelligent fashion. The dusk value was not output as 18:35, but (correctly) instead as 6:35 PM. Similarly, a Time value whose number of minutes is less than 10 should contain a zero in the output (e.g., 11:05 AM would be printed, and not 11:5 AM). class Time private int hour, minute; public Time (int h, int m)??????????1 public Time addminutes (int m) int totalminutes = (60*hour + minute + m) % (24*60); if (totalminutes < 0) totalminutes = totalminutes + 24*60; return??????????2 public String tostring () if ((hour == 0) && (minute == 0)) return "midnight"; else if (??????????3 ) return "noon"; else??????????4 public boolean priorto (Time t)??????????5