GE8151 - PSPP - UNIT III
CONTROL FLOW, FUNCTIONS These flash cards help you to revise the important questions related with Control flow statements and Functions.
64 flashcards · Computer Science · by Kamala Viswanathan
What does the following code print to the console? if 2 == 2: print("ice cream is tasty!")
ice cream is tasty!
What does the following code print to the console? if "cat" == "dog": print("prrrr") else: print("ruff")
ruff
What does the following code print to the console? if 5 > 10: print("fan") elif 8 != 9: print("glass") else: print("cream")
glass
What does the following code print to the console? name = "maria" if name == "melissa": print("usa") elif name == "mary": print("ireland") else: print("colombia")
colombia
What does the following code print to the console? if 88 > 100: print("cardio")
Nothing
What does the following code print to the console? hair_color = "blue" if 3 > 2: if hair_color == "black": print("You rock!") else: print("Boring")
Boring
What does the following code print to the console? if True: print(101) else: print(202)
101
What does the following code print to the console? if False: print("Nissan") elif True: print("Ford") elif True: print("BMW") else: print("Audi")
Ford
What does the following code print to the console? if 1: print("1 is truthy!") else: print("???")
1 is truthy!
What does the following code print to the console? if 0: print("huh?") else: print("0 is falsy!")
0 is falsy!
In programming, what is iteration? The repetition of steps within a program The order in which instructions are carried out A decision point in a program Testing a program to make sure it works
The repetition of steps within a program
Why is iteration important? It determines the order in which instructions are carried out It allows code to be simplified by removing duplicated steps It allows multiple paths through a program It ensures the code works correctly
2.It allows code to be simplified by removing duplicated steps
Which two statements are used to implement iteration? IF and WHILE ELSE and WHILE FOR and WHILE IF and ELSE
3.FOR and WHILE
The condition for a while loop to continue could include which of the following? While something equals something While something is greater than something While something is True All of these
All of these
Which of the following symbols is used in Python to mean "Equal to"? = != == >=
=
5 4 3 2 1 4 3 2 1 0 5 4 3 2 1 4 3 2 1 0
4 3 2 1 0
54321
5 4 3 2 1
Hello world!
Try again!
Hello world!
To repeat a fixed number of times use a while loop for loop if loop indentation
for loop
To repeat until a particular condition is true use while loop for loop if loop indentation
while loop
The character that must be at the end of the line for if, while, for etc. : . , ;
:
To solve a problem that requires the user to enter 10 numbers would use what type of iteration? While loop For loop Variable Selection
while loop
In a Python program, what would be the iteration variable (looping variable) in the FOR LOOP code? n num value i
n
In a Python program, what would be the starting value in the FOR LOOP code? 100 10 5 20
5
In a Python program, what would be the starting value in the FOR LOOP code? 25 4 1 0
0
What would be the output of the following snippet? n=1 while True: print (n) n+=1 if n==5: break print("After Break")
1 2 3 4 After Break
What would be the output of the following snippet? for str in "Python": if str == "t": break print(str) print("Exit from loop")
P y Exit from loop
What would be the output of the following snippet? n=0 while n < 5: n+=1 if n==3: continue print (n) print("Loop Over")
1 2 4 5 Loop Over
What would be the output of the following snippet? n=0 for n in range(5): n+=1 if n==3: continue print(n) print("Loop Over")
1 2 4 5 Loop Over
Write the correct way writing empty function in python.
def fun(): pass
Which of the following items are present in the function header? A. function name B. function name and parameter list C. parameter list D. return value
B. function name and parameter list
Which of the following enclose the input parameters or arguments of a function? A. brackets B. parentheses C. curly braces D. quotation marks
B. parentheses
Which of the following keywords marks the beginning of the function block? A. fun B. define C. def D. function
C. def
What is the name given to that area of memory, where the system stores the parameters and local variables of a function call? A. a heap B. storage area C. a stack D. an array
C. a stack
Which of the following function definition does not return any value? A. a function that prints integers from 1 to 100. B. a function that returns a random integer from 1 to 100. C. a function that checks whether the current second is an integer from 1 to 100. D. a function that converts an uppercase letter to lowercase.
A. a function that prints integers from 1 to 100.
What is the output of the following code snippet? def func(message, num = 1): print(message * num) func('Welcome') func('Viewers', 3) A. Welcome Viewers B. Welcome ViewersViewersViewers C. Welcome Viewers,Viewers,Viewers D. Welcome
B. Welcome ViewersViewersViewers
What is the output of the following code snippet? def myfunc(text, num): while num > 0: print(text) num = num - 1 myfunc('Hello', 4) A. HelloHelloHelloHelloHello B. HelloHelloHelloHello C. invalid call D. infinite loop
D. infinite loop
Which of the following would you relate to a function call made with an argument passed as its parameter? A. function invocation B. pass by value C. pass by reference D. pass by name
B. pass by value
What is the output of the following code snippet? def func(x = 1, y = 2): x = x + y y += 1 print(x, y) func(y = 2, x = 1) A. 1 3 B. 2 3 C. The program has a runtime error because x and y are not defined. D. 3 2 E. 3 3
E. 3 3
What is the output of the following code snippet? num = 1 def func(): num = 3 print(num) func() print(num) A. 1 3 B. 3 1 C. The program has a runtime error because x is not defined. D. 1 1 E. 3 3
B. 3 1
What is the output of the following code snippet? num = 1 def func(): num = num + 3 print(num) func() print(num) A. 1 4 B. 4 1 C. The program has a runtime error because the local variable ‘num’ referenced before assignment. D. 1 1 E. 4 4
E. 4 4
What is the output of the following code snippet? def test(x = 1, y = 2): x = x + y y += 1 print(x, y) test() A. 1 3 B. 3 1 C. The program has a runtime error because x and y are not defined. D. 1 1 E. 3 3
E. 3 3
Which of the following function headers is correct? A. def f(a = 1, b): B. def f(a = 1, b, c = 2): C. def f(a = 1, b = 1, c = 2): D. def f(a = 1, b = 1, c = 2, d):
C. def f(a = 1, b = 1, c = 2):
What is the output of the following code snippet? exp = lambda x: x ** 3 print(exp(2)) A. 6 B. 222 C. 8 D. None of the above
C. 8
What is the output of the following code snippet? myList = [lambda x: x ** 2, lambda x: x ** 3, lambda x: x ** 4] for f in myList: print(f(3)) A. 27 81 343 B. 6 9 12 C. 9 27 81 D. 8 27 64
C. 9 27 81
What is the output of the following code snippet? def func(): text = 'Welcome' name = (lambda x:text + ' ' + x) return name msg = func() print(msg('All')) A. Welcome All B. All Welcome C. All D. Welcome
A. Welcome All
Which of the following are the two main parts of every function in Python? The function signature and the function body The function heading and the function body The function signature and the function corpus The function head and the function tail
The function signature and the function body
def square(num): num_squared = num ** 2 return num_squared Which of the following lines of codes is the function’s signature? def square(num): return num_square None of these num_squared = num ** 2
def square(num):
Choose the correct function declaration of fun1() so that we can execute the following function call successfully fun1(25, 75, 55) fun1(10, 20) def fun1(**kwargs) No, it is not possible in Python def fun1(args*) def fun1(*data)
def fun1(args*)
What is the output of the add() function call def add(a, b): return a+5, b+5 result = add(3, 2) print(result) 15 8 (8, 7) Syntax Error
(8, 7)
Which library function helps to find the square root of a number?
sqrt()
sqrt() function is present in which module?
math
How will you use sqrt() function in programs?
math.sqrt(number)
What do you mean by GCD?
The greatest common divisor of two integers a and b is the largest integer that divides them both.
One of the efficient algorithm to find GCD is Euclidean Algorithm Number Theory
Euclidean Algorithm
Write the Euclidean formula for finding GCD.
gcd(a,b) = 'b', if 'b' divides 'a' otherwise find gcd(b,a%b)
Use the Euclidean algorithm to compute the greatest common divisor of 170 and 245.
We use the Euclidean algorithm: 245 = 1(170) + 75 170 = 2(75) + 20 75 = 3(20) + 15 20 = 1(15) + 5 15 = 3(5) The gcd is the last nonzero remainder; gcd(245, 170) = 5.
The pow() function returns the value of x to the power of y (xy). True Flase
True
The above picture depicts linear search. 1.True 2.False
True
The above picture depicts binary search. 1.True 2.False
True