12

Output Type

Question 1
What is the output of the following program :
def myfunc(a):
    a = a + 2
        a = a * 2
    return a

print myfunc(2)
A
8
B
16
C
Indentation Error
D
Runtime Error
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
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 :
print '{0:.2}'.format(1.0 / 3)
A
0.333333
B
0.33
C
0.333333:-2
D
Error
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 :
print '{0:-2%}'.format(1.0 / 3)
A
0.33
B
0.33%
C
33.33%
D
33%
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 :
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
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
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 :
print 'cd'.partition('cd')
A
(‘cd’)
B
(”)
C
(‘cd’, ”, ”)
D
(”, ‘cd’, ”)
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 :
print 'abef'.partition('cd')
A
(‘abef’)
B
(‘abef’, ‘cd’, ”)
C
(‘abef’, ”, ”)
D
Error
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 :
print 'abcefd'.replace('cd', '12')
A
ab1ef2
B
abcefd
C
ab1efd
D
ab12ed2
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
Output Type    
Discuss it


There are 11 questions to complete.
12

Company Wise Coding Practice    Topic Wise Coding Practice