exam 1 review. days until the ap computer science test

34
EXAM 1 REVIEW

Upload: jeremy-rice

Post on 03-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

EXAM 1 REVIEW

days until the AP Computer Science

test

197

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) TRUEb) FALSE

Exam Review

2. The following is an example of an implicit cast: int a = (int) (5.0 / 9.0);

a) TRUEb) FALSE

Explicit cast vs. implicit castAn implicit cast occurs when the cast is automatically done by the compiler and no code is required.

int i = 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.

int i = (int)(Math.sqrt(2));

Exam Review

3. The concatenation operator is the plus sign.

a) TRUEb) 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) TRUEb) FALSE

Exam Reviewint a = 9;int b = 5; for (int i = 0; i <= a; i++) { b = b + i; a = a - i;}

a = b = i =

WALKTHROUGH

Exam Review5. What are the values of a and b after the following code is executed? int a = 9;int b = 5; for (int i = 0; i <= a; i++) { b = b + i; a = a - i;} a) a = -36, b = 9b) a = -36, b = 50c) a = 3, b = 11d) a = 9, b = 50e) a = 9, b = 11

Exam Review6. What is the output of the following code?

for (int i = 1; i < 3; i++) { for (int j = 3; j > i; j--) { System.out.print(i+j + " "); }} a) 4 3 5b) 4 3 5 4c) 4 3 2 5 4 3d) 1 3 1 2 2 3e) 1 3 1 2 2 3 2 2

Exam Review7. Which code will produce the following output? The quick brown foxjumped 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) Noneb) I onlyc) II onlyd) III onlye) I and II onlyf) I, II, and III

Exam Reviewpublic static int method() { int a; for (a = 1; a <= 3; a++) { a = (int)Math.pow(2, a); } return a;}

a =

WALKTHROUGH

Exam Review8. 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) 1b) 4c) 8d) 9

Exam Reviewpublic class Program { public static void main(String[] args) { int a = 1; int b = 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 =

Console:WALKTHROUGH

Exam Review9. What is the output of the following program?

public class Program { public static void main(String[] args) { int a = 1; int b = 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 1b) 2 2 1 2c) 2 1 2 1d) 2 2 2 2e) 1 2 2 2

Exam Review10. 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);

int indexOf(String str) Returns the index within this string of the first occurrence of the specified substring.

String substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string, from beginIndex to endIndex (not including the character at endIndex).

Exam Review10. 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 strinb) s is a stringc) strind) string

Exam Review1. 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/3b. 2.1 * 3.141 * (3/12) + 1.5c. 75 % 10 + 3 % 10 - 12 % 3d. 2 * 3 + "." + (8 + 4) + 3 * 3e. 982/10/10/2.0 * 2 + 21/5f. 9768 % 10 % 10g. 3 + 2 + “+” + 3 + 2

Exam Review10 + 3 * 5/3

2.1 * 3.141 * (3/12) + 1.5

10 + 15 / 310 + 515

2.1 * 3.141 * 0 + 1.5

0 + 1.5

1.5

Exam Review75 % 10 + 3 % 10 - 12 % 3

2 * 3 + "." + (8 + 4) + 3 * 3

5 + 3 - 08

2 * 3 + “.” + 12 + 3 * 3

6 + “.” + 12 + 9

“6.” + 12 + 9

“6.12” + 9

“6.129”

Exam Review982/10/10/2.0 * 2 + 21/598 / 10 / 2.0 * 2 + 21 / 59 / 2.0 * 2 + 21 / 54.5 * 2 + 21 / 5

9.0 + 4

13.0

Exam Review9768 % 10 % 10

3 + 2 + “+” + 3 + 2

8 % 108

5 + “+” + 3 + 2

“5+” + 3 + 2

“5+3” + 2“5+32”

Exam Review2. (12 points) What is the output of the following program?pubic static void main (String args[]) { int one = 1; int two = 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 int theMethod(int one, int two, double three) { int returnValue = (int) (one + two - three); System.out.println(returnValue); return returnValue;}

Exam Reviewpubic static void main (String args[]) { int one = 1; int two = 2; double three = 3.0; one = 0 + 2 + 0; System.out.println("one + two is " + (one + two));}

public static int theMethod(int one, int two, double three) { int returnValue = (int) (one + two - three); System.out.println(returnValue); return returnValue;}

WALKTHROUGH

Exam Review020one + two is 4

Rubric3 points for each correct line of output1 point for having “one + two is” in your answer

Exam Review3. (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 Reviewpublic 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 Reviewpublic 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 Reviewpublic 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 Review4. (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: 3321211 Size of triangle: 5543214321321211 

Exam Reviewpublic static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print(“enter triangle size: “); int size = console.nextInt(); triangles(size); console.close();}

Rubric2 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 Reviewpublic static void triangles(int n) { for (int i = n; i > 0; i--) { for (int j = i; j > 0; j--) { System.out.print(j); } System.out.println(); }}

Rubric6 points for correct outer for loop -2 points each for the initialization, test and update of the for loop6 points for inner inner for loop -2 points each for the initialization, test and update of the for loopIf 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