name: ap computer science summer work 2019-2020 › accnt_167542...instance variables: leg1, leg2,...

13
Name: AP Computer Science Summer Work 2019-2020 Multiple Choice 1. Consider the following code segment: int j = 0; int[] arr1 = new int[100]; int[] arr2 = new int[100]; /*code to populate the arrays not shown*/ while( (j < arr1.length) && (arr1[j] != arr2[j]) ) j++ Which of the following must be true after the while loop terminates? a) j >= arr1.length b) j >= arr1.length && arr1[j] == arr2[j] c) j < arr1.length && arr1[j] != arr2[j] d) j >= arr1.length || arr1[j] != arr2[j] e) j >= arr1.length || arr1[j] == arr2[j] 2. Which of the following would create an output of 97? i. char c1 = ‘a’; int x = c1; System.out.println(x); ii. System.out.println(195/2); iii. int p = 9; int q = 7; System.out.println(p + q); iv. String s1 = “9”; String s2 = “7”; System.out.println(s1 + “” + s2); a) i only b) i and ii c) i, ii, and iv d) all of the above

Upload: others

Post on 10-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Name: AP Computer Science Summer Work 2019-2020 › accnt_167542...instance variables: leg1, leg2, and hypotenuse. You will need to write a constructor that takes in three int parameters

Name:

AP Computer Science Summer Work

2019-2020

Multiple Choice

1. Consider the following code segment:

int j = 0;

int[] arr1 = new int[100];

int[] arr2 = new int[100];

/*code to populate the arrays not shown*/

while( (j < arr1.length) && (arr1[j] != arr2[j]) )

j++

Which of the following must be true after the while loop terminates?

a) j >= arr1.length

b) j >= arr1.length && arr1[j] == arr2[j]

c) j < arr1.length && arr1[j] != arr2[j]

d) j >= arr1.length || arr1[j] != arr2[j]

e) j >= arr1.length || arr1[j] == arr2[j]

2. Which of the following would create an output of 97?

i. char c1 = ‘a’; int x = c1;

System.out.println(x);

ii. System.out.println(195/2);

iii. int p = 9; int q = 7;

System.out.println(p + q);

iv. String s1 = “9”; String s2 = “7”;

System.out.println(s1 + “” + s2);

a) i only

b) i and ii

c) i, ii, and iv

d) all of the above

Page 2: Name: AP Computer Science Summer Work 2019-2020 › accnt_167542...instance variables: leg1, leg2, and hypotenuse. You will need to write a constructor that takes in three int parameters

3. What is the output of the following code:

int[] arr = new int[20];

int sum = 0;

for (int i = 0; i < arr.length; i++)

arr[i] = i*i;

int j = 0;

while (j < arr.length)

{

if (j%2 == 1)

sum += arr[j];

j++;

}

System.out.println(sum);

a) 0

b) 1140

c) 1330

d) 2470

For questions 4 – 6, use the following class:

public class Point

{

private int myX;

private int myY;

public Point()

{

myX = 0;

myY = 0;

}

public int getX()

{

return myX;

}

public int getY()

{

return myY;

}

}

Page 3: Name: AP Computer Science Summer Work 2019-2020 › accnt_167542...instance variables: leg1, leg2, and hypotenuse. You will need to write a constructor that takes in three int parameters

4. Which of the following statememnts creates a Point object with the coordinates (0, 0)?

i. Point p = new Point();

ii. Point p = new Point(0, 0);

iii. Point = new Point(0, 0);

iv. Point p = new Point(1, 1); p.myX = 0;

p.myY = 0;

a) ii only

b) i and ii

c) i, ii, and iii

d) i, ii, and iv

5. Which of the following is true regarding the Point class?

a) The class won’t compile because there are two methods named Point.

b) Variables myX and myY can be changed from outside the class.

c) The instance variables for Point objects are unable to be changed.

d) It’s impossible to create a Point object with coordinates other than (0, 0).

6. Which of the following segments of code cannot appear in a client program that utilizes the

Point object?

a) Point p0;

p0 = new Point();

int n = p0.getX();

b) Point p1; Point p2 = new Point(9, 8);

Point p3 = new Point(p2.myX, p1.myY);

c) Point p4 = new Point();

p4 = new Point(19, 99);

d) Point p5;

Page 4: Name: AP Computer Science Summer Work 2019-2020 › accnt_167542...instance variables: leg1, leg2, and hypotenuse. You will need to write a constructor that takes in three int parameters

For questions 7 and 8, consider the following method:

public int doSomething(int k, int j)

{

while (k > j)

k--;

if (k < j)

j = k;

else

return j;

return k;

}

7. What best characterizes the return value of this method?

a) Returns k – j

b) Returns j – k

c) Returns the smaller of j and k, or j if they’re equal

d) Returns the larger of j and k , or j if they’re equal

e) Returns j if k and j are equal, k otherwise

8. Consider the following code segment:

int p = 5;

int q = 6;

doSomething(p, q);

System.out.println(p + “ ” + q);

What is printed?

a) 5 6

b) 5 5

c) 6 6

d) p q

e) There is a compile error because the return value of doSomething is not stored in a

variable.

Page 5: Name: AP Computer Science Summer Work 2019-2020 › accnt_167542...instance variables: leg1, leg2, and hypotenuse. You will need to write a constructor that takes in three int parameters

9. Consider the following code that will assign a letter grade depending on the student’s test score:

if (score >= 90) grade = ‘A’;

if (score >= 80) grade = ‘B’;

if (score >= 70) grade = ‘C’;

if (score >= 60) grade = ‘D’;

else grade = ‘F’;

a) this code will work correctly in all cases

b) this code will work correctly if the grade is greater than or equal to 60

c) this code will work correctly if the grade is less than 60

d) this code will work correctly if the grade is less than 70

e) this code will not work correctly under any circumstances

10. Consider the following data field and method.

private int[] nums;

public void doStuff(int n)

{

for (int i = 0; i < nums.length; i++)

{

if (nums[i] > n)

nums[i] = 0;

}

}

Which statement is always true about the contents of the nums array after the following

call?

doStuff(m);

a) All values are less than or equal to 0.

b) All values are greater than or equal to 0.

c) All values are less than or equal to m.

d) All values are greater than or equal to m.

e) None of the above.

Page 6: Name: AP Computer Science Summer Work 2019-2020 › accnt_167542...instance variables: leg1, leg2, and hypotenuse. You will need to write a constructor that takes in three int parameters

Free Response Questions

11. Random Range

You will write a method called randomRange. This method will take in two int parameters and

return a random int value from the lower number to the higher number (including both). If the

parameters are equal, that value is returned.

12. Multiples of N or M

You will write a method called multiplesOfNorM. This method will take in two int

parameters, m and n. It will not return anything, but it will print out all of the values between 1

and 1000 that are multiples of n or m, but not values that are multiples of both n and m.

13. Fibonacci Number

You will write a method to determine if a given int parameter appears in the Fibonacci

sequence. The Fibonacci Sequence is a sequence of numbers starting with 1, 1, … where every

number in the sequence is the sum of the previous two numbers in the sequence. (1, 1, 2, 3, 5,

8, 13, …) This method should return true if the parameter appears in the Fibonacci sequence

and false otherwise.

14. Factorial

Write a method called factorial that takes in an int parameter. This method should return the

factorial of the given number. The factorial of a number is the product of every number

between and including the given number and 1. Example: factorial(5) = 5*4*3*2*1. By

definition, factorial(0) = 1. If the parameter is less than zero, return -1.

15. Verify Curly Brackets

You will write and complete the verifyCurlyBrackets(String s) method. This method will take in a

String as a parameter and return a boolean. This method should return true under two

conditions: the parameter String is empty or the curly brackets are aligned properly. The

method should return false otherwise. For the curly brackets to be aligned properly, (a) every

left curly bracket must have a corresponding right curly bracket and (b) a right curly bracket

should never precede the corresponding left curly bracket. This will simulate a compiler looking

for misplaced/missing curly brackets.

Examples:

Parameter Return Value “” true

“{}” true

“{}{{}{}{{}}}” true

“{}{{}” false

“{}}{}” false

Page 7: Name: AP Computer Science Summer Work 2019-2020 › accnt_167542...instance variables: leg1, leg2, and hypotenuse. You will need to write a constructor that takes in three int parameters

16. Generate Password

You will write and complete the generatePassword() method. This method should return a

randomly generated String. The String will consist of 8 characters in total. The first three

characters should be letters (these can be uppercase or lowercase). The next two characters

should be numbers (0 – 9). The following two characters should be letters (uppercase or

lowercase) and the last character should be a number (0 – 9).

Here are 10 examples of possible Strings returned by a call to generatePassword():

HgY79rh9

JZN26BI8

SSk30kF8

eMV63aP2

iXl14Rl2

cXG95jW9

pjv15VA8

Yjj80UY1

GJx62zy3

ecT18br3

Hint: Keep in mind the ASCII table and the fact that any char has an int value that represents it.

17. Pythagorean Triple Class

You are going to write the class for a PythagoreanTriple object. A

PythagoreanTriple is meant to represent a combination of 3 positive integers that

satisfy the Pythagorean Theorem (𝑎2 + 𝑏2 = 𝑐2). Your object should have 3 private

instance variables: leg1, leg2, and hypotenuse. You will need to write a constructor

that takes in three int parameters in no particular order. If the three values represent a

Pythagorean Triple, the three variables should be set to the parameter values with

hypotenuse being the largest of the three values. If the three parameter values do not

represent a Pythagorean Triple, the three instance variables should all be set to -1. You

will need to write two methods for this class:

isPythagoreanTriple()

returns true if the three instance variables store a Pythagorean Triple. False, otherwise.

PythagoreanTriple scale(int s)

If the object is a Pythagorean Triple, this method returns a new PythagoreanTriple

object whose values are the three instance variables scaled by the parameter. Otherwise,

returns null.

*Example of code utilizing this class on the next page*

Page 8: Name: AP Computer Science Summer Work 2019-2020 › accnt_167542...instance variables: leg1, leg2, and hypotenuse. You will need to write a constructor that takes in three int parameters

Example:

PythagoreanTriple p0 = new PythagoreanTriple(4, 5, 3);

//leg1 = 4, leg2 = 3, hypotenuse = 5

boolean b0 = p0.isPythagoreanTriple();

//true

PythagoreanTriple p1 = new PythagoreanTriple(3, 4, 6);

// leg1 = -1, leg2 = -1, hypotenuse = -1

boolean b1 = p1.isPythagoreanTriple();

//false

PythagoreanTriple p2 = p0.scale(2);

//leg1 = 6, leg2 = 8, hypotenuse = 10

PythagoreanTriple p3 = p1.scale(3);

//p3 is null

Page 9: Name: AP Computer Science Summer Work 2019-2020 › accnt_167542...instance variables: leg1, leg2, and hypotenuse. You will need to write a constructor that takes in three int parameters

Name:

AP Computer Science Summer Work

2019-2020

Answer Section

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11. Random Range

Page 10: Name: AP Computer Science Summer Work 2019-2020 › accnt_167542...instance variables: leg1, leg2, and hypotenuse. You will need to write a constructor that takes in three int parameters

12. Multiples of N or M

13. Fibonacci Number

Page 11: Name: AP Computer Science Summer Work 2019-2020 › accnt_167542...instance variables: leg1, leg2, and hypotenuse. You will need to write a constructor that takes in three int parameters

14. Factorial

15. Count Parentheses

Page 12: Name: AP Computer Science Summer Work 2019-2020 › accnt_167542...instance variables: leg1, leg2, and hypotenuse. You will need to write a constructor that takes in three int parameters

16. Generate Password

Page 13: Name: AP Computer Science Summer Work 2019-2020 › accnt_167542...instance variables: leg1, leg2, and hypotenuse. You will need to write a constructor that takes in three int parameters

17. Pythagorean Triple Class