Python Unit 2 mod 1-4

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

runs command interface supporting curl

!

Which choice best represents the output of the following code? student_name = "iteration" count = 0 for letter in student_name: if letter.lower() == "e": print(count) count += 1 A "2" B "3" C "-4" D "-5"

"2"

3. Choose the correct print output for the following code: name = "Alton" print(name[::2]) A "Atn" B "lo" C "on" D "Al"

"Atn"

Which choice best represents the output of the following code? student_name = "iteration" new_word = "" for letter in student_name[:3]: new_word += letter print(new_word) A "ration" B "ite" C "iter" D "ation"

"ite"

Which print output is the result of the following code? A "w" B "s" C "i" D "" (empty string)

"s"

Which best describes the result of the following code? days = [12, 9, "Monday", 1] days.remove("Friday") A ValueError: list.remove(x): x not in list because "Friday" was not found and can't be removed B nothing happens, because "Friday" was not found and can't be removed C "Friday" is added to the list D all of these

A ValueError: list.remove(x): x not in list because "Friday" was not found and can't be removed

What is the best prediction for the output of the following Python code using .join()? wisdom_list = ["mistakes", "are", "a", "part", "of", "learning"] wisdom_string = "()".join(wisdom_list) print(wisdom_string) A mistakes()are()a()part()of()learning B mistakes are a part of learning C ()mistakes are a part of learning D wisdom_list

A mistakes()are()a()part()of()learning

Which best describes the result of the following code? numbers = [2, 3, 2] total = 0 while numbers == true: total += numbers.pop() A numbers becomes an empty list and total is 7 B numbers is [2, 3, 2] and total is 7 C infinite loop D all of these

A numbers becomes an empty list and total is 7

Which Python code results in reversing the order of the scores list permanently until reversed again or reinitialized? scores = [ 0, 5, 2, 11, 1, 9, 7] A scores[-1] B reverse(scores) C scores.reverse() D all of these

C scores.reverse()

Choose the best representation of the output expected for the following code. numbers = "" for x in range(7): numbers += str(x) print(numbers) A 0 B 7 C 1234567 D 0123456

D 0123456

Choose the best representation of the output expected for the following code. numbers = "" for x in range(2,8,2): numbers += str(x) print(numbers) A 2 B 8 C 2468 D 246

D 246

What is the best prediction for the output of the following Python code using .split()? code_tip = "-PythonO-usesO-spacesO-forO-indentation" tip_words = code_tip.split('O') print(tip_words) A Python uses spaces for indentation B -Python -uses -spaces -for -indentation C ['O-Python', 'O-uses', 'O-spaces', 'O-for', 'O-indentation'] D ['-Python', '-uses', '-spaces', '-for', '-indentation']

D ['-Python', '-uses', '-spaces', '-for', '-indentation']

Which best describes a valid Python use of the following list named days which contains a human count of 6 items? A days.append("Wednesday") B days.insert(4, "Wednesday") C print(days[5]) D all of these

D all of these

Which best describes the valid Python syntax use of the following days list? A days.append("Wednesday") B days.append(14) C print(days[-1]) D all of these

D all of these

Which code will result in ltr_cnt = 2 for counting the letter "c" found in the following test_string? test_string = 'Mexico City' A ltr_cnt = test_string.count('C' or 'c'') B ltr_cnt = test_string.count('c') C ltr_cnt = test_string.count('C') D none of these

D none of these

Which code places another integer, 2, to the end of the following list? numbers = [1, 1, 2] A numbers + 2 B numbers.add(2) C numbers += 2 D numbers.append(2)

D numbers.append(2)

Choose the item that best completes the following sentence: "After using a .insert method on a Python list..." A one of the index datavalues gets overwritten. B an error occurs if the inserted object is not of the same type of the other list items. C the length of the list remains the same. D the length of the list increases by 1.

D the length of the list increases by 1.

Which Python code results in a list called tip_list being equal to ['Strings', 'are', 'a', 'sequence', 'of', 'characters.'] Given tip = 'Strings are a sequence of characters.' A tip_list = tip[0:-1] B tip_list.split(tip) C tip_list = list(tip) D tip_list = tip.split()

D tip_list = tip.split()

In the following for loop code, the variable name is "student" because it can only be called "student" and cannot be replaced with another valid variable name such as "pupil". students = ["Alton", "Hiroto", "Skye", "Tobias"] for student in students: print("Hello", student)

False

In the for loop below, the variable "word" is an arbitrary loop control variable name. Any valid variable name can be used since its value is assigned only within this loop. See code example below: word = "the word of the day" for letter in word: print(letter)

False

The .append() method adds items to the beginning of a list.

False

The .count() string method returns the number of times a for/in loop iterates.

False

The first character in a string is at index 1.

False

who's gonna fail?

alana

enables curl that can download files

curl

name curl will give the file

poem1.txt (can be anything in replace of poem)

Which is valid Python code that will print "Green", given the following code? colors = ["Red", "Blue", "Green", "Yellow"] A print(colors[0]) B print(colors[4]) C print(colors[3]) D print(colors[2])

print(colors[2])

Choose the statement that will print the letter "d", given the variable declaration word = "windows" print(word[-2]) B print(word[-1]) C print(word[2]) D print(word[3])

print(word[3])

Which choice will print the first half of the word "Consequences" (Conseq), given the following variable declaration? word = "Consequences" A print(word[6:]) B print(word[:6]) C print(word[1:7]) D print(word[7:1])

print(word[:6])

Which choice will print the word "tac", given the following variable declaration? word = "cat" A print(word[::-1]) B print(word[::1]) C print(word[0:1:2]) D print(word[2:1:0])

print(word[::-1])

Which is valid syntax to initialize a Python list? A scores = [11, 8, 0, 24, 31, 12, 19] B listdef scores(11, 8, 0, 24, 31, 12, 19) C scores.list(11, 8, 0, 24, 31, 12, 19) D scores = {11:8:0:24:31:12:19}

scores = [11, 8, 0, 24, 31, 12, 19]

Which statement best describes a String in Python/Computer Science?

Sequence of characters

open file at the end and append to it

'a'

read only no writing at all

'r'

read and overwrite data

'r+'

delete contents and start writing only

'w'

tells curl write data to a file

-o

to free system resources

.close

Which string method returns an index of first character or substring that matches the methods parameter? A .find() B .count() C .isalpha() D while

.find()

creates an object that can be addressed in python code

.open()

loads the content of the file into memory as a string, including formatting such as new line (\n)

.read()

to read data from file, one line at a time

.readline()

to read text from files as a list of lines

.readlines()

to set file read or write location

.seek()

to remove new line characters and other whitespaces

.strip()

Choose the best representation of the output expected for the following code. cities = ["New York", "Shanghai", "Munich", "Tokyo", "Dubai", "Mexico City", "São Paulo", "Hyderabad"] for city in cities: if city.startswith("P"): print(city) elif city.startswith("D"): print(city) A São PauloDubai B Dubai C São Paulo D none of these

B Dubai

Choose the item that best completes the following sentence: "A Python list..." A uses index 1 for the first item in the list. B can contain a mix of strings or numbers as list items. C can contain only all strings or only all numbers as list items - cannot contain a mix. D cannot contain another list - a list in a list.

B can contain a mix of strings or numbers as list items.

Which code overwrites or replaces the "0" with a "5" in the following list named numbers? numbers = [1, 1, 0] A numbers[3] = 5 B numbers[2] = 5 C numbers.append(5) D numbers.insert(2, 5)

B numbers[2] = 5

Which Python code results in output of a sorted scores list from small to big, with changing the original scores list to the sorted order? scores = [ 0, 5, 2, 11, 1, 9, 7] A scores.reverse() print(scores) B scores.sort() print(scores) C sorted(scores) print(scores) D all of these

B scores.sort() print(scores)

Choose the best representation of the output expected for the following code. numbers = "" for x in range(0,5,2): numbers += str(x) print(numbers) A 0 B 5 C 024 D 24

C 024

Choose the item that best completes the sentence: "A Python list..." A cannot be changed after initialization. B can add items of the same datatype only. C can add items to the end of the list using the .append() method or the .insert(-1, datavalue). D always appends to index 0 (zero) - [0] no matter how many items are in the list.

C can add items to the end of the list using the .append() method or the .insert(-1, datavalue).

Which code is the best use of Python that deletes the integer, 5, in the following list named numbers? numbers = [7, 1, 5, 8, 0] A numbers[3] = "" B numbers[2].delete() C del numbers[2] D numbers.remove(2)

C del numbers[2]

Which code is the best use and practice of Python to iterate through the list "numbers"? numbers = [7, 1, 5, 8, 0] A for range[5] in numbers: B for int in list: C for number in numbers: D all of these

C for number in numbers:

Which Python code results in output of a sorted scores list from small to big, without changing the original scores list defined below? scores = [ 0, 5, 2, 11, 1, 9, 7] A print(scores) B print(scores.sort()) C print(sorted(scores)) D all of these

C print(sorted(scores))

What will this output be: poem_file = open('poem1.txt', 'r') poem_10char = poem_file.read(10) print(poem_10char) poem_10char

Loops I re 'Loops I re'

In Python: list(range(0,5)) is equivalent to the list [0, 1, 2, 3, 4].

True

The .pop() method both returns for storage as an assignment and deletes an item from a list.

True

The first item in a Python List is located at index 0 (zero) - [0].

True

The following code will print "act". word = "characteristics" print(word[4:7])

True

The .insert(index, datavalue) method overwrites or replaces the list item at the given index.

false

The python code: list_name.extend(another_list) creates 2 new lists one called list_name and the other called another_list

false

Which is NOT a start to a string iteration loop, given the following string variable assignment? word = "health" A for letter in word: B for word in letter: C for ltr in word[1:]: D for part in "health":

for word in letter:

number of characters to read

n

To convert a string called line1 that is a sentence made up of words, into a list named words made of line1's words, we can use the .split() method and set the return value to a variable called words. See example below: words = line1.split()

true

Which parameter choice passed to the .find() string method will return -1, given the following variable assignment? word = "Python" A word.find("P") B word.find("y") C word.find("Z") D word.find("o")

word.find("Z")

Which choice will store the length of the given string variable named 'word' in the integer variable named 'x'? word = "Python" A x = len(word) B x = word.len() C x.length(word) D for x in length of word

x = len(word)


Kaugnay na mga set ng pag-aaral

Missed questions practice test #2

View Set

how to think like a computer scientist ch. 2 questions

View Set

Taxation of Life Insurance & Annuities

View Set

Unit 2 New and Improved Chem1040

View Set

ECON 301 Ch 3 Interest Rates and Rates of Return

View Set

The History and Arrangement of the Periodic Table

View Set