Python Output Type

  • Last Updated : 19 Jun, 2019

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)
A

8

B

16

C

Indentation Error

D

Runtime Error

Python Output Type    
Discuss it


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
A
27
B
9
C
3
D
1
Python Output Type    
Discuss it


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)
A

0.333333

B

0.33

C

0.333333:-2

D

Error

Python Output Type    
Discuss it


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)
A

0.33

B

0.33%

C

33.33%

D

33%

Python Output Type    
Discuss it


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

A

0 1 2 3 0

B

0 1 2 0

C

0 1 2

D

Error

Python Output Type    
Discuss it


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)

A
0 1 2 0
B
0 1 2
C
Error
D
None of the above
Python Output Type    
Discuss it


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')
A

(‘cd’)

B

(”)

C

(‘cd’, ”, ”)

D

(”, ‘cd’, ”)

Python Output Type    
Discuss it


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')
A

(‘abef’)

B

(‘abef’, ‘cd’, ”)

C

(‘abef’, ”, ”)

D

Error

Python Output Type    
Discuss it


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')
A

ab1ef2

B

abcefd

C

ab1efd

D

ab12ed2

Python Output Type    
Discuss it


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])
A
1 1
B
1 44
C
3 1
D
3 44
Python Output Type    
Discuss it


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.
My Personal Notes arrow_drop_up


Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.