Python for STEM Module 5 Quiz Questions

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

(3) Given the following String: answer = "The answer to life, the universe, and everything is 42!" Match the code with the value returned.

"42" in answer = ? "A" in answer = ? answer.count("a") = ? answer.count("A") = ? answer.find(",", 33) = -1 answer[answer.find('4'):answer.find('!')] = 42 answer.find(",", 18) = 18 answer.find(",", 19) = 32

(1) Match the following function with the best description on what it does. Assume the variable f exists, and has been initialized correctly for the methods that use it.

(1) reads the entire contents of the file into a single String. = f.read() (2) uses the file, and reads it in based on comma splitting lines = (3) opens the file, "CatSpieces.csv", stores in f, but file reference only exists as long as code is indented under the command = with open("CatSpieces.csv", "r") as f: (4) reads the entire content of the file, storing each line as a value in a list. = f.readlines() (5) opens the file, "CatSpieces.csv" and stores a reference to that file in variable f = (6) reads a single line until the line end, and stores it as a String = f.readline()

(1) given the following code, place it in an ordering that would make sense/be valid. The correct order should generate the output Line count: 3 Average: 2.0 Total: 6 The final contents are: Scores,Student 2,Amy 2,Rory 2,Claire There is only one correct ordering, as long you follow the comment on the two lines we give.

1) total = 0 count = 0 # You should place this line first 2) with open('grades.csv', 'r') as csvfile: 3) grades_reader = csv.reader(csvfile, delimiter=',') 4) first_row = True # this is the fourth line, even though it could be the third 5) for row in grades_reader: 6) if first_row: 7) first_row = False 8) Continue 9) score = int(row[0]) 10) total += score count += 1 11) print("Line count: {}".format(count)) print("Average: {}".format(total/count)) print("Total: {}".format(total))

(2) What is printed, given the following code: def some_formula(lst): sum = 0 for val in lst: sum += val return int(sum/len(lst)) print(some_formula([2,5,5,2]))

3

(3) What is printed, given the following code: def some_formula(lst): sum = 0 for val in lst: sum += val return int(sum/len(lst)) print(some_formula([6,10,1,0]))

4

(1) What is printed, given the following code: def some_formula(lst): sum = 0 for val in lst: sum += val return int(sum/len(lst)) print(some_formula([8,1,6,6]))

5

(2): Analyze the code below and select the option that correctly describes what function1 does. def function1(lst): newLst = [] for i in range(len(lst)): if lst[i] > 0 and lst[i] % 3 == 0: newLst.append(i) return newLst print(function1([-3, 4, 6, 18, -12]))

Function1 returns a list of indexes of the positive numbers that are divisible by 3.

(1) What is the correct output for the code below? lst = [2,3,1,5,4] lst.pop(1) lst[1] = 100 lst.append(44) lst.remove(4) print(lst)

[2, 100, 5, 44]

(2) What is the correct output for the code below? lst = [2,3,1,5,4] lst.append(44) lst.pop(len(lst)-2) lst[4] = 100 lst.remove(1) lst.append(lst[1]+lst[len(lst)-1]) print(lst)

[2, 3, 5, 100, 103]

(1) Analyze the code below and select the correct output if that code is executed. def function1(lst, value): for elem in lst: if elem == value: return True return False def function2(lst): lst2 = [ ] for elem in lst: if function1(lst2, elem) == False: lst2.append(elem) return lst2 lst = [7,1,3,4,2,3,1] print(function2(lst))

[7, 1, 3, 4, 2]

(2) Given the following String: aiea = "Aiea;21°23'10.817\"N;157°55'31.929\"W" Match the code with the value returned.

aiea[aiea.find(";"):aiea.find(";", aiea.find(";") + 1)] = ;21°23'10.817"N aiea[aiea.find(";", aiea.find(";") + 1)+1:] = 157°55'31.929"W aiea[:aiea.find(";")] = Aiea

(1) Given the following String: foco = "Fort Collins;40°35'6.936\"N;105°5'3.9228\"W" Match the code with the value returned.

foco[foco.find(";") + 1:foco.find(";", foco.find(";")+1)] = 40°35'6.936"N foco[:foco.find(";")] = Fort Collins foco[foco.find(";", foco.find(";")+1):] = ;105°5'3.9228"W

(1) Given the following file: Doctor,Companion,Dead or Alive 1st,Sara Kingdom,Dead 9th,Jack Harkness,Alive 10th,Astrid Peth,Dead 11th,Amy Pond,Alive 12th,Clara Oswald,Dead given the following code import csv with open('companions.csv', 'r') as csvfile: grades_reader = csv.reader(csvfile, delimiter=',') line = -1 for row in grades_reader: line += 1 if line % 2 == 0: continue print("The {} Doctor's companion, {} is {}".format(row[0], row[1], row[2])) Match the output with the best description.

is printed first = The 1st Doctor's companion, Sara Kingdom is Dead is printed second = The 10th Doctor's companion, Astrid Peth is Dead is printed third = The 12th Doctor's companion, Clara Oswald is Dead is not printed = The 11th Doctor's companion, Amy Pond is Alive

(1) Match the correct pair of "lst" values with the output of the function below: def doSomething(lst): sum = 0 for elem in lst: if elem % 2 == 0: sum += elem return sum print(doSomething(lst))

lst = [13, 20, 5, 7, 8] = 28 lst = [1, 2, 3, 4, 5] = 6 lst = [33, 22, 11, 8, 7] = 30 lst = [40, 20, 10, 20, 15] = 90

(2) Match the "lst" initialization with the correct output for this program. def function1(lst): return max(lst) def function2(lst, value): return lst.index(value) #lst needs to be initialized here value = function1(lst) print(function2(lst, value))

lst = [13, 200, 12, 44, 201] = 4 lst = [33, 100, 234, 12, 21] = 2 lst = [130, 20, 12, 44, 2] = 0 lst = [11, 22, 2, 1, 21] = 1


Set pelajaran terkait

MICRO ECONOMICS (Chapters 10-11, 21 & 13)

View Set

Mental Dental Complete NBDE part II

View Set

National Economy Chapter 13 questions and answers

View Set

AJ1 Alkotmány, Alkotmánytörténet

View Set

Test 2 - Chapter 7 - T-Cell Development

View Set