
Unit 10 APCSA

Quiz
•
Computers
•
12th Grade
•
Medium
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
Similar Resources on Wayground
48 questions
Mastering Arrays in C

Quiz
•
12th Grade
40 questions
ĐỀ CƯƠNG ÔN TẬP CUỐI KÌ 1_TIN HỌC 10_KNTT

Quiz
•
12th Grade
41 questions
Quiz on Flow of Control in Python

Quiz
•
12th Grade
44 questions
Python Test 2

Quiz
•
7th - 12th Grade
48 questions
Business Letters

Quiz
•
10th Grade - Professi...
40 questions
PTS TLJ-XII TKJ_TA 2023/2024

Quiz
•
12th Grade
44 questions
Conceitos básicos de rede

Quiz
•
9th - 12th Grade
40 questions
Python Data Types

Quiz
•
8th - 12th Grade
Popular Resources on Wayground
10 questions
Video Games

Quiz
•
6th - 12th Grade
20 questions
Brand Labels

Quiz
•
5th - 12th Grade
15 questions
Core 4 of Customer Service - Student Edition

Quiz
•
6th - 8th Grade
15 questions
What is Bullying?- Bullying Lesson Series 6-12

Lesson
•
11th Grade
25 questions
Multiplication Facts

Quiz
•
5th Grade
15 questions
Subtracting Integers

Quiz
•
7th Grade
22 questions
Adding Integers

Quiz
•
6th Grade
10 questions
Exploring Digital Citizenship Essentials

Interactive video
•
6th - 10th Grade