exam 1

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What values will the following code display? (Don't worry about the order in which they will be displayed.) dct = {1:[0, 1], 2:[2, 3], 3:[4, 5]} for k,i in dct.items(): print(k,i)

1 [0, 1] 2 [2, 3] 3 [4, 5]

What will the following program display? def main(): num = 0 show_me(num) def show_me(arg): if arg < 10: show_me(arg + 1) else: print(arg) main()

10

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? https://drive.google.com/file/d/10Kqy8aX_qQTxhyaKZlMWORezt5DbJlu5/view?usp=sharing

3 mutator, 3 accessor

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 = 2 num2 = 3answer = pass_it(num1, num2) print(answer)

4

What will be displayed after the following code is executed? total = 0 for count in range(4,6): total += count print(total)

4 9

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

4. Don't forget the blank space character

A function is called from the main function for the first time and then calls itself seven times. What is the depth of recursion?

7

1. An ___________________, sometimes called an ADT, is a logical description of how we view the data and the operations that are allowed without regard to how they will be implemented. A dictionary is an example of an ADT.

Abstract data type

___________ allows us to separate l_________ and p____________ perspectives of problems. The user does not need to understand the details as long as he/she understand how to work with the interface. It helps us to reduce and manage complexity.

Abstraction logical physical

_______________ is the study of problems that are and that are not computable. It is also the study of abstraction.

Computer science

A subclass may not override any method other than the __init__ method.

False

All class definitions are stored in the library so that they can be imported into any program.

False

An object is a stand-alone program but is used by programs that need its service.

False

The elements in a dictionary are stored in ascending order, by the keys of the key-value pairs

False

There must be only one function involved in any recursive solution.

False

What is the first step to take in order to apply a recursive approach?

Identify at least one case in which the problem can be solved without recursion.

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

It returns a default value

What would be the output of the following code? cast = {"Cardinal Ximenez" : "Michael Palin", "Cardinal Biggles" : "Terry Jones", "Cardinal Fang" : "Terry Gilliam"} cast["customer"] = "John Cleese" cast["shopkeeper"] = "Michael Palin" print(cast["shopkeeper"]) print(cast["Cardinal Ximenez"]) print(cast["Cardinal Fang"])

Michael Palin Micheal Palin Terry Gilliam

What will be the output after the following code is executed and the user enters 75 and -5 at the first two prompts? def main(): try: total =int(input("Enter total cost of items")) num_items= int(input("number of items") average=total/num_items except zero_division_error: print("Error:cannot have 0 items") except Value_error print("Error: number of items cannot be negative")

Nothing; there is no print statement to display average. The ValueError will not catch the error

Given the following code fragment what is its Big-O running time? i = n while i > 0: k = 2 + 2 i = i // 2

O(log(n))

What gives a program the ability to call the correct method depending on the type of object that is used to call it?

Polymorphism

Each subclass has a method named __init__ that overrides the superclass's __init__ method.

True

What does the acronym UML stand for?

Unified Modeling Language

What will be the value of the variable list2 after the following code executes? list1 = [1, 2, 3] list2 = [] for element in list1: list2.append(element) list1 = [4, 5, 6]

[1, 2, 3]

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

__init__

When an object is passed as an argument, ________ is passed into the parameter variable.

a reference to the object

Assume that you have the following class to keep track of inventory. class Inventory: item = "" barcode = 0 quantity = 0 price = 0.00 sales = 0.00 def __init__(self, product, bar, pr): self.item = product self.barcode = bar self.price = pr def changeprice(self, newprice): self.price = newprice def sell(self, n): self.quantity -= n self.sales += self.price*n def restock(self, n): self.quantity += n a. What would be the output of the following code? [3 points] widget = Inventory("widget", 1112223334, 10.00) widget.restock(30) widget.sell(10) print(widget.quantity) print(widget.sales) widget.changeprice(20.0) widget.sell(10) print(widget.quantity) print(widget.sales) b. Does this class use public or private attributes or both? [1 point] Selected Answer: a: 30 100 10 200 b: private attributes that are used only within this class

a. 20 100 10 300 b. Public

A number of very common order of magnitude functions will come up over and over as you study algorithms. These include: f(n) n n^2 2^n 1 nlog(n) n^3 log https://drive.google.com/file/d/1OvOsQwcb721Vgk4Wt9a8h7Y5WE5sEjbQ/view?usp=sharing The figure shows graphs of the above functions f(n). Label the curves shown with the most representative functions from your table above

a. 2^n - exponential b. n^3 - Cubic c. n^2 - Quadratic d. nlogn - log linear e. n - linear f. log - logarithmic g. 1 - Constant

A method that returns a value from a class's attribute but does not change it is known as a(n) ________ method.

accessor

Which of the following can be thought of as a self-contained unit that consists of data attributes and the methods that operate on the data attributes?

an object

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

class Bear(Mammal): def init (self): Mammal. init (self, 'bear')

Write a class definition named Car. The Car class should have data a attribute for a Car's make. 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 Car: # Initializer def __init__(self, make): self.__make = make # Mutators def set_make(self, make): self.__make = make # Accessors def get_make(self): return self.__make # __str__ method def __str__(self): state_string = ('make: ' + self.__make + '\n') return state_string

The following function uses a loop. Rewrite it as a recursive function that performs the same operation. def traffic_sign(n): while n > 0: print('No Parking') n = n - 1

def traffic_sign(n): if n > 0: print ('No Parking') traffic_sign(n - 1)

In order to avoid KeyError exceptions, you can check whether a key is in the dictionary using the ________ operator

in

Each object that is created from a class is called a(n) ________ of the class.

instance

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

is a

Write a statement that creates a dictionary containing the following key-value pairs: 'a' : 1 'b' : 2 'c' : 3

mydict = {'a':1, 'b':2, 'c':3}

Which would be the base case in a recursive solution to the problem of finding the factorial of a number. Recall that the factorial of a non-negative whole number is defined as n! where: If n = 0, then n! = 1 If n > 0, then n! = 1 x 2 x 3 x ... x n

n=0

Recursion is

never required to solve a problem

A recursive function includes ________ which are not necessary in a loop structure.

overhead actions

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

pop

All the cases of a recursive solution other than the base case are called the ________ case

recursive

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 ]

What will be displayed after the following code executes? (Note: the order of the display of entries in a dictionary are not in a specific order.) cities = {'GA' : 'Atlanta', 'NY' : 'Albany', 'CA' : 'San Diego'} if 'CA' in cities: del cities['CA'] cities['CA'] = 'Sacramento' print(cities)

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

After the following statement executes, what elements will be stored in the myset set? myset = set(['a', 'bb', 'ccc', 'dddd'])

{'a', 'bb', 'ccc', 'dddd'}

What would be the output of the following code? primes = {3, 5, 7, 11, 13, 17, 19, 23, 29} teens = set([13, 14, 15, 16, 17, 18, 19]) print(primes - teens)

{3, 5, 7, 11, 23, 29}


संबंधित स्टडी सेट्स

1.5 - Habitat, Niches, and Interactions

View Set

JAVA 3252 Chapter 11 polymorphism

View Set

anatomy and physiology: chapter 12

View Set

CHAPTER 2 Strategic Leadership: Managing the Strategy Process

View Set

ADV5503 Media Consumer Behavior Exam 2 Study Guide

View Set

Chapter 20 (Campylobacter and Helicobacter) and Chapter 21 (Nonfermenters and Misfits)

View Set