Font size
WorksheetsComputer Science Principles
Total questions: 65
Worksheet time: 47mins
Cutting a big program up into smaller subprograms (functions, procedures, etc)
modularity
procedural abstraction
In computer programming, this allows you to use a function knowing WHAT it does but not necessarily HOW it does it.
modularity
procedural abstraction
Directions for how a collection of functions behave and can be used
Library
API
A group of functions (procedures) that may be used in creating new programs
library
API
the orange x is a parameter
the orange x is an argument
the "7" is a parameter
the "7" is an argument
the variable "y" on line 2 is a parameter
the variable "y" on line 2 is an argument
the variable "year" in function getYear(year)
is a parameter
the variable "year" in function getYear(year)
is an argument
a variable used as a place holder for a value passed to a function
parameter
argument
a value passed to a function
parameter
argument
<MISSING CODE> should be:
x % 2 == 0
<MISSING CODE> should be:
x % 2 == 1
<MISSING CODE> should be:
x % 1 == 0
<MISSING CODE> should be:
x % 1 == 2
<MISSING CODE> should be:
x % 1 == 1
0
5
4
0.2
3
3.75
0.267
0
1
2.14
2
0.41
In the above picture, this would be an example of a:
Local Variable
Nested Variable
Global Variable
Variable Scope
In the above picture, this would be an example of a:
Local Variable
Nested Variable
Global Variable
Variable Scope
A variable whose scope is "global" to the program, it can be used and updated by any part of the code. Its global scope is typically derived from the variable being declared (created) outside of any function, object, or method.
Global Variable
Local Variable
Wide Variable
Limited Variable
A variable with local scope is one that can only be seen, used and updated by code within the same scope. Typically this means the variable was declared (created) inside a function -- includes function parameter variables.
Short Variable
Small Variable
Global Variable
Local Variable
A function must always have parameters in order for it to work.
True
False
A function must have a return value in order for it work properly.
True
False
A function can have a parameter(s) but not necessarily have a return value.
True
False
A function can have a return value but not necessarily have any parameters.
True
False
Which of the following is FALSE regarding functions?
(a) Functions with parameters and return values help us simplify our code
(b) A function can have parameters, but no return values
(c) A function can return multiple values at one time
A, B, C are all false
Look at this code. Which of the following will be shown in the console log when this program is ran?
This line always runs
This line always runs
But this one never does
But this one never does
Nothing will run in the console log
A function is also known as a procedure
True
False
An argument can include:
(a) a number
(b) a string
Both A and B
Neither A or B
Which of the following is FALSE regarding return values?
(A) It stops the flow of the function
(B) If a return is inside of a conditional, if that condition is met the function ends there.
(C) It returns a value to the place where the function was called.
None of these (A, B, C are all True)
A function return value can never be stored inside a variable.
True
False
Parameters and return values allow you to write programs that are more organized and cleaner.
True
False
Look at this code. Which of the following will be shown in the console log when this program is ran?
10
20
1000
10
1000
Nothing will run in the console log
Generally it is up to you (as the programmer) to decide how a returned value will be used in the rest of your program.
True
False
Which of the following is FALSE regarding guidelines you should follow in writing functions with return values?
(1) Decide what values you need as input to your computation. Make these parameters of your function.
(2) Use your parameters to calculate your result and save that result in a single variable.
(3) Return that variable outside of this function
All three of these are True
In the example above, we say that lives is a local variable.
True
False
We say that function parameters are a form of a local variable because (choose all that apply):
they get created and initialized when a function is called
they get used while the function runs
they get destroyed when the function completes
they can be used both inside and outside of a given function
One of the disadvantages of writing functions that use parameters and return values is that it repeats code multiple times making your program disorganized and not very clean.
True
False
Once functions are written that take parameters and return values, it's easier to reuse that code in other places of your program.
True
False
Looking at this image, which of the following would be considered a parameter for the function totalCost?
total
check
cost
There are no parameters for this function
The function move(id, direction) could be used to move an element in any direction, rather than writing separate functions for each direction. This is considered an example of:
Modularity
Procedural Abstraction
Library
API
What will print to the console after running this code segment?
10
60
54
56
What is printed to the console log?
console.log (12 % 3);
4
3
0
2
A library should have all of the following documentation EXCEPT:
(A) how each function works
(B) a complete list of the arguments
(C) what (if anything) will be returned
A, B, C should all be included
Looking at the above documentation here, what else should be included here (in place of the question marks)?
number
string
list
Can be either number, string, or list (depends)
In Lesson 7 you answered we discussed potential problems that could come up if you tried to use a function without knowing what it does or how to interact with it. Those included all of the following EXCEPT:
Guessing what value is returned
Determining what argument(s) your function needs
Knowing what the function does
Finding an error when you run the code when the function is called
Before adding a function to a library, check for any use of a local variables within the function. If there is, rework the function using global variables and a return.
True
False
Before adding a function to a library, check if another function is called in this function. If so, both functions should be included in the library.
True
False
When creating a library (check all that apply):
Same as functions; do not start with a Capital Letter
Same as a function; no spaces in the library name
"Library" must be included in the name
Include what type of value the return and/or parameter has (ex. string)
As a user, what information would you need to know in order to use a particular library function?
You need to know the documentation for the functions so you can call them correctly and understand what will be returned
You need to know how the traversal looks in the function
You must know what arguments are used
You have to know what the coding looks like for the entire function
One benefit of hiding all of the code when filtering the dataset in a library would be that it makes your coding organized and neat.
True
False
What are the benefits of using existing algorithms instead of brand new algorithms (check all that apply)?
reduced development time
reduced testing
simplification and identification of errors
When we organize functions into a library and then call them in another program, we are really practicing this (check all that apply):
Modularity
API
Library
Procedural Abstraction
What is the output of the following program?
5
15
15
5
nothing will print
5
What is printed by the following program?
-7
7
=13
13
What will be printed to the screen when the following program is run?
function start(){
println(doubleNumber(doubleNumber(10)));
}
function doubleNumber(x){
var doubledX = 2 * x;
return doubledX;
}
20
100
40
x
In the following function printThreeTimes:
function printThreeTimes(word){
println(word);
println(word);
println(word);
}
What is the parameter of the function?
word
word
word
word
no parameter
println
What is the minimum number of parameters that must be given when calling the ‘drawCircle’ function?
function drawCircle(radius, x, y, color) {
var circle = new Circle(radius);
circle.setPosition(x, y);
circle.setColor(color);
add(circle);
}
1
2
0
4
// This function prints out the area of a rectangle given its base and height function rectangleArea(base, height){
var area = base * height;
println(area);
}
What are the names of the parameters?
base and height
length and width
var area
println(area)
What is printed by the program?
2
1
0
two
one
zero
nothing will print
0
1
2
How many parameters go into the function sum, and how many return values come out of the function sum
function sum(first, second, third){
var result = first + second + third;
println(first);
println(second);
println(third);
return result;
}
3 parameters go in,
1 return value comes out
3 parameters go in,
3 return value comes out
No parameters go in
No result comes out
Look at the screenshot. Examples of arguments would include (click all that apply):
Laquan
name
October
birthMonth
Look at the screenshot. Examples of parameters would include (click all that apply):
age
Lenora
March
birthMonth
Look at the screenshot. An example of a return that could take place in this function would be 12
True
False
Look at the screenshot. In order to rewrite this program to include parameters for the "searchTypeList" function, would could eliminate:
Just the global variable "typeInput"
Just the global variable "ratingInput"
Both global variables "typeInput" and "ratingInput"
Look at the screenshot. In order to eliminate the global variable called "selection", one solution would be to add a parameter to the "checkAnswer" function.
True
False
