INSY FINAL EXAM

Ace your homework & exams now with Quizwiz!

The self parameter is required in every method of class except the __init__ method. True or False?

False

What will the output be after running the following code snippet? Ist = ["oranges", "bananas", ""] Ist.remove("oranges") print(Ist)

['bananas', "'']

What will be the value of the variable list after the following code executes? list = [1, 2, 3, 4] list[2] = 10

[1, 2, 10, 4]

The strip() method returns a copy of the string with all the leading whitespace characters removed but does not remove trailing whitespace characters. True or False?

False

The union of two sets is as et that contains only the elements that appear in both sets. True or False?

False

When a class inherits another class, it is required to use all the data attributes and methods of the superclass. True or False?

False

what will be the output of the following code? text= "Mary bought dozen apples" print('oranges' in text) False or True?

False

What will be displayed after the following code executes? mystr = 'yes' yourstr = 'no' mystr += yourstr * 2 print(mystr)

yesnono

Which method can be used to add a group of elements to a set?

update

What will be assigned to the variable some _nums after the following code executes? special = '0123456789 some_nums = special 0:10:21

'02468'

What will be displayed after the following code executes? cities = {GA': 'Atlanta', 'NY' : 'Albany', 'CA': 'San Diego" if 'FL' in cities: ㅤdel cities ['FL'] cities ['FL'] = 'Tallahassee' print(cities ['FL'I)

'Tallahassee'

Which mode specifier will erase the contents of a file if it already exists and create the file if it does NOT already exist?

'w'

What will the output be after executing the following code? ㅤTupl = ['List', 'Tuple'] print(tuple(Tupl)

('List', 'Tuple')

what will be the output when this code is executed? ui_elements = dict([(radio_button', 2), ('text_box', 3) ('standard button', 5)]) popped_element = ui_elements.popitem() print(popped_element)

('standard_button', 5)

What will be the output after the following code is executed and the user enters 15 and -3 at the first two prompts? def main): ㅤtry: ㅤㅤtotal = intlinput("Enter total cost of items? ")) ㅤㅤnum_items = int(input("Number of items ")) ㅤㅤprint (total / num items) ㅤexcept ZeroDivisionError: ㅤㅤprint(ERROR: cannot have 0 items') ㅤexcept ValueError: ㅤㅤprint('ERROR: number of items cannot be negative') main)

-5

What will be the output after running the following code? numbers= dict([('first',30),('second',1),('third',2]) print(numbers.pop('sec',0))

0

What are the valid indexes for the string 'New York'?

0 through 7

What will the following program display? def main): ㅤ× = 10 ㅤy = 5 ㅤprint(x, y, end=' ') ㅤchange_us(x, y) ㅤprint(x, y, end- ') def change_us(x, y): ㅤx = 0 ㅤу= 0 ㅤprint(x, y, end=' ") main()

10 5 0 0 10 5

What is the output of the following code: Ist = [0, 1, 2] * 4 print(len(Ist) )

12

What will be displayed after the following code is executed? def pass_it(X, y): ㅤz=x*y ㅤresult = get_result(z) ㅤreturn(result) def get_result(number): ㅤz, = number + 2 ㅤreturn(Z) num1 = 4 num2 = 3 answer = pass_it(num1, num2) print(answer)

14

What will the output be after running the following code snippet? def tripler(num): ㅤreturn num * 6 def doubler(num): ㅤreturn num *2 print(tripler(3))

18

Given the following beginning of a class definition for a superclass named clock, how many accessor and mutator methods will be needed to complete the class definition? class clock: ㅤdef init_ (self, shape, color, price): ㅤself._shape = shape ㅤself.color = color ㅤself .price = price

3 mutator, 3 accessor

What will the output be after running the following code snippet? def myfun(num): ㅤif num > = 5: ㅤㅤreturn num - 1 ㅤelse: ㅤㅤreturn num print(myfun(4))

4

What will be output of following program? a= 'Python' i = o while i len(a): ㅤi+=1 print(i)

6

What will the output be after calling the following function? def sum(a,b): ㅤreturn a * b ㅤreturn a + b print (sum(20,3))

60

What will display after the following code is executed? def main: ㅤprint(magic(3)) def magic(num): ㅤanswer = num + 12 / 3 ㅤreturn answer main)

7

What will be the output after the following code is executed? def pass_it(x, y): ㅤz=y**x ㅤreturn(z) num2 = 3 num1 = 2 answer = pass_it(num1, num2) print(answer)

9

What is the output of the following code? num= 'a', print(type(num))

<class 'tuple'>

What type of method provides a safe way for code outside a class to retrieve the values of attributes, without exposing the attributes in a way that could allow them to be changed by code outside the method?

Accessor

The index of the first element in a list is 1, the index of the second element is 2, and so forth. True or False?

False

Of the two classes, Cherry and Flavor, which would most likely be the subclass?

Cherry

In the following line of code, what is the name of the base class? class Python(Course):

Course

What is the number of the first index in a dictionary?

Dictionaries are not indexed by numbers

A function definition specifies what a function does and causes the function to execute. True or False?

False

A superclass inherits attributes and methods from its subclasses without any of them having to be rewritten. True or False?

False

All instances of a class share the same values of the data attributes in the class. True or False?

False

In a UML diagram box that represents a class, the first of three sections holds the list of the class's methods. True or False?

False

Sets are immutable True or False?

False

The elements in a dictionary are stored in ascending order, by the keys of the key-value pairs. True or False?

False

The following code will display 'yes + no': mystr = 'yes' vourstr = 'no' mystr += yourstr print(mystr True or False?

False

The following expression is valid: string[i]='i' True or False?

False

________ allows a new class to inherit members of the class it extends

Inheritance

What will display after the following code executes? password = 'ILOVEPYTHON if password.isalpha: ㅤprint('Invalid, must contain one number.') elif password isdigit0: ㅤprint('Invalid, must have one non-numeric character.') elif password.isupper0: ㅤprint(Invalid, cannot be all uppercase characters.') else: ㅤprint (Your password is secure!!)

Invalid, must contain one number.

What will be the output after the following code is executed? def pass_it(x, y): ㅤZ=X+Y ㅤreturn(Z) name2 = "Julian" namel = "Smith" fullname = pass_it(name2, name 1) print(fullname)

JulianSmith

What will be the result of the following code? ages = {'Aaron': 6, 'Kelly': 3, 'Abigail": 1} value= ages['Kelley']

KeyError

What will the output be after executing this code? h={'blue':1,'red':2,'yellow":3} when len(h) > 3: print(h)

Nothing is printed

What type of programming contains class definitions?

Object-Oriented

_________ has the ability to define a method in a subclass and then define a method with the same name in a superclass.

Polymorphism

In the following line of code, what is the name of the subclass? class Rose(Flower)

Rose

What does not apply to sets?

The elements are in pairs.

What is the return value of the string method lstrip()?

The string with all leading whitespaces removed

"set1 <= set2" is used to determine whether set1 is a subset of set2. True or False?

True

A dictionary can include the same value several times but cannot include the same key several times. True or False?

True

Each subclass has a method named __init__ that overrides the superclass's __init__ method. True or False?

True

If you try to retrieve a value from a dictionary using a nonexistent key, a KeyError exception is raised. True or False?

True

In slicing, if the end index is not specified, Python will use the length of the list instead. True or False?

True

Indexing works with both strings and lists. True or False?

True

Negative indexes to access list elements are allowed in python. True or False?

True

New attributes and methods may be added to a subclass. True or False?

True

Polymorphism works on any two methods that have the same name True or False?

True

The difference of set1 and set2 is a set that contains only the elements that appear in set1 but do not appear in set 2. True or False

True

The set remove and discard methods behave differently only when a specified item is not found in the list. True or False?

True

What will be the output of the following code: cat = Dog() print ( isinstance (cat, Dog) ) True or False?

True

What will the output be after running the following code snippet? Ist1 = [1, 2, 1, 2] Ist2 = [1,2] *2 print(Ist1 == Ist2) True or False?

True

What will the output be after running the following code? def oddoreven(num): ㅤif (num % 2 == 1): ㅤㅤprint('YES') ㅤelse: ㅤㅤprint("NO") oddoreven(9)

YES

What values will list2 contain after the following code executes? list1 = [1, 10, 3, 6] list2 = item * 2 for item in list1 if item > 5]

[20, 12]

What will the output be after running the following code? nums = [1, 2, 3, 4, 5, 6,7] print(nums (:-1])

[7,6,5,4,3,2,1]

Which method is automatically executed when an instance of a class is created in memory?

__init__

Which method is automatically called when you pass an object as an argument to the print function?

__str__

What will the output be, if we run the following code? tup1 = ('a', 'b') tup2 = ('C', 'd') ㅤfor × in tup1: for y in tup2: ㅤㅤprint (x, y)

a c a d b c bd

In an inheritance relationship, what is a specialized class called?

a subclass

Which statement can be used to handle some of the runtime errors in a program?

a try/except statement

What does a subclass inherit rom a superclass?

attributes and methods

In a dictionary, you use a(n) ________________ to locate a specific value

key

Which is the first line needed when creating a class named Mammal?

class Mammal:

Which of the following is the correct syntax for defining a class, table, which inherits from the furniture class?

class table(furniture):

Given that the customer file references a file object, and the file was opened using the 'w' mode specifier, how would you write the string 'Mary Smith' to the file?

customer.write('Mary Smith')

Which would you use to delete an existing key-value pair from a dictionary?

del

Combining data and code in a single object is known as...

encapsulation

Which method would you use to determine whether a certain substring is present in a string?

find(substring)

Which of the following is the correct way to open a file named users txt in 'r' mode?

infile = open('users.txt', 'r')

What is the relationship called in which one object is specialized version of another object?

is a

What does the get method do if the specified key is NOT found in the dictionary?

it returns a default value

Which method would you use to get all the elements in a dictionary retuned as a list of tuples?

items

Which would you use to get the number of elements in a dictionary?

len

The procedure that an object performs are called

methods

What will the output be after executing the following code snippet? language = "Python" print(language[-1]

n

What is the output of the following snippet of code: def func(num): ㅤwhile num > 0: ㅤㅤnum = num - 1 num=3 func(num)

nothing is printed

Which of the following will assign a random integer in the range of 1 through 50 to the variable number?

number = random.randint (1,50)

The primary difference between a tuple and a list is that

once a tuple is created, it cannot be changed

The ________ keyword is ignored by the Python interpreter and can be used as a placeholder for code that will be written later.

pass

Which method would you use to get the value associated with a specific key and remove they key-value pair from the dictionary?

pop

What will be the output of the following code: s=(1, 2, 3) print(s[1])

runtime error - cannot index

Mutator methods are also known as

setters

Which of the following will create an object called tiger, of the Cat class?

tiger = Cat()

One reason not to use global variables is that it makes a program hard to debug. True or False?

true

What will be displayed after the following code executes? cities = {'CA': 'San Diego', 'NY': 'Albany', 'GA': 'Atlanta' } if 'GA' in cities: ㅤdel cities='GA' ㅤcities['GA'] = 'Atlanta' cities['CA'] = 'Sacramento' print(cities)

{'CA': 'Sacramento', 'NY': 'Albany', 'GA': 'Atlanta'}

What is the output of the following code snippet? address_book=['name':"John", 'age':23 'city':"London") while len(address_book) >= 2: address book.popitem() print(address_book)

{'name': "John"}

What will be the output after running the following code? d = {} d[1] = [2,3] d[0] = 'Python' d[weekends'] = ("Saturday","Sunday") print(d)

{1: [2,3], 0:'Python', 'weekends': ('Saturday', 'Sunday')}

What values will d2 contain after the following code executes? d = {1: 10, 2: 20, 3: 30} d2 = {k:v*2 for k,v in d.items() if v> 10}

{2: 40, 3: 60}


Related study sets

integrated science a - unit 1: exam

View Set

Microbiology DNA Transcription to RNA Key Enzymes

View Set

human anatomy and physiology chapter 16

View Set

Ch. 56 assessment of endocrine system (no rationale)

View Set