0% found this document useful (0 votes)
8 views11 pages

2.control Structures in Python

The document contains a series of questions and explanations related to control structures in Python, including loops, conditionals, and list operations. It addresses common misconceptions and provides correct answers with explanations for each question. Key concepts include the behavior of loops, the use of the 'if-else' statement, and the properties of lists.

Uploaded by

Mohan Vishal
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)
8 views11 pages

2.control Structures in Python

The document contains a series of questions and explanations related to control structures in Python, including loops, conditionals, and list operations. It addresses common misconceptions and provides correct answers with explanations for each question. Key concepts include the behavior of loops, the use of the 'if-else' statement, and the properties of lists.

Uploaded by

Mohan Vishal
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
You are on page 1/ 11

CONTROL STRUCTURES

1. How many times does the print statement will execute.


A=10
While A<=10:
Print(‘first time’)
A=A+1
(a) Two times
(b) Three times
(c) Zero time
(d) One time

1. What is the output of the following?


x = ['ab', 'cd']
for i in x:
i.upper()
print(x)

a) [‘ab’, ‘cd’].
b) [‘AB’, ‘CD’].
c) [None, None].
d) none of the mentioned

EXPLANATION: The function upper() does not modify a string in place, it returns a new
stringwhich isn’t being stored anywhere.

2. Find the error line in the below program

A=10

B=20

If A=B:

Print(‘A and B are equal’)

Else:

Print(‘they are not equal)

(a) third line


(b) fourth line
(c) fifth line
(d) sixth line
CONTROL STRUCTURES

2. python has only two loop types for and while loops?

a. True

b. false

EXPLANATION: Python has 3 loop types they are for, while and nested loops

3. What will be the output to the below code?

india=[14,08,1947]

print(india [2])

(a) 14
(b) 08
(c) 1947
(d) None

3.In python, a decision can be made by using if else statement


a. True
b. False
EXPLANATION: The if statement alone tells us that if a condition is true it will execute
ablock of statements and if the condition is false it won’t. But what if we want to do
something else if the condition is false. Here comes the else statement. We can use the else
statement with if statement to execute a block of code when the condition is false.

4. What will be the output to the below code

A=85

B=50

C=A+B

D=145

If d<c:

Print(‘less than’)

Elif d>c:

Print(‘greater than’)

Else:
CONTROL STRUCTURES

Print(‘none’)

(a) Less than


(b) Greater than
(c) None
(d) None of the above

4.What is the output of the following?

i=0

while i < 5:

print(i)

i += 1

if i == 3:

break

else:

print(0)

a) 0 1 2 0

b) 0 1 2

c) error

d) none of the mentioned

EXPLANATION: The else part is not executed if control breaks out of the loop.

5.what will be the output

List=[25,35,78,81]

Print (List [3] %3, List [2] %3]

(a) 0 1
(b) 1 0
(c) 0 0
(d) 1 1
CONTROL STRUCTURES

5. what will be the output

List= [23,35,78,81]

print (List [0] %3, List [2] %7]

(a)0 1

(b)1 0

(c)2 1

(d)1 1

EXPLANATION: In python % symbol will give remainder number as output, 23 % 3 will


give 2 as remainder and for 78 % 7 gives 1 as remainder.

6. How many times does the word ‘print’ will appear in the output

A=85

While a>85:

Print(‘print’)

A=A-1

(a) 0 times
(b) 84 times
(c) 86 times
(d) Infinite time
CONTROL STRUCTURES

6. which is the output of the following?

i=1

while True:

if i%7 == 0:

break

print(i)

i += 1

a) 1 2 3 4 5 6

b) 1 2 3 4 5 6 7

c) error

d) none of the mentioned

EXPLANATION: Control exits the loop when i becomes 7.

7. What problem does the previous question code exhibits

(a) invalid syntax

(b) indentation problem

(c) Infinite loop

(d) none of the above

7.The purpose of using a loop is to

a. repeat operation(s)many times

b. certain process is done

c. both a & b

d. make decision

Explanation: A loop is a sequence of instruction s that is continually repeated until a


certain condition is reached
CONTROL STRUCTURES

8. How does the elements in the list are defined

(a) Flower bracket

(b) Square bracket

(c) Curly braces

(d) None of the above

8. The pass statement is a null operation, what will happen when it executes

a) skip

b) syntax error

c) nothing

d) none of the above

EXPLANATION: The pass statement is a null operation; nothing happens when it executes

9.How the elements within the list are separated

(a) Semi colon

(b) Colon

(c) Comma

(d) None of the above

9. which one throws an error? for the below input

my_list = ['p','r','o','b','e']

a. print(my_list[0])

b. print(my_list[2])

c. my_list[4.0]

d. print(my_list[4])

EXPLANATION: Only integer can be used for indexing


CONTROL STRUCTURES

10. How many times does the elif block can be assigned within the if else statement

(a) three times

(b) two times

(c) any number of times

(d) None of the above

10. loops are traditionally used when you have a block of code which you want to repeat a
fixed number of times

a. True

b. False

11.What will be the output for the code

My_list = [56,52,48,44]

print (My_list[4]/8)

(a) 5.5
(b) 7
(c) 13
(d) IndexError

EXPLANATION: It will give IndexErrorbecause 4 is not part of My_list. Instead of 4 if we


give My_list[3] / 8 we will get 5.5 as output

12. For what purpose does the for statement is used

(a) to execute the logic till the assigned condition fails

(b) to execute the logic for fixed number of times

(c) to execute the logic a time

(d) none of the above


CONTROL STRUCTURES

12. what is the output of the following?

True = False

while True:

print(“True”)

break

a) True

b) False

c) None

d) SyntaxError

EXPLANATION: It will throw a SyntaxError because of True is a keyword and it’s value
cannot be changed

13.Python does not appear to have a switch statement.

(a) True

(b)False

EXPLANATION: In other languages we would use a switch or case statement, but Python
does not appear to have a switch statement

14. Does the interpreter recognizes the pass statement

(a) yes

(b)no
CONTROL STRUCTURES

14. What is the output of the following?

i=1

while True:

if i%3 == 0:

break

print(i)

i+=1

a) 1 2

b) 1 2 3

c) SyntaxError

d) none of the mentioned

EXPLANATION: SyntaxError, there shouldn’t be a space between + and = in +=.

15. What does the break statement do

(a) returns the control to the top of the loop

(b)exits the loop and control goes for next statement

(c) stops the execution of the program

(d) none of the above

16. Which one is having the highest precedence

(a) **

(b) +

(c) *

(d) /

EXPLANATION: The order of precedence is: PEMDA,

Parentheses ( ), Exponentiation ** , Multiplication *, Division /, Addition +


CONTROL STRUCTURES

17.Which one is having lowest precedence

(a) identity operators

(b) membership operators

(c) logical operators

(d) none of the above

17. What is the output of the following expression:

print(4.00/(2.0+2.0))

(a) error

(b) 1.0

(c) 1.00

(d) 1

EXPLANATION: The result of the expression shown above is 1.0 because print rounds off
digits.

18. What will be the output to the below code

List=[18,20,33,a,44]

Print(List[1:4])

(a) 18 20 33 a 44

(b) 20 33 a 44

(c) 18 20 33 a

(d) None of the above

18. What will be the output to the below code

List=[18,20,33,'a',44]

print(List[1:4])

(a) [18,20,33, a]
(b) [20,33, ‘a’,44]
(c) [18,20,33, a]
(d) [20, 33, ‘a’]
CONTROL STRUCTURES

19. Which one of the following is used for concatenating the lists

(a) ||

(b)+

(c) c

(d) &&

EXPLANATION: In order to merge two strings into a single object, you may use the “+”
operator. When writing code that would like this: str1 = “Hello ” str2 = “World” str1 + str2
we get “Hello World”

20.Which one of the following keyword is used to check whether an element in an list exist
or not

(a) is

(b) in

(c) where

(d) none

20. what is the output

A= [25,89,56,56,85]

len (A)

(a) 5

(b) 4

(c) 6

(d)none

EXPLANATION: len( ) function is an inbuilt function in Python programming language that


returns the length of the string. Return Value: It returns an integer which is the length
of the string

You might also like