MIS 207 exam 2 quizzes

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Given the code snippet below, what is returned by the function call: mystery(mystery(5, 3), mystery(5, 3))? def mystery(num1, num2) : result = num1 * num2 return result

225

What are the values in the following dictionary? numbers = {1: 5.5, 2.0: 77, 3: 33}

5.5, 77 and 33

Consider the following class: class Contact : def __init__(self, name, phone="") self._name = name self._phone = phone def getName(self) : return self._name def setName(self, new_name) : self._name = new_name def getPhone(self) : return self._phone def setPhone(self, new_phone) : self._phone = new_phone What is output by the following code segment?

555-123-4567

Given the following list, what value is at index 5? values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

6

What are the keys in the following dictionary? fruit = {"Apple": "Green", "Banana": "Yellow"}

Apple and Banana

Which of the following is considered by the text to be the most important consideration when designing a class?

Each class should represent a single concept or object from the problem domain.

Which of the following patterns can be used for designing your class to add error checking such as not allowing a blank name for a bank account?

Managing Properties of an Object

What items should be considered when creating the public interface?

Method headers and method comments

Consider the following class:class Counter : def getValue(self) : return self._value def click(self) : self._value = self._value + 1 def unClick(self) : self._value = self._value - 1 def reset(self) : self._value = 0 Which method(s) are mutators?

Only click, unClick and reset

Which of the following is NOT true about constructors?

The constructor is defined using the special method name __default__

What does an object reference specify?

The location of an object

What output is generated by the following code segment? table = [["T", "U", "V"], ["W", "X", "Y"]] print(table[1][1])

X

What output is generated by the following code segment? table = [["T", "U", "V"], ["W", "X", "Y"]] print(table[0])

["T", "U", "V"]

Which of the following statements best represents a deck of cards?

cards = [["Ace of Hearts", "Ace of Spades", "Ace of Diamonds", "Ace of Clubs"], ["2 of Hearts", "2 of Spades", "2 of Diamonds", "2 of Clubs"], etc.]

Which code segment creates a dictionary with keys that are integers and values that are lists?

cards = dict() cards[1] = ["Ace", "Spades"] cards[2] = ["Two", "Spades"]

Which statement creates a new, empty list?

data = []

Which of the following code segments creates a dictionary of lists?

days = {} days["February"] = [28, 29]

Which of the following method headers represent a constructor?

def __init__(self) :

Given a list values that contains the first ten prime numbers, which statement prints the index and value of each element in the list?

for i in range(10) : print(i, values[i])

Consider the following functions: def printIt(x) : print(x) def incrementIt(x) : return x + 1 def decrementIt(x) : return x - 1 def doubleIt(x) : return x * 2 Which of the following function calls is not a reasonable thing to do?

print(printIt(5))

Consider the following table: table = [["T", "U", "V"], ["W", "X", "Y"]] Which statement will display Y?

print(table[len(table) - 1][len(table[1]) - 1])

What library is used to read and write sound files?

scipy.io.wavfile

What is printed when the following code snippet executes? names = set(["Jane", "Joe", "Amy", "Lisa"]) names.add("Amber") names.add("Zoe") names.clear() print(names)

set()

When should a computation be turned into a function?

when it may be used more than once

Which of the following statements sets x so that it refers to no object at all?

x = None

Consider the following code segment: x = values[0] for i in range(1, len(values)) : if values[i] > x : x = values[i] Which of the following statements assigns the same value to x as the code segment above?

x = max(values)

Given the variable assignment sentence = "", what is the value of len(sentence)?

0

Consider the following program: def main() : a = 5 print(doubleIt(a)) def doubleIt(x) : return x * 2 main() What output is generated when this program is run?

10

Given the following code snippet, what is considered an argument(s)? def mystery(num1, num2) : result = num1 ** num2 return result mystery(10, 2)

10, 2

Consider the following program: def main() : a = 2 doubleIt(a) print(a) def doubleIt(x) : x = x * 2 main() What output is generated when this program is run?

2

How many values does a stereo sound file have for each sample?

2

What is printed from the following code snippet? prices = [[ 1.0, 3.50, 7.50 ], [ 10.0, 30.50, 70.50 ], [ 100.0, 300.50, 700.50 ], [ 1000.0, 3000.50, 7000.50 ]] print(prices[1][2])

70.5

Which statement is most correct?

A Python dictionary stores associations between keys and values.

What structure should be used to store a collection of unique values when the order of the elements is not important?

A set

What is stored in contents when a sound file is read by the scipy library using the following statement? contents=scipy.io.wavfile.read("meow.wav")

A tuple containing the sample rate and a NumPy array

Which of the following is NOT a pattern used to help design the data representation of a class?

An object can collect other objects in a list

What is wrong with the following code? def grade(score) : if score >= 90 : return "A" elif score >= 80 : return "B" elif score >= 70 : return "C" elif score >= 60 : return "D"

Another return statement needs to be added to the function

When using the process of tracing objects for problem solving, which types of methods update the values of the instance variables?

Both B and C

Before invoking a method on an object, what must be done first?

Construct the object

Which of the following patterns can be used for designing your class for a train object that drives along a track and keeps track of the distance from the terminus?

Describing the Position of an Object

What is displayed by the following code segment? values = ["Q", "W", "E", "R", "T", "Y"] print(min(values))

E

What does the following code segment do? x = 0 for i in range(1, len(values)) : if values[i] > values[x] : x = i

It finds the position of the largest item in values and stores the position in x

What does the following code segment do? x = 0 for i in range(1, len(values)) : if values[i] < values[x] : x = i

It finds the position of the smallest item in values and stores it in x

Parameter variables should not be changed within the body of a function because

It is confusing because it mixes the concept of a parameter with that of a variable

What does the following code segment do? i = 0 while i < len(values) : if values[i] >= 4 and values[i] <= 6 : values.pop(i) else : i = i + 1

It removes all 4s, 5s and 6s from values

According to the textbook, what is the best practice for updating instance variables?

Restrict access to instance variables, only allow updates through methods

Which of the following is NOT true about objects?

The == and != operators test whether two variables are aliases

What part of the virtual machine is responsible for removing objects that are no longer referenced?

The garbage collector

What term is used to describe the process of verifying that a class works correctly in isolation, outside of a complete program?

Unit testing

Suppose you have a class ShoppingList, with instance variables _quantity, _cost, and _itemName. How should you access these variables when writing code that is not contained within the ShoppingList class?

Use methods provided by the ShoppingList class.

How do you access instance variables in a method?

Using the self reference

For a program that reads city names repeatedly from the user and calculates the distance from a company's headquarters, which of the following would be a good design based on stepwise refinement?

Write one function that reads city name and another function that calculates distance

Given the following code snippet, what are the contents of the list fullNames? firstNames = ["Joe", "Jim", "Betsy", "Shelly"] lastNames = states = ["Jones", "Patel", "Hicks", "Fisher"] fullNames = firstNames + lastNames

["Joe", "Jim", "Betsy", "Shelly", "Jones", "Patel", "Hicks", "Fisher"]

What is the value of names after the following code segment has run? names = [] names.append("Amy") names.append("Bob") names.append("Peg") names[0] = "Cy" names.insert(0, "Ravi") names.insert(4, "Savannah")

['Ravi', 'Cy', 'Bob', 'Peg', 'Savannah']

What is the value of names after the following code segment has run? names = [] names.append("Amy") names.append("Bob") names.append("Peg") names[0] = "Cy" names.insert(0, "Ravi")

['Ravi', 'Cy', 'Bob', 'Peg']

What is in values after the following code segment executes? values = [1, 2, 3] values = values * 4

[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

What list is stored in x after this code segment has run? x = [] for i in range(3) : x.append([]) for j in range(i) : x[j].append(0)

[[0, 0], [0], []]

Consider the following class which will be used to represent complex numbers: class Complex: def __init__(self, real, imaginary): self._real = real self._imaginary = imaginary def ____________________: real = self._real + rhsValue._real imaginary = self._imaginary + rhsValue._imaginary return Complex(real, imaginary) What code should be placed in the blank so that two complex numbers can be added using the + operator?

__add__(self, rhsValue)

What is the name of the instance variable in the following code segment? class Fruit : def getColor(self) : return self._color

_color

Consider the following class: class Pet: ____________________ def __init__(self, name): self._name = name Pet._lastID = Pet._lastID + 1 self._registrationID = Pet._lastID What line of code should be placed in the blank to create a class variable that keeps track of the most recently used registration identifier?

_lastID = 0

What happens in this code snippet if sideLength = -10? def cubeSurfaceArea(sideLength) : if sideLength >= 0 : return 6 * (sideLength * sideLength) # There are six sides to a cube; surface area of each side is sideLength squared

a special value of None will be returned from the function

Consider the following code segment: def main() : avg = 0 total = 0 for i in range(6) : iSquared = i * i total = total + iSquared avg = total / i print(total) print(avg) Which of the following answers lists all of the local variables in this code segment?

avg, total, i, iSquared

Which of the following statements is used to begin the implementation of a new class named Fruit?

class Fruit :

Which function call correctly invokes the partial drawShape function listed below and prints a star triangle? def drawShape(type) : length = len(type) if length == 0 : return if type == "triangle" : print(" *") print(" ***") print("*****") drawShape("triangle")

drawShape("triangle")

Given a list containing prices, how do you find the highest priced item and remove it from the list:

find the maximum, remove it from the list

What is the purpose of the following pseudocode: i = 0 j = length / 2 While i < length / 2 # Swap elements at positions i and j temp = a[i] a[i] = a[j] a[j] = temp i = i + 1 j = j + 1

flip the first half of a list with the second half

Given the list values = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], which statement fills the list with these numbers: 1 4 9 16 25 36 49 64 81 100

for i in range(10) : values[i] = (i + 1) * (i + 1)

Which of the following code segments displays the favoriteFoods dictionary in alphabetical order by name? favoriteFoods = {"Peg": "burgers", "Cy": "hotdogs", "Bob": "apple pie"}

for name in sorted(favoriteFoods) : print(name, favoriteFoods[name])

A ___________________________ is a sequence of instructions with a name.

function

Which method below would be considered an accessor method?

getCount()

Consider the following class: class Counter : def getValue(self) : return self._value def click(self) : self._value = self._value + 1 def unClick(self) : self._value = self._value - 1 def reset(self) : self._value = 0 Which method is an accessor?

getValue

Given the following code snippet, how can you test if they reference the same object (or does not refer to any object)? bankAcct2 = bankAcct

if bankAcct is bankAcct2 : print("The variables are aliases")

Given the following code snippet, which statement tests to see if all three sets are equal? fruit = set(["apple", "banana", "grapes", "kiwi"]) fruit2 = set(["apple", "banana", "grapes", "kiwi"] fruit3 = set(["apple", "banana", "pears", "kiwi"])

if fruit == fruit2 and fruit == fruit3 :

Which of the following statements determines if x currently refers to an object containing an integer?

if isinstance(x, int) :

Which statement determines if the middleInit variable is empty or does not refer to any object?

if middleInit is None : print("No middle initial")

What code completes this code snippet to swap the first and last element in the list? states = ["Alaska", "Hawaii", "Florida", "Maine"] i = 0 ________________________ temp = states[j] states[j] = states[0] states[i] = temp

j = len(states) - 1

One advantage of designing functions as black boxes is that

many programmers can work on the same project without knowing the internal implementation details of functions.

Which statement(s) allows us to initialize the list numbers with 10 elements all set to zero?

numbers = [0] * 10

Which statement correctly identifies the number of elements in the set flags?

numflags = len(flags)

In what order are the elements of a set visited when the set is traversed using a for loop?

random order

The following two objects are created from the Counter class: studentCounter, and teacherCounter to represent the total number of students and the total number of teachers respectively. If the Counter class contains an instance variable, _num, that is initialized to zero and increases every time the user executes the add method, what is stored in each object's instance variable after the following code snippet executes?studentCounter.add() teacherCounter.add() studentCounter.add()

studentCounter : 2, teacherCounter : 1

The purpose of a function that does not return a value is

to package a repeated task as a function even though the task does not yield a value

Consider a function named calc. It accepts two integer arguments and returns their sum as an integer. Which of the following statements is a correct invocation of the calc function?

total = calc(2, 3)

Which statement correctly creates a list that contains four elements?

values = [1, 2, 3, 4]

Which of the following code segments will result in values containing the list ["Hydrogen", "Helium", "Lithium"]?

values = [] values.append("Hydrogen") values.append("Helium") values.append("Lithium")

Which of the following statements is NOT true about functions and lists:

when calling a function with a list argument, the function receives a copy of the list

Consider the following code segment: primes = {2, 3, 5, 7} odds = {1, 3, 5, 7} Which line of code will result in x containing {1, 2, 3, 5, 7}?

x = primes.union(odds)

Which of the following is a correct call to Python's round function?

x = round(3.14159)

Which statement creates a set of 3 elements and stores it in x?

x = {1, 2, 3}

Consider the following code segment: fruit = {"Apple": "Green", "Banana": "Yellow"} fruit["Plum"] = "Purple" After it executes, what is the value of fruit?

{"Apple": "Green", "Banana": "Yellow", "Plum": "Purple"}

What is the value of x after the following code segment executes? x = {1, 2, 3} x.add(1)

{1, 2, 3}


Kaugnay na mga set ng pag-aaral

Write These Numbers In Standard Form

View Set

MKTG Exam 1 Review - IN CLASS DISCUSSION

View Set

Comparative Government Semester 1 Final

View Set

Related questions of Saunders Test PREP off website 8th edition

View Set

Articulaciones - Enfermedades Infecciosas

View Set