# Python Programming Quiz: Trivia Test!

Approved & Edited by ProProfs Editorial Team
At ProProfs Quizzes, our dedicated in-house team of experts takes pride in their work. With a sharp eye for detail, they meticulously review each quiz. This ensures that every quiz, taken by over 100 million users, meets our standards of accuracy, clarity, and engagement.
| Written by Ashwin
A
Ashwin
Community Contributor
Total Contribution - 1 | Total attempts - 223
Questions: 15 | Attempts: 223  Settings  Python programming quiz: trivia test! There are a lot of people who think python is hard to understand, but it is actually the easiest to understand for beginners. Do you think you have enough basic knowledge about the program to answer all the questions in this quiz? Do give it a shot and see if you might need a refresher to understand the programming language much better. All the best!

• 1.

### Which of the following is a bad Python variable name?

• A.

SPAM23

• B.

23spam

• C.

Spam

• D.

_spam

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.

Rate this question:

• 2.

### What will be the value of x after the following statement executes: x = 1 + 2 * 3 - 8 / 4

• A.

5

• B.

4

• C.

2.0

• D.

5.0

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.

Rate this question:

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

• A.

1

• B.

3

• C.

2

• D.

6

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.

Rate this question:

• 4.

### What does the following code print out? x = 'banana' y = max(x) print(y)

• A.

A

• B.

N

• C.

B

• D.

Syntax error

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'.

Rate this question:

• 5.

### What is a good statement to describe the “is” operator as used in the following if statement: if smallest is None :      smallest = value

• A.

Looks up ‘None’ in the smallest variable if it is a string

• B.

The if statement is a syntax error

• C.

Matches both type and value

• D.

Is true if the smallest variable has a value of -1

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.

Rate this question:

• 6.

### Which statement can be used to get the user entered data in desired data type?

• A.

Type()

• B.

Convert()

• C.

Trans()

• D.

Map()

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.

Rate this question:

• 7.

### Which of the following slicing operations will produce the list [12, 3]? t = [9, 41, 12, 3, 74, 15]

• A.

T[2:2]

• B.

T[2:4]

• C.

T[2:3]

• D.

T[12:3]

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.

Rate this question:

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

• A.

• B.

• C.

• D.

A. Print(sum([int(num) for num in re.findall('[0-9]+',fh.read())]))
Explanation

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.

Rate this question:

• 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

• A.

Counts[key] = counts.get(key,0)+1

• B.

Counts[key] = (counts[key]*1)+1

• C.

Counts[key] = (key in counts)+1

• D.

Counts[key] = counts.get(key,-1)+1

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.

Rate this question:

• 10.

### In the following Python code, what will end up in the variable y? x = { 'chuck' : 1 , 'fred' : 42, 'jan': 100} y = x.items()

• A.

A list of tuples

• B.

A list of integers

• C.

A list of strings

• D.

A tuple with three integers

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.

Rate this question:

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

• A.

It sorts the dictionary based on its key values

• B.

It computes the largest of all of the values in the dictionary

• C.

It creates the list of tuples where each tuple is a value, key pair

• D.

It computes the average of all of the values in the dictionary

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.

Rate this question:

• 12.

### Given that Python lists and Python tuples are quite similar - when might you prefer to use a tuple over a list?

• A.

For a temporary variable that you will use and discard without modifying

• B.

For a list of items that want to use strings as key values instead of integers

• C.

For a list of items you intend to sort in place

• D.

For a list of items that will be extended as new items are found

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.

Rate this question:

• 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}]                 }

• A.

For i in event.items():     for k in range(len(i)):         val = list(i[k].values())         for j in val:             print(j)

• B.

For i in event.values():     for k in range(len(i)):         val = list(i[k].values())         for j in val:             print(j)

• C.

For i in event.values():     for k in range(len(i)):         val = list(i[k].items())         for j in val:             print(j)

• D.

For i in event.items():     for k in range(len(i)):         val = list(i[k].items())         for j in val:             print(j)

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.

Rate this question:

• 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?

• A.

While ((line = fl.readLine()) != null) {

• B.

• C.

For line in fl:

• D.

While (<fl>) {

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.

Rate this question:

• 15.

### What is the alternate keyword used to open a file in Python other than using a file handler variable?

• A.

File

• B.

Let

• C.

With

• D.

As Back to top