Python Programming Quiz: Trivia Test!

15 Questions | Total Attempts: 102

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

  • 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

  • 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

  • 4. 
    What does the following code print out? x = 'banana' y = max(x) print(y)
    • A. 

      A

    • B. 

      N

    • C. 

      B

    • D. 

      Syntax error

  • 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

  • 6. 
    Which statement can be used to get the user entered data in desired data type?
    • A. 

      Type()

    • B. 

      Convert()

    • C. 

      Trans()

    • D. 

      Map()

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

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

  • 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

  • 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

  • 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

  • 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

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

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

  • 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

Related Topics
Back to Top Back to top