python test 1

Ace your homework & exams now with Quizwiz!

In a Python class, how do you hide an attribute from code outside the class?

By starting its name with two underscore characters.

Suppose a program uses two classes: Airplane and JumboJet. Which of these would most likely be the subclass?

JumboJet

What does a subclass inherit from its superclass?

The superclass's attributes and methods.

An algorithm that uses a loop will usually run faster than an equivalent recursive algorithm.

True

Dictionaries are not sequences.

True

If the + operator is used on strings, it produces a string that is a combination of the two strings used as its operands

True

A function is called once from a program's main function, then it calls itself four times. The depth of recursion is

four

Assume the variable dct references a dictionary. Write an if statement that determines whether the key 'James' exists in the dictionary. If so, display the value that is associated with that key. If the key is not in the dictionary, display a message indicating so.

if 'James' in dct: print(dct['James']) else: print('James is not in the dictionary.')

The ________ method returns a randomly selected key-value pair from a dictionary in versions of Python earlier than 3.7.

popitem()

When recursion is used to solve a problem, why must the recursive function call itself to solve a smaller version of the original problem?

By reducing the problem with each recursive call, the base case will eventually be reached and the recursion will stop.

What will the following code display? dct = {'Monday':1, 'Tuesday':2, 'Wednesday':3} print(dct['Tuesday'])

2

What will the following code display? myset = set('1 2 3') print(len(myset))

4. Don't forget the blank space character

What is encapsulation?

Encapsulation refers to the combining of data and code into a single object.

A class method does not have to have a self parameter.

False

You can store duplicate elements in a set.

False

You cannot use the isinstance function to determine whether an object is an instance of a subclass of a class

False

Is recursion ever required to solve a problem? What other approach can you use to solve a problem that is repetitive in nature?

No, recursion is not required. You can alternatively use a loop

Give the Big-O performance of the following code fragment: for i in range(n): k = 2 + 2

O(n)

Q-1: Given the following code fragment, what is its Big-O running time? test = 0 for i in range(n): for j in range(n): test = test + i * j

O(n**2)

In one approach to identifying a class's data attributes and methods, the programmer identifies the class's .

Responsibilities

What is an overridden method?

When a subclass method has the same name as a superclass method, it is said that the method is overridden.

Why should an object's data attributes be hidden from code outside the class?

When an object's data attributes are hidden from outside code, and access to the data attributes is restricted to the object's methods, the data attributes are protected from accidental corruption. In addition, the code outside the object does not need to know about the format or internal structure of the object's data. The code only needs to interact with the object's methods. When a programmer changes the structure of an object's internal data attributes, he or she also modifies the object's methods so that they may properly operate on the data. The way in which outside code interacts with the methods, however, does not change.

Suppose that we have the fragment of Python code. Using assignments as your indicator of running time complexity: a. Write a formula for T(n) (running time wrt to n) for this program: _____________________ (fill in the blank) b. This program is O(___) of complexity (fill in the blank) a=5 b=6 c=10 for i in range(n): for j in range(n): x = i * i y = j * j z = i * j for k in range(n): w = a*k + 45 v = b*b d = 33

a. T(n)=3+3n**2+2n+1=3n**2+2n+4 T(n)=3+3n**2+2n+1=3n**2+2n+4. b. O(n**2)

Suppose that we have the fragment of Python code. Using assignments as your indicator of running time complexity: a. Write a formula for T(n) (running time wrt to n) for this program: _____________________ (fill in the blank) b. This program is O(___) of complexity (fill in the blank) a=5 b=6 c=10 for i in range(n): for j in range(n): x = i * i y = j * j z = i * j for k in range(n): w = a*k + 45 v = b*b d = 33

a. T(n)=3+3n**2+2n+1=3n**2+2n+4 T(n)=3+3n**2+2n+1=3n**2+2n+4. b. O(n**2)

You can add one element to a set with this method.

add

A(n) ______________ is a step-by-step list of instructions for solving any instance of the problem that might arise.

algorithm

By doing this, you can hide a class's attribute from code outside the class.

begin the attribute's name with two underscores

A recursive function .

calls itself

Write a class definition named Book. The Book class should have data attributes for a book's title, the author's name, and the publisher's name. The class should also have the following: a. An _ _init_ _ method for the class. The method should accept an argument for each of the data attributes. b. Accessor and mutator methods for each data attribute. c . _ _str_ _ method that returns a string indicating the state of the object

class Book: # Initializer def __init__(self, title, author, pub): self.__title = title self.__author = author self.__pub = pub # Mutators def set_title(self, title): self.__title = title def set_author(self, author): self.__author = author def set_pub(self, pub): self.__pub = pub # Accessors def get_title(self): return self.__title def get_author(self): return self.__author def get_pub(self): return self.__pub # __str__ method def __str__(self): state_string = ('Title: ' + self.__title + '\n' + 'Author: ' + self.__author + '\n' + 'Publisher: ' + self.__pub + '\n') return state_string

Look at the following class definition: class Beverage: def _ _init_ _(self, bev_name): self._ _bev_name = bev_name Write the code for a class named Cola that is a subclass of the Beverage class. The Cola class's __init__ method should call the Beverage class's __init__ method, passing 'cola' as an argument.

class Cola(Beverage): def init (self): Beverage. init (self, 'cola')

It is not necessary to have a base case in all recursive algorithms.

false

One way to find the classes needed for an object-oriented program is to identify all of the verbs in a description of the problem domain.

false

Assume the variable dct references a dictionary. Write code that pickles the dictionary and saves it to a file named mydata.dat.

import pickle output_file = open('mydata.dat', 'wb') # write out in binary pickle.dump(dct, output_file) output_file.close()

The ________ operator can be used to determine whether one string is contained in another string.

in

When function A calls function B, which calls function A, it is called recursion.

indirect

The ________ built-in function is used to read a number that has been typed on the keyboard.

input()

Which method can be used to place an item at a specific index in a list?

insert

Suppose my_car is the name of a variable that references an object, and go is the name of a method. Write a statement that uses the my_car variable to call the go method. (You do not have to pass any arguments to the go method.)

my_car.go()

The ________ of a local variable is the function in which that variable is created.

scope

Look at the following code: set1 = set([100, 200, 300, 400, 500]) set2 = set([200, 400, 500]) Which of the sets is a superset of the other?

set1 is a superset of set2

Assume each of the variables set1 and set2 references a set. Write code that creates another set containing the elements that appear in set1 but not in set2, and assigns the resulting set to the variable set3.

set3 = set1.difference(set2)

Assume each of the variables set1 and set2 references a set. Write code that creates another set containing only the elements that are found in both set1 and set2, and assigns the resulting set to the variable set3.

set3 = set1.intersection(set2)

The ________ method returns the list of the words in a string.

split()

What is the equivalent list comprehension for the following ?: sq_list = [] for x in range(1, 11): if x % 2 == 0: sq_list.append(x * x)

sq_list = [x*x for x in range(1,11) if x%2 == 0 ]

You can use this to determine whether an object is an instance of a class.

the isinstance function

A subclass can have a method with the same name as a method in the superclass.

true

Object reusability has been a factor in the increased use of object-oriented programming

true

Polymorphism allows you to write methods in a subclass that have the same name as methods in the superclass.

true

You cannot directly call the _ _str_ _ method.

true

You can use __________ to create an empty dictionary.

{}


Related study sets

Cell Membrane- controls what enters and leaves cell.

View Set

Stereotypes, Prejudice, and Discrimination: Social Psychology

View Set

Chapter 1: The Challenge of Economics

View Set

Muscular system for UNC EXSS 175

View Set