21ec643 Python MDR Quiz Questions
21ec643 Python MDR Quiz Questions
The computer is overheating and just wants you to stop to let it cool down
The computer has used GPS to find your location and hates everyone from your town
The computer did not understand the statement that you entered
2. What will the following program print out:
>>> x = 15
>>> x = x + 5
>>> print(x)
"print x"
x+5
15
20
3. Python scripts (files) have names that end with:
.doc
.png
.exe
.py
4. Which of these words is a reserved word in Python ?
pizza
while
payroll
names
5. Which of these words are reserved words in Python ?
break
concat
machine
if
todo
// stop
quit()
7. Which of the parts of a computer actually executes the program instructions?
Secondary Memory
Main Memory
Input/Output Devices
Main Memory
Secondary Memory
Output Device
Variables, expressions and statements
print(98.6)
What is "98.6"?
A conditional statement
A variable
A constant
// This is a test
# This is a test
/* This is a test */
* This is a test
3. What does the following code print out?
print("123" + "abc")
123+abc
123abc
hello world
4. In the following code,
x = 42
What is "x"?
A constant
A function
A variable
5. Which of the following is a bad Python variable name?
23spam
SPAM23
Spam
_spam
6. Which of the following variables is the "most mnemonic"?
variable_173
hours
x1q3z9ocd
for
if
speed
else
8. Which of the following is not a Python reserved word?
continue
iterate
else
break
9. Assume the variable x has been initialized to an integer value (e.g., x = 3). What does the following
statement do?
x = x + 2
Create a function called "x" and put the value 2 in the function
Retrieve the current value for x, add two to it and put the sum back into x
Produce the value "false" because "x" can never equal "x+2"
10. Which of the following elements of a mathematical expression in Python is evaluated first?
Parentheses ( )
Subtraction -
Multiplication *
Addition +
11. What is the value of the following expression
42 % 10
10
420
1042
12. What will be the value of x after the following statement executes:
x = 1 + 2 * 3 - 8 / 4
5.0
15
3.0
13. What will be the value of x when the following statement is executed:
x = int(98.6)
98
99
100
14. What does the Python input() function do?
!=
==
>=
>
if x == 5 :
print('Is 5')
print('Is Still 5')
print('Third 5')
Depending on the value of x, either all three of the print statements will execute or none of the
statements will execute
Only two of the three print statements will print out if the value of x is less than zero.
The string 'Is 5' will always print out regardless of the value for x.
The string 'Is 5' will never print out regardless of the value for x.
4. When you have multiple lines in an if block, how do you indicate the end of the if block?
You use a curly brace { after the last line of the if block
You de-indent the next line past the if block to the same level of indent as the original if statement
You capitalize the first letter of the line following the end of the if block
if x == 6 :
print('Is 6')
It looks perfect but Python is giving you an 'Indentation Error' on the second print statement. What is the
most likely reason?
Python has reached its limit on the largest Python program that can be run
In order to make humans feel inadequate, Python randomly emits 'Indentation Errors' on perfectly
good code - after about an hour the error will just go away without any changes to your program
6. What is the Python reserved word that we use in two-way if tests to indicate the block of code that is to be
executed if the logical test is false?
otherwise
else
iterate
x = 0
if x < 2 :
print('Small')
elif x < 10 :
print('Medium')
else :
print('LARGE')
print('All done')
Small
if x < 2 :
print('Below 2')
elif x >= 2 :
print('Two or more')
else :
print('Something else')
x = 2.0
This code will never print 'Something else' regardless of the value for 'x'
x = -2
x=2
9. In the following code (numbers added) - which will be the last line to execute successfully?
1
10. For the following code:
What will the value be for istr after this code executes?
-1
FUNCTIONS
1. Which Python keyword indicates the start of a function definition?
break
def
sweet
help
2. In Python, how do you indicate the end of the block of code that makes up the function?
You put a
You add the matching curly brace that was used to start the function
You de-indent a line of code to the same indent level as the def keyword
You put the "END" keyword in column 7 of the line which is to be the last line of the function
3. In Python what is the input() feature best described as?
A user-defined function
A conditional statement
A built-in function
def thing():
print('Hello')
print('There')
defthing
Hello
There
thingHelloThere
5. In the following Python code, which of the following is an "argument" to a function?
x = 'banana'
y = max(x)
print(y)
'banana'
x and y
max
6. What will the following Python code print out?
def func(x) :
print(x)
func(10)
func(20)
funcfunc
x20
x10x20
1020
7. Which line of the following Python program will never execute?
def stuff():
print('Hello')
return
print('World')
stuff()
stuff()
def stuff():
return
print ('World')
print('Hello')
print(greet('fr'),'Michael')
defHolaBonjourHelloMichael
Hola Michael
HolaBonjourHelloMichael
Bonjour Michael
9. What is the most important benefit of writing your own functions?
Following the rule that no function can have more than 10 statements in it
Avoiding writing the same non-trivial code more than once in your program
To avoid having more than 10 lines of sequential code without an indent or de-indent
Following the rule that whenever a program is more than 10 lines you must use a function
n = 5
while n > 0 :
print(n)
print('All done')
Jumps to the "top" of the loop and starts the next iteration
Jumps to the "top" of the loop and starts the next iteration
tot = 0
for i in [5, 4, 3, 2, 1] :
tot = tot + 1
print(tot)
5
15
10
friend
Joseph
Glenn
Sally
6. What is a good description of the following bit of Python code?
zork = 0
for thing in [9, 41, 12, 3, 74, 15] :
zork = zork + thing
print('After', zork)
smallest_so_far = -1
for the_num in [9, 41, 12, 3, 74, 15] :
if the_num < smallest_so_far :
smallest_so_far = the_num
print(smallest_so_far)
Hint: This is a trick question and most would say this code has a bug - so read carefully
42
-1
3
74
8. What is a good statement to describe the is operator as used in the following if statement:
if smallest is None :
smallest = value
def
for
break
indef
while
10. How many times will the body of the following loop be executed?
n = 0
while n > 0 :
print('Lather')
print('Rinse')
print('Dry off!')
MODULE-2
STRINGS
1. What does the following Python Program print out?
str1 = "Hello"
str2 = 'there'
bob = str1 + str2
print(bob)
Hello
Hellothere
Hello there
2. What does the following Python program print out?
x = '40'
y = int(x) + 2
print(y)
x2
402
int402
42
3. How would you use the index operator [] to print out the letter q from the following string?
x = 'From [email protected]'
print(x[7])
print(x[8])
print(x[q])
print(x[9])
print x[-1]
4. How would you use string slicing [:] to print out 'uct' from the following string?
x = 'From [email protected]'
print(x[15:3])
print(x[14+17])
print(x[14:17])
print(x[14/17])
print(x[15:18])
print(x[14:3])
5. What is the iteration variable in the following Python code?
in
letter
for
'banana'
6. What does the following Python code print out?
print(len('banana')*7)
banana7
42
bananabananabananabananabananabananabanana
7. How would you print out the following variable in all upper case in Python?
print(uc($greet));
console.log(greet.toUpperCase());
puts(greet.ucase);
print(greet.upper())
8. Which of the following is not a valid string method in Python?
startswith()
join()
split()
shout()
pos = data.find('.')
print(data[pos:pos+3])
uct
mar
Sat
.ma
10. Which of the following string methods removes whitespace from both the beginning and end of a string?
strip()
strtrunc()
wsrem()
rltrim()
LISTS
1. How are "collection" variables different from normal variables?
for / in
def / return
foreach / in
try / except
3. For the following list, how would you print out 'Sally'?
print(friends[2:1])
print(friends[2])
print friends[3]
print(friends['Sally'])
4. What would the following Python code print out?
fruit = 'Banana'
fruit[0] = 'b'
print(fruit)
banana
Nothing would print - the program fails with a traceback error
Banana
[0]
5. Which of the following Python statements would print out the length of a list stored in the variable data?
print(data.Len)
print(data.length())
print(len(data))
print(data.length)
print(length(data))
print(strlen(data))
6. What type of data is produced when you call the range() function?
x = list(range(5))
A string
A list of characters
A list of integers
A list of words
7. What does the following Python code print out?
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(len(c))
[4, 5, 6]
15
[1, 2, 3]
[1, 2, 3, 4, 5, 6]
21
8. Which of the following slicing operations will produce the list [12, 3]?
t[:]
t[1:3]
t[12:3]
t[2:4]
t[2:2]
9. What list method adds a new item to the end of an existing list?
pop()
append()
forward()
index()
add()
push()
10. What will the following Python code print out?
Sally
Glenn
Joseph
friends
TUPLES
1. What is the difference between a Python tuple and Python list?
Tuples can be expanded after they are created and lists cannot
pop()
append()
reverse()
index()
sort()
x , y = 3, 4
A list of integers
A list of strings
A list of tuples
5. Which of the following tuples is greater than x in the following Python sequence?
x = (5, 1, 3)
if ??? > x :
...
(6, 0, 0)
tmp = list()
for k, v in c.items() :
tmp.append( (v, k) )
data.sort(reverse=True)
data = sortrev(data)
data = data.sort(-1)
data.sort.reverse()
8. Using the following tuple, how would you print 'Wed'?
print(days[1])
print(days{2})
print(days[2])
print(days(2))
print[days(2)]
print(days.get(1,-1))
9. In the following Python loop, why are there two iteration variables (k and v)?
Because for each item we want the previous and current key
For a list of items that want to use strings as key values instead of integers
For a list of items that will be extended as new items are found
For a temporary variable that you will use and discard without modifying
DICTIONARIES
1. How are Python dictionaries different from Python lists?
Python lists can store strings and dictionaries can only store words
Python lists store multiple values and dictionaries store a single value
2. How are Python dictionaries different from Python lists?
Python lists can store strings and dictionaries can only store words
Python lists store multiple values and dictionaries store a single value
Python lists are indexed using integers and dictionaries can use strings as indexes
3. What is a term commonly used to describe the Python dictionary feature in other programming languages?
Sequences
Lambdas
Associative arrays
Closures
stuff = dict()
print(stuff['candy'])
-1
0
The program would fail with a traceback
candy
stuff = dict()
print(stuff.get('candy',-1))
'candy'
-1
6. (T/F) When you add items to a dictionary they remain in the order in which you added them.
True
False
7. What is a common use of Python dictionaries in a program?
if key in counts:
counts[key] = counts[key] + 1
else:
counts[key] = 1
counts[key] = counts.get(key,-1) + 1
counts[key] = counts.get(key,0) + 1
counts[key] = (counts[key] * 1) + 1
counts[key] = key + 1
9. In the following Python, what does the for loop iterate through?
x = dict()
...
for y in x :
...
It loops through the integers in the range from zero through the length of the dictionary
values()
all()
keys()
items()
each()
11. What is the purpose of the second parameter of the get() method for Python dictionaries?
MODULE-3
FILES
1. Given the architecture and terminology we introduced in Chapter 1, where are files stored?
Secondary memory
Machine Language
Main Memory
Motherboard
2. What is stored in a "file handle" that is returned from a successful open() call?
The handle has a list of all of the files in a particular folder on the hard drive
All the data from the file is read into memory and stored in the handle
Whether we want to read data from the file or write data to the file
alert()
file_input()
input()
gets()
5. What is the purpose of the newline character in text files?
It allows us to open more than one files and read them in a synchronized manner
It indicates the end of one line of text and the beginning of another line of text
What statement would we use to read the file one line at a time?
while (<xfile>) {
7. What is the purpose of the following Python code?
8. fhand = open('mbox.txt')
9. x = 0
10. for line in fhand:
11. x = x + 1
12. print(x)
Remove the leading and trailing spaces from each line in mbox.txt
From: [email protected]
From: [email protected]
From: [email protected]
From: [email protected]
rstrip()
trim()
ljust()
startswith()
14. The following code sequence fails with a traceback when the user enters a file that does not exist. How
would you avoid the traceback and make it so you could print out your own error message when a bad file
name was entered?
try / except
setjmp / longjmp
signal handlers
15. What does the following Python code do?
fhand = open('mbox-short.txt')
inp = fhand.read()
Turns the text in the file into a graphic image like a PNG or JPG
REGULAR EXPRESSIONS
1. What character do you add to the "+" or "*" to indicate that the match is to be done in a non-greedy
manner?
++
**
2. What will the '$' regular expression match?
A dollar sign
An empty line
An integer
A boolean
A single character
A list of strings
A string
5. What is the "wild card" character in a regular expression (i.e., the character that matches any character)?
?
6. What is the difference between the "+" and "*" character in regular expressions?
The "+" matches at least one character and the "*" matches zero or more characters
The "+" matches the beginning of a line and the "*" matches the end of a line
The "+" indicates "start of extraction" and the "*" indicates the "end of extraction"
The "+" matches the actual plus character and the "*" matches any character
The "+" matches upper case characters and the "*" matches lowercase characters
7. What does the "[0-9]+" match in a regular expression?
['From:']
^F.+:
From: