Python Output Type

Question 1

What is the output of the following program : 

Python3

def myfunc(a):
    a = a + 2
        a = a * 2
    return a

print myfunc(2)
Cross

8

Cross

16

Tick

Indentation Error

Cross

Runtime Error



Question 1-Explanation: 

Python creates blocks of code based on the indentation of the code. Thus, new indent defines a new scope.

Question 2
What is the output of the expression : 3*1**3
Cross
27
Cross
9
Tick
3
Cross
1


Question 2-Explanation: 
Precedence of ** is higher than that of 3, thus first 1**3 will be executed and the result will be multiplied by 3.
Question 3

What is the output of the following program : 

Python3

print '{0:.2}'.format(1.0 / 3)
Cross

0.333333

Tick

0.33

Cross

0.333333:-2

Cross

Error



Question 3-Explanation: 

.2 defines the precision of the floating point number.

Question 4

What is the output of the following program : 

Python3

print '{0:-2%}'.format(1.0 / 3)
Cross

0.33

Cross

0.33%

Tick

33.33%

Cross

33%



Question 4-Explanation: 

The % converts the 0.33 to percentage with respect to 1.0

Question 5

What is the output of the following program : 

Python3

i = 0
while i < 3: 
	print i 
	i += 1
else: 
	print 0

Cross

0 1 2 3 0

Tick

0 1 2 0

Cross

0 1 2

Cross

Error



Question 5-Explanation: 

The else part is executed when the condition in the while statement is false.

Question 6
What is the output of the following program :
i = 0
while i < 5:
    print(i)
    i += 1
    if i == 3:
        break
else:
    print(0)

Cross
0 1 2 0
Tick
0 1 2
Cross
Error
Cross
None of the above


Question 6-Explanation: 
The else part is not executed if control breaks out of the loop.
Question 7

What is the output of the following program : 

Python3

print 'cd'.partition('cd')
Cross

(‘cd’)

Cross

(”)

Cross

(‘cd’, ”, ”)

Tick

(”, ‘cd’, ”)



Question 7-Explanation: 

The entire string has been passed as the separator hence the first and the last item of the tuple returned are null strings.

Question 8

What is the output of the following program : 

Python3

print 'abef'.partition('cd')
Cross

(‘abef’)

Cross

(‘abef’, ‘cd’, ”)

Tick

(‘abef’, ”, ”)

Cross

Error



Question 8-Explanation: 

The separator is not present in the string hence the second and the third elements of the tuple are null strings.

Question 9

What is the output of the following program : 

Python3

print 'abcefd'.replace('cd', '12')
Cross

ab1ef2

Tick

abcefd

Cross

ab1efd

Cross

ab12ed2



Question 9-Explanation: 

The first substring is not present in the given string and hence nothing is replaced.

Question 10
What will be displayed by the following code?
def f(value, values):
    v = 1
    values[0] = 44
t = 3
v = [1, 2, 3]
f(t, v)
print(t, v[0])
Cross
1 1
Cross
1 44
Cross
3 1
Tick
3 44


Question 10-Explanation: 
The value of t=3 is passed in funcion f(value,values) , v [list] is passed as values in the same function. The v is stored in values and values[0]=44 , changes the value at index[‘0’] in the list hence v=[44,2,3].
There are 11 questions to complete.

  • Last Updated : 19 Jun, 2019

Share your thoughts in the comments
Similar Reads