wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

AP Computer Science Principles Unit 7 Review

Total questions: 26

Worksheet time: 52mins

Name
Class
Date
1.

Which code segment results in "true" being returned if a number is even? Replace "MISSING CONDITION" with the correct code segment.

function isEven(num){

if(MISSING CONDITION){

return true;

} else {

return false;

}

}

a)

num % 2 == 0;

b)

num % 0 == 2;

c)

num % 1 == 0;

d)

num % 1 == 2;

2.

Here is the API for a robot library.

// moves the robot forward

function moveForward();


// turns the robot to the left

function rotateLeft();


// turns the robot to the right

function rotateRight();


// checks if a robot can move in any direction

// direction {string} - the direction to be checked

// return {Boolean} - true if the robot can move in that direction, otherwise returns false

function canMove(direction);


Which code segment will guarantee that the robot makes it to the gray square without hitting a wall or a barrier (black square)?

a)
b)
c)
d)
3.

What will be printed to the console after this program runs?


var numbers = [2, 5, 3, 1, 6]


function changeNums(numList, addNum, subtractNum){

for(var i=0; i<numList.length; i++){

if(numList[i] % 3 == 0){

numList[i] = numList[i] + addNum;

} else {

numList[i] = numList[i] - subtractNum;

}

}

}


changeNums(numbers, 3, 2);

console.log(numbers);

a)

[2, 5, 3, 1, 6]

b)

[0, 3, 6, -1, 9]

c)

[-1, 2, 6, -2, 8]

d)

[5, 2, 6, 3, 9]

4.

Which function calls would provide the most helpful test of this function?


Remember: With tests, you are attempting to figure out all the possible ways the function could be broken.


function findMin(num1, num2){

if(num1 < num2){

return num1;

} else {

return num2;

}

}

a)

findMin(-1, 0)

findMin(2,4)

findMin(5,10)

b)

findMin(5,3)

findMin(7,2)

findMin(5,1)

c)

findMin(1,1)

findMin(-2,2)

findMin(0,3)

d)

findMin(-1,1)

findMin(1,-1)

findMin(1,1)

5.

You have imported a library with the birthMonth() function. Based on the API, how many strings are inputed to calculate the birth month?


// calculate birth month based on the day of the month, day of the week, and the birth year

// dayMonth {number} - a day of a month from 1 to 31

// dayWeek {string} - the name of the day of the week

// year {number} - the birth year

// return {string} - the month you were born

BirthdayLibrary.birthMonth(dayMonth, dayWeek, year);

a)

1

b)

4

c)

0

d)

3

6.

listAverage() returns the average number in a list. Which of these functions does this correctly?

a)
b)
c)
d)
7.

What is printed to the console?


console.log(15 % 4);

a)

2

b)

3

c)

4

d)

1

8.

Which of the following is NOT true about procedural abstraction?

a)

Procedural abstraction improves code readability

b)

Procedural abstraction manages complexity by allowing for code reuse

c)

Procedural abstraction improves the speed at which a program executes

d)

Procedural abstraction allows a solution to a large problem to be based on the solution of smaller subproblems

9.

This function checks if a character is a vowel. If it is, it returns true. Otherwise, it returns false.

Where should return false; be written in the code?


function checkVowel(character){

var vowels = ["a", "e", "i", "o", "u"];

for(var i=0; i<vowels.length; i++){

if(vowels[i] == character){

return true;

OPTION A

}

OPTION B

}

OPTION C

}

OPTION D

a)

OPTION A

b)

OPTION B

c)

OPTION C

d)

OPTION D

10.

This function finds the minimum number in a list. What should <MISSING CODE SEGMENT> be replaced with in order for this function to operate as expected?


function min(numList){

var min = numList[0];

for(var i=0; i<numList.length; i++){

if(numList[i] < min){

<MISSING CODE SEGMENT>

}

}

return min;

}

a)

numList[i] = min;

b)

min = numList[i];

c)

min = numList;

d)

numList = min;

11.

Algorithms can be created in all the following ways EXCEPT:

a)

creating from an idea

b)

combining existing algorithms

c)

removing sequencing, selection, and iteration from an algorithm

d)

modifying existing algorithms

12.

Using existing algorithms as building blocks for new algorithms has all the following benefits EXCEPT:

a)

reduces development time

b)

reduces testing

c)

simplifies debugging

d)

removes procedural abstraction

13.

What is one of the benefits of using a library in a program?

a)

simplifies creating a complex program

b)

increases development time

c)

removes all testing

d)

reduces abstractions in the program

14.

Which call of the function correctly follows the instructions laid out in the API?

a)

moveElement("button1", 2, 23);

b)

moveElement("button1", "left", "thirty");

c)

moveElement("button1", 30, "two");

d)

moveElement("button1", "down", 25);

15.

Dividing a program into separate subprograms (such as libraries) is known as:

a)

modularity

b)

iteration

c)

API

d)

documentation

16.

Parameter

a)

the value passed to the parameter

b)

used to return the flow of control to the point where the procedure (also known as a function) was called and to return the value of expression.

c)

Extracting shared features to generalize functionality

d)

a variable in a function definition. Used as a placeholder for values that will be passed through the function.

17.

Argument

a)

The subdivision of a computer program into separate subprograms

b)

the value passed to the parameter

c)

a variable in a function definition. Used as a placeholder for values that will be passed through the function.

18.

Return

a)

Extracting shared features to generalize functionality

b)

a group of functions (procedures) that may be used in creating new programs

c)

The subdivision of a computer program into separate subprograms

d)

used to return the flow of control to the point where the procedure (also known as a function) was called and to return the value of expression.

19.

Procedural abstraction

a)

a group of functions (procedures) that may be used in creating new programs

b)

Extracting shared features to generalize functionality

c)

The subdivision of a computer program into separate subprograms

d)

Application Program Interface - specifications for how functions in a library behave and can be used

20.

What is another word for a function?

a)

function

b)

command

c)

abstraction

d)

to call

21.

If I want to make a function I must add parameters.

a)

True

b)

False

22.

How many parameters can I add to a function.

a)

1

b)

2

c)

3

d)

as many as you would like

23.

Parameters are different than arguments

a)

True

b)

False

24.

What will print to the console after running this code segment?

a)

15

b)

16

c)

21

d)

18

25.

What are the benefits of writing functions that use parameters and return? Try to list at least two.

4 lines
26.

What is the answer to


5 mod 2

a)

3

b)

2

c)

1

d)

0