1.
Which of the following is a bad Python variable name?
Correct Answer
B. 23spam
Explanation
The variable name "23spam" is a bad Python variable name because Python variable names cannot start with a number. Variable names in Python must start with a letter or an underscore.
2.
What will be the value of x after the following statement executes:
x = 1 + 2 * 3 - 8 / 4
Correct Answer
D. 5.0
Explanation
The given expression consists of arithmetic operations. According to the order of operations, multiplication and division are performed before addition and subtraction. Therefore, the expression simplifies as follows: 2 * 3 = 6, 8 / 4 = 2, and 1 + 6 - 2 = 5.0. Thus, the value of x after the statement executes is 5.0.
3.
In the following code - which will be the last line to execute successfully?
astr = 'Hello Bob'
istr = int(astr)
print('First', istr)
astr = '123'
istr = int(astr)
print('Second', istr)
Correct Answer
A. 1
Explanation
The last line to execute successfully will be line 6. This is because line 6 is the last line of code in the given code snippet. Line 6 prints the string "Second" followed by the value of the variable istr, which is 123.
4.
What does the following code print out?
x = 'banana'
y = max(x)
print(y)
Correct Answer
B. N
Explanation
The code assigns the string 'banana' to the variable x. The max() function is then used to find the maximum value in the string, which in this case is 'n'. The code then prints out 'n'.
5.
What is a good statement to describe the “is” operator as used in the following if statement:
if smallest is None :
smallest = value
Correct Answer
A. Looks up ‘None’ in the smallest variable if it is a string
Explanation
The "is" operator in the if statement checks if the value of the variable "smallest" is equal to None. If it is, then the code assigns the value of "value" to "smallest". This statement does not check if "smallest" is a string, it only checks if it is None.
6.
Which statement can be used to get the user entered data in desired data type?
Correct Answer
D. Map()
Explanation
The map() function can be used to get the user entered data in the desired data type. The map() function applies a specified function to each item in an iterable and returns a new iterator with the results. This means that we can use the map() function to apply a conversion function to each element of the user entered data, thus obtaining the data in the desired data type.
7.
Which of the following slicing operations will produce the list [12, 3]?
t = [9, 41, 12, 3, 74, 15]
Correct Answer
B. T[2:4]
Explanation
The slicing operation t[2:4] will produce the list [12, 3]. This is because the slice starts at index 2 (inclusive) and ends at index 4 (exclusive), so it includes the elements at indexes 2 and 3 of the list t, which are 12 and 3 respectively.
8.
Consider a text file with both plain text and a lot of numbers as the input and choose the appropriate option to print out the total of all numbers from the file.
Kindly assume that “regular expression” is being used in this code:
Correct Answer
A. Print(sum([int(num) for num in re.findall('[0-9]+',fh.read())]))
Explanation
The correct answer is: print(sum([int(num) for num in re.findall('[0-9]+',fh.read())]))
This code snippet correctly uses regular expressions to find all the numbers in the text file. It uses the re.findall() function to find all the matches of the regular expression pattern '[0-9]+' which matches one or more digits. It then converts each matched number into an integer using int(num) and adds them all together using the sum() function. Finally, it prints out the total sum of all the numbers in the file.
9.
The following lines of Python is equivalent to which of the statements (provided as choices) assuming that counts is a dictionary?
if key in counts:
counts[key] = counts[key] + 1
else:
counts[key] = 1
Correct Answer
C. Counts[key] = (key in counts)+1
Explanation
The given correct answer, "counts[key] = (key in counts)+1", is equivalent to the provided lines of Python code. It checks if the key exists in the "counts" dictionary. If it does, it assigns the value of the key in the "counts" dictionary plus 1 to the "counts[key]" variable. If the key does not exist, it assigns the value of 0 + 1 to the "counts[key]" variable.
10.
In the following Python code, what will end up in the variable y?
x = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}
y = x.items()
Correct Answer
A. A list of tuples
Explanation
The code is using the items() method on the dictionary x, which returns a list of tuples. Each tuple contains a key-value pair from the dictionary. Therefore, the variable y will end up containing a list of tuples.
11.
What does the following Python code accomplish, assuming c is a non-empty dictionary?
tmp = list()
for k, v in c.items() :
tmp.append((v, k))
Correct Answer
C. It creates the list of tuples where each tuple is a value, key pair
Explanation
The given Python code creates a list of tuples where each tuple consists of a value-key pair from the dictionary. The code iterates over the items in the dictionary using the `items()` method and appends each tuple `(v, k)` to the `tmp` list. This means that the values from the dictionary become the first element of each tuple, while the keys become the second element. Therefore, the correct answer is that the code creates a list of tuples where each tuple represents a value-key pair.
12.
Given that Python lists and Python tuples are quite similar - when might you prefer to use a tuple over a list?
Correct Answer
A. For a temporary variable that you will use and discard without modifying
Explanation
Tuples are immutable, meaning they cannot be modified once created. If you have a temporary variable that you will use and discard without modifying, using a tuple would be more appropriate because it prevents accidental modification of the data. Lists, on the other hand, are mutable and can be modified after creation.
13.
Consider the below dictionary and choose the correct option to extract only the values from the dictionary. Example: To extract only Ecuador,ec....
event = {'input': [{'location': 'Ecuador',
'country_code': 'ec',
'latitude': -1.831239,
'longitude': -78.18340599999999},
{'location': 'Norway',
'country_code': 'no',
'latitude': 60.47202399999999,
'longitude': 8.468945999999999}]
}
Correct Answer
B. For i in event.values():
for k in range(len(i)):
val = list(i[k].values())
for j in val:
print(j)
Explanation
The correct answer is a loop that iterates over the values of the dictionary using the `values()` method. It then iterates over each item in the values, converting them to a list of values using the `values()` method. Finally, it prints each value. This code correctly extracts and prints only the values from the dictionary.
14.
If we open a file as follows: fl = open('demo.txt')
What statement would we use to read the file one line at a time?
Correct Answer
A. While ((line = fl.readLine()) != null) {
Explanation
The correct statement to read the file one line at a time is "while ((line = fl.readLine()) != null)". This statement uses the readLine() method to read each line from the file and assigns it to the variable "line". The condition "line != null" ensures that the loop continues until all lines in the file have been read.
15.
What is the alternate keyword used to open a file in Python other than using a file handler variable?
Correct Answer
C. With
Explanation
The keyword "with" is used as an alternate way to open a file in Python without the need for a file handler variable. The "with" statement ensures that the file is properly closed after its use, even if an exception occurs during the file operations. This helps in preventing resource leaks and makes the code more concise and readable.