What will this output?
a. 11
b. 12
c. Syntax Error
d. Type error
e. Index out of bounds error
TypeError: 'int' object is not iterable
You would need to define a range, of length(i) to iterate over those ints.
A way to extract only a portion of a sequential type, such as a list, tuple or string. It is achieved by specifying a range of indices using square brackets and a colon, where the first index marks the beginning and the second index marks the end.
example:
type[1:4]
What is slicing?
What will this output?
num = 41
print(f'{num % 2 = }')
a. '41 % 2 = '
b. num % 2 = 1
c. 1
d. 41 % 2 = 1
D. The f string will do textual replacement for the variable 'num' as 41, and then perform the modulo operation and print the result using the string format.
Suppose I want to write to a NEW file, which usage would do this?
a. with open("new1.txt", 'w') as f:
b. with open("new1.txt", 'x') as f:
c. open("new1.txt", 'a') as f:
d. open("new1.txt", 'r') as f:
B.
If you open a file in mode x , the file is created and opened for writing – but only if it doesn't already exist.
a method of solving a computational problem where the solution depends on solutions to smaller instances of the same problem
What is recursion?
Which of the following methods can be used to iterate over each character in a string in Python?
A. for i in range(len(s)):
B. for char in s:
C. for i, char in enumerate(s):
D. All of the above.
D. All of the above
Any object that can return an object that can be used to loop over the element of the object, Examples include: lists, tuples, sets, dicts, strings, and range objects. Allows for easy and efficient traversal of collection types.
What is an iterable?
What will this output?
True.
The min() returns the item with the lowest value in an iterable, if the values are strings, it compares them alphabetically.
What will this print?
a. the first 2 lines of the file
b. the first 2 chars of the file
c. the file, twice
d. Error, invalid passing of integer as an argument
B. This will only read the first two characters in the first line of the file.
Only problems that are recursively defined can be solved using recursion.
true or false
False. There are many other problems can also be solved using recursion.
Which of the following statements about list indexing and referencing in python is true? (select all that apply)
a. List indices start at 0
b. List elements can be accessed using the get() method
c. List elements can be modified using the assignment operator (=)
d. A list can contain elements of different data types
A, C, and D. Indices always start at 0. Elements CAN be modified using an assignment operator, and obviously lists can store different data types in the same list.
A way of explicitly specifying the expected data types of the input parameters and return values of a function or method.
What will this print?
1.
The in operator works by iterating over the elements in the list and comparing each element to the specified value. If a match is found, the in operator returns True and stops searching. If no match is found, the in operator returns False.
What will the following code snippet print to the console?
for i in range(1, 10, 2):
print(i)
A. 1 3 5 7 9
B. 1 3 5 7 9 10
C. 0 2 4 6 8
D. 0 1 2 3 4 5 6 7 8 9
A.
Remember (start, stop, step), here we start at 1, and stop at 10 (excluding 10), incrementing by 2 each iteration.
Which of these is NOT true about recursion?
a) Recursive function can be replaced by a non-recursive function
b) Recursive functions usually take more memory space than non-recursive function
c) Recursive functions run faster than non-recursive function
d) Recursion makes programs easier to understand
C. The speed of a program using recursion is typically slower than the speed of its non recursive equivalent.
Which of the following is the correct way to slice a list in Python? Select all that apply.
A. lst[1:4]
B. lst[4:1]
C. lst[1:4:-1]
D. lst[4:1:-1]
A and D.
When using slice notation, the first index is the starting point, and second index is the endpoint (exclusive). The third argument, optional, is the step. In B the starting index is greater than the ending index, which will result in an empty list. In C, the step size is negative, but the starting index is 1, so negative indexing will never access any elements.
An algorithm for performing encryption or decryption—a series of well-defined steps that can be followed as a procedure.
What is a cipher? (Or encipherment)
What will this print?
a. '_kg_to_lbs_'
b. 'kg_to_lbs'
c. ['kg', 'to', 'lbs, '_']
d. Error. Cannot join a string and list.
B.
The join() method is a string method that takes an iterable (like a list or a tuple) as an argument and returns a string. The method concatenates the elements of the iterable, separated by the string on which it is called.
Finish the type contract
a. vals: str, c: str, -> bool
b. vals: str, c: bool, -> str
c. vals: str, c: char, -> bool
d. vals: char, c: char -> str
A. vals: str, c: str, -> bool
Examine the usage at the bottom. There is technically no native 'char' type in python. All single characters are represented as length 1 strings. Therefore, you use str for c.
Fill in the blank
def fact(num):
if num == 0:
return 1
else:
return ____
num * fact(num - 1)
Suppose num = 5, then we want 5*4*3*2*1 until we decompose to the base case of n = 0.
A built-in function that takes an iterable object, such as a list or tuple, and returns an iterator that generates pairs of the form (index, element) for each element in the iterable, where index is the index of the element in the iterable, starting from 0.
What is enumerate()
A stream of data that is provided to a program through the keyboard or from a file. Can also be redirected from a file or other source using input redirection or piping, allowing a program to read input from a file or another program's output instead of the keyboard.
Invoked using input() or sys.stdin.readline()
Which methods do list and str types share in python? (Select all that apply)
a. len()
b. index()
c. min(), max()
d. split()
e. sort()
A, B, C.
Split is a string method, sort is a list method.
Fill in the blanks
for char in string
unique += char
Examine usages, read comments as hints.
What will this output?
0 1 1 2.
This will print the fibonacci sequence from 0 to 4.