Your Name: Your Pitt (mail NOT peoplesoft) ID: Part Question/s Points available Rubric Your Score A 1-6 6 1 pt each B 7-12 6 1 pt each C 13-16 4 1 pt each D 17-19 5 1 or 2 pts each E 20-23 5 1 or 2 pts each F 24-27 5 1 or 2 pts each G 28-32 5 1 pt each H 33-38 6 1 pt each I 39-46 8 1 pt each Total out of 50 Do Not Open Until the Exam Begins! Note There are NO syntax errors in this exam! Unless the question specifically states that the code might not comple, if you see one, it DOES NOT EXIST!!! A modified String API is on the last page of the exam. the mgmt Page 1
Part A From Exam 1 - worth 6% (1 pt each) Given these variables and corresponding initializations: Variable Declarations int in1, in2; double db1, db2; char ch1, ch2; Variable Initializations in1 = 1; in2 = 2; db1 = 1.1; db2 = 2.2; boolean b1, b2; String s1, s2; ch1 = 'a'; ch2 = 'b'; b1 = true; b2 = false; s1 = "hello"; s2 = "world"; What do these printlns display on the screen? Mark an X if they do not compile. 1 2 3 4 5 6 System.out.println( in1 / in2 ); System.out.println( in1 % in2 ); System.out.println( in1 * db1 ); System.out.println( ch1!= ch2 ); System.out.println( b1 b2 ); System.out.println ( s1 + " " + (in1 + in2) ); Page 2
Part B Types of Data - worth 6% (1 pt each) Mark [T] in the blank for any statements that are true. Leave false statements blank 7 [ ] Primitive types of variables include int, double, char, Boolean, and String. 8 [ ] A primitive type of variable can have only one name and one value. 9 [ ] A reference type of variable such as an object of the Card class and the Deck class that you wrote for Program 09 can have multiple references and can store multiple values of different types. 10 [ ] A primitive type variabls can be accessed, used, and changed only by code that is within the braces where the primitive variable is declared. 11 [ ] A reference types of variable such as an object of the Card class and the Deck class can be accessed, used, and changed by code that is outside the braces where the variable is declared. 12 [ ] All of the data stored in an array must be the same type of data. Page 3
Part C Representing the Structure of Data in Memory - 4 points (1 pt each) In the following questions, a series of drawings similar to those Jim used on the board in class to represent how the data is stored in memory are shown. Mark the drawing that best represents the type of data listed. 13. Mark the sketch that best shows how a primitive variable is represented: int num = 42; 14. Mark the sketch that best shows how a String object is represented: String str = Hi ; Page 4
15. Mark the sketch that best shows how a one-dimensional array is represented: int [ ] list = 1, 2, 3 ; 16. Mark the sketch that best shows how a two-dimensional array is represented: int [ ] [ ] mat = 1, 2, 3, 4 ; Page 5
Part D String Objects worth 5 points (1 pt each ) 17. 1 point Given this code fragment specifically the underlined code: String s1 = "Hi"; String s2 = new String( "Hi" ); if ( s1 == s2 ) System.out.println( "They are equal." ); else System.out.println( "They are NOT equal." ); Mark T in the blank for any of the statements that are true. Leave the false statements unmarked. This will not compile because the use of the equality operator for Strings is not allowed in Java. This compiles but does not always work properly. Depending on the way the Strings are initialized, it could return true or it could return false. This compiles and always works properly. It is always safe to use. 18. 2 points ( 1 error = -1, more than 1 error = -1 ) Given this code fragment: String s3 = "01234"; for( int i = s3.length( ) - 1; i > 0; i-- ) System.out.print( s3.charat( i ) ); System.out.println( ); Write the output for this code in the box below: Page 6
19. 2 points ( 1 error = -1, more than 1 error = -2 ) Given this code fragment: String s4 = "Hello"; for( int i = 0; i < s4.length( ); i++ ) System.out.print(s4.charAt(s4.length( ) - 1 - i)); System.out.println( ); Write the output for this code in the box below: Part E One Dimensional Arrays worth 5 points 20. 1 point Given this method definition: static public int mysterymethod1( int [ ] numbers, int key ) for ( int i = 0; i < numbers.length; i++) if ( numbers[ i ] == key ) return i; return -1; What does this method do? (Do NOT write what the code does: It is traversing the array. Rather, write what the method accomplishes when it is finished)? What would be a better name for this method? Page 7
21. 1 point Given this main method code: static public void main( String [ ] args ) int [ ] list = 4, 2, 8, 5, 3, 1 ; mysterymethod2( list ); And this method definition: static public void mysterymethod2( int [ ] numbers ) for ( int i = 0; i < numbers.length; i++ ) if ( numbers[ i ] % 2 == 1) System.out.print( numbers[ i ] + " " ); System.out.println( ); Will the method, mysterymethod2 crash when it is executed [ yes ] / [ no ]? If the answer is [ yes ], why? If the answer is [ no ], write the output in the box below: Page 8
22. 1 point Given this main method code: static public void main( String [ ] args ) int [ ] list = 4, 2, 8, 5, 3, 1 ; mysterymethod4( list ); And this method definition: static public void mysterymethod4( int [ ] list ) int temp = list[0]; for ( int i = 0; i < list.length - 1; i++ ) list[i] = list[i+1]; list[list.length-1] = temp; Before this method begins execution, the arrangement of the values in the array looks like this: [0] [1] [2] [3] [4] [5] 4 2 8 5 3 1 What is the arrangement of values in the array when the method is finished? [0] [1] [2] [3] [4] [5] Page 9
23. 2 points ( 1 error = -1, more than 1 error = -2 ) Given this main method code: static public void main( String [ ] args ) int [ ] list = 4, 2, 8, 5, 3, 1 ; mysterymethod3( list ); And this method definition: static public void mysterymethod3( int [ ] list ) for ( int i = 0; i < list.length; i++ ) int hiindex = i; for( int j = i + 1; j < list.length; j++) if ( list[j] > list[hiindex] ) hiindex = j; int temp = list[hiindex]; list[hiindex] = list[i]; list[i] = temp; Before this method begins execution, the arrangement of the values in the array looks like this: [0] [1] [2] [3] [4] [5] 4 2 8 5 3 1 The method has two loops. The outer loop uses the loop variable i. The inner loop uses the loop variable j. After the end of the first iteration of the outer loop ( the loop that uses the loop variable, i) what is the arrangement of values in the array? [0] [1] [2] [3] [4] [5] Page 10
Part F Two Dimensional Arrays worth 5 points 24. 1 point Given this main method code: static public void main( String [ ] args ) int [ ] [ ] mat = 0, 1, 2, 3, 4, 5 ; System.out.println( mysterymethod1( mat ) ); And this method definition: static public int mysterymethod1( int [ ] [ ] mat ) int number = 0; for ( int row = 0; row < mat.length; row ++) for( int col = 0; col < mat[row].length; col++) number = number + mat[row][col]; return number; What is the output of the println? 25. 1 point Given this main method code: static public void main( String [ ] args ) int [ ] [ ] mat2 = 0, 1, 2, 3, 4, 5, 6, 7, 8 ; System.out.println( mysterymethod2( mat2 ) ); And this method definition: static public int mysterymethod2( int [ ] [ ] mat2 ) int number = 0; for ( int i = 0; i < mat2.length; i++) number = number + mat2[i][i]; return number; What is the output of the println? Page 11
Given this main method code: static public void main( String [ ] args ) int [ ] [ ] mat2 = 0, 1, 2, 3, 4, 5, 6, 7, 8 ; int [ ] [ ] mat3 = 0, 1, 2, 3, 3, 3, 0, 1, 2 ; System.out.println( mysterymethod3( mat2 ) ); System.out.println( mysterymethod3( mat3 ) ); And this method definition: static public boolean mysterymethod3( int [ ] [ ] mat2 ) int toprow = 0, bottomrow = mat2.length-1; while( toprow < bottomrow ) int col = 0, right = mat2[0].length; while( col < right ) if ( mat2[toprow][col]!= mat2[bottomrow][col] ) return false; col = col + 1; toprow = toprow + 1; bottomrow = bottomrow - 1; return true; 26. (1 point) What does this method do? (Do NOTwrite what the code does: It is traversing the array. or It returns true. Rather, write what the method accomplishes when it is finished)? 27. (2 points) What is the output of the two printlns? Println #1: Println #2: Page 12
Part G Properties of Arrays worth 5 points Below is a one dimensional array named list and a variable named count. Use this array for your answers to the questions in this part: list 4 6 1 8 6 3 9????????? count 7 28. 1 point When we work with one dimensional arrays that are not full, we have to have a variable named count to use when we traverse the loop. Why? Mark the true statements in the following list with a T. Leave the false statements unmarked. 1 point each 29 [ ] The array is full when the value of count is equal to list.length. 30 [ ] If there is room in the array, new values go in element list[count]. 31 [ ] The index of the last valid element of the array is list[list.length]. 32 [ ] The following code fragment effectively deletes the last element in the array: count = count 1; Page 13
Part H OOP worth 6 points (1 pt each) 33. Below is the definition of the class Coin used in lecture. Circle any and all constructors ( if any are present in the code ): public class Coin private int value; private String name; public int getvalue( ) return value; Coin ( int v, String n ) setvalue( v ); setname( n ); Coin( ) this( 1, "Penny"); private void setvalue ( int newvalue ) if ( newvalue > 0 ) value = newvalue; else value = 0; public String getname( ) return name; public String tostring( ) return "Name: " + name + " --- value: " + value + " cents"; private void setname ( String newname ) if (newname == null) name = "Error"; else name = newname; 34. In the diagram label the default constructor if one exists in the code. Page 14
35. Below is the definition of the class Coin used in lecture. Circle any and all accessors ( if any are present in the code ): public class Coin private int value; private String name; public int getvalue( ) return value; Coin ( int v, String n ) setvalue( v ); setname( n ); Coin( ) this( 1, "Penny"); private void setvalue ( int newvalue ) if ( newvalue > 0 ) value = newvalue; else value = 0; public String getname( ) return name; public String tostring( ) return "Name: " + name + " --- value: " + value + " cents"; private void setname ( String newname ) if (newname == null) name = "Error"; else name = newname; 36 In the following code fragment, the first line declares and news a Coin object named c. The second line must print the value of this Coin object. Finish the code: Coin c = new Coin( 10, Dime ); System.out.println( ); Page 15
The following code fragment declares and initializes an array of Coin object that can contain ten coins with different values and different names. Coin[ ] coincolletion = new Coin [ 10 ]; int count = 0; count = readfile( coincollection, args[ 0 ] ); 37. Write the code needed to print the value and name of the last Coin in the array, coincollection : 38. Write the code needed to print the name of the first Coin in the array, coincollection : Page 16
Part I Scope (ownership) of Variables worth 8 points (1 point each) public class PartI1 static public void main( String [ ] args ) int number = 42; System.out.println( number ); // print #1 testmethod( number ); System.out.println( number ); // print #4 static void testmethod( int number ) System.out.println( number ); // print #2 number = 0; System.out.println( number ); // print #3 What is the output for each of the numbered printlns? 39. Print #1: 40. Print #2: 41. Print #3: 42. Print #4: Page 17
public class PartI2 static public void main( String [ ] args ) int [ ] numberlist = 42 ; System.out.println( numberlist[ 0 ] ); // print #1 testmethod( numberlist ); System.out.println( numberlist[ 0 ] );; // print #4 static void testmethod( int [ ] numberlist ) System.out.println( numberlist[ 0 ] ); // print #2 numberlist[ 0 ] = 0; System.out.println( numberlist[ 0 ] ); // print #3 What is the output for each of the numbered printlns? 43. Print #1: 44. Print #2: 45. Print #3: 46. Print #4: =============================================== This is a modified partial String API for your use on the exam. char charat(int index) Returns the char value at the specified index. boolean equals(string str) Compares this string to the argument int length() Returns the length of this string. Page 18