Search Header Logo

Unit 10 APCSA

Authored by Jeff Cougger

Computers

12th Grade

Used 5+ times

Unit 10 APCSA
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

44 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Consider the following method.

public int addFun(int n)

{

if (n <= 0)

return 0;

if (n == 1)

return 2;

return addFun(n - 1) + addFun(n - 2);

}

What value is returned as a result of the call addFun(6) ?

12

10

16

26

32

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Consider the following method, which implements a recursive binary search.

/** Returns an index in arr where the value x appears if x appears

* in arr between arr[left] and arr[right], inclusive;

* otherwise returns -1.

* Precondition: arr is sorted in ascending order.

* left >= 0, right < arr.length, arr.length > 0

*/

public static int bSearch(int[] arr, int left, int right, int x)

{

if (right >= left)

{

int mid = (left + right) / 2;

if (arr[mid] == x)

{

return mid;

}

else if (arr[mid] > x)

{

return bSearch(arr, left, mid - 1, x);

}

else

{

return bSearch(arr, mid + 1, right, x);

}

}

return -1;

}

The following code segment appears in a method in the same class as bSearch.

int[] nums = {0, 4, 4, 5, 6, 7};

int result = bSearch(nums, 0, nums.length - 1, 4);

What is the value of result after the code segment has been executed?

1

2

3

4

5

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Consider the following method, which implements a recursive binary search.

/** Returns an index in myList where target appears,

* if target appears in myList between the elements at indices

* low and high, inclusive; otherwise returns -1.

* Precondition: myList is sorted in ascending order.

* low >= 0, high < myList.size(), myList.size() > 0

*/

public static int binarySearch(ArrayList<Integer> myList,

int low, int high, int target)

{

int mid = (high + low) / 2;

if (target < myList.get(mid))

{

return binarySearch(myList, low, mid - 1, target);

}

else if (target > myList.get(mid))

{

return binarySearch(myList, mid + 1, high, target);

}

else if (myList.get(mid).equals(target))

{

return mid;

}

return -1;

}

Assume that inputList is an ArrayList of Integer objects that contains the following values.

[0, 10, 30, 40, 50, 70, 70, 70, 70]

What value will be returned by the call binarySearch(inputList, 0, 8, 70) ?

-1

5

6

7

8

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Consider the following method.

public static int calcMethod(int num)

{

if (num == 0)

{

return 10;

}

return num + calcMethod(num / 2);

}

What value is returned by the method call calcMethod(16) ?

10

26

31

38

41

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Consider the following two static methods, where f2 is intended to be the iterative version of f1.

public static int f1(int n)

{

if (n < 0)

{

return 0;

}

else

{

return (f1(n - 1) + n * 10);

}

}

public static int f2(int n)

{

int answer = 0;

while (n > 0)

{

answer = answer + n * 10;

n--;

}

return answer;

}

The method f2 will always produce the same results as f1 under which of the following conditions?

I. n < 0

II. n = 0

III. n > 0

I only

II only

III only

II and III only

I, II, and III

6.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Consider the following method.

public String goAgain(String str, int index)

{

if (index >= str.length())

return str;

return str + goAgain(str.substring(index), index + 1);

}

What is printed as a result of executing the following statement?

System.out.println(goAgain("today", 1));

today

todayto

todayoday

todayodayay

todayodaydayayy

7.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Directions: Select the choice that best fits each statement. The following question(s) refer to the following information

Consider the following binarySearch method. The method correctly performs a binary search.

Consider the following code segment.

int [ ] values = {1, 2, 3, 4, 5, 8, 8, 8};int target = 8;

What value is returned by the call binarySearch (values, target) ?

-1

3

5

6

8

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?