340 likes | 458 Views
Exam 1 Review. 197. d ays until the AP Computer Science test. Test Taking Skills. Read questions carefully, look for exactly what is being asked. Scan the entire test to understand what parts look easy and what parts are worth the most points .
E N D
197 days until the AP Computer Science test
Test Taking Skills • Read questions carefully, look for exactly what is being asked. • Scan the entire test to understand what parts look easy and what parts are worth the most points. • Never leave questions blank. Write down anything you can think of that is relevant. Blank answers can’t get partial credit. • Pace yourself. Don’t spend lots of time on a question worth only a few points if you still need to do a question worth lots of points.
Exam Review 1. All the following are primitive data types: int, char, double, boolean, String a) TRUE b) FALSE
Exam Review 2. The following is an example of an implicit cast: inta = (int) (5.0 / 9.0); a) TRUE b) FALSE
Explicit cast vs. implicit cast An implicit cast occurs when the cast is automatically done by the compiler and no code is required. inti = 3; double d = 3; public static compute(double d) {…} compute(42); An explicit cast requires code from the programmer to inform the compiler of the cast. inti = (int)(Math.sqrt(2));
Exam Review 3. The concatenation operator is the plus sign. a) TRUE b) FALSE
Exam Review 4. Adding a double and an int implicitly converts the int variable to a double and the sum is a double. a) TRUE b) FALSE
Exam Review WALKTHROUGH inta = 9; int b = 5; for (inti = 0; i <= a; i++){ b = b + i; a = a - i; } a = b = i =
Exam Review 5. What are the values of a and b after the following code is executed? int a = 9; int b = 5; for (inti = 0; i <= a; i++){ b = b + i; a = a - i; } a) a = -36, b = 9 b) a = -36, b = 50 c) a = 3, b = 11 d) a = 9, b = 50 e) a = 9, b = 11
Exam Review 6. What is the output of the following code? for (inti = 1; i < 3; i++){ for (int j = 3; j > i; j--){ System.out.print(i+j + " "); } } a) 4 3 5 b) 4 3 5 4 c) 4 3 2 5 4 3 d) 1 3 1 2 2 3 e) 1 3 1 2 2 3 2 2
Exam Review 7. Which code will produce the following output? The quick brown fox jumped over the lazy dog. I. System.out.print(“The quick brown fox\njumped over the lazy dog.”); II. System.out.print(“The quick brown fox”); System.out.println(“jumped over the lazy dog.”); III. System.out.print(“The quick brown fox”); System.out.print(“jumped over the lazy dog.”); a) None b) I only c) II only d) III only e) I and II only f) I, II, and III
Exam Review WALKTHROUGH public static int method() { int a; for (a = 1; a <= 3; a++) { a = (int)Math.pow(2, a); } return a; } a =
Exam Review 8. What does the following method return? public static int method() { int a; for (a = 1; a <= 3; a++) { a = (int)Math.pow(2, a); } return a; } a) 1 b) 4 c) 8 d) 9
Exam Review WALKTHROUGH Console: public class Program{ public static void main(String[] args){ inta = 1; intb = 2; swap(a, b); System.out.println(a + " " + b + " "); } public static void swap(int a, int b){ a = b; b = a; System.out.print(a + " " + b + " "); } } a = b = a = b =
Exam Review 9. What is the output of the following program? public class Program{ public static void main(String[] args){ inta = 1; intb = 2; swap(a, b); System.out.println(a + " " + b + " "); } public static void swap(int a, int b){ a = b; b = a; System.out.print(a + " " + b + " "); } } a) 1 2 2 1 b) 2 2 1 2 c) 2 1 2 1 d) 2 2 2 2 e) 1 2 2 2
Exam Review 10. What is the output of the following code? String str = "This is a string."; String newStr = str.substring(str.indexOf('s'), str.indexOf('g')); System.out.println(newStr); intindexOf(String str) Returns the index within this string of the first occurrence of the specified substring. String substring(intbeginIndex, intendIndex) Returns a new string that is a substring of this string, from beginIndex to endIndex(not including the character at endIndex).
Exam Review 10. What is the output of the following code? String str = "This is a string."; String newStr = str.substring(str.indexOf('s'), str.indexOf('g')); System.out.println(newStr); a) s is a strin b) s is a string c) strin d) string
Exam Review 1. For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type (e.g., 7.0 rather than 7 for a double, Strings in quotes). (Note that all are worth 3 points except for a, which is worth 2 points) a. 10 + 3 * 5/3 b. 2.1 * 3.141 * (3/12) + 1.5 c. 75 % 10 + 3 % 10 - 12 % 3 d. 2 * 3 + "." + (8 + 4) + 3 * 3 e. 982/10/10/2.0 * 2 + 21/5 f. 9768 % 10 % 10 g. 3 + 2 + “+” + 3 + 2
Exam Review 10 + 3 * 5/3 2.1 * 3.141 * (3/12) + 1.5 10 + 15 / 3 10 + 5 15 2.1 * 3.141 * 0 + 1.5 0 + 1.5 1.5
Exam Review 75 % 10 + 3 % 10 - 12 % 3 2 * 3 + "." + (8 + 4) + 3 * 3 5 + 3 - 0 8 2 * 3 + “.” + 12 + 3 * 3 6 + “.” + 12 + 9 “6.” + 12 + 9 “6.12” + 9 “6.129”
Exam Review 982/10/10/2.0 * 2 + 21/5 98 / 10 / 2.0 * 2 + 21 / 5 9 / 2.0 * 2 + 21 / 5 4.5 * 2 + 21 / 5 9.0 + 4 13.0
Exam Review 9768 % 10 % 10 3 + 2 + “+” + 3 + 2 8 % 10 8 5 + “+” + 3 + 2 “5+” + 3 + 2 “5+3” + 2 “5+32”
Exam Review 2. (12 points) What is the output of the following program? pubic static void main (String args[]) { intone = 1; inttwo = 2; double three = 3.0; one = theMethod (one, 2, three) + theMethod(1, two, 1.0) + theMethod(one, two, three); System.out.println("one + two is " + (one + two)); } public static inttheMethod(int one, int two, double three) { intreturnValue = (int) (one + two - three); System.out.println(returnValue); return returnValue; }
Exam Review WALKTHROUGH pubic static void main (String args[]) { intone = 1; int two = 2; double three = 3.0; one = 0+ 2+ 0; System.out.println("one + two is " + (one + two)); } public static inttheMethod(int one, int two, double three) { intreturnValue = (int) (one + two - three); System.out.println(returnValue); return returnValue; }
Exam Review 0 2 0 one + two is 4 Rubric 3 points for each correct line of output 1 point for having “one + two is” in your answer
Exam Review 3. (18 points) The usual rules for changing standard English into Pig Latin are as follows: For words that begin with a consonant, the initial consonant is moved to the end of the word, and "ay" is added, as in the following examples: "happy" → "appyhay" "duck" → "uckday” For words that begin with a vowel, you just add "way" to the end. Examples: "egg" → "eggway" "inbox" → "inboxway" Write 2 methods which convert an English word to Pig Latin. Both methods take an input parameter which is a single English word. The method should return the Pig Latin word. Do not worry about other Pig Latin rules for this question.
Exam Review public static String englishToPigLatin_ConsonantAtStart( String englishWord) { return word.substring(1) + return.charAt(0) + “ay”; } Rubric: 1 point for using substring(), 3 for getting it correct. 1 point for using charAt(), 3 for getting first char correctly. May use substring(0,1) for full credit. 1 point for concatenating the “ay” strings together, 3 for concatenating correctly. 1 point for using the return statement to return a string, 3 points for returning the correct string
Exam Review public static String englishToPigLatin_VowelAtStart( String englishWord) { return word + “way”; } Rubric: 1 point for concatenating the “way” strings together, 3 for concatenating correctly. 1 point for using the return statement to return a string, 3 points for returning the correct string.
Exam Review public static String englishToPigLatin_VowelAtStart( String englishWord) { return word + “way”; } Rubric: 1 point for concatenating the “way” strings together, 3 for concatenating correctly. 1 point for using the return statement to return a string, 3 points for returning the correct string.
Exam Review 4. (20 points) Write a program which reads the size of a triangle from standard input and prints a "number triangle" of that size, as per the following examples: Size of triangle: 3 321 21 1 Size of triangle: 5 54321 4321 321 21 1
Exam Review public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print(“enter triangle size: “); intsize = console.nextInt(); triangles(size); console.close(); } Rubric 2 points for correctly declaring the Scanner. 1 point for trying to declare a scanner. 1 points for closing the Scanner. 2 points for user prompt. 2 points for reading in an integer from the Scanner. 1 point for calling console.nextInt(). 1 for assigning to an int correctly.
Exam Review public static void triangles(int n) { for (inti = n; i > 0; i--) { for (int j = i; j > 0; j--) { System.out.print(j); } System.out.println(); } } Rubric 6 points for correct outer for loop -2 points each for the initialization, test and update of the for loop 6 points for inner inner for loop -2 points each for the initialization, test and update of the for loop If done incorrectly, 1 point for each for-loop they have. 1 point for the correct output
Homework • Read 3.1 • Self Check 4.1, 4.2, 4.4, 4.5, 4.6, 4.13, 4.14 • Exercise 4.3, 4.4 • FracCalc Checkpoint 1