Python Final
class Student: def __init(self, my_id): self.__id = my_id def get_id(self): return self.__id Suppose student1 is a Student object. Which of the following statements displays the variable id?
print(student1.get_id())
Suppose setA and setB are two sets in Python. Which of the following generates the union of setA and setB?
setA | setB
def main(): # insert a statement here to call the show_name function def show_name(): name = input('What is your name? ') print('Your name is', name) main() Which of the following statements correctly calls the show_name function?
show_name(name)
What does the __str__ method do?
Displaying a message that indicates an object's state
What type of exception when an input/output operation fails (e.g. "file not found")?
IOError
What about the __str__ method is true?
It returns a string It is executed automatically when the object is treated like a string
What is the output of the following program? setA = {7, 2, 4} print(setA[2])
The program has a syntax error because we cannot use index to access set elements
A student has collected data on rainfall in the last 180 days. She is writing a program to analyze the data Once the data are stored, she does not want them to be altered. She just wants to be able to retrieve them for calculations statistical analyses. What data structure in Python is the best choice?
Tuple
What is the output of the following Python program? s1 = 'CIS-115' s2 = s1.split('-') print(s2)
['CIS','115']
numbers = [-2, -2, 1, 4, 3] numbers[-2] = 5 print(numbers) What is the output?
[-2,-2,1,5,3]
list1 = [2, 5, 7, 6, 4] list2 = list1 + [] print(list2) What is the output?
[2,5,7,6,4]
ist1 = [2, 5, 7, 6, 4] list1.insert(2, 8) print(list1) What is the output?
[2,5,8,7,6,4]
What is the output of the following Python program? numbers = list(range(3, 6)) print(numbers)
[3,4,5]
What is the output of the following Python program? numbers = [5, 7] * 3 print(numbers)
[5, 7, 5, 7, 5, 7]
What is the output of the following Python program? numbers1 = [2, 5] numbers2 = [7, 1] numbers3 = numbers2 + numbers1 print(numbers3)
[7,1,2,5]
What is the output of the following Python program? numbers = list(range(9, 1, -2)) print(numbers)
[9,7,5,3]
What is the output of the following Python program? s1 = 'abcde' print(s1[:3])
abc
what Python data structure does the following statement create? student_data={}
an empty dictionary
what does the following Python statement tell us? class X(Y)
class Y is the base class of class x
def main(): num_cats = int(input('How many cats do you have? ')) display_cats(num_cats) # Insert definition of the display_cats function here main() Which of the following definitions of the display_cats function is correct?
def display_cats(): print("Number of cats you have:", num_cats) **Since num_cats is passed as an argument, the display_cats function must have a parameter, which is also used in the print statement to display the number of cats.
def main(): num_cats = int(input('How many cats do you have? ')) num_dogs = int(input('How many dogs do you have? ')) display_pets(num_dogs, num_cats) # Insert definition of the display_pets function here main() Which of the following definitions of the display_pets function is correct?
def display_pets(n_cats = num_cats, n_dogs = num_dogs): print('Number of dogs you have:', n_dogs) print('Number of cats you have:', n_cats)
def main(): num_cats = get_cats() print('Number of cats you have:', num_cats) # Insert definition of the get_cats function here main() Which of the following definitions of the get_cats function is correct?
def get_cats(): n_cats = int(input('How many cats do you have? ')) return n_cats
def main(): name = input('What is your name? ') # insert a statement here to call the show_name function def show_name(name_entered): print('Your name is', name_entered) main() Which of the following statements correctly calls the show_name function?
show_name(name)
What is the output of the following Python program? s1 = 'babbe' s1 = s1.replace('b','x') print(s1)
xaxxe
Unified modeling language is...
A graphical standardized design specification language for specifying an object-oriented design
What is the output of the following Python program? s1 = 'xyanwe' print(s1.find('ab'))
-1
numbers = [-2, -2, 1, 4, 3] s = 0 for value in numbers: s += value print(s) What is the output?
4
numbers = [-2, -2, 1, 4, 3] s = len(numbers) print(s) What is the output?
5
What is the output of the following Python program? s1 = '21' s2 = '53' s3 = s2 + s1 print(s3)
5321
values = [[2,5], [7,1], [4,3]] sum_c1 = values[0][1] + values[1][1] + values[2][1] print(sum_c1) What is the output?
9
for i in range(4): unit_price = float(input("Enter unit price: ")) print(unit_price) What will happen if the first unit price entered is "$12.50"?
A ValueError will be raised and the program will exit immediately
def main(): num_cats, num_dogs = get_pets() print('Number of cats you have:', num_cats) print('Number of dogs you have:', num_dogs) # Insert definition of the get_pets function here main() Which of the following definitions of the get_pets function is correct?
def get_pets(): n_cats = int(input('How many cats do you have? ')) n_dogs = int(input('How many dogs do you have? ')) return n_cats, n_dogs
def main(): # insert a statement here to call the get_names function print('Your first name is', first_name) print('Your last name is', last_name) def get_names(): f_name = input('What is your first name? ') l_name = input('What is your last name? ') return f_name, l_name main() Which of the following statements should be used to call the get_names function?
get_names()
Suppose the text file "course_info.txt" contains data of courses. In each line of the file there is the course name followed by a comma and the number of students. Which of the following program fragments displays the name of each course?
input_file = open('course_info.txt', 'r') for line in input_file: course = line.split(',') print(course[0])
Which of the following Python statements will open the file "studentRecords.txt" to read data from the file?
input_file=open('studentRecords.txt','r')
def main(): letter = 'p' letter = change_letter() print(letter) def change_letter(): letter_entered = 'k' return letter_entered main() What is the output?
k
list1 = [2, 5, 7, 6, 4] Which of the following statements create the list [7, 6, 4]?
list2=list1[2:] list3=[list1[-3:] list4=list1[-3:5]
Which of the following program fragments creates a dictionary to store two items?
my_dict={'chicken sandwich':4.50,'chinese burger':3.25} pricelist=[['chicken sandwich', 4.50],['chinese burger',3.25]] my_dict=dict(pricelist)
def main(): first_name = input('What is your first name? ') last_name = input('What is your last name? ') # insert a statement here to call the combine_names function print('Your full name is', name) def combine_names(f_name, l_name): full_name = f_name + ' ' + l_name return full_name main() Which of the following statements should be used to call the combine_names function?
name=combine_names(first_name,last_name)
def main(): # insert a statement here to call the get_name function print('Your name is', name_entered) def get_name(): name = input('What is your name? ') return name main() Which of the following statements should be used to call the get_name function?
name_entered-get_name()
What is the output of the following Python program? t = (2, 5, 7, 6, 4) t.append(8) print(t)
no output due to syntax error
def main(): letter = 'p' change_letter() print(letter) def change_letter(): letter = 'k' main() What is the output?
p
def main(): letter = 'p' change_letter(letter) print(letter) def change_letter(letter_entered): letter_entered = 'k' main() What is the output?
p
def main(): letter = 'p' change_letter(letter) print(letter) def change_letter(letter_entered): letter_entered = 'k' return letter_entered main() What is the output?
p
class Employee: def __init__(self, name, position): self.__name = name self.__position = position def get_name(self): return self.__name def get_position(self): return self.__position def main(): employee1 = Employee('John Doe', 'CEO') # insert statement here to display name and position Which of the following statements displays name and position?
print(employee1.get_name(), employee1.get_position())
def main(): first_name = input('What is your first name? ') last_name = input('What is your last name? ') # insert a statement here to call the show_names function def show_names(l_name, f_name): print('Your first name is', f_name) print('Your last name is', l_name) main() Which of the following statements correctly calls the show_names function?
show_names(l_name=last_name, f_name=first_name) show_names(f_name=first_mane, l_name=last_name)
def main(): first_name = input('What is your first name? ') last_name = input('What is your last name? ') # insert a statement here to call the show_names function def show_names(f_name, l_name): print('Your first name is', f_name) print('Your last name is', l_name) main() Which of the following statements correctly calls the show_names function?
show_names(last_name, first_name)
def main(): first_name = input('What is your first name? ') last_name = input('What is your last name? ') # insert a statement here to call the show_names function def show_names(l_name, f_name): print('Your first name is', f_name) print('Your last name is', l_name) main() Which of the following statements correctly calls the show_names function?
show_names(last_name,first_name)