CS 102 / CS 107 - Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010 What is your name?: There are two sections: I. True/False..................... 60 points; ( 30 questions, 2 points each) II. Multiple Choice............... 40 points; ( 10 questions, 4 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. In Java a class called Book should be stored in a file called Book.java 2. In Eclipse it is possible to step through a program one line at a time, however to do this you must enclose the code in the program inside of special comment markers. 3. If you want to use Eclipse to develop your Java programs, your computer must also have a Java Development Kit (JDK) installed. 4. Java programs can be compiled and run from the command line. 5. The main() method in a Java program must always call a mainloop() method, otherwise the program cannot run. 6. The name of a Java class must begin with a capital letter, otherwise the program will not run. 7. Variable names in Java by convention use mixed case, where the words are concatenated together and the first letter of each word is capitalized. 8. In a Java program a method that does not return anything can either have a return type of void or have no return type listed at all. 9. Java methods are private by default, which means they cannot be called from outside of that file. 10. The ASCII code value for A is decimal 65 11. The ASCII code value for a is decimal 95 12. The decimal value 23 in binary is 11011 13. Binary numbers use a left-most digit of 2 to represent negative numbers CS 102 / CS 107 - Intro. to Programming, Midterm Exam #1, page 1 of 8
14. Single line comments using // can be nested inside block comments using /* */ 15. A single System.out.println(...) statement can cause 3 output lines to be displayed. 16. Several System.out.print(...) statements can cause a single line of output to be displayed. 17. Each opening brace must have a corresponding closing brace 18. Variables that are of type boolean can be used interchangeably with variables of type int 19. Variables that are of type int can be stored into double variables, but double variables cannot be conversely stored into an int variable. 20. float and double variables are exactly the same. Those two variable declarations can be used interchangeably. 21. To end a Java program both the break or the exit statement may be used. 22. A do loop (also called a do-while loop) is the best choice for displaying a menu and handling the user response. 23. 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); 24. The section of code shown below would compile and run: int sum = 0; for( ; ; ) ; System.out.println( sum); 25. Any code that can be written with a switch-case statement could also be written with multiple if-else statements. 26. The following statements in Java would be useful for helping check if the length of a String is 3 characters long. String name = "123"; if( name.length() = 3) System.out.println("Length is three."); 27. The output of the following lines of code is: Not Done End boolean Done = false; if (Done = false) System.out.println("Not"); if( Done = true) System.out.println("Done "); else System.out.println("Undecided "); System.out.println("End"); CS 102 / CS 107 - Intro. to Programming, Midterm Exam #1, page 2 of 8
28. The output of the following statements is: 10 Done int y = 4; int z = 6; System.out.print( "" + z + y); System.out.println(" Done"); 29. 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"); 30. The following Java program could have all the spaces and new-line characters removed so that it was all on one line, and it would still compile, run, and give the same output. public class Sample public static void main(string[] args) int x = 5; int y = x+2; System.out.println("x+y:" + x + y); CS 102 / CS 107 - Intro. to Programming, Midterm Exam #1, page 3 of 8
II. Multiple Choice (4 points each) 31. What is the output of the code segment shown below: int i=3, j=5; int k = i+++j; System.out.println(i + " " + j + " " + k); a) 3 5 8 b) 4 5 8 c) 4 5 9 d) 3 6 9 32. Assume the code segment shown below, where method swapvalues2 is called. Output of this segment of code is: //... other code int x = 3, y = 7; System.out.println( change( x, y) ); //... other code public int change(int x, int y) int temp = y; y = x; x = temp; return x + y; a) 73 b) 37 c) 10 d) Does not compile CS 102 / CS 107 - Intro. to Programming, Midterm Exam #1, page 4 of 8
33. The output of the following code in Java is equivalent to: a) 3 * 9 b) 3 + 9 c) 39 d) 93 int value = 3; int limit = 9; int result = 0; for(int x=0; x<limit; x++) result = result + value; System.out.println( result); 34. Consider the program segment given below. Its output is: int value = 0; for( int i=0; i<7; i++) value += i%2; System.out.println("Value is: " + value); a) Value is: 3 b) Value is: 7 c) Value is: 10 d) Does not compile 35. Consider the code given below. If its output is: 12 16 20 24 15 20 25 30 18 24 30 36 what are the values for variables start, end, first and last? for( int i=start; i<=end; i++) for( int j=first; j<last; j++) System.out.printf("%5d",i*j); System.out.println(); a) int start=7, end=3, first=6, last=4; b) int start=4, end=7, first=3, last=6; c) int start=7, end=4, first=6, last=3; d) int start=3, end=7, first=4, last=6; CS 102 / CS 107 - Intro. to Programming, Midterm Exam #1, page 5 of 8
36. What is the output of the code given in the two columns below when an object of type Confuse is created and used to call method startup()? class Confuse private int x; private int y; public Confuse() x = 3; y = 6; private void first(int z) x = z; y++; private void second(int s, int t) setxy( (y+t), (x-s)); s = 1; t = 3; private void setxy( int s, int y) x = s; this.y = y; first( y); private void display() System.out.println(x + y); public void startup() first( y); second( y, x); display(); //end class Confuse a) 7 b) 6 c) 12 d) 11 37. Consider method second shown at right, which itself uses method first. For positive numbers, how would you best describe the return value of method second? 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 += 1; return z; public int second(int x, int y) int z=1; for (int i=0; i<y; i++) z = first( y, x); return z; CS 102 / CS 107 - Intro. to Programming, Midterm Exam #1, page 6 of 8
38. What would be the output of the method call: method38( 1324); void method38( 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) 4 b) 1 c) 10 d) Compiler error 39. Consider ClassA given below, along with the driver class ClassADriver for it. class ClassA private int x; public ClassA() x = 1; public ClassA( int x) this.x = x+1; class ClassADriver public void doit() int value = 7; ClassA instance1 = new ClassA( value); instance1.x = value - 1; instance1.changevalue( 2); System.out.println("value is: " + instance1.x); //end ClassADriver public void changevalue(int val) x = x + val; //end ClassA 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 / CS 107 - Intro. to Programming, Midterm Exam #1, page 7 of 8
40. Consider the method shown below. When passed a String, what does it return? public String method40( 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; a) Every other letter of the original String, starting with the first b) Every other letter of the original String, starting with the second c) The original String with all duplicate letters eliminated d) Only the letters from the original String that had duplicates CS 102 / CS 107 - Intro. to Programming, Midterm Exam #1, page 8 of 8
CS 102 / CS 107 - Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010 What is your name?: There are two sections: I. True/False..................... 60 points; ( 30 questions, 2 points each) II. Multiple Choice............... 40 points; ( 10 questions, 4 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. In Java a class called Book should be stored in a file called Book.java 2. In Eclipse it is possible to step through a program one line at a time, however to do this you must enclose the code in the program inside of special comment markers. 3. If you want to use Eclipse to develop your Java programs, your computer must also have a Java Development Kit (JDK) installed. 4. Java programs can be compiled and run from the command line. 5. The main() method in a Java program must always call a mainloop() method, otherwise the program cannot run. 6. The name of a Java class must begin with a capital letter, otherwise the program will not run. 7. Variable names in Java by convention use mixed case, where the words are concatenated together and the first letter of each word is capitalized. 8. In a Java program a method that does not return anything can either have a return type of void or have no return type listed at all. 9. Java methods are private by default, which means they cannot be called from outside of that file. 10. The ASCII code value for A is decimal 65 11. The ASCII code value for a is decimal 95 12. The decimal value 23 in binary is 11011 13. Binary numbers use a left-most digit of 2 to represent negative numbers CS 102 / CS 107 - Intro. to Programming, Midterm Exam #1, page 1 of 8
14. Single line comments using // can be nested inside block comments using /* */ 15. A single System.out.println(...) statement can cause 3 output lines to be displayed. 16. Several System.out.print(...) statements can cause a single line of output to be displayed. 17. Each opening brace must have a corresponding closing brace 18. Variables that are of type boolean can be used interchangeably with variables of type int 19. Variables that are of type int can be stored into double variables, but double variables cannot be conversely stored into an int variable. 20. float and double variables are exactly the same. Those two variable declarations can be used interchangeably. 21. To end a Java program both the break or the exit statement may be used. 22. A do loop (also called a do-while loop) is the best choice for displaying a menu and handling the user response. 23. 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); 24. The section of code shown below would compile and run: int sum = 0; for( ; ; ) ; System.out.println( sum); 25. Any code that can be written with a switch-case statement could also be written with multiple if-else statements. 26. The following statements in Java would be useful for helping check if the length of a String is 3 characters long. String name = "123"; if( name.length() = 3) System.out.println("Length is three."); 27. The output of the following lines of code is: Not Done End boolean Done = false; if (Done = false) System.out.println("Not"); if( Done = true) System.out.println("Done "); else System.out.println("Undecided "); System.out.println("End"); CS 102 / CS 107 - Intro. to Programming, Midterm Exam #1, page 2 of 8
28. The output of the following statements is: 10 Done int y = 4; int z = 6; System.out.print( "" + z + y); System.out.println(" Done"); 29. 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"); 30. The following Java program could have all the spaces and new-line characters removed so that it was all on one line, and it would still compile, run, and give the same output. public class Sample public static void main(string[] args) int x = 5; int y = x+2; System.out.println("x+y:" + x + y); CS 102 / CS 107 - Intro. to Programming, Midterm Exam #1, page 3 of 8
II. Multiple Choice (4 points each) 31. What is the output of the code segment shown below: int i=3, j=5; int k = i+++j; System.out.println(i + " " + j + " " + k); a) 3 5 8 b) 4 5 8 c) 4 5 9 d) 3 6 9 32. Assume the code segment shown below, where method swapvalues2 is called. Output of this segment of code is: //... other code int x = 3, y = 7; System.out.println( change( x, y) ); //... other code public int change(int x, int y) int temp = y; y = x; x = temp; return x + y; a) 73 b) 37 c) 10 d) Does not compile CS 102 / CS 107 - Intro. to Programming, Midterm Exam #1, page 4 of 8
33. The output of the following code in Java is equivalent to: a) 3 * 9 b) 3 + 9 c) 39 d) 93 int value = 3; int limit = 9; int result = 0; for(int x=0; x<limit; x++) result = result + value; System.out.println( result); 34. Consider the program segment given below. Its output is: int value = 0; for( int i=0; i<7; i++) value += i%2; System.out.println("Value is: " + value); a) Value is: 3 b) Value is: 7 c) Value is: 10 d) Does not compile 35. Consider the code given below. If its output is: 12 16 20 24 15 20 25 30 18 24 30 36 what are the values for variables start, end, first and last? for( int i=start; i<=end; i++) for( int j=first; j<last; j++) System.out.printf("%5d",i*j); System.out.println(); a) int start=7, end=3, first=6, last=4; b) int start=4, end=7, first=3, last=6; c) int start=7, end=4, first=6, last=3; d) int start=3, end=7, first=4, last=6; CS 102 / CS 107 - Intro. to Programming, Midterm Exam #1, page 5 of 8
36. What is the output of the code given in the two columns below when an object of type Confuse is created and used to call method startup()? class Confuse private int x; private int y; public Confuse() x = 3; y = 6; private void first(int z) x = z; y++; private void second(int s, int t) setxy( (y+t), (x-s)); s = 1; t = 3; private void setxy( int s, int y) x = s; this.y = y; first( y); private void display() System.out.println(x + y); public void startup() first( y); second( y, x); display(); //end class Confuse a) 7 b) 6 c) 12 d) 11 37. Consider method second shown at right, which itself uses method first. For positive numbers, how would you best describe the return value of method second? 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 += 1; return z; public int second(int x, int y) int z=1; for (int i=0; i<y; i++) z = first( y, x); return z; CS 102 / CS 107 - Intro. to Programming, Midterm Exam #1, page 6 of 8
38. What would be the output of the method call: method38( 1324); void method38( 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) 4 b) 1 c) 10 d) Compiler error 39. Consider ClassA given below, along with the driver class ClassADriver for it. class ClassA private int x; public ClassA() x = 1; public ClassA( int x) this.x = x+1; class ClassADriver public void doit() int value = 7; ClassA instance1 = new ClassA( value); instance1.x = value - 1; instance1.changevalue( 2); System.out.println("value is: " + instance1.x); //end ClassADriver public void changevalue(int val) x = x + val; //end ClassA 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 / CS 107 - Intro. to Programming, Midterm Exam #1, page 7 of 8
40. Consider the method shown below. When passed a String, what does it return? public String method40( 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; a) Every other letter of the original String, starting with the first b) Every other letter of the original String, starting with the second c) The original String with all duplicate letters eliminated d) Only the letters from the original String that had duplicates CS 102 / CS 107 - Intro. to Programming, Midterm Exam #1, page 8 of 8