CS II Final

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

The primary difference between a tuple and a list is that ______________

- once a tuple is created, it cannot be changed.

The _________ method returns a randomly selected key-value from a dictionary.

- pop()

Give the output: class Animal: def __init__(self, name): self.__name = name def getName(self): return self.__name def __str__(self): return 'I am an animal. My name is ' + self.getName() class Dog(Animal): def __str__(self): return 'I am a dog. My name is ' + self.getName() def printAnimalName(x): if isinstance(x, Animal): print(x) else: print('This is not an animal') def main(): critter = Animal('critter') Bo = Dog('Bo') printAnimalName(critter) printAnimalName(Bo) print(isinstance(Bo, Animal)) print(isinstance(critter, Dog)) printAnimalName('critter') main()

I am an animal. My name is critter. I am a dog. My name is Bo. True False This is not an animal

Which of the following statement creates a dictionary of favorite foods?

favoriteFoods = {"Peg": "burgers", "Cy": "hotdogs", "Bob": "apple pie"}

Which statement(s) below print the set colors in sorted order?

for color in sorted(colors): print(colors[i])

Give code to print every element of a list with an even index.

for element in list: if element % 2 == 0: print(list[element])

Which code segment prints only the values stored in the fruit dictionary?

for item in fruit.values() print(item)

Which of the following code segments displays the favoriteFoods dictionary in alphabetical order by name?

for name in favoriteFoods.sort(): print(name, favoriteFoods[name])

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])

Given four sets set1, set2, set3 and set4, write an expression or expressions that create a fifth set, set5, that contains all elements that are common to both set1 and set2 as well as elements are common to set3 and set4.

sets = set1.intersection(set2) setss = set3.intersection(set4) set5 = sets.union(setss) print(set5)

Give code to add the first row of a 2D array with 3 columns.

sum = [0,0] + [0, 1] + [0,2]

Which line of code should be placed in the blank to achieve this goal?

while line != "":

To add an element to a set used _____________

- .add()

To add an element to a list use ______________

- .append()

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

- 0 through 7

Consider the following code segment: What is displayed when the code segment is executed? data = {"A" : 65, "B" : 66, "C" : 67} data["D] = 68 print(len(data))

- 4

circle the immutable below:

- 432 - float - string - 6.02 - tuple

set1 = set{'vodka'} set2 = set{'cognac'} set3 = set{'tequila'} print(len(set1), len(set2), len(set3))

- 5, 5, 7

In the following code snippet, what happens if the "output.txt" file does not exist?

- An empty file is created.

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

- Course

Sets are immutable

- False.

Consider the class Employee: def __init__(self, firstName, lastName, employeeId): self._name = lastName + ", " + firstName self._employeeId = employeeId if an object is constructed as: sam = Employee("Sam", "Fisher", 54321) What is the contents of the instance variable _name?

- Fisher, Sam

Name one common error when working with lists:

- Indexing with a number out of range. - misspelling words. - missing the {}.

Give the contents of the list after the actions listed: Names = ['Tyler'] Names.insert(1, 'Shannon') Names.insert(0, 'Nia') Names.pop(2) Names.append('Mary')

- Nia, Tyler, Mary

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

- Number = random.randint(1, 50)

What is the purpose of the self parameter?

- Refers to the object on which the method was invoked.

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

- Rose

One ket difference between a set and a list is:

- Set elements are not stored in any particular order.

Which of the following does not apply to sets?

- The elements are pairs.

The self parameter is required in every method of a class.

- True

When reading a file in Python, you must specify two items:

- a file name and mode.

What is the difference between a list and a dictionary?

- a list stores individual elements but a dictionary stores key/value pairs.

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

- a subclass

predict the output of the following code segment s = 'i think therefore i am' x = s.split() x.sort() y = '-'.join(x) print(y)

- am-i-i-therefore-think

In the code snippet below, if the file contains the following words: apple, pear, and banana stored one per line, what would be the output? infile = open("input.txt", "r") for word in infile: word = word.rstrip print(word)

- apple pear banana

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

- class table(furniture):

set1 = set{'vodka'} set2 = set{'cognac'} set3 = set{'tequila'} Write an expression that prints the set of elements common to set1 and set2 but not found in set3.

- commonSet = set1.intersection(set2) notIn = common.difference(set3) print(notIn)

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

- getColor

which method is accessor?

- getValue.

Accessor methods are also known as

- getters

What will be the result of the following code segment? a = 'hello' b = 123.456 c = ('honey', 'toasted', 'oats') x = (b, a*3, c) print(x[1])

- hello, hello, hello

______________ in a menu-driven program, what statement is used to determine and carry out the user's desired action?

- if-elif-else

Which of the following statements open a text file for reading?

- infile = open("my file.txt", "r")

Give the code to open a file name myFile for reading.

- infile = open("myFile.txt", "r")

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

- init

This function returns the length of a list:

- len

Built-in function that returns the highest value in a list:

- max()

Consider the following code segment: names = set(["Jane", "Joe", "Amy", "Lisa"]) names1 = set(["Joe", "Amy", "Lisa", "Bob")] names2 = set(["Joe", "Amy", "Lisa"]) which of the following statements is true?

- names2 is a subset of names.

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

- random order.

Once a file has been opened, what method is used to read data from a file?

- readline()

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

- rstrip() method creates a new string in which all whitespaces (blanks, tabs, and newlines) at the end of the string has been removed.

Every method of a class needs this parameter:

- self

One key difference between a set and a list is:

- set elements are not sorted in any particular order.

Mutator methods are also known as

- setters

When using the deadline method, what data type is returned?

- string

When using the deadline method what data type is returned?

- string.

In an inheritance relationship, a minivan can be thought of as a(n) _____________ of the vehicles class.

- subclass

The __________ of two sets is a set that contains all the elements of both sets.

- union

What method can be used to combine two sets in Python?

- union()

How can you make sure the elements in a set will be printed in sorted order?

- use the sorted function when printing the set.

Fill in the missing parts of the lines: count = 0 total = 0 line = inline.readline() while ______________ value = float(value) outfile.write("%15.2\n" % value) total = total + value count = count + 1 line = ______________

- while line != "" : - line = infile.readline()

The following code segment is supposed to read all of the lines from test.txt and save in copy.txt. infile = open("test.txt", "r") outfile = open("copy.txt", "w") line = infile.readline() _____________________________________ outline.write(line) line = infile.readline() infile.close() outfile.close()

- while line != "":

Which statement creates an empty set and stores it in x?

- x = set()

What would be the output? Size = 3; Row_num = 3; Col_num = 3? for Row_num in range(size): for col_num in range(size): A[Row_num][Col_num] = Row_num print(A)

0 0 0 1 1 1 2 2 2

What will the following code display? Numbers = [1, 2, 3, 4, 5] Numbers[2] = 99 print(Numbers)

1, 99, 3, 4, 5

What will the following code display? Numbers = [1, 2, 3, 4, 5] My_list = Numbers[1:] print(My_list)

2, 3, 4, 5

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

5.5, 77, and 33

In the code snippet below, if the file contains the following words: Monday! Tuesday. Wednesday? stored one per line, what would be output? infile = open("input.txt", "r") for word in infile: word = word.rstrip(".!\n") print(word)

Monday Tuesday Wednesday?

Write a statement that creates a two-dimensional list named TwoD with 2 rows and 3 columns. All values should be assigned 0.

TwoD = [[0, 0, 0] [0, 0, 0]]

Which list will be referenced by the variable Number after the execution of the following code? Number = range(0, 9, 2)

[0, 2, 4, 6, 8]

Which list will be referenced by the variable number after the execution of the following code? number = range(0, 9, 2)

[0, 2, 4, 6, 8]

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

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

What would be the value of the variable List after the execution of the following code? List = [1, 2, 3, 4] List[3] = 10

[1, 2, 3, 10]

What will the following code display? Numbers = [10, 2] * 3 print(Numbers)

[10, 2, 10, 2, 10, 2]

What is the output? Num = [2, 4] Num2 = Num * 3 print(Num2)

[2, 4, 2, 4, 2, 4]

What will the following code display? Values = [2, 3, 6, 8, 10] print(Value[1:3])

[3, 6]

Fill in the code to accomplish the comments purpose. a) #create a list named Fruity with four fruit names. b) #insert a new fruit at the end of the list. c) #sort and print the list.

a) Fruity = ["peaches", "apples", "oranges", "blueberries"] b) Fruity.append("kiwis") c) Fruity.sort() print(Fruity)

myList = [4, 5, 6, 1, 2, 3] a) Give code to sort, then print the list. b) Give code to print the maximum of the list.

a) myList.sort() print(myList) b) print(max(myList))

s1 = {1, 4, 5, 6} s2 = {1, 3, 6, 7} a) print(s1.union(s2)) b) print(s1 & s2) c) print(s1.difference(s2))

a) {1, 3, 4, 5, 6, 7} b) {1, 6} c) {4, 5}

Which method below would be considered a mutator method?

addItem()

Which statement correctly creates a set named colors that contains the 7 colors in a rainbow?

colors = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"}

Which of the following method header represent a constructor?

def __init__(self):

Which of the following code segment prints red is a color of the rainbow when the set colors contains the string "red"?

if "red" in colors: print("red is a color of the rainbow")

The ________________ of two sets is a set that contains only the elements that appear in both sets.

intersection

Write a statement that will add up row 1 of a 4 x 3 two-dimensional list.

l = list sum = l[0][0] + l[0][1] + l[0][2]

Give python code to create a 2D list with 3 rows and 2 columns. Fill it with zeros.

my2D = [[0, 0], [0, 0], [0, 0]]

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

numflags = len(flags)

Give code to create a set containing the numbers 2, 4, 6, 8, create a sorted version of a set, and print that version.

nums = {2, 4, 6, 8} sortedNums = nums.sort() print(sortedNums)

The term ______________ refers to an object's ability to take different forms.

polymorphism

Assume that a dictionary has been initialized as shown below: fruit = {"Apple": "Green", "Banana": "Yellow", "Plum": "Purple"} which statement prints the color of a banana?

print(fruit["Banana"])

The following two objects are created from the Counter class: studentCounter, and teacherCounter to represent the total number of students and 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 objects instance variable after the following code snippet executes? studentCounter.add() teacherCounter.add() studentCounter.add()

studentCounter : 2, teacherCounter : 1

Give a code segment that will read numbers from a file infile = open("theinfo.txt", "r"). The segment should find the total of the numbers and print the results.

total = 0 num = infile.readline() while num != "": num = int(num.rstrip("\n")) total = total + num num = infile.readline() print(total)

Suppose the input file contains the line of text Hello, World!. What are the values of word1 and word2 after this code executes? line = line.rstrip() Parts = line.split(",") word1 = Parts[0] word2 = Parts[1]

word1 = "Hello" word2 = "World"

Assume a class exists named Fruit. Which of the follow statements constructs an object of the Fruit class?

x = Fruit()

Which of the following is a possible output after the following code snippet is executed? names = set(["Jane", "Joe", "Amy", "Lisa"]) names.add("Amber") names.add("Zoe") names.discard("Jim") print(names)

{'Jane', 'Joe', 'Zoe', 'Lisa', 'Amber', ' Amy'}


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

Public Speaking Final Review UNWSP

View Set

IGGY Chapter 08: Concepts of Care for Patients at End of Life

View Set

Chapter 4 - Review of Essential Terms and Concepts

View Set