Intro to Programming Logic Final Exam
Questions with Complete Solutions
Write a statement that creates a list with the following strings: 'Einstein', 'Newton',
'Copernicus', and 'Kepler' . - ANSWER-names = ['Einstein', 'Newton', 'Copernicus',
'Kepler']
Assume names references a list. Write a for loop that displays each element of the
list. - ANSWER-for element in names:
print(element)
Assume the list of numbers1 has 100 elements, and numbers2 is an empty list. Write
code that copies the values in numbers1 to numbers2. - ANSWER-for item in
numbers1:
numbers2.append(item)
What will the following code print?
list1 = [40, 50, 60 ]
list2 = [ 10, 20, 30 ]
list3 = list1 + list2
print (list3) - ANSWER-[40, 50, 60, 10, 20, 30].
What does the following of the code display?
mystr = 'yes'
mystr += 'no'
mystr += 'yes'
print (mystr) - ANSWER-yesnoyes
What does the following code display?
mystr = 'abc' * 3
print (mystr) - ANSWER-abcabcabc
What will the following code display?
mystring = 'abcdefg'
print (mystring [2:5] ) - ANSWER-cde
What does the following code display?
numbers = [1, 2, 3, 4, 5, 6, 7 ]
print (numbers [4 : 6] ) - ANSWER-[5, 6]
What does the following code display?
, name = 'joe'
print (name . lower ( ) )
print (name. upper ( ) )
print (name) - ANSWER-joe
JOE
joe
joe
Assume choice references a string. The following if statement determines whether
choice is equal to 'Y' or 'y':
if choice == 'Y' or choice == 'y' :
Rewrite this statement so it only makes one comparison, and does not use the or
operator. (Hint: use either the upper or lower methods. ) - ANSWER-if choice.upper()
== 'Y'
Write a loop that counts the number of digits that appear in the string referenced by
mystring. - ANSWER-counter = 0
for ch in mystring:
if ch.isdigit():
counter +=1
print ('The number of digits is', counter)
Write a function that accepts a string as an argument and returns true if the
argument ends with the substring ' .com ' . Otherwise, the function should return
false. - ANSWER-def com_check(mystring):
if mystring.endswith('.com'):
return True
return False
Assume mystring references a string. Write a statement that uses a slicing
expression and displays the first 3 characters in the string. - ANSWER-
print(mystring[:3])
Look at the following statement:
mystring= ' cookies>milk>fudge>cake>ice cream '
Write a statement that splits this string, creating the following list:
[ 'cookies' , 'milk' , 'fudge' , 'cake' , 'ice cream' ] - ANSWER-mylist = mystring.split('>')
What will be the output after the following code is executed?
def pass_it(x, y):
z = x + ", " + y
Questions with Complete Solutions
Write a statement that creates a list with the following strings: 'Einstein', 'Newton',
'Copernicus', and 'Kepler' . - ANSWER-names = ['Einstein', 'Newton', 'Copernicus',
'Kepler']
Assume names references a list. Write a for loop that displays each element of the
list. - ANSWER-for element in names:
print(element)
Assume the list of numbers1 has 100 elements, and numbers2 is an empty list. Write
code that copies the values in numbers1 to numbers2. - ANSWER-for item in
numbers1:
numbers2.append(item)
What will the following code print?
list1 = [40, 50, 60 ]
list2 = [ 10, 20, 30 ]
list3 = list1 + list2
print (list3) - ANSWER-[40, 50, 60, 10, 20, 30].
What does the following of the code display?
mystr = 'yes'
mystr += 'no'
mystr += 'yes'
print (mystr) - ANSWER-yesnoyes
What does the following code display?
mystr = 'abc' * 3
print (mystr) - ANSWER-abcabcabc
What will the following code display?
mystring = 'abcdefg'
print (mystring [2:5] ) - ANSWER-cde
What does the following code display?
numbers = [1, 2, 3, 4, 5, 6, 7 ]
print (numbers [4 : 6] ) - ANSWER-[5, 6]
What does the following code display?
, name = 'joe'
print (name . lower ( ) )
print (name. upper ( ) )
print (name) - ANSWER-joe
JOE
joe
joe
Assume choice references a string. The following if statement determines whether
choice is equal to 'Y' or 'y':
if choice == 'Y' or choice == 'y' :
Rewrite this statement so it only makes one comparison, and does not use the or
operator. (Hint: use either the upper or lower methods. ) - ANSWER-if choice.upper()
== 'Y'
Write a loop that counts the number of digits that appear in the string referenced by
mystring. - ANSWER-counter = 0
for ch in mystring:
if ch.isdigit():
counter +=1
print ('The number of digits is', counter)
Write a function that accepts a string as an argument and returns true if the
argument ends with the substring ' .com ' . Otherwise, the function should return
false. - ANSWER-def com_check(mystring):
if mystring.endswith('.com'):
return True
return False
Assume mystring references a string. Write a statement that uses a slicing
expression and displays the first 3 characters in the string. - ANSWER-
print(mystring[:3])
Look at the following statement:
mystring= ' cookies>milk>fudge>cake>ice cream '
Write a statement that splits this string, creating the following list:
[ 'cookies' , 'milk' , 'fudge' , 'cake' , 'ice cream' ] - ANSWER-mylist = mystring.split('>')
What will be the output after the following code is executed?
def pass_it(x, y):
z = x + ", " + y