Reference no: EM132356347
Answer the Following Questions:
1. Which of the code snippets will print the string Python in reverse order?
1 s = "Python" r=reversed(s)
print("The string in reverse form is ", r)
2 s = "Python"
r=reversed(s)
print("The string in reverse
form is ", "".join(r))
3 s = "Python" r=reversed(s) for i in r:
print(i, end='')
4 Only 1 and 2
5 Only 2 and 3
2. What is the output of the following code?
stationery = ("book", "pen", "pencil")
for i in range(len(stationery)):
print(stationery[-i] ,
end=" ")
1 book pencil pen
2 book pen pencil
3 pencil pen book
4 None of these
3. What is the output of the following code?
word ="xyz"
for i in range(len(word)): print(word[-i], end=" ")
1 x z y
2 x y z
3 z y x
4 None of these
4. What is the output of the following code?
taste
=["sweet","sour","bitter"] print("s" in taste)
1 False
2 True
3 sweet sour
4 None of these
5. Which of the following statements will create and initialise an array a with values 0, 2, 4, 6, 8
1 a=[0 for i in range(5)]
2 a=[i for i in range(5)]
3 a=[2*i for i in range(5)]
4 None of these
6. What is the output of the following code?
arr=[i * i for i in range(5)] print(arr)
1 [0, 1, 4, 9, 16]
2 [0, 0, 0, 0, 0]
3 [0, 1, 2, 3, 4]
4 None of these
7. If arr is declared as an array and initialised, which of the code snippets will output [0, 1, 4, 9, 16] ?
1 arr=[i*i for i in range(5)]
print(arr)
2 arr=[i for i in range(5)] for x in range(5):
arr[x]*=x
print(arr)
3 arr=[0 for i in range(5)] for x in range(len(arr)):
arr[x]+=x*x
print(arr)
4 All the above
8. If arr is an array which of the code snippets will output 1 2 3 4 5 from the array?
1 arr=[i for i in range(5,0,-1)] for x in range(len(arr), 0, - 1):
print(arr[x-1], end=" ")
2 arr=[i for i in range(1,6)] for j in range(5):
print(arr[j], end=" ")
3 arr=[i for i in range(5)] for j in range(5):
print(arr[j], end=" ")
4 Only 2 and 3
5 Only 1 & 2
9. What is the value of y after the following code is executed?
arr=[x for x in (12, 36, 1,9)]
y=0
for x in range(len(arr)): if arr[x] > y:
y = arr[x]
1 0
2 1
3 9
4 12
5 36
10. Array arr is initialised with elements 1, 2, 3, and 4. Which of the code snippets will double each element of the array arr?
1 arr=[x for x in (1, 2, 3, 4)] for x in range(len(arr)):
arr[x]+=arr[x]
2 arr=[x for x in (1, 2, 3, 4)] for x in range(len(arr)):
arr[x]+=x+1
3 Both 1 & 2
4 Only 1
11. Which is the statement that declares and initialises(with zero) a two dimensional array variable m with 3 rows and 4 columns?
1 m=[[0 for i in j in range(4)] range(3)] for
2 m=[4*[0] for i in range(3)]
3 m=[[0 for i in j in range(3)] range(4)] for
4 Both 1 and 2
5 Both 2 and 3
12. Which of the code snippets will output a 3 X 4 matrix(two dimensional array with 3 rows and 4 columns) as below:
1 m=[[i for i in range(4)] for j in range(3)]
for row in m:
for e in row:
print(e, end=" ") print()
0 1 2 3
0 1 2 3
0 1 2 3
2 m=[4*[i] for i in range(3)] for row in m:
for e in row:
print(e, end=" ") print()
3 Both 1 and 2
4 None of these
13. m is a matrix with the following elements: 0 1 2 3 1 for i in range(3):
for j in range(4): print(2 *m[i][j],
end=" ")
print()
0 1 2 3
0 1 2 3
Which code snippet will double each element so that m is altered and printed as: 2 for i in range(3):
for j in range(4): m[i][j]+=m[i][j]
print(m[i][j], end="
")
print()
0 2 4 6
0 2 4 6
0 2 4 6
3 for i in range(3):
for j in range(4): m[i][j]*=m[i][j]
print(m[i][j], end="
")
print()
4 None of these
14. m is a matrix with the following elements:
2 4 6 8
2 4 6 8
2 4 6 8
What is the output of the following code:
for i in range(3): s=0
for j in range(4): s+=m[i][j]
print(s, end=" ")
1 2 4 6 8
2 20 20 20
3 60
4 None of these
15. m is a matrix with the following elements:
2 4 6 8
2 4 6 8
2 4 6 8
What is the output of the following code:
for i in range(4): s=0
for j in range(3): s+=m[j][i]
print(s, end=" ")
1 2 4 6 8
2 20 20 20
3 60
4 None of these
16. Array variable x is initialised as follows:
x=[[i*j for j in range(3)] for i in range(2)]
What will the print(x) statement output?
1 [[0, 0, 0], [0, 1, 2]]
2 [[0, 0, 0], [0, 0, 0]]
3 [[0, 1, 2], [0, 1, 2]]
4 None of these
17. Array variable x is initialised as follows:
x=[[i*j for j in range(3)] for i in range(2)]
Which is the element at the position
x[1][2]?
1 0
2 1
3 2
4 3
5 None of these
18. m is a matrix with the following elements:
0 1 2 3
0 1 2 3
0 1 2 3 1 for i in range(3):
for j in range(4): m[i][j]+=m[i][j]
print(m[i][j], end="
")
print()
Which code snippet will double each element so that m is altered and printed as:
2 for i in range(3):
for j in range(4): m[i][j]*=m[i][j]
print(m[i][j], end="
")
print()
0 2 4 6
0 2 4 6
0 2 4 6
3 for i in range(len(m)): for j in range(len(m[i])):
print(m[i][j],end=" ")
print()
4 Both 1 & 2
5 Both 1 & 3
19. An example of an immutable sequence in Python is ...
1 Lists
2 Strings
3 Integers
4 None of these
20. a=(‘dog', ‘cat', ‘fish').
Here, a is an example
of a ... variable.
1 String
2 Block
3 List
4 Tuple