Python Questions and Answers – While and For Loops – 4
This set of Python Questions & Answers focuses on “While and For Loops” and is useful for freshers who are preparing for their interviews.
1. What is the output of the following?
a) 1 2 3
b) 123
c) error
d) none of the mentioned
View Answer
b) 123
c) error
d) none of the mentioned
View Answer
Answer: c
Explanation: Objects of type int are not iterable.
Explanation: Objects of type int are not iterable.
2. What is the output of the following?
a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer
Answer: a
Explanation: Loops over the keys of the dictionary.
Explanation: Loops over the keys of the dictionary.
3. What is the output of the following?
a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer
Answer: d
Explanation: Error, objects of type int aren’t iterable.
Explanation: Error, objects of type int aren’t iterable.
4. What is the output of the following?
advertisements
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer
Answer: c
Explanation: Loops over key, value pairs.
Explanation: Loops over key, value pairs.
5. What is the output of the following?
a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer
Answer: b
Explanation: Loops over the keys and prints the values.
Explanation: Loops over the keys and prints the values.
6. What is the output of the following?
a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer
Answer: b
Explanation: Loops over the values.
Explanation: Loops over the values.
7. What is the output of the following?
a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer
Answer: d
Explanation: Causes a KeyError.
Explanation: Causes a KeyError.
8. What is the output of the following?
advertisements
b) None None None
c) error
d) none of the mentioned
View Answer
Answer: c
Explanation: Objects of type set have no attribute values.
Explanation: Objects of type set have no attribute values.
9. What is the output of the following?
a) 0 1 2
b) {0, 1, 2} {0, 1, 2} {0, 1, 2}
c) error
d) none of the mentioned
View Answer
b) {0, 1, 2} {0, 1, 2} {0, 1, 2}
c) error
d) none of the mentioned
View Answer
Answer: a
Explanation: Loops over the elements of the set and prints them.
Explanation: Loops over the elements of the set and prints them.
10. What is the output of the following?
a) 0 1 2
b) 0 1 2 0 1 2 0 1 2 …
c) None None None
d) none of the mentioned
View Answer
b) 0 1 2 0 1 2 0 1 2 …
c) None None None
d) none of the mentioned
View Answer
Answer: c
Explanation: Variable x takes the values 0, 1 and 2. set.add() returns None which is printed.
Explanation: Variable x takes the values 0, 1 and 2. set.add() returns None which is printed.
11. What is the output of the following?
a) 0
b) (nothing is printed)
c) error
d) none of the mentioned
View Answer
b) (nothing is printed)
c) error
d) none of the mentioned
View Answer
Answer: b
Explanation: range(0) is empty.
Explanation: range(0) is empty.
No comments:
Post a Comment