[go: up one dir, main page]

0% found this document useful (0 votes)
60 views6 pages

Python Programming Questions and Answers

The document consists of a series of Python programming questions covering topics such as string manipulation, list operations, loops, functions, and error handling. Each question requires a specific answer or explanation related to Python code snippets. The format suggests it is likely a test or exercise for students learning Python programming.

Uploaded by

Hùng Hà Mạnh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views6 pages

Python Programming Questions and Answers

The document consists of a series of Python programming questions covering topics such as string manipulation, list operations, loops, functions, and error handling. Each question requires a specific answer or explanation related to Python code snippets. The format suggests it is likely a test or exercise for students learning Python programming.

Uploaded by

Hùng Hà Mạnh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Name:

StudentID:

Part I
[Link] does the following Python Program print out?
str1 = "Hello"
str2 = 'there'
bob = str1 + str2
print(bob)
Answer==>

[Link] does the following Python program print out?


x = '40'
y = int(x) + 2
print(y)
Answer==>

[Link] would you use the index operator [] to print out the letter q from the
following string?
x = 'From marquard@[Link]'
Answer==>

[Link] would you use string slicing [:] to print out 'uct' from the following
string?
x = 'From marquard@[Link]'
Answer==>

[Link] is the iteration variable in the following Python code?


for letter in 'banana' :
print(letter)
Answer==>

[Link] does the following Python code print out?


print(len('banana')*7)
Answer==>

[Link] would you print out the following variable in all upper case in Python?
greet = 'Hello Bob'
Answer==>

[Link] of the following is not a valid string method in Python?


Answer==>

[Link] will the following Python code print out?


data = 'From [Link]@[Link] Sat Jan 5 [Link] 2008'
pos = [Link]('.')
print(data[pos:pos+3])
Answer==>
[Link] 10
Which of the following string methods removes whitespace from both the beginning
and end of a string?
Answer==>

11. Which of the following Python statements would print out the length of a list
stored in the variable data?
Answer==>

12. What type of data is produced when you call the range() function?
x = range(5)
Answer==>

[Link] does the following Python code print out?


a = [1, 2, 3];
b = [4, 5, 6];
c = a + b;
print(len(c))
Answer==>

[Link] of the following slicing operations will produce the list [12, 3]?
t = [9, 41, 12, 3, 74, 15]
Answer==>

[Link] list method adds a new item to the end of an existing list?
Answer==>

[Link] will the following Python code print out?


friends = [ 'Joseph', 'Glenn', 'Sally' ];
[Link]();
print(friends[0])
Answer==>

17. How are Python dictionaries different from Python lists?


Answer==>

18. Which method in a dictionary object gives you a list of the values in the
dictionary?
Answer==>

[Link] is the difference between a Python tuple and Python list?


Answer==>

[Link] of the following methods work both in Python lists and Python tuples?
Answer==>

[Link] will end up in the variable y after this code is executed?


x , y = 3, 4
Answer==>
[Link] the following Python code, what will end up in the variable y?
x = { 'chuck' : 1 , 'fred' : 42, 'jan': 100};
y = [Link]()
Answer==>

[Link] of the following tuples is greater than x in the following Python


sequence?
x = (5, 1, 3);
if ??? > x :
...
Answer==>

[Link] does the following Python code accomplish, assuming the c is a non-empty
dictionary?
tmp = list();
for k, v in [Link]():
[Link]( (v, k))
Answer==>

[Link] the variable data is a Python list, how do we sort it in reverse order?
Answer==>

[Link] the following tuple, how would you print 'Wed'?


days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')
Answer==>

[Link] the following Python loop, why are there two iteration variables (k and v)?
c = {'a':10, 'b':1, 'c':22};
for k, v in [Link]() :
...
Answer==>

[Link] that Python lists and Python tuples are quite similar - when might you
prefer to use a tuple over a list?
Answer==>

29 .What is wrong with this Python loop:


n = 5
while n > 0 :
print(n)
print('All done')
Answer==>

[Link] does the break statement do?


Answer==>

[Link] does the continue statement do?


Answer==>

[Link] does the following Python program print out?


tot = 0
for i in [5, 4, 3, 2, 1] :
tot = tot + 1
print(tot)
Answer==>

[Link] is the iteration variable in the following Python code:


friends = ['Joseph', 'Glenn', 'Sally']
for friend in friends :
print('Happy New Year:', friend)
print('Done!')
Answer==>

[Link] 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)
Answer==>

[Link] will the following code print out?


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)
Answer==>

[Link] is a good statement to describe the is operator as used in the following if


statement:
if smallest is None :
smallest = value
Answer==>

[Link] reserved word indicates the start of an "indefinite" loop in Python?


Answer==>

[Link] many times will the body of the following loop be executed?
n = 0
while n > 0 :
print('Lather')
print('Rinse')
print('Dry off!')
Answer==>

[Link] Python keyword indicates the start of a function definition?


Answer==>

[Link] Python, how do you indicate the end of the block of code that makes up the
function?
Answer==>

[Link] Python what is the input() feature best described as?


Answer==>

[Link] does the following code print out?


def thing():
print('Hello')

print('There')
Answer==>

[Link] the following Python code, which of the following is an "argument" to a


function?
x = 'banana'
y = max(x)
print(y)
Answer==>

[Link] will the following Python code print out?


def func(x) :
print(x)

func(10)
func(20)
Answer==>

[Link] line of the following Python program will never execute?


def stuff():
print('Hello')
return
print('World')

stuff()
Answer==>

[Link] will the following Python program print out?


def greet(lang):
if lang == 'es':
return 'Hola'
elif lang == 'fr':
return 'Bonjour'
else:
return 'Hello'

print(greet('fr'),'Michael')
Answer==>

[Link] does the following Python code print out? (Note that this is a bit of a
trick question and the code has what many would consider to be a flaw/bug - so read
carefully).
def addtwo(a, b):
added = a + b
return a

x = addtwo(2, 7)
print(x)
Answer==>

[Link] is the most important benefit of writing your own functions?


Answer==>

[Link] do we do to a Python statement that is immediately after an if statement to


indicate that the statement is to be executed only when the if statement is true?
Answer==>

[Link] of these operators is not a comparison / logical operator?


Answer==>

[Link] is true about the following code segment:


if x == 5 :
print('Is 5')
print('Is Still 5')
print('Third 5')
Answer==>
[Link] you have multiple lines in an if block, how do you indicate the end of the
if block?
Answer==>

[Link] look at the following text:


if x == 6 :
print('Is 6')
print('Is Still 6')
print('Third 6')
It looks perfect but Python is giving you an 'Indentation Error' on the second
print statement. What is the most likely reason?
Answer==>

[Link] 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?
Answer==>

[Link] will the following code print out?


x = 0
if x < 2 :
print('Small')
elif x < 10 :
print('Medium')
else :
print('LARGE')
print('All done')
Answer==>

[Link] the following code,


if x < 2 :
print('Below 2')
elif x >= 2 :
print('Two or more')
else :
print('Something else')
What value of 'x' will cause 'Something else' to print out?
Answer==>

[Link] the following code (numbers added) - which will be the last line to execute
successfully?
(1) astr = 'Hello Bob'
(2) istr = int(astr)
(3) print('First', istr)
(4) astr = '123'
(5) istr = int(astr)
(6) print('Second', istr)
Answer==>

[Link] the following code:


astr = 'Hello Bob'
istr = 0
try:
istr = int(astr)
except:
istr = -1
What will the value be for istr after this code executes?
Answer==>

PartII

You might also like