midterm exam #1 - prof. reed spring 2010 10%logos.cs.uic.edu/programming tests/spring 10 tests...

8
CS 102 - Intro. to Programming, Midterm Exam #1, page 1 of 8 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 gen- erate a compiler error and would execute without the program crashing. I. True/False: (2 points each) T F 1. A Java program can only be run from within BlueJ. T F 2. Variable names in Java must have at least one upper-case letter. T F 3. Variable names in Java are limited to a length of 128 characters. T F 4. Java programs should have a main() method as a starting point. T F 5. Portions of a line of code in Java that go past the 80th column are ignored. T F 6. To end a Java program both the break or the exit statement may be used. T F 7. A do loop (also called a do-while loop) is the best choice for displaying a menu and handling the user response. T F 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. T F 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. T F 10. Any Java program that can be written using the Eclipse development environment can also be written using the BlueJ development environment. T F 11. To change your password in your CS account on ernie.cs.uic.edu, you should use the pass- word command. T F 12. Indentation does not change the meaning of your code in Java.

Upload: phammien

Post on 26-Apr-2018

219 views

Category:

Documents


3 download

TRANSCRIPT

CS 102 - Intro. to Programming, Midterm Exam #1, page 1 of 8

CS 102 - Introduction to ProgrammingMidterm 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 gen-

erate a compiler error and would execute without the program crashing.

I. True/False: (2 points each)

T F 1. A Java program can only be run from within BlueJ.

T F 2. Variable names in Java must have at least one upper-case letter.

T F 3. Variable names in Java are limited to a length of 128 characters.

T F 4. Java programs should have a main() method as a starting point.

T F 5. Portions of a line of code in Java that go past the 80th column are ignored.

T F 6. To end a Java program both the break or the exit statement may be used.

T F 7. A do loop (also called a do-while loop) is the best choice for displaying a menu and handling the user response.

T F 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.

T F 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.

T F 10. Any Java program that can be written using the Eclipse development environment can also be written using the BlueJ development environment.

T F 11. To change your password in your CS account on ernie.cs.uic.edu, you should use the pass-word command.

T F 12. Indentation does not change the meaning of your code in Java.

CS 102 - Intro. to Programming, Midterm Exam #1, page 2 of 8

T F 13. The section of code shown below would compile and run and give as output: 3

T F 14. The section of code shown below would compile and run:

T F 15. The section of code shown below would compile and run and give as output: Is three Done

T F 16. Any code that can be written with multiple if-else statements could also be written with a switch-case statement.

T F 17. Any code that can be written with a switch-case statement could also be written with multiple if-else statements.

T F 18. An if-else statement could be rewritten using two if statements without an else.

T F 19. Postfix ++ has precedence over Prefix ++.

T F 20. The following statements are valid (compile and run) in Java:

T F 21. The following statements are valid (compile and run) in Java:

T F 22. Parenthesis are not necessary in an if statement in Java, but simply help us separate sections of code.

T F 23. The following statements give a run-time error in Java:

int sum = 0;for( int i=0; i<3; i++)

sum += i;System.out.println( sum);

int sum = 0;for( ;sum>0; )

;System.out.println( sum);

int x = 3;if( x = 3);

System.out.print("Is three ");System.out.println("Done");

int x = 0;x+= 1;

int x = 3;int y = 5;x += x+++y++;

String name = "123";if( name.length() == 3) { System.out.println(name.charAt( 3));}

CS 102 - Intro. to Programming, Midterm Exam #1, page 3 of 8

T F 24. To check and see if the value of variable x is between 3 and 9 we could use:.

T F 25. The output of the statement below is: 2

T F 26. The output of the statement below is: They are equal Done

T F 27. After running the code shown below, the value stored in variable y is: 10

T F 28. The output of the following lines of code is: Not Done End

T F 29. The output of the following statements is: 3 Done

T F 30. The following code prints the words: 1 Done

int x = 4;if( 3 < x < 9) { System.out.println("x is between 3 and 9");}

System.out.println( 7 % -3);

String name = "Hamlet";if( name = "Hamlet")

System.out.println("They are equal");System.out.println("Done")

int x = 2;int y = 4;y = x++ * ++y;

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");

int x = 2;int y = 1;System.out.print( "" + x + y);System.out.println(" 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 4 of 8

II. Multiple Choice (4 points each)

31. The output of the following code in Java is equal to:

a) 2 * 5b) 2 + 5c) 25d) 32e) None of the above

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:

a) All the numbers from 1 to 30, with a line break after the number 10b) Even numbers from 1 to 30 all one one line.c) Odd numbers from 1 to 30, each on its own lined) Even numbers from 1 to 30, each on its own linee) None of the above

int value = 2; int limit = 5; int result = 1;

for(int x=0; x<limit; x++) { result = result * value;

} System.out.println( result);

int i=1;while (i<=30) {

i++;System.out.print(i);if( i%2 == 0)

System.out.println();i++;

}

CS 102 - Intro. to Programming, Midterm Exam #1, page 5 of 8

33. Assume an instance of class Confuse is created, and that instance is used to call method doIt(). What is the output?

a) Answer is: 2b) Answer is: 3c) Answer is: 4d) Answer is: 7e) None of the above

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

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 6 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 counter-clockwise circle.

d) A square that moves in an outward growing clockwise spiral.

e) None of the above.

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

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;

}

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 + yb) x * xc) x * yd) x ye) None of the above

CS 102 - Intro. to Programming, Midterm Exam #1, page 7 of 8

36. What does the method shown below do? (Warning, this is a bit involved, so perhaps leave it to the end.)

a) Erase the Stringb) Remove every other letter, starting with the firstc) Remove every other letter, starting with the secondd) Eliminate all duplicate letterse) None of the above

37. What would be the output of the method call: method37( 14679));

a) 9b) 1c) 6d) Compiler errore) None of the above

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()

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); }

CS 102 - Intro. to Programming, Midterm Exam #1, page 8 of 8

38. Consider the class given below, along with the driver class for it.

When running method doIt() in the ClassADriver class, the output will be:a) value is: 5b) value is: 6c) value is: 7d) doesn’t compilee) None of the above

class ClassADriver{

public void doIt(){

int value = 7;ClassA instance1 = new ClassA();instance1.addValue( 5);System.out.println("value is: " +

instance1.x);}

}//end ClassADriver

class ClassA {

private int x;

public ClassA(){ x = 1;

}

public void addValue(int val) {

x = x + val;}

}//end ClassA