# 14.6. Exam 5 for the AP CS A Exam (not timed)¶

The following problems are similar to what you might see on the AP CS A exam. Please answer each to the best of your ability.

14-6-1: Suppose `x, y, and z` are variables of type `int`. Consider the following three conditions
1. (x == y) && (y == z) && (x == z)

2. (x==y) || (y==z) && (x == z)

3. (x - y) * (x - z) * (y - z) == 0

Which of these conditions is (are) always true if x == y is true?

• I only
• We don't know the value of z
• II only
• II is true but there is (are) other statements that evaluate to true
• III only
• III is true but there is (are) other statements that evaluate to true
• I and II only
• For I, we don't know the value of z
• II and III only
• III executes to true if either a||b true, III is true because x-y will become 0 then 0 * a *b... = 0

14-6-2: Consider the following method. What will be returned by a call to `multiply(8)`?

```public int multiply(int n)
{
if (n == 0)
return 1;
else if(n % 2 == 1)
return n;
else
return n * multiply(n - 2);
}
```
• 1
• n != 0
• 8
• There are some recursive calls since n % 2 != 1
• 8*7*6*5*4*3*2
• Subtracts 2 from original number of 8, so could never call an odd number to be multiplied
• 8*6*4*2
• Will recursively call 8 -2, then 6-2, and lastly 4-2
• 7*5*3
• Subtracts 2 from original number of 8, so could never call an odd number to be multiplied

14-6-3: Which of the following code segments correctly stores in `x` a random real number such that `0.6 <= x < 1`?

• double x = 0.6 * Math.random( ) + 0.4;
• This gives .4 <= x <1
• double x = 0.4 * Math.random( ) + 0.6;
• Math.random() returns 0(inclusive) to 1(exclusive) so this makes 0 + .6 the lower bound, and .99999 the upper bound
• double x = Math.random( ) - 0.4;
• This gives -.4 <= x < 6
• double x = (double) (Math.random( ) * 0.4);
• This doesn't ensure lower bound and upper bound
• double x = (double) (Math.random( ) + 0.6);
• This doesn't ensure lower bound and upper bound

14-6-4: Suppose methods `f1` and `f2` are defined as follows. What value is returned from the call `f1(5)`?

```public int f1(int x)
{
if(x == 0)
return 0;
else
return f2(x -2);
}

public int f2(int x)
{
if(x == 1)
return 1;
else
return f1(x + 1) + x;
}
```
• -4
• Trace out the recursive calls. See https://tinyurl.com/AP19-Q6
• 4
• Trace out the recursive calls. See https://tinyurl.com/AP19-Q6
• 6
• Trace out the recursive calls
• 8
• Trace out the recursive calls. See https://tinyurl.com/AP19-Q6
• -6
• Trace out the recursive calls. See https://tinyurl.com/AP19-Q6

14-6-5: Which of the following is the equivalent in decimal to 2D in hexadecimal?

• 32
• Convert to binary first
• 44
• Convert to binary first
• 45
• 2D in binary is 0010 1101
• 46
• Convert to binary first
• 720
• Convert to binary first

14-6-6: Which statement about constructors is `false`?

• It is legal for the value of a static variable to be changed in a constructor.
• Static variables can still have their values changed
• The constructor in a subclass must use the keyword super to initialize the private instance variables from its superclass.
• This is true, under the hood if you don't explicitly use the 'super' keyword, the compiler will do it for you automatically
• An interface never has constructors.
• There is no point to make your interface class have constructors because we can never make an instance of that class anyways
• An abstract class never has constructors.
• Can have unlimitted number of constructors
• If a subclass does not explicitly provide a constructor and its superclass has just one constructor with a parameter, an error will occur when an attempt is made to create an instance of a subclass object.
• Because of the inheritance, the constructor from super class will be called and it is expecting a passed in parameter

14-6-7: Consider the following method. What will the output of `mystery(6)` return?

```public int mystery(int n)
{
if(n == 1 || n ==2)
return 2;
else
return mystery(n -1) + mystery(n - 2);
}
```
• 10
• Trace out the recursive calls, see https://tinyurl.com/AP19-Q10
• 12
• Trace out the recursive calls, see https://tinyurl.com/AP19-Q10
• 16
• Trace out the recursive calls, see https://tinyurl.com/AP19-Q10
• 26
• Trace out the recursive calls, see https://tinyurl.com/AP19-Q10
• 32
• Trace out the recursive calls, see https://tinyurl.com/AP19-Q10
14-6-8: Suppose `a and b` are `boolean` variables. The expression below

`!a && !b`

…will evaluate to `false` whenever

1. a and b are both false

2. a is false and b is true

3. a is true and b is false

• I only
• This is true because !false && !false becomes true && true
• II only
• If either a or b (or both a AND b) is true whole expression becomes false
• III only
• If either a or b (or both a AND b) is true whole expression becomes false
• II and III only
• If either a or b (or both a AND b) is true whole expression becomes false
• I, II, and III
• If either a or b (or both a AND b) is true whole expression becomes false

14-6-9: Consider the following code segment. What will be the output after execution of this code segment?

```int x = 10, y = 0;
while(x > 5)
{
y = 3;
while(y < x)
{
y *= 2;
if( y % x == 1)
y += x;
}
x -= 3
}
Systen.out.println(x + " " + y);
```
• 1 6
• condition (y % x ==1) never true. Trace it out https://tinyurl.com/AP19-Q12
• 7 12
• condition (y % x ==1) never true. Trace it out https://tinyurl.com/AP19-Q12
• -3 12
• condition (y % x ==1) never true. Trace it out https://tinyurl.com/AP19-Q12
• 4 12
• condition (y % x ==1) never true
• -3 6
• condition (y % x ==1) never true. Trace it out https://tinyurl.com/AP19-Q12

14-6-10: What value is stored in `result` if:

```int result = 13 - 3 * 6 / 4 % 3
```
• -5
• the %, *, and / all have the same precedence
• 0
• the %, *, and / all have the same precedence
• 13
• the %, *, and / all have the same precedence
• -1
• the %, *, and / all have the same precedence
• 12
• the %, *, and / all have the same precedence

14-6-11: Consider the folllowing code segment. Assume `k` is some positive integer greater than 2. How many times will “SMALL” be printed?

```for(int i = 2; i <= k; i++)
{
if(arr[i] < someValue)
System.out.print("SMALL");
}
```
• 0
• k > 2 so will print out "SMALL"
• 1
• k can be greater than 3
• k -1
• max of k-1 iterations (end - start + 1 is k - 2 + 1)
• k -2
• it is <= k so inclusive of k
• k
• max of k -1 iterations

14-6-12: Which best describes what the `printSomething` method below does?

```public void printSomething(String str)
{
if(str.length() > 0)
{
System.out.print(str.substring(0,1));
printSomething(str.substring(1));
}

}
```
• It prints string str
• Prints out the leftmost character at the start of the recursive call. Then always trims off the left most character, but substring(x) with single parameter x gives the remaining string from index x until the end. See https://tinyurl.com/AP19-Q17
• It prints string str in reverse order
• substring(0,1) prints leftmost char not the rightmost char
• It prints only the first two characters of string str
• there is a recursive call of a substring at each iteration
• It prints only the first two characters of string str
• goes until s.length > 0
• It prints only the last character of string str
• goes until s.length > 0 and there are recursive calculates
14-6-13: A square matrix is declared as

`int[][] mat = new int [SIZE][SIZE]`

where `SIZE` is an appropriate integer constant. Consider the code below.

If mat is initialized to be:

```2 6 3
4 5 1
7 1 2
```

What value will be returned by a call to `sum(mat)`?

```public static int sum(int[][] mat)
{
int total = 0;
for(int r = 0; r < mat.length; r++)
{
for(int c = 0; c <= r; c++)
{
total += mat[r][c];
}
}
}
```
• 9
• Second for loop goes until colum <= row index
• 15
• Second for loop goes until colum <= row index
• 19
• Second for loop goes until colum <= row index
• 21
• adds the lower half of the matrix
• 31
• Second for loop goes until colum <= row index

14-6-14: The elements of an array are to be sorted in increasing order. Which represent the first step of a merge sort algorithm?

• Find the smallest element in arr and swap it into arr
• This is for selection sort
• Compare arr with arr and, if necessary, shift and replace elements such that arr is less than arr
• This is for bubble sort
• Compare arr with the middle element of arr, arr[mid] , and if arr is less than arr[mid] , swap these elements
• Merge sort deals with cutting sets into halves and repeating this process
• Compare the first and last elements, and if the first is less than the last, swap these elements.
• Merge sort deals with cutting sets into halves and repeating this process
• Break the array into roughly two equal halves.
• Firs step is to cut sets in halves until you get to a single element in a 'set'

14-6-15: Which of the following could replace the body of `compute` so it does the same thing.

```public static int compute(int n)
{
for(int i = 1; i < 4; i++)
{
n *= n;
}
return n;
}
```
• return 4 * n;
• 3 iterations of the loop and each loop does n^2
• return 8 * n;
• 3 iterations of the loop and each loop does n^2
• return 64 * n;
• 3 iterations of the loop and each loop does n^2
• return (int) Math.pow(n,4);
• 3 iterations of the loop and each loop does n^2
• return (int) Math.pow(n,8);
• Method basically does (n^2)^3 which is the same as n ^ 8

14-6-16: How many times will the `asterik("*")` be printed?

```for(int k = 4; k < 10; k +=2)
{
for(int j = 1; j <= 5; j++)
{
System.out.print("*");
}
}
```
• 36
• Outer loop does gets incremented by 2, NOT by 1. Also, j starts at 1, NOT 0
• 30
• Inner loop starts at 1 and is ends at 5 inclusive so exectues 5 times
• 35
• Outer loop does gets incremented by 2, NOT by 1
• 15
• Outerloop executes 3 times and inner loop executes 5 times.
• 18
• See http://tinyurl.com/AP19-Q23

14-6-17: Consider the following code. What is printed?

```class Dog{

public void act(){
System.out.print("run ");
eat();
}
public void eat(){
System.out.print("eat ");
}
}

public class UnderDog extends Dog{

public void act(){
super.act();
System.out.print("bark ");
}

public void eat(){
super.eat();
System.out.print("bark ");
}

public static void main(String[] args){
Dog fido = new UnderDog();
fido.act();
}
}
```
• run eat
• Because the fido is an "Underdog", we will call the eat() from class Underdog, http://tinyurl.com/AP19-Q25
• run eat sleep
• Because the fido is an "Underdog", we will call the eat() from class Underdog, http://tinyurl.com/AP19-Q25
• run eat sleep bark
• Because the fido is an "Underdog", we will call the eat() from class Underdog, http://tinyurl.com/AP19-Q25
• run eat bark sleep
• Because the fido is an "Underdog", we will call the eat() from class Underdog, http://tinyurl.com/AP19-Q25
• Nothing is printed due to infinite recursion
• Because the fido is an "Underdog", we will call the eat() from class Underdog, http://tinyurl.com/AP19-Q25

14-6-18: What is the output of the `System.out.println` statement?

```int x = 3, y = -2;
while(x > y)
{
x--;
y++;
}
System.out.println(x - y);
```
• -1
• x becomes 0 and y becomes 1 so 0 - 1 = -1
• 1
• make sure to do x-y and not y-x
• 0
• http://tinyurl.com/AP19-Q26
• -2
• http://tinyurl.com/AP19-Q26
• 2
• http://tinyurl.com/AP19-Q26

14-6-19: What will be the value of `sum` after the execution of code above?

```int sum = 0;
for(int i = 0; i < 3; i++)
{
if((i % 2) - 1 ==0)
sum += 3;
else
sum++;
}
```
• 4
• i%2 -1 ==0 means "is this number odd". See http://tinyurl.com/AP19-Q27
• 5
• i%2 -1 ==0 means "is this number odd". See http://tinyurl.com/AP19-Q27
• 6
• i%2 -1 ==0 means "is this number odd". See http://tinyurl.com/AP19-Q27
• 7
• i%2 -1 ==0 means "is this number odd". See http://tinyurl.com/AP19-Q27
• 8
• i%2 -1 ==0 means "is this number odd". See http://tinyurl.com/AP19-Q27

14-6-20: Which of the following is the equivalent in decimal to 2D in hexadecimal?

```String s = "Computer Science is fun!";
String s1 = s.substring(0,8);
String s2 = s1.substring(1);
String s3 = s2.substring(1,3);

System.out.println(s3);
```
• mput
• The .substring() function is inclusive left, exclusive right. Also if passed a single parameter it will take that as the starting index for the substring up to the rest of the string.
• mpu
• The .substring() function is inclusive left, exclusive right. Also if passed a single parameter it will take that as the starting index for the substring up to the rest of the string.
• mp
• The .substring() function is inclusive left, exclusive right. Also if passed a single parameter it will take that as the starting index for the substring up to the rest of the string.
• omp
• The .substring() function is inclusive left, exclusive right. Also if passed a single parameter it will take that as the starting index for the substring up to the rest of the string.
• Om
• The .substring() function is inclusive left, exclusive right. Also if passed a single parameter it will take that as the starting index for the substring up to the rest of the string.