4 Flow Controll

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Flow Controll

1. In a Python program, a control structure:

(a) directs the order of execution of the statements in the program.


(b) dictates what happens before the program starts and after it terminates.
(c) defines program-specific data structures
(d) manages the input and output of control characters

2. An empty/null statement in Python is _____.

(a) go
(b) pass
(c) over
(d) ;

3. The order of statement execution in the form. of top to bottom, is known _____ as
construct.

(a) selection
(b) repetition
(c)sequence
(d) flow

4. The _____ construct allows to choose statements to be executed, depending upon


the result of a condition.

(a) selection
(b) repetition
(c)sequence
(d) flow

5. The _____ construct repeats a set of statements a specified number of times or as ng


as a condition is true.

(a) selection
(b) repetition
(c)sequence
(d) flow

6. Which of the following statements will make a selection construct?

(a) if
(b) if-else
(c) for
(d) while

7. Which of the following statements will make a repetition construct?

(a) if
(b) if-else
(c) for
(d) while

8. In Python, which of the following will create a block in a compound statement ?

(a) colon
(b) statements indented at a lower, same level
(c) indentation in any form
(d) { }

9. What signifies the end of a statement block or suite in Python?

(a) A comment
(b) }
(c) end
(d) A line that is indented less than the previous line

10. Which one of the following if statements will not execute successfully?

(a)
if (1, 2) :
print('foo')

(b)
if (1, 2)
print('foo')

(c)
if (1, 2) :
print('foo')

(d)
if (1):
print('foo')

11. What does the following Python program display?

x=3
if x == 8:
print ("Am I here?", end = '')
elif x == 3:
print("Or here?", end = "")
else:
pass
print ("Or over here?")

(a) Am I here?
(b) Or here?
(c) Am I here? Or here?
(d) Or here? Or over here ?
(e) Am I here? Or over here?

12. If the user inputs: 2<ENTER>, what does the following code snippet print ?

x = float(input())
if(x == 1):
print("Yes")
elif (x >= 2):
print("Maybe")
else:
print ("No")

(a) Yes
(b) No
(c) Maybe
(d) Nothing is printed
(e) Error

Consider the following code segment for the questions 13-17:

a = int(input("Enter an integer: "))


b = int(input("Enter an integer: "))
if a <= 0:
b=b+1
else:
a=a+1
if a > 0 and b > 0:
print("W")
elif a > 0:
print("X")
if b > 0:
print("Y")
else:
print("Z")
13. What letters will be printed if the user enters 0 for a and 0 for b?

(a) Only W
(b) Only X
(d) W and X
(c) Only Y
(e) W, X and Y

14. What letters will be printed if the user enter; for a and 1 for b ?

(a) W and X
(b) W and Y
(c) X and Y
(d) X and Z
(e) W, X and Y

15. What letters will be printed if the user enter 1 for a and -1 for b?

(a) W and X
(b) X and Y
(c) Y and Z
(d) X and Z
(e) W and Z

16. What letters will be printed if the user enter 1 for a and 0 for b?

(a) W and X
(b) X and Y
(c) Y and Z
(d) X and Z
(e) W and Z

17. What letters will be printed if the user enter -1 for a and -1 for b?

(a) Only W
(b) Only X
(c) Only Y
(d) Only Z
(e) No letters are printed

18. What values are generated when the function range(6, 0, -2) is executed?

(a) [4, 2]
(b) [4, 2, 0]
(c) [6, 4, 2]
(d) [6, 4, 2, 0]
(e) [6, 4, 2, 0, -2]

19. Which of the following is not a valid loop a in Python ?

(a) for
(b) while
(c) do-while
(d) if-else

20. Function range(3) is equivalent to :

(a) range(1, 3)
(b) range(0, 3)
(c) range(0, 3, 1)
(d) range(1, 3, 0)

21. Function range(3) will yield an iteratable sequence like

(a) [0, 1, 2]
(b) [0, 1, 2, 3]
(c) [1, 2, 3]
(d) [0, 2]

22. Function range(0, 5, 2) will yield on iteratable sequence like

(a) [0, 2, 4]
(b) [1, 3, 5]
(c) [0, 1, 2, 5]
(d) [0, 5, 2]

23. Function range(10, 5, -2) will yield an iteratable sequence like

(a) [10, 8, 6]
(b) [9, 7, 5]
(c) [6, 8, 10]
(d) [5, 7, 9]

24. Function range(10, 5, 2) will yield an iteratable sequence like

(a) [ ]
(b) [10, 8, 6]
(c) [2, 5, 8]
(d) [8, 5, 2]

25. Consider the loop given below:


for i in range(-5):
print (i)

How many times will this loop run?

(a) 5
(b) 0
(c) infinite
(d) Error

26. Consider below:

for i in range(10, 5, -3):


print (i)

How many times will this loop run ?

(a) 3
(b) 2
(c) 1
(d) infinite

27. Consider the loop given below:

for i in range (3):


pass

What will be the final value of i after this loop?

(a) 0
(b) 1
(c) 2
(d) 3

28. Consider the loop given below:

for i in range (7, 4,-2):


break

What will be the final value of i after this loop?

(a) 4
(b) 5
(c) 7
(d) -2
29. In for a in _____:, the blank can be filled with

(a) an iterable sequence


(b) a range() function
(c) a single value
(d) an expression

30. Consider the following code segment:

for i in range (2, 4):


print (i)

What values(s) are printed when it executes ?

(a) 2
(b) 3
(c) 2 and 3
(d) 3 and 4
(e) 2, 3 and 4

31. What is the output produced when this code executes ?

for i in range(4,8):
if i % 2 == 0:
a=a+i
print(a)

(a) 4
(b) 8
(c) 10
(d) 18

32. Which of the following code segments contain an example of a nested loop ?

(a)
for i in range (10):
print (i)
for j in range (10):
print (j)

(b)
for i in range (10):
print(i)
for j in range(10):
print(j)
(c)
for i in range(10):
print(i)
for i in range(20):
print (i)
i=i+1
(d)
for i in range(10):
print(i)
for i in range(20):
print (i)

Answer :-

1. (a)
2. (b)
3. (c)
4. (a)
5. (b)
6. (a), (b)
7. (c), (d)
8. (b)
9. (d)
10. (b), (c)
11. (d)
12. (c)
13. (c)
14. (b)
15. (d)
16. (d)
17. (d)
18. (d)
19. (c), (d)
20. (b), (c)
21. (a)
22. (a)
23. (a)
24. (a)
25. (b)
26. (b)
27. (c)
28. (c)
29. (a), (b)
30. (c)
31. (c)
32. (b), (c)

1. The _____ statement forms the selection construct in Python.

2. The _____ statement is a do nothing statement in Python.

3. The _____ and _____ statements for the repetition construct in Python.

4. Three constructs that govern the flow of control are _____, _____ and _____.

5. In Python, _____ defines a block of statements.

6. An _____ statement has less number of conditional checks than two


successive ifs.

7. The _____ operator tests if a given value is contained in a sequence or not.

8. The two membership operators are _____ and _____ _____.

9. An iteration refers to one repetition of a _____.

10. The _____ loop iterates over a sequence:

Answer :-

1. if
2. pass
3. for, while
4. sequence, selection/decision, repetition/iteration
5. indentation
6. if-else
7. in
8. in, non in
9. loop
10. For

1. An if-else tests less number of conditions than two successive ifs.

2. A for loop is termed as a determinable loop.


3. The range() creates an iterable sequence.

4. The for loop can also tests a condition before executing the loop-body.

5. The range() function can only be used in for loops.

6. An if-elif-else statement is equivalent to a nested-if statement.

7. A for loop can contain another for loop in it.

Answer :-

1. True
2. True
3. True
4. False
5. False
6. True
7. True

You might also like