Python Unit 8 Test Project Stem

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Consider the following code: def tryIt(s): temp = "" for i in range(len(s)): if(s[i] == "e"): temp = temp + "X" elif(s[i] == "t"): temp = temp + "2" else: temp = temp + s[i] print (temp) # ********** MAIN ********** f = "to be or not to be" tryIt(f) What is output?

2o bX or no2 2o bX This code will replace every "e" character with the "X" character and every "o" character with the "2" character

Consider the following code: def mystery (a): for i in range(len(a)): if (i % 2 == 1): print(a[i], end = " ") # ********** MAIN ********** s = [63, 72, 21, 90, 64, 67, 34] mystery(s) What is output?

72 90 67 This code prints out the elements at indices where the index mod 2 is equal to 1. In other words, it prints the elements at odd indices.

Consider the following code: h = [3, 9, 5, 1, 0, 6, 7, 9, 8, 3, 4, 6] print (h[1]) What is the output?

9 9 is the element at index 1 in the list h

Which of the following statements is false? 1. An index of a list can only be integers 2. An element refers to actual data within the list while an index refers to a memory location. 3. An index refers to actual data within the list while an element refers to a memory location. 4. An element of a list can be accessed using an index

An index refers to actual data within the list while an element refers to a memory location. This statement should be reversed to be true

What is true about a function?

It is a collection of commands that are given a name. A function defines a name for a collection of instructions that the program will execute.

Consider the following code: def mystery (a): for i in range(len(a)): if (i % 2 >= 0): print(a[i], end = " ") # ********** MAIN ********** stuff = ["Juneau", "Atlanta", "Helena", "Madison", "Annapolis", "Topeka"] mystery(stuff) What is output?

Juneau Atlanta Helena Madison Annapolis Topeka The conditional checks if the modular division of the indices by 2 is greater than equal to 0. Since any number mod 2 is either 0 or 1, this will print out every element in the list.

Consider the following code: def mystery (a): for i in range(len(a)): if (i % 2 == 0): print(a[i], end = " ") # ********** MAIN ********** stuff = ["Juneau", "Atlanta", "Helena", "Madison", "Annapolis", "Topeka"] mystery(stuff) What is output?

Juneau Helena Annapolis This is printing the elements at even indices which is what the if condition is checking for. Remember that list indices start at 0.

Which of the following statements are true? 1. Sorting algorithms can only sort lists in ascending order 2. Sorting algorithms are used to find a particular element in the list 3. Searching algorithms can be used to sort a list 4. None of the above

None of the above

Which of the following statements is true about algorithms?

Searching for an element can be done using one loop It only takes one loop to check all the elements

Consider the following code: tests = [78, 86, 83, 80, 89, 92, 91, 94, 67, 72, 80, 95] c = int(input("Cutoff value: ")) n = 0 for i in range(len(tests)): if (tests[i] > c): n = n + 1 print("Values above " + str(c) + ": " + str(n)) What is output when the user enters 80?

Values above 80: 7 This code counts the number of elements greater than the input number. Since the user enters 80, the program outputs the number of elements greater than 80, which is 7.

Consider the following code: def mystery (a): for i in range(len(a)): j = a[i] a[i] = j.replace("A", "@") j = a[i] a[i] = j.replace("a", "A") # ********** MAIN ********** stuff = ["Juneau", "Atlanta", "Helena", "Madison", "Annapolis", "Topeka"] mystery(stuff) print(stuff) What is output?

['JuneAu', '@tlAntA', 'HelenA', 'MAdison', '@nnApolis', 'TopekA'] The "A" characters have been replaced with "@", and then "a" characters are placed with "A".

Consider the following code: s = [63, 72, 21, 90, 64, 67, 34] s.sort(reverse = False) print(s) What is output?

[21, 34, 63, 64, 67, 72, 90] This list is sorted and in descending order

Consider the following code: def mystery (a): for i in range(len(a)): a[i] = a[i] % 100 # ********** MAIN ********** ar = [84, 11, 67, 70, 93, 39, 46, 27] mystery(ar) print (ar) What is output?

[84, 11, 67, 70, 93, 39, 46, 27] This code will find the remainder of each element after dividing by 100. Since each element is less than 100, the list will be unchanged.

The ____________ method can be used to add elements in the middle of the list.

insert Used to add an element to a list at any position

Why do we use for loops with lists?

For loops let us access every element of a list. For loops that loop over the indices of a list lets us access each and every element.

The ___________ method removes elements from a list by the location, not the value.

pop Used to remove an element from a list based on the given index

Consider the following code that is supposed to find the total length of all the words in a list: stuff = ["dog", "cat", "frog", "zebra", "bat", "pig", "mongoose"] c = 0 for i in range(len(stuff)): c = c + stuff[i] print (c) What is the error in the code?

The statement inside the for loop should be "c = c + len(stuff[i])" This is the correct way of summing up the length of all the words in the list.

Which of the following can be used to create a list that has some initial values?

intializer list

The ___________ method removes the first occurrence of an element in a list.

remove This function will remove the first occurrence of the element in a list.

Consider the following list: stuff = ["DOG", "cAT", "fRoG", "zEbrA", "BAT", "PIg", "mongoose"] for i in range(len(stuff)): print (______) To print the words in the lists in all LOWERCASE, you would replace the ______________ with:

stuff[i].lower() This will make all the characters in each word lowercase


Ensembles d'études connexes

Adult Nursing - Chapter 39: Assessment of Musculoskeletal Function - PrepU

View Set

BUSINESS: Chapter 11: Corporate Dividends

View Set