ISE 135 Midterm 2
Consider the following code snippet. import pandas as pd courses = ['ise135', 'ise311', 'ise361', 'ise408'] myCoutses = pd.DataFrame[courses] Its execution creates a DataFrame from the list, courses. Select one: True False
False
Consider the following code snippet. import numpy as np a = [2, 4, 6] b = [1, 2, 3] c = a/b print(c) Its execution will produce the following output: c = [2 2 2] Select one: True False
False
Consider the following code snippet. import numpy as np a =np.array([3, 1, 2]) b = np.array([False, 0, 3]) c = a + b print(c) Its execution will producethe following output: [3 1 5] Select one: True False
False
Consider the following code snippet. import numpy as np a =np.array([True, 1, 2]) b = np.array([False, True, 3]) c = a + bprint(c) Its execution will produce an error Select one: True False
False
Consider the following code snippet. import numpy as np a =np.array([[1, 3, 5], [7, 9, 2], [4, 6, 8]]) print(a[1][0:2]) Its execution will produce the following output: [7 9 2] Select one: True False
False
Consider the following code snippet. import numpy as npa = [2, 4, 6]b = [1, 2, 3]c = a/bprint(c) Its execution will produce the following output: c = [2 2 2] Select one: True False
False
Consider the follwoing code snippet. Suppose "MyMatrix.txt" is in the same folder as this program. import numpy as np a=np.genfromtxt("MyMatrix.txt", skip_header=0) print(a) Running this program produces an error. Select one: True False
False
import pandas as pd records = pd.read_csv("Records.csv") print(records) Consider the above three-line code snippet where its execution generates the following output: Students course Grade 0 Tom ise135 88 1 John ise311 76 2 Sue ise361 93 3 Jeel ise408 83 Its execution after replacing the last line with the following line will cause an error. print(list(records)) Select one: True False
False
import pandas as pd records = pd.read_csv("Records.csv") print(records) Consider the above three-line code snippet where its execution generates the following output: Students course Grade 0 Tom ise135 88 1 John ise311 76 2 Sue ise361 93 3 Jeel ise408 83 Its execution after replacing the last line with: print(head(2)) will print : Students course Grade 0 Tom ise135 88 1 John ise311 76 Select one: True False
False
import pandas as pd records = pd.read_csv("Records.csv") print(records) Consider the above three-line code snippet where its execution generates the following output: Students course Grade 0 Tom ise135 88 1 John ise311 76 2 Sue ise361 93 3 Jeel ise408 83 Its execution after replacing the last line with: print(records['Students'][2]) John Select one: True False
False
import pandas as pd records = pd.read_csv("Records.csv") print(records) Consider the above three-line code snippet where its execution generates the following output: Students course Grade 0 Tom ise135 88 1 John ise311 76 2 Sue ise361 93 3 Jeel ise408 83 Its execution after replacing the last line with: row2 = records.iloc[2] print(row2) will print: Students John course ise311 Grade 76 Name: 1, dtype: object Select one: True False
False
Consider the following code snippet. import numpy as np a =np.array([[1, 3, 5], [7, 9, 2], [4, 6, 8]]) print(a.reshape(1,9)) Its execution will produce the following output: [[1 3 5 7 9 2 4 6 8]] Select one: True False
True
Consider the following code snippet. import pandas as pd records = {'Students': ['Tom', 'John', 'Sue', 'Jeel'], 'course' : ['ise135', 'ise311', 'ise361', 'ise408'], 'Grade' : [88, 76, 93, 83]} rd = pd.DataFrame(records) print(rd) Its execution generates the following output: Students course Grade 0 Tom ise135 88 1 John ise311 76 2 Sue ise361 93 3 Jeel ise408 83 Select one: True False
True
Consider the following code snippet. import numpy as np a = [2, 4, 6] b = [1, 2, 3] c = a + b print(c) Its execution will produce the following output: c = [2, 4, 6, 1, 2, 3] Select one: True False
True
Consider the following code snippet. import numpy as np a = np.arange(9) a = a.reshape((3,3)) print(a) Its execution will produce the following output: a = [[0 1 2] [3 4 5] [6 7 8]]
True
Consider the following code snippet: import numpy as np x = np.eye(3) print(x) Its execution will produce: [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] Select one: True False
True
List holds different data types Select one: True False
True
import pandas as pd records = pd.read_csv("Records.csv") print(records) Consider the above code snippet where its execution generates the following output: Students course Grade 0 Tom ise135 88 1 John ise311 76 2 Sue ise361 93 3 Jeel ise408 83 Exchanging the second line with the following line will use column Students as the index. records = pd.read_csv("Records.csv", index_col = "Students") Select one: True False
True
import pandas as pd records = pd.read_csv("Records.csv") print(records) Consider the above three-line code snippet where its execution generates the following output: Students course Grade 0 Tom ise135 88 1 John ise311 76 2 Sue ise361 93 3 Jeel ise408 83 Its execution after replacing the last line with: print(records['Students'][1]) will print: John Select one: True False
True
import pandas as pd records = pd.read_csv("Records.csv") print(records) Consider the above three-line code snippet where its execution generates the following output: Students course Grade 0 Tom ise135 88 1 John ise311 76 2 Sue ise361 93 3 Jeel ise408 83 Its execution after replacing the last line with: row2 = records.iloc[2] print(row2) will print: Students Sue course ise361 Grade 93 Name: 2, dtype: object Select one: True False
True
import pandas as pdrecords = pd.read_csv("Records.csv")print(records) Consider the above code snippet where its execution generates the following output: Students course Grade 0 Tom ise135 88 1 John ise311 76 2 Sue ise361 93 3 Jeel ise408 83 We can use any of the three columns, Student, course, or Grade as an index for the DataFrame. Select one: True False
True
In the following code snippet, what happens if the "output.txt" file does not exist? outfile = open("output.txt", "w") Select one: a. An empty file is created b. An error message is displayed c. Nothing, this statement is ignored d. A new file is created with at least one record
a. An empty file is created
Consider the following code segment: data = {"A": 65, "B": 66, "C": 67} print(data["Z"]) Select one: a. An error message indicating that an exception was raised. b. -1 c. 0 d. {}
a. An error message indicating that an exception was raised.
One key difference between a set and a list is: Select one: a. Set elements are not stored in any particular order b. List elements only contain string values c. List elements are not stored in any particular order d. Set elements can be accessed directly using their position
a. Set elements are not stored in any particular order
How do you add items to a dictionary? Select one: a. [] operator b. {} operator c. add method d. insert method
a. [] operator
What method is used to produce a new set with the elements that belong to the first set but not the second? Select one: a.difference() b.intersection() c.subset() d.inboth()
a. difference()
Which statement below can be used to read data from a file one character at a time? Select one: a.inputFile.read(1) b.inputFile.split(1) c.inputFile.get(1) d.inputFile.open(1)
a. inputFile.read(1)
What method is used to test if one set is contained entirely within another set in Python? Select one: a.issubset() b.inboth() c.isintersection() d.difference()
a. issubset()
Which of the following statements creates a dictionary with 4 entries? Select one: a. x = {"A": 1, "B": 2, "C": 3, "D": 4} b. x = {4} c. x = {"A": 1, "B": 2} d. x = dict(4)
a. x = {"A": 1, "B": 2, "C": 3, "D": 4}
In the code snippet below, if the file contains the following words: Monday! Tuesday. Wednesday? stored one per line, what would be the output? infile = open("input.txt", "r") for word in infile : word = word.rstrip(".!\n") print(word) Select one: a.Monday! Tuesday. Wednesday b.Monday Tuesday Wednesday? c.Monday Tuesday Wednesday d. Monday Tuesday Wednesday
b. Monday Tuesday Wednesday?
In the following code snippet, what happens if the "output.txt" file already exists? outfile = open("output.txt", "w") Select one: a. Nothing, this statement is ignored b. The existing file is emptied c. An error message is displayed d. Any new data is appended to the end
b. The existing file is emptied
The following code segment is supposed to read and display the contents of a file. What problem does it have in its current form? infile = open("test.txt", "r") line = infile.readline() while line != "" : print(line) line = infile.readline() infile.close() Select one: a. The program displays all of the lines, except for the first one b. The program displays the contents of the file, but it is double spaced c. The program displays all of the lines, except for the last one d. There is no problem -- the program works as desired
b. The program displays the contents of the file, but is double spaced
When reading a file in Python, you must specify two items: Select one: a. a file name and file properties b. a file name and mode c. a file name and size d. a file name and data type
b. a file name and mode
How do you remove items from a dictionary? Select one: a. delete method b. pop method c. get method d. remove method
b. pop method
The following code segment is supposed to read all of the lines from test.txt and save them in copy.txt. infile = open("test.txt", "r") outfile = open("copy.txt", "w") line = infile.readline() ____________________ outfile.write(line) line = infile.readline() infile.close() outfile.close() Select one: a. while line == "\n" : b. while line != "" : c. while line == "" : d. while line != "\n" :
b. while line != "" :
Suppose the input file contains a person's last name and age on the same line separated by a space. Which statements extract this information correctly? Select one: a. record = inputFile.readline() data = record.split() age = int(data[0]) name = data[1].rstrip() b. record = inputFile.read(1) data = record.split() name = data[0].rstrip() age = int(data[1]) c. record = inputFile.readline() data = record.split() name = data[0].rstrip() age = int(data[1]) d. record = inputFile.readline() name = record[0].rstrip() age = int(record[1])
c. record= inputFile.readline() data = record.split() name= data[0].rstrip() age=int(data[1])
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? Select one: a. names1 is a subset of names b. names is the union of names1 and names2 c. names2 is a subset of names d. names is the intersection of the sets names1 and names2
c. names2 is a subset of names
Before accessing a file, the program must: Select one: a. read the file b. name the file c. open the file d. close the file
c. open the file
Assume that outfile is a file object that has been opened for writing. Which of the following code segments stores Hello World in the file? Select one: a. outfile.write("Hello\n", "World\n") b. outfile.print("Hello\n", "World\n") c. outfile.write("Hello\nWorld\n") d. outfile.print("Hello\nWorld\n")
c. outfile.write("Hello\nWorld\n")
Which of the following methods provides a way to split the contents of the string sentence into a list of individual words? Select one: a. sentence.separate() b. sentence.splice() c. sentence.split() d. sentence.strip()
c. sentence.split()
What method can be used to combine two sets in Python? Select one: a. sunset() b. both() c. union() d. join()
c. union()
A set is a container that stores a collection of: Select one: a. models b. lists c. unique values d. operations
c. unique values
Which code segment creates a dictionary with keys that are integers and values that are lists? Select one: a. cards = dict() cards.put("Ace", "Spades", 1) cards.put("Two", "Spades", 2) b. cards = dict() cards = {"Ace", "Spades", 1) c ards = {"Two", "Spades", 2) c. cards = dict() cards.add("Ace", "Spades", 1) cards.add("Two", "Spades", 2) d. cards = dict() cards[1] = ["Ace", "Spades"] cards[2] = ["Two", "Spades"]
d. cards = dict() cards[1] = ["Ace", "Spades"] cards[2] = ["Two", "Spades"]
Which statement is most correct? Select one: a. Every value in a Python dictionary must have the same data type. b. A Python dictionary contains a list of all of Python's reserved words. c. Each value in a Python dictionary must be unique. d. A Python dictionary stores associations between keys and values.
d. A Python dictionary stores associations between keys and values.
What will be stored in substrings after the following code snippet has run? states = "Michigan,Maine,Minnesota,Montana,Mississippi" substrings = states.split(",") Select one: a. "Michigan" b. "Michigan,Maine,Minnesota,Montana,Mississippi" c. ["Michigan,Maine,Minnesota,Montana,Mississippi"] d. ["Michigan", "Maine", "Minnesota", "Montana", "Mississippi"]
d. ["Michigan" , "Maine" , "Minnesota" , "Montana" , Mississippi"]
Consider the following code segment: line = "hello world!" parts = line.split() print(parts) What is displayed when this code segment runs? Select one: a. hello,world! b. "hello", "world!" c. hello world! d. ["hello", "world!"]
d. ["hello","world!"]
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) Select one: a. apple pear banana b. apple, pear, banana c. apple pear banana d. apple pear banana
d. apple pear banana
Which operator tests to see if a key exists in a dictionary? Select one: a. has b. insubset c. contains d. in
d. in
After executing the following code snippet, what part is the file object? infile = open("input.txt", "r") Select one: a. "input.txt" b. input c. "r" d. infile
d. infile
Consider the following dictionary: favoriteFoods = {"Peg": "burgers", "Cy": "hotdogs", "Bob": "apple pie"} Select one: a. print("Peg's favorite food is: ", favoriteFoods(Peg)) b. print("Peg's favorite food is: ", favoriteFoods.get(1)) c. print("Peg's favorite food is: ", favoriteFoods[1]) d. print("Peg's favorite food is: ", favoriteFoods["Peg"])
d. print("Peg's favorite food is: ", favoriteFoods["Peg"])
Consider the following code segment: fruit = {"Apple": "Green", "Banana": "Yellow"} fruit["Apple"] = "Red" After it executes, what is the value of fruit? Select one: a. {"Apple": "Green", "Banana": "Yellow"} b. The fruit dictionary has no value because the program terminates with a runtime error c. {"Apple": "Green", "Banana": "Yellow", "Apple": "Red"} d. {"Apple": "Red", "Banana": "Yellow"}
d. {"Apple": "Red", "Banana": "Yellow"}
Which statement correctly creates a set named colors that contains the 7 colors in a rainbow? Select one: a.colors = ["red", "orange", "yellow" "green", "blue", "indigo", "violet"] b.colors = [red, orange, yellow, green, blue, indigo, violet] c.colors = {red, orange, yellow, green, blue, indigo, violet} d.colors = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"}
d.colors = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"}
You are creating a program that includes a dictionary where the keys are people's names and the values are their favorite foods.Which of the following statements adds an entry to the dictionary that indicates that Ravi's favorite food is chocolate? Select one: a. favoriteFoods["Ravi"] = "chocolate" b. favoriteFoods["Ravi"] = "chocolate" c. favoriteFoods = {"Ravi", "chocolate"} d. favoriteFoods.add("Ravi", "chocolate")
favoriteFoods["Ravi"] = "chocolate"
Which statement creates an empty set and stores it in x? Select one: a. x = () b. x = [] c. x = {} d. x = set()
x= set()