Name: Class: Date: AP CS Fall Semester Final Multiple Choice Identify the choice that best completes the statement or answers the question. 1. What is printed by the following code? int k = 3; for (int pj = 5; pj <= 100; pj++) k = pj + 1; System.out.println(3 * k + pj); a. Won t compile b. Never prints, endless loop c. 3082 d. 5820 2. What is printed by the following code? int jj = 2; do int gk = 1 + jj++; while(jj < 10); System.out.println(gk); a. Won t compile b. Never prints, endless loop c. 45 d. 55 3. String sss = I like school ; Using the two paramater form of substring, which line of code will print the last character of sss? 4. String sss = I like school ; Using the one paramater form of substring, which line of code will print the last character of sss? a. System.out.println( sss.substring(11) ); b. System.out.println( sss.substring(12) ); c. System.out.println( sss.substring(13) ); d. System.out.println( sss.substring(12, 14) ); 5. String sss = I like school ; Using charat, which line of code will print the last character of sss? a. System.out.println( sss.charat(11) ); b. System.out.println( sss.charat(12) ); c. System.out.println( sss.charat(13) ); d. System.out.println( sss.charat(14) ); 6. Assume s is a String. Using the two-parameter form of substring, which line of code will print the last character of s? a. System.out.println( s.substring( s.length( ) - 1, s.length( ) ) ); b. System.out.println( s.substring( s.length( ), s.length( ) ) ); c. System.out.println( s.substring( s.length( ) + 1, s.length( ) ) ); d. System.out.println( s.substring( s.length( ) - 1, s.length ) ); a. System.out.println( sss.substring(12, 13) ); b. System.out.println( sss.substring(12) ); c. System.out.println( sss.substring(12, 11) ); d. System.out.println( sss.substring(12, 14) ); 1
Name: 7. Assume s is a String. Using the one-parameter form of substring, which line of code will print the last character of s? a. System.out.println( s.substring(s.length( ) - 1 ) ); b. System.out.println( s.substring(s.length( ) + 1 ) ); c. System.out.println( s.substring(s.length( ) ) ); d. System.out.println( s.substring(s.length - 1 ) ); 8. Assume s is a String. Using charat, which line of code will print the last character of s? a. System.out.println( s.charat(s.length 1 ) ); b. System.out.println( s.charat(s.length( ) ) ); c. System.out.println( s.charat(s.length( ) +1 ) ); d. System.out.println( s.charat(s.length( ) 1 ) ); 9. What is printed by the following? String p = ABCDE ; System.out.println( X + p.substring(2,2) + X ); a. Illegal, won t compile b. XBX c. XCX d. XX 10. What s printed by the code below? int k = 23; System.out.println(Integer.toBinaryString( k )); a. 10101 b. 11111 c. 10110 d. 10011 11. What code that will printout the hex equivalent of int m? a. System.out.println( Integer.toString(m, hex ) ); b. System.out.println( Integer.toHexString(m) ); c. System.out.println( Integer.toString(m) ); d. System.out.println( Integer.toString(m, 16) ); 12. What code that will printout the octal equivalent of int m? a. System.out.println( Integer.toString(m, oct ) ); b. System.out.println( Integer.toOctalString(m) ); c. System.out.println( Integer.toString(m) ); d. System.out.println( Integer.toString(m, 8) ); 13. What is printed by the following? String s1 = "m"; String s2 = "Z"; System.out.println(s1.compareTo(s2)); a. A positive number b. A negative number c. 0 d. true e. false 14. What is printed by the following? int x = 9; double y = 1.3; String s = Felix ; System.out.println(x + y + s + x + y); a. 91.3Felix91.3 b. 10.3Felix91.3 c. 91.3Felix10.3 d. Illegal, won t compile 2
Name: 15. What code that will print the following? Cat\"hello"X'm' a. System.out.println("Cat//\"hello\"X\'m\'"); b. System.out.println("Cat///"hello/"X\'m/'"); c. System.out.println("Cat\\\"hello\"X\'m\'"); d. System.out.println("Cat\"hello"X'm'"); 16. What are all objects equal to before they are initialized? a. null b. zero c. d. Illegal. They must be initialized when declared. 17. What code replaces <##> below so as to make it skip the remaining code in the loop, but still continue looping? for(j = 0; j < 10; j++) if (j >= 5) <##> m = j *2; m =j + 2; sum = sum + m; a. continue; b. break; c. exit; d. stop; 18. What is printed by the following code? int x = 5; double y = (x >8)? Math.pow(x,2) : Math.pow(x,3); System.out.println(y); a. Illegal, won t compile b. 125 c. 125.0 d. 1.25E0 19. What is printed by the following code? int x = 462; if(!x > 0) System.out.println( positive ); else System.out.println( negative ); a. Illegal, won t compile b. positive c. negative d. Nothing is printed 20. What is printed by the following? System.out.println(17.2%4); a. 1.2 b. 4.3 c. 1 d. 1.0 3
Name: 21. Does the conditional part of the if below return a true, false, or give an error? a. true b. false c. error int x = 3, y = 5; if ( (x < 8 ) && (y > -1) ) some code 22. Of which of the following is y *= z + m the equivalent? a. y = y * (z + m); b. y = y * z + m; c. Neither of these 23. What is printed by the following? System.out.println(-12%5); a. 2 b. -2 c. Illegal, won t compile d. -2.4 24. What is printed by the following? System.out.println( 12%(-5) ); a. 2 b. -2 c. Illegal, won t compile d. -2.4 25. What is printed by the following? System.out.println( -12%(-5) ); a. 2 b. -2 c. Illegal, won t compile d. 2.4 26. What is printed by the following? int x = 7; int y = 3; double d = (double)(x/y); System.out.println(d); a. 2 b. 2.0 c. 2.3333333333333 d. Illegal, won t compile 27. What is printed by the following? int x = 7; int y = 3; double d = (double)x/y; System.out.println(d); a. 2 b. 2.0 c. 2.33333333333333 d. Illegal, won t compile 28. Specify the accessibility provided by public: a. Accessible from anywhere b. Accessible from just within its own class c. All methods of classes in the same package can access the feature. This is the default access control if none is specified. d. Access is permitted by methods of the same class, subclasses, and classes in the same package. 29. Specify the accessibility provided by private: a. Accessible from anywhere b. Accessible from just within its own class c. All methods of classes in the same package can access the feature. This is the default access control if none is specified. d. Access is permitted by methods of the same class, subclasses, and classes in the same package. 4
Name: 30. What is printed by System.out.println(10 / 4 * 50); a..05 b. 125 c. 125.0 d. 100 31. What code will convert double d to a String and store the result in s. a. String s = (String)d; b. String s = d + ; c. String s = + d; d. Both A and C e. Both B and C 32. What does the following print? double p = 3.07; double q = 2.9; int count = 2; if ( (p / q == 1) && (count-- < 59))... System.out.println(count); a. 1 b. 2 c. 3 d. 0 33. What does the following print? double p = 3.07; double q = 2.9; int count = 2; if ( (p / q == 1) (count-- < 59))... System.out.println(count); a. 1 b. 2 c. 3 d. 0 34. What does the following print? double p = 3.07; double q = 3.07; int count = 2; if ( (p / q == 1) && (count-- < 59))... System.out.println(count); a. 1 b. 2 c. 3 d. Illegal, won t compile e. Not enough information 35. Suppose you wish to find the index of the first occurrence of the character A (starting with index 4) in String mystring. Which line of code will store this value in the integer k. a. int k = mystring.indexof(65, 4); b. int k = mystring.indexof( A, 4); c. int k = mystring.lastindexof(65); d. int k = mystring.charat(65, 4); e. Both A and B 36. Which line of code will find the first occurrence of ee in the sentence stored in String sentc. Store the index in jj. a. int jj = sentc.indexof(ascii(69), 2); b. int jj = sentc.indexof(69,69); c. int jj = sentc.indexof( ee ); d. int jj = sentc.index( ee ); 37. What is another name for method? a. function b. parameters c. signature d. header 38. What is another name for header? a. function b. parameters c. signature d. method 5
Name: 39. What is another name for function? a. method b. parameters c. signature d. header 40. What is another name for signature? a. function b. parameters c. method d. header 41. To what is a numeric data member automatically initialized (in the absence of specific initialization)? a. 0 or 0.0 b. 1 c. null d. Nothing, not initialized 42. To what is a local numeric variable automatically initialized (in the absence of specific initialization)? a. 0 or 0.0 b. 1 c. null d. Nothing, not initialized 43. When Boolean OR-ing two boolean expressions, when is only one expression evaluated? a. When the left expression is true b. When the right expression is true c. When the left expression is false d. When the right expression is false 44. When Boolean AND-ing two boolean expressions, when is only one expression evaluated? 45. In order to determine if the character at index j of String sp is a whitespace character, which of the following would return the appropriate boolean? a. Character.sp.isWhitespace(j) b. sp.charat(j).iswhitespace c. Character.isWhitespace(sp.charAt(j)) d. sp.charat(iswhitespace( )) 46. If c is of type char and i is of type int, is the following legal or illegal? i = c; a. Legal b. Illegal 47. If c is of type char and i is of type int, is the following legal or illegal? a. Legal b. Illegal c = (char)i; 48. If c is of type char and i is of type int, is the following legal or illegal? c = i; a. Legal b. Illegal 49. What is the range of ASCII codes for the following? The digits 0,1-9 a. 48-57 b. 49-58 c. 65-74 d. 97-106 a. When the left expression is true b. When the right expression is true c. When the left expression is false d. When the right expression is false 6
Name: 50. What is the range of ASCII codes for the following? The letters a z a. 48-73 b. 49-58 c. 65-90 d. 97-122 51. What is the range of ASCII codes for the following? The letters A Z a. 48-73 b. 49-58 c. 65-90 d. 97-122 52. What is returned by Math.ceil(-1032.13)? a. -1032.0 b. -1033.0 c. -1032 d. -1033 53. What is returned by Math.floor(150.788)? a. 150.8 b. 150.7 c. 150.79 d. 150.78 54. What is returned by Math.round(7/2)? a. 4 b. 3.5 c. 3.0 d. 3 7
AP CS Fall Semester Final Answer Section MULTIPLE CHOICE 1. ANS: A The scope of pj is limited to inside the loop, starting with for and ending with. 2. ANS: A The scope of gk is limited to inside the loop, starting with do and ending with. 3. ANS: A Play around with these in BlueJ s Code Pad (View Show Code Pad) 4. ANS: B 5. ANS: B 6. ANS: A 7. ANS: A 8. ANS: D 9. ANS: D Start at index 2, up to but not including index 2, is an empty String: 10. ANS: E...answer is 10111 11. ANS: E...both B and D 12. ANS: E...both B and D 13. ANS: A See ASCII table for lesson on char. Think of compareto as s1 - s2, in that order. 14. ANS: B + signs are evaluated left to right. x+y evaluates to 10.3. 10.3 + Felix results in a String, 10.3Felix. Append x to the end results in 10.3Felix9. Concatenate y results in choice b. 1
15. ANS: C \,,, etc. are escape chacters. They have special meanings. To include them in Strings, you have to put a backslash (\) in front. 16. ANS: A 17. ANS: A continue means skipping the rest of a loop, straight to the step expression, j++. 18. ANS: C The second line can be rewritten as: double y; if (x > 8) y = Math.pow(x,2); else y = Math.pow(x,3); 19. ANS: A! has top priority here. (Unary operators, like! and -, usually have precedent). Should be (!(x>0)). 20. ANS: A The multiple of 4 closest to 17.2 is 16. 17.2-16=1.2 21. ANS: A 22. ANS: A 23. ANS: B Closest multiple of 5 to -12 is -10. 24. ANS: A Closest multiple of -5 to 12 is 10. Another way of thinking is, if the questions asks 10%(-5), the answer is 0, because (-5)(-2) = 10. 25. ANS: B Same as (-12)%(-5). 26. ANS: B...The parenthesis around x/y creates a little world of its own and since x and y are both integers, integer arithmetic is done giving 2 (not 2.33333333333333). This answer is then cast into a double. 2
27. ANS: C...Only the x is cast as a double. x/y is now done with double arithmetic since x is a double. Casting has high precedence. 28. ANS: A 29. ANS: B 30. ANS: D * and /, + and _, have same precedence, so evaluate from left to right. int/int = int. 31. ANS: E Primitive types can t be cast into objects like String, or vice versa. 32. ANS: B p/q is not equal to 1. So false && (...) will never be true. Java short-circuits and skips the whole if statement. It doesn t bother to evaluate count-- < 59, because it doesn t matter. So count remains the same value. 33. ANS: A p/q is not equal to 1. So the value of the if condition depends on whether count-- < 59. BTW, avoid writing programs like this unless you re an expert at the differences between x-- and --x. It actually compares count < 59, then do count--. 34. ANS: E p/q is 1.0. Well, it could also be 1.00000001 or 0.99999997. Floating point numbers are not stored exactly in most programming languages. So it s a really bad idea to compare them to integers. Include a margin of error. 35. ANS: E... both A and B 36. ANS: C Strings are objects. No ASCII table. 37. ANS: A... both A and B 38. ANS: C. 3
39. ANS: A... both A and B 40. ANS: D 41. ANS: A A Circle class has an instance variable radius. A BankAccount class has an object variable balance and name. (These are the same names for data member. They are declared inside the class but outside all the methods.) radius and balance are set to default values if not initialized. 42. ANS: D Local variables are declared inside a method. In the circle class s area() method, we may temporarily store the area into a new variable double a before returning it. But if we don t initialize a, it doesn t have any values. The compiler would complain. 43. ANS: A Look up short-circuiting. 44. ANS: C Look up short-circuiting. 45. ANS: C 46. ANS: A char goes from 0 to 127, so no problem storing it as an int. 47. ANS: A i could be 65 ( A ), or 5000. This code will always compile, but the latter would result in a run-time exception. 48. ANS: B 49. ANS: A 50. ANS: D 51. ANS: C 52. ANS: A What is the smallest number greater than (less negative) -1032? Return it as a double. 53. ANS: E... answer is 150.0 4
54. ANS: C 7/2 is integer arithmetic and gives 3. After rounding, it s still 3.0 5