CS 102 - Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2010 What is your name?: There are two sections: I. True/False..................... 60 points; ( 30 questions, 2 points each) II. Multiple Choice............... 40 points; ( 8 questions, 5 points each) --------------- 100 points total This test is worth 10% of your final grade. Please fill in your answers on the bubble form. After the test you may keep these pages, but you must turn in your bubble form. This test is open book and open notes. You have 50 minutes. For the multiple choice problems, select the best answer for each one and select the appropriate letter on your answer sheet. Be careful - more than one answer may seem to be correct. Many questions are tricky. Some problems ask you to determine whether something is valid. Something is valid if it would not generate a compiler error and would execute without the program crashing. I. True/False: (2 points each) 1. A Java program can only be run from within BlueJ. 2. Variable names in Java must have at least one upper-case letter. 3. Variable names in Java are limited to a length of 128 characters. 4. Java programs should have a main() method as a starting point. 5. Portions of a line of code in Java that go past the 80th column are ignored. 6. To end a Java program both the break or the exit statement may be used. 7. A do loop (also called a do-while loop) is the best choice for displaying a menu and handling the user response. 8. Consider code used to sum all the numbers between 1 and 1000. Using a for loop will likely mean you will have fewer lines of code as compared to the same code implemented using a while loop. 9. When checking for a logical condition (e.g. whether or not all the pieces are off the board for a player) a while loop is the best choice. 10. Any Java program that can be written using the Eclipse development environment can also be written using the BlueJ development environment. 11. To change your password in your CS account on ernie.cs.uic.edu, you should use the password command. 12. Indentation does not change the meaning of your code in Java. CS 102 - Intro. to Programming, Midterm Exam #1, page 1 of 8
13. The section of code shown below would compile and run and give as output: 3 int sum = 0; for( int i=0; i<3; i++) sum += i; System.out.println( sum); 14. The section of code shown below would compile and run: int sum = 0; for( ;sum>0; ) ; System.out.println( sum); 15. The section of code shown below would compile and run and give as output: Is three Done int x = 3; if( x = 3); System.out.print("Is three "); System.out.println("Done"); 16. Any code that can be written with multiple if-else statements could also be written with a switch-case statement. 17. Any code that can be written with a switch-case statement could also be written with multiple if-else statements. 18. An if-else statement could be rewritten using two if statements without an else. 19. Postfix ++ has precedence over Prefix ++. 20. The following statements are valid (compile and run) in Java: int x = 0; x+= 1; 21. The following statements are valid (compile and run) in Java: int x = 3; int y = 5; x += x+++y++; 22. Parenthesis are not necessary in an if statement in Java, but simply help us separate sections of code. 23. The following statements give a run-time error in Java: String name = "123"; if( name.length() == 3) System.out.println(name.charAt( 3)); CS 102 - Intro. to Programming, Midterm Exam #1, page 2 of 8
24. To check and see if the value of variable x is between 3 and 9 we could use:. int x = 4; if( 3 < x < 9) System.out.println("x is between 3 and 9"); 25. The output of the statement below is: 2 System.out.println( 7 % -3); 26. The output of the statement below is: They are equal Done String name = "Hamlet"; if( name = "Hamlet") System.out.println("They are equal"); System.out.println("Done") 27. After running the code shown below, the value stored in variable y is: 10 int x = 2; int y = 4; y = x++ * ++y; 28. The output of the following lines of code is: Not Done End boolean Done = true; if (Done = false) System.out.println("Not"); if( Done = true) System.out.println("Done "); else System.out.println("Undecided "); System.out.println("End"); 29. The output of the following statements is: 3 Done int x = 2; int y = 1; System.out.print( "" + x + y); System.out.println(" Done"); 30. The following code prints the words: 1 Done char c= a ; switch (c) case a : System.out.print("1"); case b : System.out.print("2"); case c : System.out.print("3"); System.out.println(" Done"); CS 102 - Intro. to Programming, Midterm Exam #1, page 3 of 8
II. Multiple Choice (4 points each) 31. The output of the following code in Java is equal to: a) 2 * 5 b) 2 + 5 c) 25 d) 32 int value = 2; int limit = 5; int result = 1; for(int x=0; x<limit; x++) result = result * value; System.out.println( result); 32. Consider the program segment given below. (Remember that the % operator is the modulus operator, which gives the remainder after integer division.) Its output is: int i=1; while (i<=30) i++; System.out.print(i); if( i%2 == 0) System.out.println(); i++; a) All the numbers from 1 to 30, with a line break after the number 10 b) Even numbers from 1 to 30 all one one line. c) Odd numbers from 1 to 30, each on its own line d) Even numbers from 1 to 30, each on its own line CS 102 - Intro. to Programming, Midterm Exam #1, page 4 of 8
33. Assume an instance of class Confuse is created, and that instance is used to call method doit(). What is the output? class Confuse int x; public Confuse() x = 3; void first( int x, int y) int temp = x; x = y; y = temp; //end method first void second( int a, int b) first( b, a); x = a + 1; //end method second a) Answer is: 2 b) Answer is: 3 c) Answer is: 4 d) Answer is: 7 public void doit() int x = 1; int y = 3; first( x,y); second( y,x); System.out.println("Answer is: " + this.x); //end method doit() //end class Confuse CS 102 - Intro. to Programming, Midterm Exam #1, page 5 of 8
34. Consider the code shown at right below, that uses the Square class demonstrated during class. Note that the display Canvas starts with position (0,0) in the upper-left corner. Adding to the x (horizontal) value moves to the right, and adding to the y value (vertical) moves down. What does the output of this code look like? a) A square that grows larger as it moves to the right. b) A square that moves in a clockwise circle. c) A square that moves in a counterclockwise circle. d) A square that moves in an outward growing clockwise spiral.. public class picture public static void main() Circle firstcircle = new Circle(); firstcircle.makevisible(); for (int i=1;i<31;i++) firstcircle.slowmovevertical((i)*2); firstcircle.slowmovehorizontal((-i)*2); firstcircle.slowmovevertical(-i*2); firstcircle.slowmovehorizontal(i*2); //end for( int i... //end main... //end class picture 35. Consider method second shown at right, which itself uses method first. For positive numbers, how would you best describe its return value? a) x + y b) x * x c) x * y d) x y public int first(int x, int y) int z=0; for (int i=0; i<y; i++) z += x; return z; public int second(int x, int y) int z=1; for (int i=0; i<y; i++) z = first( z, x); return z; CS 102 - Intro. to Programming, Midterm Exam #1, page 6 of 8
36. What does the method shown below do? (Warning, this is a bit involved, so perhaps leave it to the end.) public String method36( String theword) char c; String newword = ""; for( int i=0; i<theword.length(); i++) c = theword.charat( i); newword = ""; for( int j=0; j<theword.length(); j++) if( (j<=i)) newword = newword + theword.charat( j); else if( theword.charat( j)!= c) newword = newword + theword.charat( j); theword = newword; return newword; //end method36() a) Erase the String b) Remove every other letter, starting with the first c) Remove every other letter, starting with the second d) Eliminate all duplicate letters 37. What would be the output of the method call: method37( 14679)); void method37( int number) int x = number; int count = 0; while ( x > 0) x = x / 10; count++; for( int i=0; i<count/2; i++) number = number / 10; System.out.println( number%10); a) 9 b) 1 c) 6 d) Compiler error CS 102 - Intro. to Programming, Midterm Exam #1, page 7 of 8
38. Consider the class given below, along with the driver class for it. class ClassA private int x; public ClassA() x = 1; public void addvalue(int val) x = x + val; //end ClassA class ClassADriver public void doit() int value = 7; ClassA instance1 = new ClassA(); instance1.addvalue( 5); System.out.println("value is: " + instance1.x); //end ClassADriver When running method doit() in the ClassADriver class, the output will be: a) value is: 5 b) value is: 6 c) value is: 7 d) doesn t compile CS 102 - Intro. to Programming, Midterm Exam #1, page 8 of 8