Python Programming Quiz: Trivia Test!

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Ashwin
A
Ashwin
Community Contributor
Quizzes Created: 1 | Total Attempts: 262
| Attempts: 262 | Questions: 15
Please wait...
Question 1 / 15
0 %
0/100
Score 0/100
1. Given that Python lists and Python tuples are quite similar - when might you prefer to use a tuple over a list?

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.

Submit
Please wait...
About This Quiz
Python Programming Quiz: Trivia Test! - Quiz

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.... see moreDo 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!
see less

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

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.

Submit
3. Which of the following is a bad Python variable name?

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.

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

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.

Submit
5. 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:

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.

Submit
6. 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))

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.

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

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.

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

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

Submit
9. 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?

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.

Submit
10. 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)

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.

Submit
11. 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

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.

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

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.

Submit
13. What is a good statement to describe the "is" operator as used in the following if statement: if smallest is None :      smallest = value

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.

Submit
14. Which statement can be used to get the user entered data in desired data type?

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.

Submit
15. 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}]                 }

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.

Submit
View My Results

Quiz Review Timeline (Updated): Mar 20, 2023 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 20, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • May 28, 2020
    Quiz Created by
    Ashwin
Cancel
  • All
    All (15)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Given that Python lists and Python tuples are quite similar - when...
In the following Python code, what will end up in the variable y?...
Which of the following is a bad Python variable name?
Which of the following slicing operations will produce the list [12,...
Consider a text file with both plain text and a lot of numbers as the...
What does the following Python code accomplish, assuming c is a...
What will be the value of x after the following statement executes:...
What does the following code print out?...
If we open a file as follows: fl = open('demo.txt') ...
In the following code - which will be the last line to execute...
The following lines of Python is equivalent to which of the statements...
What is the alternate keyword used to open a file in Python other than...
What is a good statement to describe the "is" operator as used in the...
Which statement can be used to get the user entered data in desired...
Consider the below dictionary and choose the correct option to extract...
Alert!

Advertisement