Unit 2 python example quiz that helps he get a 100

¡Supera tus tareas y exámenes ahora con Quizwiz!

"w+" mode opens a file for write plus read, such as in the following Python code: schools = open("school_names.txt", "w+") A True B False

A

.readline() is used to read one line, as a string, from an open text file. A True B False

A

A file opened in read mode cannot be changed (we cannot write to the file). A True B False

A

Given that the file school_names.txt, contains the following text with 3 school names: City Elementary Maths High School Early Learner PreSchool if we run the following code: schools = open("school_names.txt", "w+") Which item is Not True? A The file school_names will contain the original 3 school names. B The file school_names will be erased and empty. C We can now read to the school_names by using schools.readline(). D schools.read() will return an empty string.

A

In the for loop below, the variable "letter" 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) A True B False

A

The .append() method adds items to the end of a list. A True B False

A

The .count() string method returns the number of times a a character appears in a string. A True B False

A

The first character in a string is at index 0. A True B False

A

The python code: list_name.extend(another_list) appends another_list to list_name A True B False

A

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() A True B False

A

To move the pointer 5 characters past the current pointer location, what should you use? A .seek(5, 1) B .seek(5, 2) C .seek(5, 0) D all of these

A

To move the pointer in an open file to the first character, what method should you use? A .seek(0) B .seek(-1) C reset(filename) D all of these

A

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

Which best describes a valid Python use best practice of the following list named days which contains a human count of 6 items? A days.append("Wednesday") B days.insert(4, "January") C days.append("the day of week") D none of these

A

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

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

A

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"

A

Which choice will print the second half of the word "Consequences" (uences), 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])

A

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

A

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

A

Which is valid Python code that will print "Red", 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])

A

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

A

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("H"): print(city) elif city.startswith("D"): print(city) A São PauloDubai B Dubai Hyderabad C São Paulo D none of these

B

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 lis

B

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

B

Given the following code, which choice is best to represent the first line in the text file books.txt in Title format, where every word is capitalized? books = open('books.txt', 'r') A print(books[0].upper()) B print(books.readline().title()) C print(books.readline().upper()) D all of these

B

Given the following starting code: test1 = open('test1.txt', 'r') We can read 10 characters from a file into memory by using which Python code? A ten_char = test1.read(11) B ten_char = test1.read(10) C ten_char = test1[0:11] D all of these

B

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

B

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 best use variable name such as "pupil". students = ["Alton", "Hiroto", "Skye", "Tobias"] for student in students: print("Hello", student) A True B False

B

The .insert(index, datavalue) method inserts the datavalue item after the given index value. A True B False

B

The .pop() method only deletes an item from a list. A True B False

B

The first item in a Python List is located at indexnumber 1. A True B False

B

The following code will print "act". word = "characteristics" print(word[4:6]) A True B False

B

To read each line of a file as an item in a list, use .listread() method. A True B False

B

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

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

B

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

B

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":

B

Which is an invalid syntax to initialize a Python list named scores? A scores = [97.3, 89.4, 67.0, 91.2] B listdef scores(11, 8, 0, 24, 31, 12, 19) C scores = [] D scores = [2, 2, 2, 3, 3, 1, 1]

B

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

C

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

C

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

What is the best choice to complete the following sentence? Using .readlines() to read an open file results in.. A reading one line at a time. B reading the entire file as a string. C reading each line in the file as an item in a list. D Menu: Cell... > Run Cells.

C

When using .readlines(), it is common to remove the last character of each string in the generated list, because... A .readlines() adds a character that cannot be processed. B the last character is always duplicated when using .readlines(). C the last character is typically a whitespace newline character, "\n". D all of these

C

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

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

Which answer best describes what .strip() does in the following code sample? poem_line = poem1.readline().strip() A .strip() removes leading and trailing parenthesis B .strip() removes all whitespace in a string, including the '\n' formatting character C .strip() removes leading and trailing whitespace, including the '\n' formatting character D all of these

C

Which best describes the result of the following code? days = [12, 9, "Monday", 1] days.append("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 end of the list D all of these

C

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 integer in numbers: D all of these

C

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

C

Which is the best choice to complete the following sentence? Opening a file in Python using read mode ('r')... A reads the file as a string 10 characters at a time. B creates a new file. C makes file contents available for Python to read from the file. D erases all of the contents of the file being opened.

C

Which print output is the result of the following code? word = "windows" print(word[-3]) A "w" B "s" C "o" D "" (empty string)

C

After we open a file (first_test) using: test1 = open('test1.txt', 'r') we can read the file into memory with which Python code A for test_data in test1: B test_data.open.read(test1) C test_data = open(test1) D test_data = test1.read()

D

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

D

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

D

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

Given that the file school_names.txt, contains the following text: *City*Elementary* *Maths*High*School* *Early*Learner*PreSchool* Which answer best describes the output of the following code? schools = open("school_names.txt", "r") name = schools.readline().strip('*\n') while name: if name.startswith("M"): print(name) else: pass name = schools.readline().strip('*\n') A Maths High School B *Maths*High*School* C MathsHighSchool D Maths*High*School

D

The .close() method conserves system memory because... A file .close() compresses files by 50%. B file .close() closes any unused files created by Python. C file .close() permanently deletes all text data in the target file. D the file .close() method removes the reference created from file open() function.

D

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

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

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

D

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' and 'c') B ltr_cnt = test_string.count('c') C ltr_cnt = test_string.count('C') D none of these

D

Which statement Does Not describes a String in Python/Computer Science? A a sequence of characters B a series of 1's and 0's C a set of characters which are represented in/from the ASCII table by holding hte Alt- plus 4 digit decimal number D object existing in four dimensions

D

Which string method below returns a boolean datavalue? A .find() B .count() C .isalpha() D while

c


Conjuntos de estudio relacionados

Micro Final (Exercises, Tests and Quizzes)

View Set

2nd half: section 9 Loan Qualification & Financing in. WA

View Set

Chapter 5: Understanding Metrics

View Set

Chapter 16 Therapeutic Relationships intro to OT

View Set

Insurance Property and Casualty test ver

View Set

Chapter 15 & 16 Physical, Cognitive, Developmental, and Emotion Psych of Middle Adulthood

View Set

Chapter 3: Classification and Diagnosis

View Set

Dilemmas of Democracy, The Constitution and Federalism [chapt. 1-3]

View Set