Q. What is the output of the following program :

Code:
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
πŸ’¬ Discuss
βœ… Correct Answer: (B) 0 1 2
Explanation: In the code above, i is initialise with 0. While loop runs till the value of i is reached to 5. But when I==3 there is break; so while loop will end.

Output will be 0 1 2.

Q. What is the output of the following program :

Code:
print 'cd'.partition('cd')
  • (A) (β€žcd?)
  • (B) (”)
  • (C) (β€žcd?, ”, ”)
  • (D) (”, β€žcd?, ”)
πŸ’¬ Discuss
βœ… Correct Answer: (D) (”, β€žcd?, ”)

Q. What is the output of the following program :

Code:
print 'abcefd'.replace('cd', '12')
  • (A) ab1ef2
  • (B) abcefd
  • (C) ab1efd
  • (D) ab12ed2
πŸ’¬ Discuss
βœ… Correct Answer: (B) abcefd
Explanation: in abcefd , cd is not present so there will not be any change.

Q. What will be displayed by the following code?

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
πŸ’¬ Discuss
βœ… Correct Answer: (D) 3 44

Q. Predict the output of following python programs

Code:
dictionary1 = {'Google' : 1,
'Facebook' : 2,
'Microsoft' : 3
}
dictionary2 = {'GFG' : 1,
'Microsoft' : 2,
'Youtube' : 3
}
dictionary1.update(dictionary2);
for key, values in dictionary1.items():
print(key, values)
  • (A) Compilation error
  • (B) Runtime error
  • (C) (β€žGoogle?, 1) (β€žFacebook?, 2) (β€žYoutube?, 3) (β€žMicrosoft?, 2) (β€žGFG?, 1)
  • (D) None of these
πŸ’¬ Discuss
βœ… Correct Answer: (C) (β€žGoogle?, 1) (β€žFacebook?, 2) (β€žYoutube?, 3) (β€žMicrosoft?, 2) (β€žGFG?, 1)

Q. What is the output of the following program?

Code:
dictionary1 = {'GFG' : 1,
'Google' : 2,
'GFG' : 3
}
print(dictionary1['GFG']);
  • (A) Compilation error due to duplicate keys
  • (B) Runtime time error due to duplicate keys
  • (C) 3
  • (D) 1
πŸ’¬ Discuss
βœ… Correct Answer: (C) 3

Q. What is the output of the following program?

Code:
temp = dict()
temp['key1'] = {'key1' : 44, 'key2' : 566}
temp['key2'] = [1, 2, 3, 4]
for (key, values) in temp.items():
print(values, end = "")
  • (A) Compilation error
  • (B) {β€žkey1?: 44, β€žkey2?: 566}[1, 2, 3, 4]
  • (C) Runtime error
  • (D) None of the above
πŸ’¬ Discuss
βœ… Correct Answer: (B) {β€žkey1?: 44, β€žkey2?: 566}[1, 2, 3, 4]

Q. What is the output of the following program?

Code:
data = [2, 3, 9]
temp = [[x for x in[data]] for x in range(3)]
print (temp)
  • (A) [[[2, 3, 9]], [[2, 3, 9]], [[2, 3, 9]]]
  • (B) [[2, 3, 9], [2, 3, 9], [2, 3, 9]]
  • (C) [[[2, 3, 9]], [[2, 3, 9]]]
  • (D) None of these
πŸ’¬ Discuss
βœ… Correct Answer: (A) [[[2, 3, 9]], [[2, 3, 9]], [[2, 3, 9]]]

Q. What is the output of the following program?

Code:
data = [x for x in range(5)]
temp = [x for x in range(7) if x in data and x%2==0]
print(temp)
  • (A) [0, 2, 4, 6]
  • (B) [0, 2, 4]
  • (C) [0, 1, 2, 3, 4, 5]
  • (D) Runtime error
πŸ’¬ Discuss
βœ… Correct Answer: (B) [0, 2, 4]

Q. What is the output of the following program?

Code:
L1 = [1, 2, 3, 4]
L2 = L1
L3 = L1.copy()
L4 = list(L1)
L1[0] = [5]
print(L1, L2, L3, L4
  • (A) [5, 2, 3, 4] [5, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]
  • (B) [[5], 2, 3, 4] [[5], 2, 3, 4] [[5], 2, 3, 4] [1, 2, 3, 4]
  • (C) [5, 2, 3, 4] [5, 2, 3, 4] [5, 2, 3, 4] [1, 2, 3, 4]
  • (D) [[5], 2, 3, 4] [[5], 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]
πŸ’¬ Discuss
βœ… Correct Answer: (D) [[5], 2, 3, 4] [[5], 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]