0% found this document useful (0 votes)
11 views15 pages

Python Flow Control Concepts

Uploaded by

haniyamuskan11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views15 pages

Python Flow Control Concepts

Uploaded by

haniyamuskan11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Ganganagar, Bangalore

SUBJECT Computer Science


Topic : CHAPTER 6: Flow of Control
CLASS: I PUC
(1MCQ + 2M + 5M)

1. The interpreter checks indentation levels very strictly and throws up……………. errors if
indentation is not correct.
a. syntax errors b. logical errors c. both a & b d. None of the above

2. The types of looping constructs in Python


a. for and while b. for c. while d. only a

3. The types of selection constructs in Python


a. if b. if .. else c. if..elif d. all the above

4. The output generated by >>>list(range(0, 30, 5))


a. [0, 5, 10, 15, 20, 25] b. [0, 5, 10, 15, 20, 25 ,30]
c. (0, 5, 10, 15, 20, 25) d. (0, 5, 10, 15, 20, 25 ,30)

5. The output is :
for num in range (5): if num > 0:
print (num * 10)
a. 10 20 30 40 b. [0 10 20 30 40]
c. 10 20 30 40 50 d. None

6. What is the output for this code list(range (-10))


a. [ ] b. Syntax Error
c. [-1, -2, -3, -4, -5, -6, -7, -8,-9] d. [0, -1, -2, -3, -4, -5, -6, -7, -8,-9]

7. for statement range values can be


a. Numeric b. string c. list, or tuple d. all of the above

8. In function range ( ), by default the list starts from


a. Zero b. one c. both a & b d. None

9. In function range ( ), the step parameter can be


a. positive integer b. negative integer c. both a & b d. Zero

10. >>> list(range(10)) the output is


a. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] b. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10]
c. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 d. (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

11. for loops only implement the ………………. Iteration


a. collection-based b. selection-based c. both a & b d. None

12. The ……. statement alters the normal flow of execution as it terminates the current loop and
resumes execution of the statement following that loop
a. break b. continue c. exit d. None

1
13. a ……… statement is encountered, the control skips the execution of current iteration and
jumps to the beginning of the loop for the next iteration.
a. break b. continue c. exit d. None

14. ……… exit from a loop before continuing further in the loop
a. break b. continue c. exit d. None

15. …….. skip some statements of the loop before continuing further in the loop
a. break b. continue c. exit d. None

16. Python provides these statements as a tool to the programmer to control the flow of execution
of a program.
a. break b. continue c. both a & b d. only b

17. The ………. statement is used for selection or decision making.


a. if b. for c. while d. None

18. The ………. constructs allow sections of code to be executed repeatedly under some condition.
a. while and for b. if and if..else c. while d. for

19. If the condition of the while loop is initially false, the body
a. is executed even once b. is not executed even once c. is executed even repeatedly d. None

20. A loop contained within another loop is called a


a. nested loop b. loop within loop c. repeated loop d. None

21. When a ……… statement is encountered, the control jumps to the beginning of the loop for
the next iteration
a. break b. continue c. exit d. None

22. The ……… statement immediately exits a loop, skipping the rest of the loop’s body
a. break b. continue c. exit d. None

23. count = 1
while count <= 5:
print(count)
count += 1
a. 1 2 3 4 5 b. 1 2 3 4 c. 1 2 3 4 d. error

24. What is the primary purpose of the if statement in Python?


a) To execute a block of code based on a condition
b) To perform mathematical operations
c) To repeat a block of code
d) To define a function

25. What is the purpose of the elif statement in Python?


a) To execute a block of code if the previous conditions are false
b) To define a loop
c) To perform arithmetic operations
d) To exit from a loop
26. Which keyword is used to execute a block of code if the condition in the if statement is false?
a) else b) elif c) while d) for

2
27. What will be the output of the following code snippet?
x=5
if x < 3:
print("x is less than 3")
elif x == 3:
print("x is equal to 3")
else:
print("x is greater than 3")
a) x is less than 3 b) x is equal to 3 c) x is greater than 3 d) No output

28. How can you execute multiple statements under a single if block in Python?
a) Separate statements with a semicolon b) Indent the statements to the same level
c) Use the and keyword between statements d) Use the elif keyword

29. What will the following code snippet print?


x = 10
if x < 5:
print("x is less than 5")
elif x > 15:
print("x is greater than 15")
else:
print("x is between 5 and 15")
a) x is less than 5 b) x is greater than 15 c) x is between 5 and 15 d) No output

30. What is the purpose of the while loop in Python?


a) To execute a block of code repeatedly until a condition is false
b) To execute a block of code a fixed number of times
c) To define a function
d) To iterate over items in a sequence

31. What is the syntax for a while loop in Python?


a) while condition: b) while condition(): c) while (condition): d) while loop condition:

32. How can you exit a loop prematurely in Python?


a) Using the break statement b) Using the continue statement
c) Using the pass statement d) Using the exit function

33. What is the purpose of the for loop in Python?


a) To execute a block of code repeatedly until a condition is false
b) To iterate over items in a sequence
c) To execute a block of code a fixed number of times
d) To define a function

34. What is the syntax for a for loop in Python?


a) for item in sequence: b) for item in range(n):
c) for index in range(len(sequence)): d) All of the above

35. What will the following code snippet print?


fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
a) apple banana cherry b) [“apple”, “banana”, “cherry”] c) 0 1 2 d) No output

3
36. How can you skip the current iteration of a loop and continue with the next iteration?
a) Using the skip statement b) Using the pass statement
c) Using the break statement d) Using the continue statement

37. What is the purpose of the range() function in Python?


a) To generate a sequence of numbers
b) To iterate over items in a sequence
c) To define a function
d) To execute a block of code repeatedly until a condition is false

38. What will the following code snippet print?


for i in range(3):
print(i)
a) 0 1 2 b) 1 2 3 c) 2 1 0 d) 3 2 1 0

39. What will be the output of the following code?


for i in range(1, 6):
if i == 3:
continue
print(i)
a) 1 2 b) 1 2 3 c) 1 2 4 5 d) 1 2 4

40. What will be the output of the following code?


for i in range(3):
for j in range(3):
print(i + j, end=' ')
print()
a) 0 1 2 b) 0 1 2 c) 0 1 2 d) 0 1 2
123 123 123 234
234 234 234 456
345 456
41. What is the order of execution of statements in a program called?
a) Execution flow b) Control flow c) Flow of control d) Program sequence

42. Which control structures does Python support for flow of control?
a) Sequence and iteration PRIMUS b) Selection and iteration
c) Selection and repetition d) Sequence and selection

43. How does Python group statements into a single block of code?
a) Curly brackets b) Parentheses c) Indentation d) Semicolon

4. What is used to implement decision-making in Python?


a) for loop b) if..else statement c) while loop d) switch statement

45. What does the break statement do in a loop? PRIMUS PU COLLEGE, BANGALORE
a) Skips an iteration b) Repeats the current iteration c) Exits the loop d) Restarts the loop

46. What is the purpose of the continue statement?


a) Exits the loop b) Skips the current iteration c) Restarts the loop d) Ends the program

47. What is a loop inside another loop called?


a) Recursive loop b) Nested loop c) Inner loop d) Infinite loop

4
48. Which of the following is a correct example of a nested loop in Python?
a) `for i in range(3): for j in range(2): print(j)` b) `for i in range(3) { for j in range(2) { print(j) } }`
c) `for (i in range(3)) (for j in range(2)) (print(j))` d) `for i in range(3): { for j in range(2): { print(j) }
}`

49. What happens if indentation is incorrect in a Python program?


a) Program runs with warnings b) Program skips the indented block
c) Syntax error d) Indentation error

50. How can a block of code be created in Python?


a) Using curly brackets `{}` b) Using indentation c) Using parentheses `()` d) Using square brackets `[]`

51. What is the output of the following code snippet?


Python Code:
num1 = 5
num2 = 6
if num1 > num2:
print("first number is larger")
print("Bye")
else:
print("second number is larger")
print("Bye Bye")
a) Syntax error b) `first number is larger Bye` c) `second number is larger Bye Bye` d) `Bye Bye`

52. What will be the output of the following code?


Python Code: PRIMUS PU COLLEGE, BANGALORE
for i in range(1, 6):
if i == 3:
continue
print(i)
a) 1 2 3 4 5 b) 1 2 4 5 c) 1 2 4 5 6 d) 2 4 6

53. Which statement is used to skip the rest of the code inside a loop for the current iteration only?
a) break b) pass c) skip d) continue

54. What type of loop is used in the following code? PRIMUS


Python Code:
while True:
print("Infinite loop")
a) for loop b) finite loop c) infinite loop d) nested loop

55. Which of the following is the correct syntax for an if..else statement in Python?
a) `if condition: statements else: statements`
b) `if condition { statements } else { statements }`
c) `if (condition): statements; else: statements;`
d) `if condition: { statements } else: { statements }`

56. Which of the following programs will print the sum of all positive numbers entered by the user until a negative
number is entered?
a) Python Code:
sum = 0
while True:
num = int(input("Enter a number: "))
if num < 0:
5
break
sum += num
print("Sum:", sum)

b) Python Code:
sum = 0
while True:
num = int(input("Enter a number: "))
if num < 0:
continue
sum += num PRIMUS PU COLLEGE, BANGALORE
print("Sum:", sum)

c)Python Code:
sum = 0
num = int(input("Enter a number: "))
while num >= 0:
sum += num
num = int(input("Enter a number: "))
print("Sum:", sum)

d) Python Code:
sum = 0
while num >= 0:
num = int(input("Enter a number: "))
if num < 0:
break
sum += num
print("Sum:", sum) PRIM
Answer: a)
Python Code:
sum = 0
while True:
num = int(input("Enter a number: "))
if num < 0:
break
sum += num
print("Sum:", sum)

57. In a nested loop, how many times will the inner loop execute if the outer loop runs 3 times and the inner loop
runs 2 times each iteration?
a) 3 b) 6 c) 2 d) 5

58. How is a block of code indicated in Python?


a) By using curly brackets b) By using parentheses c) By using indentation d) By using semicolons

59. What type of error will you get if the indentation is not consistent in Python?
a) Logical error b) Syntax error c) Runtime error d) Semantic error

60. What will be the output of the following code?


Python Code:
for i in range(5):
if i == 2:
continue
print(i) PRIMUS PU COLLEGE, BANGALORE
6
a) 0 1 2 3 4 b) 1 2 3 4 5 c) 0 1 3 4 d) 0 1 3 4 5

61. How can the flow of control be altered in a program?


a) Using operators b) Using control structures c) Using functions d) Using variables

62. What does the else part of an if statement represent?


a) Code that runs if the condition is false b) Code that runs if the condition is true
c) A secondary condition d) An error handling block

63. What does the following code do?


Python Code:
x=5
y = 10
if x < y:
print("x is less than y")
else:
print("x is not less than y")
a) Prints "x is not less than y" b) Prints "x is less than y" c) Prints nothing d) Syntax error

64. What is the purpose of an infinite loop?


a) To iterate a fixed number of times b) To loop until a condition is met
c) To keep running without termination PRIMUS PU COLLEGEd) To generate an error

65. Which of the following is true about the break statement in a loop?
a) It skips to the next iteration b) It stops the current iteration and continues with the next
c) It stops the loop entirely d) It only works with for loops

66. What is the output of the following code?


Python Code:
for i in range(4):
print(i)
if i == 2:
break
a) 0 1 2 3 b) 0 1 c) 0 1 3 PRIMUS PU COLLEGE, BANd) 0 1

67. How does the continue statement affect a loop's execution?


a) Terminates the loop
b) Skips the rest of the code inside the loop for the current iteration
c) Repeats the current iteration
d) Exits the loop and continues with the next iteration

68. What is the difference between a for loop and a while loop?
a) For loop is used for definite iteration, while loop is used for indefinite iteration
b) For loop has a fixed number of iterations, while loop runs indefinitely
c) For loop is used for selection, while loop is used for repetition
d) There is no difference

69. What will be the output of the following code snippet?


Python Code: PRIMUS PU COLLEGE, BANGALORE
count = 0
while count < 5:
count += 1
print(count)
a) 0 1 2 3 4 5 b) 1 2 3 4 5 c) 0 1 2 3 4 d) 1 2 3 4

7
70. How many times will the loop run in the following code?
Python Code:
for i in range(3):
for j in range(2):
print(i, j)
a) 3 times b) 6 times c) 2 times PRIMUS PU COd) 5 times

71. Which statement is used to exit a loop in Python?


a) exit b) break c) continue d) stop

72. What is the purpose of a nested loop?


a) To create infinite loops
b) To break out of a loop
c) To perform repeated operations on each element of a sequence
d) To skip iterations in a loop

73. Which statement correctly describes the functionality of break and continue in Python?
a) break terminates the loop, continue skips the current iteration
b) break skips the current iteration, continue terminates the loop
c) break and continue both terminate the loop
d) break and continue both skip the current iteration

74. What is the output of the following code?


Python Code:
for i in range(3):
for j in range(2):
if j == 1:
break
print(i, j)
a) 0 0 0 1 1 0 1 1 2 0 2 1
b) 0 0 1 0 2 0
c) 0 0 0 1 1 0 2 0 2 1
d) 0 0 1 0 1 1 2 0 2 1

75. Which of the following is a correct syntax for a while loop in Python?
a) `while condition: { statements }`
b) `while (condition) { statements }`
c) `while condition: statements`
d) `while (condition): statements

76. What happens when the break statement is executed inside a nested loop?
a) Exits all loops
b) Exits the inner loop only
c) Continues with the next iteration of the inner loop
d) Restarts the inner loop

77. Which of the following is the correct syntax for a for loop in Python?
a) `for (i in range(5)): print(i)`
b) `for i in range(5): print(i)`
c) `for i in range(5) { print(i) }`
d) `for (i in range(5)) { print(i) }`

78. How can you create a block of code in Python?


a) Using curly brackets b) Using indentation c) Using parentheses d) Using semicolons PRIMUS
PU COLLEGE, BANGALORE
8
21
79. What is the output of the following code?
Python Code:
count = 0
while count < 3:
print(count)
count += 1
a) 0 1 2 3 b) 1 2 3 c) 0 1 2 d) 0 1

80. Which of the following statements will break out of a loop?


a) exit b) continue c) stop d) break PRIMUS PU COLLEGE, BANGALORE
22
81. What is the purpose of the continue statement in a loop?
a) To terminate the loop
b) To skip the rest of the code inside the loop for the current iteration
c) To exit the loop and continue with the next iteration
d) To repeat the current iteration

82. What will be the output of the following code?


Python Code:
for i in range(3):
for j in range(2):
if j == 1:
break
print(i, j)
print("Out of nested loop")
a) 0 0 1 0 2 0 Out of nested loop b) 0 0 0 1 1 0 2 0 2 1 Out of nested loop PR
c) 0 0 1 0 2 0 Out of nested loop d) 0 0 1 0 1 1 2 0 2 1 Out of nested loop

83. What is the output of the following code?


Python Code:
for i in range(5):
if i == 3:
break
print(i)
else:
print("Completed")
a) 0 1 2 Completed b) 0 1 2 c) 0 1 2 3 4 Completed d) 0 1 2 3
Completed 4

84. What is the output of the following code snippet?


Python Code:
for i in range(2):
for j in range(2):
print(i, j)
a) 0 0 1 0 b) 0 0 1 1 c) 0 0 0 1 1 0 1 1 d) 0 0 1 0 1 1

85. Which of the following is true about the for loop?


a) It runs indefinitely b) It is used for definite iteration
c) It is used for selection d) It is used for decision-making

89. What will be the output of the following code?


Python Code:
for i in range(3):

9
if i == 1:
break
print(i)
else:
print("Loop completed")
a) 0 1 Loop completed
b) 0 Loop completed
c) 0 1
d) 0

90. How do you implement a nested loop in Python?


a) By placing one loop inside another loop
b) By placing one function inside another function
c) By using indentation
d) By using a break statement

Two marks and Five Marks Questions

1. Explain selection statement with example.


If the condition is true, then the indented statement(s) are executed. The indentation implies that
its execution is dependent on the condition. There is no limit on the number of statements that
can appear as a block under the if statement.

The syntax of if statement is:


if condition:
statement(s)

Example
age = int(input("Enter your age "))
if age >= 18:
print("Eligible to vote")

2. Explain if..else selection statement with example.


if..else statement allows us to write two alternative paths and the control condition determines
which path gets executed.
The syntax for if..else statement is as follows.
if condition:
statement(s)
else:
statement(s)

Example
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")

3. Explain if..elif selection statement with example.


When it require multiple conditions to be checked in such cases we can chain the conditions
using if..elif .
If the first condition is false, then the next condition is checked, and so on. If one of the
conditions is true, then the corresponding indented block executes, and the if statement

10
terminates.

The syntax for if..elif statement is as follows.


if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)

Example: # Check whether a number is positive, negative, or zero.


number = int(input("Enter a number: ")
if number > 0:
print("Number is positive")
elif number < 0:
print("Number is negative")
else:
print("Number is zero")

4. What is the difference between else and elif construct of if statement?


else means that if one alternative is true, the other alternatives must be false. It can be used only
once after if.
Use elif statements is possible for more than one alternative to be true simultaneously. There can
be more than one elif.

5. Explain ‘For’ Loop with syntax and example


The control variable checks whether each of the values in the range have been traversed or not.
When all the items in the range are exhausted, the statements within loop are not executed, the
control is then transferred to the statement immediately following the for loop.

Syntax of the For Loop

for <control-variable> in <sequence/items in


range>:
<statements inside body of the loop>

Example
#Print the characters in word PYTHON using
for loop
for letter in 'PYTHON':
print(letter)

11
6. What is the purpose of range() function? Give one example.
It is used to create a list containing a sequence of integers from the given start value upto stop
value excluding stop value.
The syntax for range function:
range([start], stop[, step])
By default the list starts from 0. by default the value increases by 1 in each iteration

Example
print(list(range(10))
Print(list(range(0,10))
Print(list(range(2,30,5)))
Print(list(range(-1,10))

7. Explain while loop with syntax and example.


The while statement executes a block of code repeatedly if the control condition of the loop is true.
When condition becomes false, the statements in the body of loop are not executed and the
control is transferred to the statement immediately following the body of while loop.

Syntax of while Loop


while test_condition:
body of while

Example
Program to print first 5 natural numbers using
Initialisation while loop.
Statement
#Program #Print first 5 natural numbers
using while loop
count = 1
while count <= 5:
print(count)
count += 1
Output:
1
Statements following
2
the while loop 3
4
5

8. Differentiate between break and continue statements using examples.


The break statement alters the normal flow of execution as it terminates the current
loop and resumes execution of the statement following that loop.
#Prints values from 0 to 3
for num in range(6):
num = num + 1
if num == 3:
break
print('Num has value ' + str(num))
print('End of loop')
output: 0 1 2
When a continue statement is encountered, the control skips the execution of remaining

12
statements inside the body of the loop for the current iteration and jumps to the beginning of the
loop for the next iteration.
#Prints values from 0 to 6 except 3
num = 0
for num in range(6):
num = num + 1
if num == 3:
continue
print('Num has value ' + str(num))
print('End of loop')
Output: 0 1 2 4 5 6

9. What is an infinite loop? Give one example.


An infinite loop in Python is a continuous repetition of the conditional loop until some external
factors.
Example:
N=0
While True:
print(n,end= ‘ ‘)
n=n+1

10. Find the output of the following program segments:


(i) a = 110
while a > 100:
print(a)
a -= 2
output: 110 108 106 104 102

(ii) for i in range(20,30,2):


print(i)
output : 20 22 24 26 28

(iii) country = 'INDIA'


for i in country:
print (i)
Output:
I
N
D
I
A

(iv) i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i=i+2
print (sum)

Output:
0
0
4
4
12

13
(v) for x in range(1,4):
for y in range(2,5):
if x * y > 10:
break
print (x * y)
Output:
23446869

(vi) var = 7
while var > 0:
print ('Current variable value: ', var)
var = var -1
if var == 3:
break
else:
if var == 6:
var = var -1
continue
print ("Good bye!")

Output:
Current value of val: 7
Current value of val: 5
Current value of val: 4
Good Bye!

11. Explain nested loops with example.


A loop may contain another loop inside it. A loop inside another loop is called a nested loop.
Any type of loop (for/while) may be nested within another loop.
Example: #Program to print the pattern for a number input by the user
#The output pattern to be generated is
#1
#1 2
#1 2 3
#1 2 3 4
#1 2 3 4 5
for i in range(1,6): #outer loop
...: for j in range(1,i+1,1): #inner loop
...: print(j,end=" ")
...: print(end="\n")

Programming Exercises
1. Write a program that takes the name and age of the user as input and displays a message
whether the user is eligible to apply for a driving license or not. (the eligible age is 18 years).
2. Write a function to print the table of a given number. The number has to be entered by the
user.
3. Write a program that prints minimum and maximum of five numbers entered by the user.
4. Write a program to check if the year entered by the user is a leap year or not.
5. Write a program to generate the sequence: –5, 10, –15, 20, –25….. upto n, where n is an integer
input by the user.
6. Write a program to find the sum of 1+ 1/8 + 1/27......1/n3, where n is the number input by the
user.
7. Write a program to find the sum of digits of an integer number, input by the user.
8. Write a function that checks whether an input number is a palindrome or not.
[Note: A number or a string is called palindrome if it appears same when written in reverse order
also. For example, 12321 is a palindrome while 123421 is not a palindrome]

14
9. Write a program to print the following patterns:

10. Write a program to find the grade of a student when


grades are allocated as given in the table below.
Percentage of Marks Grade
Above 90% A
80% to 90% B
70% to 80% C
60% to 70% D
Below 60% E
Percentage of the marks obtained by the student is input
to the program.

CHAPTER 6 – FLOW OF CONTROL

MCQ (1 marks) SA (2 marks) LA (3 Marks) Essay (5 Marks) Total

01 Questions 01 Question -- 01 Question 08 Marks

15

You might also like