0% found this document useful (0 votes)
8 views

Sheet 1 DynamicLanguage

The document consists of multiple lectures covering Python programming concepts, including definitions of Python, its data types, variable names, and built-in functions. It includes multiple-choice questions and answers related to Python syntax, data structures, and control flow, along with explanations for each answer. Additionally, it provides examples of Python code and programming exercises to demonstrate various functionalities.

Uploaded by

salmaabo341
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Sheet 1 DynamicLanguage

The document consists of multiple lectures covering Python programming concepts, including definitions of Python, its data types, variable names, and built-in functions. It includes multiple-choice questions and answers related to Python syntax, data structures, and control flow, along with explanations for each answer. Additionally, it provides examples of Python code and programming exercises to demonstrate various functionalities.

Uploaded by

salmaabo341
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Lecture 1

1. What is Python?

A) A programming language B) Computer language C) Binary language D) None of the above

Answer. a. Python is a programming language, basically a very high-level and a general-purpose


language.

2. Python is a scripting programming language?

A) True B) False

Answer: True

3. Python is an interpreted programming language?

A) True B) False

Answer: True

4. What is a compiler?

a) system program that converts instructions to machine language

b) system program that converts machine language to high-level language

c) system program that writes instructions to perform

d) None of the mentioned

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”.

1. Is Python case sensitive when dealing with identifiers?


a) yes
b) no
c) machine dependent
d) none of the mentioned
View Answer

Answer: a
Explanation: Case is always significant.

2. What is the maximum possible length of an identifier?


a) 31 characters
b) 63 characters
c) 79 characters
d) none of the mentioned
View Answer
Answer: d
Explanation: Identifiers can be of any length.

4. Which of the following is an invalid variable?


a) my_string_1
b) 1st_string
c) foo
d) _
Answer: b
Explanation: Variable names should not start with a number.

7. All keywords in Python are in _________


a) lower case
b) UPPER CASE
c) Capitalized
d) None of the mentioned

Answer: d
Explanation: True, False and None are capitalized while the others are in lower case.

9. Which of the following is an invalid statement?


a) abc = 1,000,000
b) a b c = 1000 2000 3000
c) a,b,c = 1000, 2000, 3000
d) a_b_c = 1,000,000

Answer: b
Explanation: Spaces are not allowed in variable names.

11. Which is the correct operator for power(x y)?


a) X^y
b) X**y
c) X^^y
d) None of the mentioned
View Answer

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?

a. Division, Power, Multiplication, Addition and Subtraction b. Division and Multiplication

c. Subtraction and Division d. Power and Division

Answer: (b) Division and Multiplication Explanation: None

The expression Int(x) implies that the variable x is converted to integer.


a) True
b) False

Answer: a

Explain docstring in Python?

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?

Ans: Built-in types in Python are as follows –

1. Integers
2. Floating-point
3. Complex numbers
4. Strings
5. Boolean
6. Built-in functions

What are Mutable and Immutable Datatypes with example


Answer:

 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

Give the main difference between tuple and list.


Answer: Tuples are immutable whereas lists are mutable.

1. Which of these in not a core data type?


a) Lists
b) Dictionary
c) Tuples
d) Class
Answer: d
Explanation: Class is a user defined data type.

What will be the output of the following Python code?

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”.

What data type is the object below?

L = [1, 23, 'hello', 1]

a) list
b) dictionary
c) array
d) tuple

Answer: a
Explanation: List data type can store any values within it.
Lists

Suppose list1 is [3, 5, 25, 1, 3], what is min(list1) ?

 A. 3
 B. 5
 C. 25
 D. 1

Answer: Option D

Suppose list1 is [1, 3, 2], What is list1 * 2 ?

 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:

Execute in the shell and verify.

4. Suppose list1 is [2445,133,12454,123], what is max(list1)?


a) 2445
b) 133
c) 12454
d) 123

Answer: c
Explanation: Max returns the maximum element in the list.

5. Suppose list1 is [3, 5, 25, 1, 3], what is min(list1)?


a) 3
b) 5
c) 25
d) 1

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.

3. Suppose list1 is [1, 3, 2], What is list1 * 2?


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: c

To insert 5 to the third position in list1, we use which command?


a) list1.insert(3, 5)
b) list1.insert(2, 5)
c) list1.add(3, 5)
d) list1.append(3, 5)

Answer: a

To remove string “hello” from list1, we use which command?


a) list1.remove(“hello”)
b) list1.remove(hello)
c) list1.removeAll(“hello”)
d) list1.removeOne(“hello”)
View Answer

Answer: a

What will be the output of the following Python code?


1. >>>"Welcome to Python".split()

a) [“Welcome”, “to”, “Python”]


b) (“Welcome”, “to”, “Python”)
c) {“Welcome”, “to”, “Python”}
d) “Welcome”, “to”, “Python”

Answer: a
Explanation: split() function returns the elements in a list.

What will be the output of the following Python code?

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.

If a={5,6,7}, what happens when a.add(5) is executed?


a) a={5,5,6,7}
b) a={5,6,7}
c) Error as there is no add function for set data type
d) Error as 5 already exists in the set

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)

a) Yes, now a is {5,5,6,7}


b) No, frozen set is immutable
c) No, invalid syntax for add method
d) Yes, now a is {5,6,7}

Answer: b
Explanation: Since a frozen set is immutable, add method doesn’t exist for frozen method.

What will be the output of the following Python code snippet?

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.

What will be the output of the following Python code snippet?

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.

What will be the output of the following Python code?

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.

What will be the output of the following Python code?

>>> a={1:"A",2:"B",3:"C"}
>>> del a

a) method del doesn’t exist for the dictionary


b) del deletes the values in the dictionary
c) del deletes the entire dictionary
d) del deletes the keys in the dictionary

Answer: c
Explanation: del deletes the entire dictionary and any further attempt to access it will throw an
error.

What is a dictionary in Python?

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

What range function does in python?


Answer: range() is a special function often use in python it is used to iterate over a number of
the sequence. range function can accept 3 arguments initial state, last state and the steps taken to
reach the last point.

e.g.

for i in range(2,10,2)
print(i)

Which numbers are printed?

Answer c)

Loop

1. What is the output of the following?


i=2
while True:
if i%3 == 0:
break
print(i)
i += 2

 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.

What is the output of the following?

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:

The instance of the string returned by upper() is being printed.

What is the output of the following?

d = {0: 'a', 1: 'b', 2: 'c'}


for x in d.values():
print(x)
 A. 0 1 2
 B. a b c
 C. 0 a 1 b 2 c
 D. none of the mentioned

Answer: Option B

Explanation:

Loops over the values.

What is the output of the following?

d = {0: 'a', 1: 'b', 2: 'c'}


for x in d.keys():
print(d[x])

 a) 0 1 2
 b) a b c
 c) 0 a 1 b 2 c
 d) none of the mentioned

Answer: Option B

Explanation:

Loops over the keys and prints the values.

What is the output of the following?

string = "my name is x"


for i in string.split():
print (i, end=", ")

 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:

Variable i takes the value of one word at a time.

What will be the output of the following Python code?

d = {0: 'a', 1: 'b', 2: 'c'}


for i in d:
print(i)

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
}

What would be printed from the following Python code segment?


for i in range(20,0,-2):
print i

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

Print all elements of a list using for loop.


a = [1,2,3,4,5]
for i in a:
print i

You might also like