Computer science 7
For this question, consider the following code: def mystery(a = 2, b = 1, c = 3): return 2 * a + b + 3 * c What is the output for mystery(1)?
12 (with margin:0)
Functions can do all of the following except:
Allow writing without proper syntax
For this question, consider the following code: def mystery(w, n): if(n > 5): return w.upper() else: return w.lower() The following line of code will output the string in upper case. print(mystery("HELLO", 0))
FALSE
What is output by the following code? def mystery(w): if (w.upper() == w): return "TRUE" else: return "FALSE" print(mystery("What a wonderful day!"))
FALSE
A collection of instructions that are given a name; sometimes called function or method.
Subprogram
For this question, consider the following code: def mystery(w, n): if(n > 5): return w.upper() else: return w.lower() The following line of code will output the string in lower case. print(mystery("bye", 0))
TRUE
Extra pieces of information passed to the function
parameter
A collection of instructions that are given a name.
Function
Which of the following applies to functions and describes a reason we use functions?
To enable to code to be reused
Consider the following code: def volume(x, y, z): return x * y * z #MAINv = 0length = int(input("Length: ")) width = int(input("Width: ")) height = int(input("Height: ")) v = volume(length, width, height) print("Volume:" + str(v)) What is the output if the user enters 3, 8, 2?
Volume: 48
Used to define a function
def
Variables used inside a function - once the function ends, the variable value is no longer accessible
local variable
def mult(a, b = 2, c = 1): return (a * b * c) print(mult(4))
8
Functions are usually declared _________ the main program in the source file.
before
Which of the following is an example of a Python built-in function?
print()
A function stops running when the program encounters the ______ keyword.
return
The purpose of indenting code in functions is:
to indicate which part of the code is in the function
Parameters are:
variables that receive the values a function is called with
For this question, consider the following code: def mystery(a = 2, b = 1, c = 3): return 2 * a + b + 3 * c What is the output for mystery()?
14
What is output by the following program? def sample(val): val = val - 8 #MAIN n = 16 sample(n) print(n)
16
For this question, consider the following program: def tryIt(x, y = 1): return 10 * x * y #MAIN n = int(input('Enter a number: ')) ans = tryIt(n) + 1print (ans) What is output if the user enters 5?
51 (with margin: 0)
Consider the following program: def test_1(): test_3() print("A") def test_2(): print("B") test_1() def test_3(): print("C") What is output by the following line of code? test_1()
C A
What is output by the following program? def subtractOne(n): n = n - 1 print ("In the function: " + str(n)) #Main value = 10 subtractOne(value) print("In main: " + str(value))
In the function 9: in main 10
Consider the following function: def avg(a, b, c = 0): if (c ==0): print((a + b)/2) else: print((a + b +c)/3) Which of the following calls is NOT correct?
avg(88)
Which keyword is used to define a function?
def
To create the body of a function, we ____________ the code.
indent
For this question, consider the following program: def tryIt(x, y = 1) : return 10 * x * y #MAINn = int(input('Enter a number: ')) ans = tryIt(n) + 1print (ans) What is output if the user enters -3?
-29 (with margin: 0)
For this question, consider the following code: def mult(x): return x * 2 def add(x, y = 5): return x + y #MAINn = int(input("Enter a number:")) print(mult(n * -1) + add(n)) What is output when the user enters 10?
-5
Consider the following code: def totalUp (a, b = 8, c = 12): return a * b + c #MAIN PROGRAM x = int(input("First value: ")) y =int(input("Second value: ")) z = int(input("Third value: ")) print(totalUp (x, y)) What is output when the user enters 3, 2 and 6?
18
Consider the following code: def calc(num1, num2 = 0): print(num1 + num2) #MAIN val1 = int(input(" ")) val2 = int(input(" ")) calc(val1, val2) What is output when the user enters 19 and 18
37
Consider the following program: def roundItUp(val): val = val + 0.5 #MAIN PROGRAM n = 5.8 roundedValue = roundItUp(n) print(int(roundedValue)) What is output?
6
Consider the following program: def roundItUp(val): val = val + 0.5 #MAIN PROGRAM n = float(input("Enter a decimal number: ")) roundedValue = roundItUp(n) print(int(roundedValue)) What is output when the user enters 6.5 ?
7
Which of the following parameters is optional? sample(a, b, c = "Latte")
C
What is output by the following program? def greeting(name = "?"): return "Hello " + name + "!" #MAIN print(greeting())
Hello?!
Consider the following code: def roomArea(l, w): return l * w #MAIN PROGRAMlength = int(input("Length: ")) width = int(input("Width: ")) height = int(input("Height: ")) ra = roomArea(length, width) print("The area of the room is: " + str(ra)) What is the output if the user enters 3, 8, 2?
The area of the room is: 24
Consider the following program: def sillyString(w): return w.replace( "a", "oo") #MAIN fruit = input ("Enter a state: ") print(sillyString(fruit)) What is output if the user enters "california"?
ccolifornioo
Variables used in the main function as well as other of functions.
global variables
When the program calls the function, a value is sent back to the function call.
return
The main section of code is where:
the central part of the program is