Sheet 1 DynamicLanguage
Sheet 1 DynamicLanguage
1. What is Python?
A) True B) False
Answer: True
A) True B) False
Answer: True
4. What is a compiler?
Answer: a
Lecture 2
8. What will be the output of the following Python code snippet?
1. def example(a):
2. a = a + '2'
3. a = a*2
4. return a
5. >>>example("hello")
a) indentation Error
b) cannot perform mathematical operation on strings
c) hello2
d) hello2hello2
View Answer
Answer: a
Explanation: Python codes have to be indented properly.
This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Variable
Names”.
Answer: a
Explanation: Case is always significant.
Answer: d
Explanation: True, False and None are capitalized while the others are in lower case.
Answer: b
Explanation: Spaces are not allowed in variable names.
Answer: b
Explanation: In python, power operator is x**y i.e. 2**3=8.
12- Which one of the following has the same precedence level?
Answer: a
The Python docstring is a string literal that occurs as the first statement in a module, function,
class, or method definition. It provides a convenient way to associate the documentation.
String literals occurring immediately after a simple assignment at the top are called "attribute
docstrings".
String literals occurring immediately after another docstring are called "additional docstrings".
Python uses triple quotes to create docstrings even though the string fits on one line.
Docstring phrase ends with a period (.) and can be multiple lines. It may consist of spaces and
other special chars.
Example
"""
Using docstring as a comment.
This code divides 2 numbers
"""
Lecture 3
What are the built-in types of python?
1. Integers
2. Floating-point
3. Complex numbers
4. Strings
5. Boolean
6. Built-in functions
Mutable are those data types in python whose value can be changed. E.g. list,
Dictionaries, and Sets
Immutable are those data types once they have been assigned you cannot manipulate their
values. E.g. Strings, Tuples and Numbers
1. >>>str="hello"
2. >>>str[:2]
3. >>>
a) he
b) lo
c) olleh
d) hello
Answer: a
Explanation: We are printing only the 1st two bytes of string and hence the answer is “he”.
a) list
b) dictionary
c) array
d) tuple
Answer: a
Explanation: List data type can store any values within it.
Lists
A. 3
B. 5
C. 25
D. 1
Answer: Option D
A. [2, 6, 4].
B. [1, 3, 2, 1, 3]
C. [1, 3, 2, 1, 3, 2] .
D. [1, 3, 2, 3, 2, 1]
Answer: Option C
Explanation:
Answer: c
Explanation: Max returns the maximum element in the list.
Answer: d
Explanation: Min returns the minimum element in the list.
6. Suppose list1 is [1, 5, 9], what is sum(list1)?
a) 1
b) 9
c) 15
d) Error
Answer: c
Explanation: Sum returns the sum of all elements in the list.
Answer: c
Answer: a
Answer: a
Answer: a
Explanation: split() function returns the elements in a list.
1. >>>t=(1,2,4,3)
2. >>>t[1:3]
a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
Answer: c
Explanation: Slicing in tuples takes place just as it does in strings.
Answer: b
Explanation: There exists add method for set data type. However 5 isn’t added again as set
consists of only non-duplicate elements and 5 already exists in the set. Execute in python shell to
verify.
Is the following Python code valid?
>>> a=frozenset([5,6,7])
>>> a
>>> a.add(5)
Answer: b
Explanation: Since a frozen set is immutable, add method doesn’t exist for frozen method.
1. d = {"john":40, "peter":45}
2. "john" in d
a) True
b) False
c) None
d) Error
Answer: a
Explanation: In can be used to check if the key is int dictionary.
1. d = {"john":40, "peter":45}
2. print(list(d.keys()))
a) [“john”, “peter”]
b) [“john”:40, “peter”:45]
c) (“john”, “peter”)
d) (“john”:40, “peter”:45)
Answer: a
Explanation: The output of the code shown above is a list containing only keys of the dictionary
d, in the form of a list.
a={1:5,2:3,3:4}
a.pop(3)
print(a)
a) {1: 5}
b) {1: 5, 2: 3}
c) Error, syntax error for pop() method
d) {1: 5, 3: 4}
Answer: b
Explanation: pop() method removes the key-value pair for the key mentioned in the pop()
method.
>>> a={1:"A",2:"B",3:"C"}
>>> del a
Answer: c
Explanation: del deletes the entire dictionary and any further attempt to access it will throw an
error.
Python dictionary is one of the supported data types in Python. It is an unordered collection of
elements. The elements in dictionaries are stored as key–value pairs. Dictionaries are indexed by
keys.
For example, below we have a dictionary named ‘dict’. It contains two keys, Country and
Capital, along with their corresponding values, India and New Delhi.
dict={‘Country’:’India’,’Capital’:’New Delhi’, }
Lecture 4
e.g.
for i in range(2,10,2)
print(i)
Answer c)
Loop
A. 2 4 6 8 10 …
B. 2 4
C. 2 3
D. error
Answer: Option B
Explanation:
The numbers 2 and 4 are printed. The next value of i is 6 which is divisible by 3 and hence
control exits the loop.
x = 'abcd'
for i in x:
print(i.upper())
A. a b c d
B. A B C D
C. a B C D
D. error
Answer: Option B
Explanation:
Answer: Option B
Explanation:
a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
Answer: Option B
Explanation:
A. m, y, , n, a, m, e, , i, s, , x,
B. m, y, , n, a, m, e, , i, s, , x
C. my, name, is, x
D. error
Answer: Option C
Explanation:
a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
Answer: a
Explanation: Loops over the keys of the dictionary.
Change the following Python code from using a while loop to for loop:
x=1
while x<10:
print x,
x+=1
Solution
for x in range(1,10):
print x,
Change the following Python code from a for loop to a while loop:
for i in range(1,50):
print i,
Solution
i=1
while i<50:
print i,
i+=1
}
Solution
20
18
16
14
12
10
8
6
4
2
Write a Python program to find those numbers which are divisible by 7 and multiple of 5, between 1500
and 2700 (both included).
nl=[]
for x in range(1500, 2701):
if (x%7==0) and (x%5==0):
nl.append(str(x))
print (','.join(nl))
Write a Python program that accepts a string and calculate the number of digits and letters.
s = input("Input a string")
d=l=0
for c in s:
if c.isdigit():
d=d+1
elif c.isalpha():
l=l+1
else:
pass
print("Letters", l)
print("Digits", d)
Write a program to find greatest common divisor (GCD) or highest common factor (HCF) of given two
numbers.
print "Enter first number"
x = input()
print "Enter second number"
y = input()
#x,y = 10, 20 means x = 10 y = 20
while y != 0:
x, y = y, x % y
print x