Professional Documents
Culture Documents
SUCCESS!!!!!!!!!
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
Answer: b a) None
Explanation: setdefault() is similar to get() b) { None:None, None:None, None:None}
but will set dict[key]=default if key is not c) {1:None, 2:None, 3:None}
already in the dictionary. d) { }
7. What is the output of the following code?
a={1:"A",2:"B",3:"C"} Answer: d
a.setdefault(4,"D") Explanation: The clear() method clears all
print(a) the key-value pairs in the dictionary.
11. Which of the following isn’t true about
a) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}. dictionary keys?
b) None. a) More than one key isn’t allowed
c) Error. b) Keys must be immutable
d) [1,3,6,10]. c) Keys must be integers
d) When duplicate keys encountered, the
Answer: a last assignment wins
Explanation: setdefault() will set
dict[key]=default if key is not already in the Answer: c
dictionary. Explanation: Keys of a dictionary may be
8. What is the output of the following code? any data type that is immutable.
a={1:"A",2:"B",3:"C"} 12. What is the output of the following code?
b={4:"D",5:"E"}
a={1:5,2:3,3:4}
a.update(b)
a.pop(3)
print(a)
print(a)
a) {1: ‘A’, 2: ‘B’, 3: ‘C’}
a) {1: 5}
b) Method update() doesn’t exist for
b) {1: 5, 2: 3}
dictionaries
c) Error, syntax error for pop() method
c) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}
d) {1: 5, 3: 4}
d) {4: ‘D’, 5: ‘E’}
Answer: b
Answer: c
Explanation: pop() method removes the key-
Explanation: update() method adds
value pair for the key mentioned in the pop()
dictionary b’s key-value pairs to dictionary a.
method.
Execute in python shell to verify.
13. What is the output of the following code?
9. What is the output of the following code?
a={1:5,2:3,3:4}
a={1:"A",2:"B",3:"C"}
print(a.pop(4,9))
b=a.copy()
b[2]="D" a) 9
print(a) b) 3
c) Too many arguments for pop() method
a) Error, copy() method doesn’t exist for
d) 4
dictionaries
Answer: a
b) {1: ‘A’, 2: ‘B’, 3: ‘C’}
Explanation: pop() method returns the value
c) {1: ‘A’, 2: ‘D’, 3: ‘C’}
when the key is passed as an argument and
d) “None” is printed
otherwise returns the default value(second
argument) if the key isn’t present in the
Answer: b dictionary.
Explanation: Changes made in the copy of
14. What is the output of the following code?
the dictionary isn’t reflected in the original
one. a={1:"A",2:"B",3:"C"}
10. What is the output of the following code? for i in a:
print(i,end=" ")
a={1:"A",2:"B",3:"C"}
a.clear() a) 1 2 3
print(a) b) ‘A’ ‘B’ ‘C’
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
Answer: d
Answer: a Explanation: Mutable members can be used
Explanation: In the second line of code, the as the values of the dictionary but they
dictionary becomes an empty dictionary. cannot be used as the keys of the
Thus, length=0. dictionary.
8. What is the output of the following snippet 11. What is the output of the following piece
of code? of code?
test = {1:'A', 2:'B', 3:'C'} >>>import collections
del test[1] >>> a=collections.Counter([1,1,2,3,3,
test[1] = 'D' 4,4,4])
del test[2] >>> a
print(len(test))
a) {1,2,3,4}
a) 0 b) Counter({4, 1, 3, 2})
b) 2 c) Counter({4: 3, 1: 2, 3: 2, 2: 1})
c) Error as the key-value pair of 1:’A’ is d) {4: 3, 1: 2, 3: 2, 2: 1}
already deleted
d) 1
Answer: c
Explanation: The statement
Answer: b a=collections.OrderedDict() generates a
Explanation: After the key-value pair of 1:’A’ dictionary with the number as the key and
is deleted, the key-value pair of 1:’D’ is the count of times the number appears as
added. the value.
9. What is the output of the following snippet 12. What is the output of the following piece
of code? of code?
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
Answer: d
Explanation: The most_common() method This set of Python Multiple Choice
returns the n number key-value pairs where Questions & Answers (MCQs)
the value is the most recurring. focuses on “Precedence and
13. What is the output of the following piece Associativity – 2”1.
of code? What is the output of the following
expression:
>>>import collections print(4.00/(2.0+2.0))
>>> b=collections.Counter([2,2,3,4,4,
4]) a)Error b)1.0 c)1.00 d)1 Answer:b
>>> b.most_common(1) Explanation: The result of the expression
shown above is 1.0 because print rounds off
a) Counter({4: 3, 2: 2, 3: 1})
digits.
b) {3:1}
c) {4:3} 2. Consider the expression given below. The
d) [(4, 3)]. value of X is:
X = 2+9*((3*12)-8)/10
Answer: d
Explanation: The most_common() method a)30.0 b)30.8 c)28.4 d)27.2 Answer:d
returns the n number key-value pairs where Explanation: The expression shown above
the value is the most recurring. is evaluated as: 2+9*(36-8)/10, which
14. What is the output of the following piece simplifies to give 2+9*(2.8), which is equal to
of code? 2+25.2 = 27.2. Hence the result of this
expression is 27.2.
>>> import collections
>>> a=collections.Counter([2,2,3,3,3, 3. Which of the following expressions
4]) involves coercion when evaluated in
>>> b=collections.Counter([2,2,3,4,4] Python?
) a)4.7–1.5 b)7.9*6.3 c)1.7%2 d)3.4+4.6
>>> a|b Answer:c
Explanation: Coercion is the implicit
a) Counter({3: 3, 2: 2, 4: 2}) (automatic) conversion of operands to a
b) Counter({2: 2, 3: 1, 4: 1}) common type. Coercion is automatically
c) Counter({3: 2}) performed on mixed-type expressions. The
d) Counter({4: 1}) expression 1.7 % 2 is evaluated as 1.7 %
2.0 (that is, automatic conversion of int to
Answer: a float).
Explanation: a|b returns the pair of keys and 4. What is the value of the following
the highest recurring value. expression:
15. What is the output of the following piece
of code? 24//6%3, 24//4//2
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
3. What is the output of the following? 8. What is the output of the following?
x = "abcdef" x = "abcdef"
while i in x: i = "a"
print(i, end=" ") while i in x[:-1]:
print(i, end = " ")
a) a b c d e f b) abcdef c) i i i i i i … d) error
Answer: d a) a a a a a b) a a a a a a c) a a a a a a …
Explanation: NameError, i is not defined. d) a Answer: c
Explanation: String x is not being altered
4. What is the output of the following? and i is in x[:-1].
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
9. What is the output of the following? 2.0 (that is, automatic conversion of int to
float).
x = "abcdef"
i = "a" 4. What is the value of the following
while i in x: expression:
x = x[1:]
print(i, end = " ") 24//6%3, 24//4//2 a) (1,3) b) (0,3) c) (
1,0) d) (3,1) Answer: a
a) a a a a a a b) a c) no output d) error Explanation: The expressions are evaluated
Answer: b as: 4%3 and 6//2 respectively. This results i
Explanation: The string x is being shortened n the answer (1,3). This is because the asso
by one character in each iteration. ciativity of both of the expressions shown ab
ove is left to right.
10. What is the output of the following?
x = "abcdef" 5. Which among the following list of
i = "a" operators has the highest precedence?
while i in x[1:]: +, -, **, %, /, <<, >>, |
print(i, end = " ")
a) <<, >> b) ** c) | d) % Answer: b
a) a a a a a a b) a c) no output d) error Explanation: The highest precedence is that
Answer: c of the exponentiation operator, that is of **.
Explanation: i is not in x[1:]
6. What is the value of the expression:
This set of Python Multiple Choice
Questions & Answers (MCQs) focuses on float(4+int(2.39)%2)
“Precedence and Associativity – 2”.
a) 5.0 b) 5 c) 4.0 d) 4 Answer: c
1. What is the output of the following Explanation: The above expression is an
expression: example of explicit conversion. It is
evaluated as: float(4+int(2.39)%2) =
print(4.00/(2.0+2.0)) float(4+2%2) = float(4+0) = 4.0. Hence the
a)Error b) 1.0 c) 1.00 d) 1 Answer: b result of this expression is 4.0.
Explanation: The result of the expression 7. Which of the following expressions is an
shown above is 1.0 because print rounds off example of type conversion?
digits. a) 4.0 + float(3) b) 5.3 + 6.3 c) 5.0 + 3 d) 3
2. Consider the expression given below. The + 7 Answer: a
value of X is: Explanation: Type conversion is nothing but
explicit conversion of operands to a specific
X = 2+9*((3*12)-8)/10 type. Options ‘b’ and ‘c’ are examples of
implicit conversion whereas option ‘a’ is an
a) 30.0 b) 30.8 c) 28.4 d) 27.2 Answer: example of explicit conversion or type
d conversion.
Explanation: The expression shown above
is evaluated as: 2+9*(36-8)/10, which 8. Which of the following expressions results
simplifies to give 2+9*(2.8), which is equal to in an error?
2+25.2 = 27.2. Hence the result of this a) float(‘10’) b) int(‘10’) c) float(’10.8’) d)
expression is 27.2. int(’10.8’) Answer: d
Explanation: All of the above examples
3. Which of the following expressions show explicit conversion. However the
involves coercion when evaluated in expression int(’10.8’) results in an error.
Python?
a) 4.7 – 1.5 b) 7.9 * 6.3 c) 1.7 % 2 d) 3.4 9. What is the value of the expression:
+ 4.6 Answer: c
Explanation: Coercion is the implicit 4+2**5//10
(automatic) conversion of operands to a a) 3 b) 7 c) 77 d) 0
common type. Coercion is automatically Answer: b
performed on mixed-type expressions. The Explanation: The order of precedence is: **,
expression 1.7 % 2 is evaluated as 1.7 % //, +. The expression 4+2**5//10 is evaluated
as 4+32//10, which is equal to 4+3 = 7.
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
Hence the result of the expression shown 3. Suppose list1 is [1, 3, 2], What is list1 * 2
above is 7. ?
a) [2, 6, 4]. b) [1, 3, 2, 1, 3]. c) [1, 3, 2,
10. The expression 2**2**3 is evaluates as: 1, 3, 2] . D) [1, 3, 2, 3, 2, 1]. Answer: c
(2**2)**3. State whether this statement is Explanation: Execute in the shell and verify.
true or false.
a) True b) False Answer: b 4. Suppose list1 = [0.5 * x for x in range(0,
Explanation: The value of the expression 4)], list1 is :
(2**2)**3 = 4**3 = 64. When the expression a) [0, 1, 2, 3]. b) [0, 1, 2, 3, 4]. c) [0.0, 0.5,
2**2**3 is evaluated in python, we get the 1.0, 1.5]. d) [0.0, 0.5, 1.0, 1.5, 2.0].
result as 256, because this expression is Answer: c
evaluated as 2**(2**3). This is because the Explanation: Execute in the shell to verify.
associativity of exponentiation operator (**)
is from right to left and not from left to right. 5. What is the output when following code is
executed ?
This set of Python Coding Interview
1. >>>list1 = [11, 2, 23]
Questions & Answers focuses on
2. >>>list2 = [11, 2, 2]
“Lists”.
3. >>>list1 < list2 is
1. What is the output when following code is
executed ? a) True b) False c) Error d) None
Answer: b
1. >>>names = ['Amir', 'Bear', ' Explanation: Elements are compared one by
Charlton', 'Daman'] one.
2. >>>print(names[-1][-1])
6. To add a new element to a list we use
a) A b) Daman c) Error d) n which command ?
Answer: d a) list1.add(5) b) list1.append(5) c)
Explanation: Execute in the shell to verify. list1.addLast(5) d) list1.addEnd(5) Answer:
b
2. What is the output when following code is Explanation: We use the function append to
executed ? add an element to the list.
1. names1 = ['Amir', 'Bear', 'Ch
7. To insert 5 to the third position in list1, we
arlton', 'Daman']
use which command ?
2. names2 = names1
a) list1.insert(3, 5) b) list1.insert(2, 5) c)
3. names3 = names1[:]
list1.add(3, 5) d) list1.append(3, 5)
4.
5. names2[0] = 'Alice'
Answer: a
6. names3[1] = 'Bob' Explanation: Execute in the shell to verify.
7. 8. To remove string “hello” from list1, we use
8. sum = 0 which command ?
9. for ls in (names1, names2, na a) list1.remove(“hello”) b)
mes3): list1.remove(hello) c)
10. if ls[0] == 'Alice': list1.removeAll(“hello”)
11. sum += 1 d)list1.removeOne(“hello”) Answer: a
12. if ls[1] == 'Bob':
Explanation: Execute in the shell to verify.
13. sum += 10
14. 9. Suppose list1 is [3, 4, 5, 20, 5], what is
15. print sum list1.index(5) ?
a) 0 b) 1 c) 4 d) 2 Answer: d
a) 11 b) 12 c) 21 d) 22
Explanation: Execute help(list.index) to get
Answer: b
details.
Explanation: When assigning names1 to
names2, we create a second reference to 10. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3],
the same list. Changes to names2 affect what is list1.count(5) ?
names1. When assigning the slice of all a) 0 b) 4 c) 1 d) 2 Answer: d
elements in names1 to names3, we are Explanation: Execute in the shell to verify.
creating a full copy of names1 which can be
modified independently.
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
5. Any odd number on being AND-ed with a) 101 b) -101 c) 100 d) -100
________ always gives 1. Hint: Any even Answer: b
number on being AND-ed with this value Explanation: Suppose we have an
always gives 0. expression ~A. This is evaluated as: -A – 1.
a) 10 b) 2 c) 1 d) 0 Answer: c Therefore, the expression ~100 is evaluated
Explanation: Any odd number on being as -100 – 1, which is equal to -101.
AND-ed with 1 always gives 1. Any even
number on being AND-ed with this value
always gives 0.
6. What is the value of this expression:
bin(10-2)+bin(12^4) a) 0b10000
b) 0b10001000 c) 0b1000b1000
d) 0b10000b1000 Answer: d
Explanation: The output of bin(10-2)= 0b100
0 and that of bin(12^4) is ob1000. Hence the
output of the above expression is: 0b10000b
1000.
7. Which of the following expressions can be
used to multiply a given number ‘a’ by 4?
a) a<<2 b) a<<4 c) a>>2 d) a>>4
Answer: a
Explanation: Let us consider an example
wherein a=2. The binary form of 2 is 0010.
When we left shift this value by 2, we get
1000, the value of which is 16. Hence if we
want to multiply a given number ‘a’ by 4, we
can use the expression: a<<2.
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)