1/94
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
A loop iterates 0 times. How many times does the loop header execute?
a. 0
b. 1
c. infinite
d. None of these
b. 1
Which of the following is True?
a. It is better to use "for" loop if you have prior knowledge of number of iterations
b. It is better to use "while" loop if have prior knowledge of number of iterations
c. It is always better to use "for" loops over "while" loops
d. It is always better to use "while" loops over "for" loops
a. It is better to use "for" loop if you have prior knowledge of number of iterations
What is an infinite loop?
a. A loop that terminates through a sentinel value
b. A loop that iterates a million times
c. A loop that never terminates
d. A loop that does not iterate
c. A loop that never terminates
The loop body is specified in a program by
a. using indentation.
b. using the end loop keyword at the end of the loop.
c. enclosing the loop in curly brackets.
d. using the end keyword at the end of the loop.
a. using indentation.
A loop iterates 15 times. How many times does the loop header execute?
a. 14
b. 15
c. 0
d. None of these
d. None of these, 16 times
What is a test expression (loop expression)?
a. An expression to calculate average
b. An expression to check if the loop will iterate or not
c. The statements in the body of the loop
d. None of these
b. An expression to check if the loop will iterate or not
What is the value of sliced?
a_list = [1, 2, 3, 4, 5]
sliced = a_list[0:1]
a. [1]
b. [1, 2]
c. 1
d. 1, 2
a. [1]
What is the value of sliced?
a_list = [1, 2, 3, 4, 5]
sliced = a_list[:15]
a. Error
b. [0]
c. [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
d. [1, 2, 3, 4, 5]
d. [1, 2, 3, 4, 5]
What is the index position of the last element of a list with a length of 15?
a. 14
b. 15
c. 16
d. None of these
a. 14
What is the length of the following list?
[10, 20, 30, 40, 50]
a. 3
b. 4
c. 5
d. None of these
c. 5
Which of the following statements will create a full duplicate copy of a_list?
a. copy1 = a_list[:]
b. copy1 = a_list[copy]
c. copy1 = a_list
d. None of these
a. copy1 = a_list[:]
What is true when passing a list by reference to another function?
a. It copies the original data of a list to the function
b. It makes a duplicate list and passes the original to the function
c. Any change made to the list in the function will be accessible in the calling function
d. None of these
c. Any change made to the list in the function will be accessible in the calling function
What is the functionality of the following program?
a_list.pop()
a. Removes all the elements from a_list
b. Removes the element at index 0 from a_list
c. Removes a random element from a_list
d. None of these
d. None of these, removes last
Which of the following is false about a Python list?
a. It is a container
b. It can only store values of the same datatype
c. It can be copied by reference
d. It has forward and reverse indexing available
b. It can only store values of the same datatype
Which built-in function can be used to insert elements from one list to another list?
a. insert
b. extend
c. append
d. None of these
b. extend
What is the index position of the first element of a list?
a. -1
b. 0
c. 1
d. None of these
b. 0
What is the output of the following program?
course = 'cp104' course = course.upper() print(course)
a. ERROR b. CP104 c. Cp104 d. Cp1
b. CP104
What is the output of the following program?
uni = "Wilfrid Laurier" print(uni[3])
a. 'fff' b. 'i' c. ERROR d. 'f' e. '' (empty string)
d. 'f'
What is the output of the following program?
string1 = 'aa' string2 = '' string3 = 'bb' print(string1 + string2 + string3)
a. ERROR b. 'aa bb' c. 'abab' d. 'aabb'
d. 'aabb'
What is the output of the following program?
string1 = 'responsibility' print(string1.rfind('i'))
a. 11 b. 7 c. -1 d. ERROR e. 0
a. 11
What is the output of the following program?
string1 = 'Good Afternoon' print(string1.split(' '))
a. 'Good Afternoon' b. ['Good', 'Afternoon'] c. 'Good' d. ERROR e. ['Good Afternoon']
b. ['Good', 'Afternoon']
What is the output of the following code?
string1 = 'Midterm' print(string1.count('m'))
a. 3 b. -1 c. 1 d. 2 e. 0
c. 1
What is the value of string1 after the following command?
string1 = '\tGood Afternoon '.strip()
a. 'Good Afternoon ' b. ERROR c. '\tGood Afternoon' d. 'Good Afternoon' e. 'GoodAfternoon'
d. 'Good Afternoon'
What is the output of the following program?
new_str = ''.join(['http://', 'www.', 'ebay', '.com']) print(new_str)
a. http://www.ebay.com b. http:// www. ebay .com c. ERROR d. ['http://www.ebay.com']
a. http://www.ebay.com
What happens after executing the following code?
uni = "Wilfrid Laurier" uni[-1] = 'R'
a. "Wilfrid LaurierR" b. ERROR c. "WilfridR LaurierR" d. "WilfriR LaurieR" e. "Wilfrid LaurieR"
b. ERROR (Python strings are immutable)
Which of the following will return False?
string1 = 'Good Afternoon'
a. string1.istitle() b. string1.isalnum() c. string1.isupper() d. string1.isalpha()
b, c, and d all return False (the question has more than one correct option)
What does the file.readlines() method return?
a. String b. Integer c. True/False d. List
d. List
Which mode opens a file for writing without overwriting it if it already exists?
a. r b. w c. a d. None of these
c. a
What is the functionality of the remove() method from the os module?
a. It deletes a binary file only. b. It deletes a file. c. It deletes a directory or a file. d. It deletes a directory.
b. It deletes a file
What is the main benefit of a with statement when working with a file?
a. It automatically closes the file. b. It automatically deletes a temporary file. c. It only works with the 'r' modifier. d. None of these.
a. It automatically closes the file
What does the file.read() method return?
a. Integer b. True/False c. List d. String
d. String
What is the data type of the value returned by the exists() method?
a. Integer b. String c. Float d. Boolean
d. Boolean
Which function can be used to create a portable path string?
a. portable() b. join() c. None of these d. path()
b. join()
Which type of files stores data in a human-readable format?
a. Text files b. Zip files c. Reading files d. Binary files
a. Text files
The file.read() method stops reading a file when it encounters:
a. } b. EOF c. \n d. .
b. EOF
Which function is used for opening a file?
a. read b. write c. read or write d. None of these
d. None of these (the function is open())
Given:
num_list = [[10, 20, 30], [40, 50, 60], [70, 80, 90], [100, 110, 120]]
Which command prints 80?
a. print(num_list[2][1]) b. print(num_list[3]) c. print(num_list[2][80]) d. print(num_list[7][0]) e. print(num_list[3][1])
a. print(num_list[2][1])
Given:
numList = [[10, 20, 30], [40, 50, 60], [70, 80, 90], [100, 110, 120]]
Which statement is true about print(len(numList[0]))?
a. It is equivalent to print(len(numList[1])). b. It is equivalent to print(len(numList)). c. It produces 3. d. It is equivalent to print(len([1, 2, 3, 4])). e. Both a and c.
e. Both a and c
What is the output?
numList = [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]] print(numList[-1])
a. 6 b. [6, 6, 6, 6] c. [[6, 6, 6, 6]] d. [4, 5, 6] e. [6]
b. [6, 6, 6, 6]
Which of the following is not a Python 2D list?
a. [[1], [2], [3]] b. [[1, 2, 3], [2, 4, 5], [3, 6, 7]] c. [1, 1, 1], [2, 2, 2], [3, 3, 3] d. [[1, 2], [2, 3], [3, 4]] e. [[1], [2, 3], [4, 5, 6]]
c. [1, 1, 1], [2, 2, 2], [3, 3, 3] (this is a tuple of lists)
What is the result of the following expression?
[[1, 2], [3, 4]] + [5, 6, 7]
a. [[1, 2], [3, 4], [5, 6, 7]] b. [[1, 2], [3, 4], 5, 6, 7] c. [[1, 2, 3, 4, 5, 6, 7]] d. Error e. [[1, 2], [3, 4, 5, 6, 7]]
b. [[1, 2], [3, 4], 5, 6, 7]
How many rows and columns are in the following matrix?
inputList = [[-5, -15, 5], [-25, -30, 0], [6, 17, 3], [8, 11, -6]]
a. 0 rows and 12 columns b. 12 rows and 0 columns c. 3 rows and 4 columns d. 4 rows and 3 columns e. 3 rows and 3 columns
d. 4 rows and 3 columns
What is the output?
numList = [[10, 20, 30], [40, 50, 60], [70, 80, 90], [100, 110, 120]] print(len(numList))
a. 4 b. 11 c. Error d. 12 e. 3
a. 4
Which represents the generic form for accessing all elements in a rectangular 2D list?
a. for i in range(len(my_list)): for j in range(len(my_list)): print(my_list[j][i])
b. for i in range(len(my_list)): for j in range(len(my_list[0])): print(my_list[j][i])
c. for i in range(len(my_list)): for j in range(len(my_list)): print(my_list[i][j])
d. for i in range(len(my_list)): for j in range(len(my_list[0])): print(my_list[i][j])
e. for i in range(len(my_list[0])): for j in range(len(my_list)): print(my_list[i][j])
d. for i in range(len(my_list)): for j in range(len(my_list[0])): print(my_list[i][j])
What is the output?
inputList = [[-5, -15], [-25, -30]] for i in range(2): for j in range(2): print(inputList[i][j])
a. -5 -15 -25 -30 b. -5 -25 -15 -30 c. -5 -15 -25 -30 d. -5 -15 -25 -30 e. -5 -25 -15 -30
c. -5, -15, -25, and -30, each printed on its own line
What happens when the following line is executed?
this_set = {(3, 4), "Waterloo Region", [519, 226, 548]}
a. Syntax error: a tuple cannot be part of a set. b. It creates a set. c. Error: a list cannot be part of a set. d. Syntax error: a string cannot be part of a set.
c. A TypeError occurs because a list is unhashable and cannot be an element of a set
Which function returns the key-value pairs from a dictionary?
a. values() b. keys() c. key-value() d. items()
d. items()
Which of the following is a tuple?
a. [] b. {} c. set() d. None of these
d. None of these (a tuple can be written as () or tuple())
Which of the following is a dictionary?
a. [] b. {} c. set() d. None of these
b. {}
Which of the following is a set?
a. [] b. {} c. set() d. None of these
c. set()
Which of the following must be immutable?
a. A set b. The keys of a dictionary c. The values in a dictionary d. None of these
b. The keys of a dictionary
Which data structure should you use to store the prices of different TV sets?
a. Tuple b. Set c. Dictionary d. List
c. Dictionary
Which of the following can be accessed using reverse indexing?
a. Set b. Dictionary c. Tuple d. None of these
c. Tuple
Which function converts a list into a tuple?
a. tuple() b. list() c. list_to_tuple() d. None of these
a. tuple()
Which of the following is an invalid dictionary initialization?
a. dict_1 = {"one": 1, "two": {}} b. dict_2 = {"one": 1 [semicolon] "two": {}} c. dict_3 = {} d. dict_4 = {5: 5, True: False, "two": "two"}
b. dict_2 = {"one": 1 [semicolon] "two": {}} (dictionary entries must be separated by commas)
What will the following code output?
pet = "Hedgehog" print(pet[5])
a. e b. H c. g d. h
d. h
Which statement about tuples is true?
a. Tuples are mutable and can be changed like lists. b. Tuples are created using square brackets. c. Tuples can store elements of different types. d. Tuples support methods like append() and sort().
c. Tuples can store elements of different types
Which of these variable names is invalid in Python?
a. value b. userName c. total_amount d. 3score
d
If a function uses return without specifying a value, what does it return by default?
a. 0 b. "None" (as a string) c. None (the special Python value) d. It causes a syntax error.
c. None (the special Python value)
What is the main function of the CPU in a computer system?
a. Running programs by performing operations on data b. Displaying output to users c. Managing power supply and cooling d. Storing data permanently
a. Running programs by performing operations on data
Which best defines an interpreter in programming?
a. A program that translates and executes instructions in a high-level language program b. A program that permanently stores source code in binary format c. A device that controls input and output operations d. A type of memory that holds instructions while the CPU executes them
a. A program that translates and executes instructions in a high-level language program
Which condition is True only if x is between 5 and 10, inclusive?
a. x > 5 and x < 10 b. x >= 5 or x
c. x >= 5 and x <= 10
Which of the following correctly defines a 2D list?
a. table = [9, 8, 7], [6, 5, 4] b. table = [[9, 8, 7], [6, 5, 4]] c. table = {(9, 8, 7), (6, 5, 4)} d. table = ([9, 8, 7], [6, 5, 4])
b. table = [[9, 8, 7], [6, 5, 4]]
Which method reads one line at a time from a text file?
a. file.read() b. file.readline() c. file.readall() d. file.readlines()
b. file.readline()
Which is the correct way to create a dictionary with two key-value pairs?
a. my_dict = (101: 'Alice', 102: 'Bob') b. my_dict = [101: 'Alice', 102: 'Bob'] c. my_dict = {101: 'Alice', 102: 'Bob'} d. my_dict = dict(101 = 'Alice', 102 = 'Bob')
c. my_dict = {101: 'Alice', 102: 'Bob'}
What does the following line do?
file = open('data.txt', 'w')
a. Reads data from data.txt. b. Appends data to data.txt. c. Opens data.txt for writing and overwrites it if it exists. d. Opens data.txt only if it already exists.
c. Opens data.txt for writing and overwrites it if it exists
What is the result of the following expression?
'Hello'.lower().replace('h', 'j')
a. 'Hello' b. 'Jello' c. 'hello' d. 'jello'
d. 'jello'
What will the following code display?
x = 5 y = 2 print(x // y)
a. 2.5 b. 3 c. 2 d. 2.0
c. 2
Which is a characteristic of RAM?
a. It is non-volatile and used for long-term storage. b. It is volatile and temporarily holds data during execution. c. It is part of the CPU chip. d. It permanently stores the operating system.
b. It is volatile and temporarily holds data during execution
Which for loop goes backwards from 10 to 1?
a. for i in range(10, 1, -1) b. for i in range(10, 0, -1) c. for i in range(1, 10, -1) d. for i in range(0, 10, -1)
b. for i in range(10, 0, -1)
Which function header is the intended valid answer?
a. def calculate_total(tip=0.1, amount): b. def calculate_total(amount, tip=0.1): c. def calculate_total(amount=50, tip): d. def calculate_total(*amount, tip):
b. def calculate_total(amount, tip=0.1): (note: d is also syntactically valid Python, with tip as a required keyword-only argument)
Which method returns True only if all characters in a nonempty string are digits?
a. isalpha() b. isdigit() c. isalnum() d. isnumeric()
b. isdigit()
Which best describes a local variable?
a. It can be accessed by any function in the program. b. It is declared outside all functions and accessible everywhere. c. It is created inside a function and can only be used within that function. d. It is passed between functions as a keyword argument.
c. It is created inside a function and can only be used within that function
Which function returns the total number of elements in a list?
a. count() b. len() c. sum() d. index()
b. len()
Why might a while loop become infinite?
a. The condition is never updated to become False. b. The condition is always False. c. The loop contains a continue statement. d. range() is missing.
a. The condition is never updated to become False
What does the following expression evaluate to?
"abc" * 3
a. abcabc b. abcabcabc c. abc3 d. Error
b. abcabcabc
What is the purpose of try and except statements in Python?
a. To improve the speed of a program b. To validate user input c. To write files more efficiently d. To handle exceptions and prevent crashes
d. To handle exceptions and prevent crashes
Which is an example of string concatenation?
a. name = input("Enter name: ") b. score = float(input("Score: ")) c. print(name, age) d. print("Hello" + "Bob")
d. print("Hello" + "Bob")
Which removes the first occurrence of the value 4 from a list named nums?
a. nums.remove(4) b. nums.delete(4) c. nums.pop(4) d. del nums[4]
a. nums.remove(4)
Which set operation returns a new set containing elements common to both sets?
a. set1.union(set2) b. set1.difference(set2) c. set1.intersection(set2) d. set1.symmetric_difference(set2)
c. set1.intersection(set2)
What does split() do when no argument is passed?
a. Returns a list of characters b. Splits the string using commas as a delimiter c. Splits the string using whitespace d. Raises a TypeError
c. Splits the string using whitespace
What is the role of step in range(start, stop, step)?
a. It limits the total number of iterations. b. It controls the order of loop execution. c. It determines how much to increment by each time. d. It decides when the loop ends.
c. It determines how much to increment by each time
What will the following code print?
matrix = [ [1, 2, 3], [4, 5, 6] ] print(matrix[1][2])
a. 2 b. 3 c. 5 d. 6
d. 6
Which statement about Python strings is true?
a. Strings are mutable, meaning their characters can be changed. b. Strings must always be enclosed in double quotes only. c. Strings are immutable, and operations such as concatenation create new strings. d. Strings can only contain alphabetic characters.
c. Strings are immutable, and operations such as concatenation create new strings
Which accesses the last element in a nonempty 2D list called grades?
a. grades[-1][-1] b. grades[len(grades)][len(grades[0])] c. grades[0][0] d. grades[1][1]
a. grades[-1][-1]
What is the result of this code?
my_order = (5,) print(type(my_order))
a.
b.
Which loop type is typically used when the number of iterations is unknown in advance?
a. for loop b. while loop c. range loop d. if statement
b. while loop
Which creates a list containing the squares of the even numbers from 0 to 9?
a. [x * x for x in range(10) if x % 2 == 0] b. [x for x in range(10) if x % 2 == 0] ** 2 c. [x ** 2 if x % 2 == 0 for x in range(10)] d. [x ** 2 in range(10) if x % 2 == 0]
a. [x * x for x in range(10) if x % 2 == 0]
Which of the following makes a loop run exactly four times?
a. range(1, 5) b. range(0, 4) c. range(2, 6) d. All of the above
d. All of the above
What does the with statement do when working with files?
a. It allows multiple files to be opened at once only. b. It automatically closes the file after the block ends. c. It prevents any exceptions from being raised. d. It appends data to a file safely.
b. It automatically closes the file after the block ends
What happens if you use square-bracket syntax to access a key that does not exist in a dictionary?
a. The program displays None. b. A KeyError is raised. c. The program skips the key. d. The key is automatically added with a None value.
b. A KeyError is raised
Which list method adds one element to the end of a list?
a. append() b. insert() c. extend() d. add()
a. append()
What is the result?
colors = ['red', 'blue', 'green'] colors.insert(1, 'yellow') print(colors)
a. ['red', 'yellow', 'blue', 'green'] b. ['yellow', 'red', 'blue', 'green'] c. ['red', 'yellow', 'green', 'blue'] d. ['red', 'green', 'blue', 'yellow']
a. ['red', 'yellow', 'blue', 'green']
How many times will the following code print "Hello"?
for i in range(3): for j in range(2): print("Hello")
a. 3 b. 2 c. 5 d. 6
d. 6
Which condition is True only when age is greater than 18, less than or equal to 65, and not equal to 30?
a. age >= 18 and age <= 65 or age != 30
b. age > 18 and age < 65 and age != 30
c. age > 18 and age <= 65 and age != 30
d. age > 18 and age < 65 or age == 30
c. age > 18 and age <= 65 and age != 30