CS 177 Spring 2010 Exam I There are 25 multiple choice questions. Each one is worth 4 points. The total score for the exam is 100. Answer the multiple choice questions on the bubble sheet given. Fill in the Instructor, Course, Signature, Test, and Date blanks in the bubble sheet. For Instructor put your RECITATION INSTRUCTOR S LAST NAME GIVEN BELOW. For Course put CS 177. For Test/Quiz put 01. Fill in the bubbles that correspond to your name, section and Student ID in the bubble sheet. For your section number, use the SECTION NUMBER of your recitation section. Consult the following list: 001 FRI 07:30 LWSN B134 Parantap Roy 002 THU 09:30 BRNG B238 Wang Chen 003 FRI 03:30 LWSN 1106 Serkan Uzunbaz 004 THU 12:30 BRNG B230 Mohammad Kazi 005 THU 12:30 BRNG 1255 Parantap Roy 006 FRI 04:30 HAAS G066 Serkan Uzumbaz 019 THU 10:30 LWSN 1106 Tyler Wykoff For your student ID, use the 10 digit ID number on your student ID card. DO NOT USE YOUR SOCIAL SECURITY NUMBER! Exams without names will be graded as zero. Only the answers on the bubble sheet will be counted. The questions will be discarded. Recitation Section Number Recitation TA s Name Student Last Name Student First Name
1. We have a program named testprogram.java. Identify the correct statement about the code block. public class TestProgram public static void main() System.out.println("I am a correct program!"); A. The program will execute correctly without any changes. B. The program will execute correctly if String[] args is added to the main() method s parameter list. C. The program will execute correctly if we change the name of the file to TestProgram.java. D. The program will execute correctly if String[] args is added to the main() method s parameter list and we change the name of the file to TestProgram.java. 2. The javac compiler converts a.java program to an intermediate.class file also known as java bytecode. Identify the correct statement about java bytecode. A. Java bytecode is the same for all machines and operating systems because java is architecture and operating system independent. B. Java bytecode is the same for all machines but different for different operating systems as java is operating system dependent. C. Java bytecode is the same for all operating systems and different for different machines as java is architecture dependent. D. Java bytecode is different for all machines and operating systems because java is architecture and operating system dependent. 3. Which one of the following is a legal identifier in Java? A. 123abc B. a*b*c C. abc_123 D. abc+123
4. Which of the following methods should be used to convert a String to a double? A. Integer.parseInt() B. Integer.toString() C. Double.parseDouble() D. Double.toString() 5. Identify the correct statements for the following code and corresponding commands. public class WeAreStillTestingAProgram public static void main(string[] args) System.out.println(Double.parseDouble(args[0])); System.out.println(Double.parseDouble(args[1])); System.out.println(Double.parseDouble(args[2])); COMMAND LINE>javac WeAreStillTestingAProgram.java COMMAND LINE>java WeAreStillTestingAProgram xyz abc 12.8091 x1 A. This program produces a compilation error as there are too many arguments in the command line. B. This program produces a runtime error because the first argument cannot be converted to a double. C. This program executes correctly and the ASCII values of the first character of the strings is displayed on the screen. D. This program executes correctly with no output on the screen. 6. Based on the precedence rules for operators in Java, which of the following expressions are equivalent? (i) a + ( b * c ) - ( d / e ) (ii) ( a + b ) * ( c - d ) / e (iii) a + b * c - d / e A. (i) and (ii) B. (i) and (iii) C. (ii) and (iii) D. (i), (ii) and (iii)
7. Choose the correct output for the following code fragment: boolean abc=false; if(!abc && abc) System.out.println("0000"); if(!abc abc) System.out.println("1111"); if(!!!abc) System.out.println("2222"); System.out.println("3333"); A. 0000 B. 1111 C. 2222 D. 3333 8. Choose the correct output for the following code fragment: boolean a = true; boolean b = false; boolean c =!!(a &&!b); System.out.println("!!(" + a + " && " + "!" + b + ") = " + c); A.!!(a && true) = true B.!!(a && true) = false C.!!(true &&!false) = true D.!!(true &&!false) = false 9. What is the value of the following expression in java? 5.5 + 2 * (14 % 4 + 4) / 5 A. 7.5 B. 7.9 C. 8.5 D. None of the above
10. What does the following code do (assume both a and b have previously been declared as an int and initialized to some value)? int t = a + b; b = t - b; a = t - a; A. The program will produce a compiler error because we can t have the same variable appearing on both the sides of an equality sign. B. Leaves the value of a and b unchanged. C. Changes the sign of the values stored in a and b. D. Swaps the values of a and b. 11. Choose the correct output for the following code fragment: double a = 5/3; double b = 5.0/3.0; System.out.print("The value of a is " + a); System.out.print(" The value of b is " + b); A. The value of a is 1 The value of b is 1 B. The value of a is 1.0 The value of b is 1.6666667 C. The value of a is 1.6666667 The value of b is 1.0 D. The value of a is 1.6666667 The value of b is 1.6666667 12. What is the value of args[2] for the following command line statement? COMMAND LINE>java Alpha Beta Gamma Theta A. Alpha B. Beta C. Gamma D. Theta 13. What the decimal equivalent of the binary number 100110? A. 35 B. 36 C. 38 D. 40
14. Choose the correct output for the following code fragment: int x = 12; if (x % 3 == 0) if (x % 4 == 1) System.out.print("Aah"); if (x % 4 == 2) System.out.print("Bar"); if (x % 3 == 1) System.out.print("Foo"); System.out.print("Wow"); A. Aah B. Bar C. Wow D. Nothing is printed. 15. Choose the correct output for the following code fragment: double x = 9 / 4; if (x > 2) System.out.println("The sqaure root of 4 is " + sqrt(x) + '.'); System.out.println("Nothing calculated."); A. Nothing calculated. B. 1.50. C. 1.414. D. Nothing. There is a syntax error.
16. Choose the correct output for the following code fragment: boolean flag1 = true; boolean flag2 = false; boolean flag3 = (flag1 &&!flag2) flag2; boolean flag4 = flag1 && (!flag1 flag2); System.out.println(flag3 + ", " + flag4); A. true, true B. true, false C. false, true D. false, false 17. Some of the boolean expressions below are equivalent. Which one is NOT equivalent to the others? HINT: Make a truth table for the possible values of a and b for each expression. A. a!b B.!(!a b) C. a &&!b D. All three are equivalent. 18. Which of the following statement should we use to raise 3 to the power of 5 and assign it to a variable x? A. double x = Math.pow(3, 5); B. double x = Math.pow(5, 3); C. double x = 3 ^ 5; D. int x = Math.pow(3, 5); 19. For what data types can the operator == be used to determine whether the values of two variables of different types are equal? A. int with char B. int with boolean C. double with String D. String with String
20. Choose the correct output for the following code fragment: double x = 6.59; double y = 9.33; int x1 = Math.round(x); int x2 = (int) x; int y1 = Math.round(y); int y2 = (int) y; System.out.println((x1 + x2) + " and " + (y1 + y2)); A. 12 and 18 B. 12 and 19 C. 13 and 18 D. 13 and 19 21. Assume that a mathematician calls an integer a perfect integer if it is odd and divisible by 11. We want to create a boolean variable that is true when the int x is a perfect integer. Which statement should we use to accomplish this task? A. boolean isperfect = (x % 2 = 1) && (x % 11 = 0); B. boolean isperfect = (x / 2 = 1) && (x / 11 = 0); C. boolean isperfect = (x % 2 == 1) && (x % 11 == 0); D. boolean isperfect = (x / 2 == 1) && (x / 11 == 0);
22. Choose the correct output for the following code fragment: int x = 20, y = 10; if(x > y) if(x < 25 && y > 10) System.out.println("1111"); System.out.println("2222"); if(x < 25 && y > 10) System.out.println("3333"); System.out.println("4444"); A. 1111 B. 2222 C. 3333 D. 4444
23. Choose the correct output for the following code fragment: int x = 12; int y = 15; if(x > y) System.out.println("1111"); if(x > 10) System.out.println("2222"); if(y < 15) System.out.println("3333"); System.out.println("4444"); A. 1111 B. 2222 C. 3333 D. 4444 24. Choose the most suitable option if the following declarations were in a java program. int boolean = 123; boolean true = true; boolean false = false; A. boolean, true, and false are keywords and therefore they are illegal identifiers. B. boolean is a legal identifier. true and false are keywords and therefore they are illegal identifiers. C. true and false are legal identifiers. boolean is a primitive data type and therefore it is a illegal identifier. D. All identifiers in the code fragment are legal.
25. Choose the correct output for the following code fragment: int x = 25; int y = 15; if(x <= y) System.out.println("1111"); if(x == y) System.out.println("2222"); if(x <= 25 && y > 10) System.out.println("3333"); A. 1111 B. 2222 C. 3333 D. Nothing will be printed.