Unit 10 APCSA

Unit 10 APCSA

12th Grade

44 Qs

quiz-placeholder

Similar activities

Python Programming Credential

Python Programming Credential

9th - 12th Grade

46 Qs

PCEP Questions

PCEP Questions

9th - 12th Grade

46 Qs

Python Practical Questions

Python Practical Questions

9th - 12th Grade

46 Qs

Python Midterm Review

Python Midterm Review

9th - 12th Grade

41 Qs

AP CS A Fall Final Study Guide

AP CS A Fall Final Study Guide

9th - 12th Grade

40 Qs

PYTHON PROGRAMMING - FINAL EXAM REVIEW

PYTHON PROGRAMMING - FINAL EXAM REVIEW

9th - 12th Grade

46 Qs

APCSA Diag

APCSA Diag

9th - 12th Grade

40 Qs

Java Fundamentals

Java Fundamentals

10th - 12th Grade

47 Qs

Unit 10 APCSA

Unit 10 APCSA

Assessment

Quiz

Computers

12th Grade

Medium

Created by

Jeff Cougger

Used 5+ times

FREE Resource

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

Create a free account and access millions of resources

Create resources
Host any resource
Get auto-graded reports
or continue with
Microsoft
Apple
Others
By signing up, you agree to our Terms of Service & Privacy Policy
Already have an account?