Python Programming Quiz: Trivia Test!

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Ashwin
A
Ashwin
Community Contributor
Quizzes Created: 1 | Total Attempts: 237
Questions: 15 | Attempts: 237

SettingsSettingsSettings
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. 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!


Questions and Answers
  • 1. 

    Which of the following is a bad Python variable name?

    • A.

      SPAM23

    • B.

      23spam

    • C.

      Spam

    • D.

      _spam

    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.

    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

    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.

    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

    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.

    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

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

    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

    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.

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

    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.

    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]

    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.

    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.

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

    • B.

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

    • C.

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

    • D.

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

    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.

    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

    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.

    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

    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.

    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

    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.

    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

    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.

    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)

    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.

    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.

      Read fl into line

    • C.

      For line in fl:

    • D.

      While (<fl>) {

    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.

    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

    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.

    Rate this question:

Quiz Review Timeline +

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

Related Topics

Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.