Python Test: Trivia Questions! Quiz

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 Examcec.1
E
Examcec.1
Community Contributor
Quizzes Created: 1 | Total Attempts: 187
Questions: 40 | Attempts: 187

SettingsSettingsSettings
Python Test: Trivia Questions! Quiz - Quiz

.


Questions and Answers
  • 1. 

    Which of the following refers to mathematical function?

    • A.

      Add

    • B.

      Sqrt

    • C.

      Square

    • D.

      Lambda

    Correct Answer
    B. Sqrt
    Explanation
    The mathematical function referred to in the given options is "sqrt", which stands for square root. This function calculates the square root of a given number.

    Rate this question:

  • 2. 

    Which of the following operator is used to find remainder in Python?

    • A.

      %

    • B.

      //

    • C.

      **

    • D.

      &

    Correct Answer
    A. %
    Explanation
    The % operator is used to find the remainder in Python. It is called the modulus operator. For example, if we divide 10 by 3 using the % operator, the remainder will be 1. This operator is commonly used in mathematical operations and can be used to check if a number is even or odd.

    Rate this question:

  • 3. 

    What will be the output of the following code: 6 * 3 + 7 * 4

    • A.

      20

    • B.

      94

    • C.

      46

    • D.

      46.82

    Correct Answer
    C. 46
    Explanation
    The given code is a simple arithmetic expression that involves multiplication and addition. According to the order of operations (PEMDAS/BODMAS), multiplication is performed before addition. Therefore, 6 * 3 equals 18 and 7 * 4 equals 28. Adding these two results together gives us 46, which is the correct answer.

    Rate this question:

  • 4. 

    What gets printed?x = Truey = Falsez = False if x or y and z:    print ("yes")else:    print ("no")

    • A.

      No

    • B.

      Yes

    • C.

      Fails to compile

    • D.

      Syntax error

    Correct Answer
    B. Yes
    Explanation
    The correct answer is "Yes" because the condition in the if statement is evaluated as x or (y and z). Since x is True, the condition x or (y and z) evaluates to True regardless of the values of y and z. Therefore, the code block inside the if statement is executed, which prints "yes".

    Rate this question:

  • 5. 

    Which Of The Following Represents A Template, Blueprint, Or Contract That Defines Objects Of The Same Type?​​

    • A.

      A Method

    • B.

      A Module

    • C.

      A Class

    • D.

      An Object

    Correct Answer
    C. A Class
    Explanation
    A class represents a template, blueprint, or contract that defines objects of the same type. It is a fundamental concept in object-oriented programming, where objects are created from classes. A class defines the properties (attributes) and behaviors (methods) that objects of that class will have. It serves as a blueprint for creating multiple instances of objects with similar characteristics and functionalities.

    Rate this question:

  • 6. 

    To read two characters from a file object infile, we use

    • A.

      Infile.read()

    • B.

      Infile.readline()

    • C.

      Infile.read(2)

    • D.

      Infile.readlines()

    Correct Answer
    C. Infile.read(2)
    Explanation
    The correct answer is infile.read(2) because the read() function in Python is used to read a specified number of characters from a file. In this case, the argument passed to read() is 2, which means it will read two characters from the file object "infile".

    Rate this question:

  • 7. 

    What is called when a function is defined inside a class?

    • A.

      Module

    • B.

      Class

    • C.

      Another function

    • D.

      Method

    Correct Answer
    D. Method
    Explanation
    When a function is defined inside a class, it is called a method. Methods are functions that are associated with a specific class and can access and modify the data within that class. They are used to perform actions or operations on the data of the class and are an essential part of object-oriented programming.

    Rate this question:

  • 8. 

    Which of the following refers to mathematical function?

    • A.

      Add

    • B.

      Sqrt

    • C.

      Square

    • D.

      Lambda

    Correct Answer
    B. Sqrt
    Explanation
    The mathematical function referred to in this question is the square root function, which is commonly denoted as "sqrt" in mathematical notation. This function calculates the square root of a given number. The other options listed, "add," "square," and "lambda," are not mathematical functions in this context.

    Rate this question:

  • 9. 

    What is the use of “a” in file handling?

    • A.

      Read

    • B.

      Append

    • C.

      Write

    • D.

      None of the the mentioned

    Correct Answer
    B. Append
    Explanation
    The use of "a" in file handling is to append data to an existing file. When a file is opened in "a" mode, new data is added to the end of the file without overwriting the existing content. This allows for the continuous addition of data to a file without losing any previous information.

    Rate this question:

  • 10. 

    Which function is used to read single line from file?

    • A.

      Readline()

    • B.

      Readlines()

    • C.

      Readstatement()

    • D.

      Readfullline()

    Correct Answer
    B. Readlines()
    Explanation
    The correct answer is "Readlines()". This function is used to read a single line from a file. It reads the file line by line and returns a list of strings, where each string represents a line from the file.

    Rate this question:

  • 11. 

    Which function is used to close a file in python?

    • A.

      Close()

    • B.

      Stop()

    • C.

      End()

    • D.

      Closefile()

    Correct Answer
    A. Close()
    Explanation
    The correct answer is Close(). In Python, the Close() function is used to close a file. When a file is opened using the open() function, it is important to close it after performing the necessary operations. Closing a file ensures that any resources used by the file are freed up and made available for other processes. It is good practice to always close files after using them to prevent any potential issues or memory leaks.

    Rate this question:

  • 12. 

    What is the maximum possible length of an identifier?

    • A.

      31 characters

    • B.

      63 characters

    • C.

      79 characters

    • D.

      None of the mentioned

    Correct Answer
    D. None of the mentioned
  • 13. 

    Which of the following is an invalid variable?

    • A.

      My_string_1

    • B.

      1st_string

    • C.

      Foo

    • D.

      _

    Correct Answer
    B. 1st_string
    Explanation
    The variable "1st_string" is invalid because variable names cannot start with a number. Variable names in programming languages must start with a letter or an underscore.

    Rate this question:

  • 14. 

    Which of the following cannot be a variable?

    • A.

      __init__

    • B.

      In

    • C.

      It

    • D.

      On

    Correct Answer
    B. In
    Explanation
    The identifier "in" cannot be a variable because it is a reserved keyword in Python. Reserved keywords have predefined meanings and are used to perform specific tasks in the programming language. Therefore, using "in" as a variable name would result in a syntax error.

    Rate this question:

  • 15. 

    Which of the following is not a keyword?

    • A.

      Eval

    • B.

      Assert

    • C.

      Nonlocal

    • D.

      Pass

    Correct Answer
    A. Eval
    Explanation
    The keyword "eval" is used in Python to evaluate a string as a Python expression. It is not a function or a variable, but a keyword that has a specific purpose in the language. The other options, "assert", "nonlocal", and "pass", are all keywords in Python that serve different purposes. Therefore, "eval" is the correct answer because it is not a keyword.

    Rate this question:

  • 16. 

    All keywords in Python are in

    • A.

      Lower case

    • B.

      UPPER CASE

    • C.

      Capitalized

    • D.

      None

    Correct Answer
    D. None
    Explanation
    In Python, all keywords are written in lowercase. Unlike some other programming languages, Python does not have any keywords that are written in uppercase or capitalized. This is an important convention to follow when writing Python code, as using uppercase or capitalized keywords will result in syntax errors. Therefore, the correct answer is "none" as there are no keywords in Python that are uppercase or capitalized.

    Rate this question:

  • 17. 

    Which keyword is use for function?

    • A.

      Fun

    • B.

      Define

    • C.

      Def

    • D.

      Function

    Correct Answer
    C. Def
    Explanation
    The keyword "def" is used to define a function in Python. It is followed by the name of the function and a pair of parentheses, which may contain parameters for the function. The "def" keyword is essential in Python to indicate the start of a function definition.

    Rate this question:

  • 18. 

    What is the output of the below program? 1. def sayHello(): 2. print('Hello World!') 3. sayHello() 4. sayHello()

    • A.

      Hello World! Hello World!

    • B.

      ‘Hello World!’ ‘Hello World!’

    • C.

      Hello Hello

    • D.

      None of the mentioned

    Correct Answer
    A. Hello World! Hello World!
    Explanation
    The output of the program is "Hello World!
    Hello World!".
    This is because the program defines a function called "sayHello()" on line 1, which simply prints "Hello World!" on line 2.
    Then, the program calls the "sayHello()" function twice on lines 3 and 4.
    As a result, the function is executed twice, printing "Hello World!" twice.

    Rate this question:

  • 19. 

    What is the output of the below program ? x = 50 def func(x): print('x is', x) x = 2 print('Changed local x to', x) func(x) print('x is still', x)

    • A.

      X is still 50

    • B.

      X is still 2

    • C.

      X is still 100

    • D.

      None of the mentioned

    Correct Answer
    A. X is still 50
    Explanation
    The program defines a global variable x with a value of 50. It then defines a function called func that takes a parameter x. Within the function, it prints the value of x, which is 2 because it is passed as an argument when the function is called. It then changes the value of the local variable x to 2 and prints it again. After the function call, it prints the value of the global variable x, which is still 50. Therefore, the output of the program is "x is 2" and "x is still 50".

    Rate this question:

  • 20. 

    What is the output of below program? def say(message, times = 1): print(message * times) say('Hello') say('World', 5)

    • A.

      Hello WorldWorldWorldWorldWorld

    • B.

      Hello World 5

    • C.

      Hello World,World,World,World,World

    • D.

      Hello d

    Correct Answer
    A. Hello WorldWorldWorldWorldWorld
    Explanation
    The program defines a function called "say" which takes two parameters: "message" and "times" (with a default value of 1). The function prints the "message" parameter multiplied by the "times" parameter.

    In the first call to the function, "say('Hello')", the "message" parameter is 'Hello' and since the "times" parameter is not provided, it takes the default value of 1. Therefore, the output is "Hello".

    In the second call to the function, "say('World', 5)", the "message" parameter is 'World' and the "times" parameter is 5. So, the output is "WorldWorldWorldWorldWorld".

    Therefore, the correct answer is "Hello WorldWorldWorldWorldWorld".

    Rate this question:

  • 21. 

    What are the two types of functions?

    • A.

      Custom function

    • B.

      Built-in function

    • C.

      User-Defined function

    • D.

      System function

    Correct Answer(s)
    B. Built-in function
    C. User-Defined function
    Explanation
    The two types of functions are built-in functions and user-defined functions. Built-in functions are pre-defined functions that are provided by the programming language or software library. These functions are already implemented and can be directly used by the programmer. User-defined functions, on the other hand, are created by the programmer to perform specific tasks. These functions are not pre-defined and can be customized according to the programmer's requirements.

    Rate this question:

  • 22. 

    Where is function defined?

    • A.

      Module

    • B.

      Class

    • C.

      Another function

    • D.

      None of the mentioned

    Correct Answer(s)
    A. Module
    B. Class
    C. Another function
    Explanation
    A function can be defined in three different places: within a module, within a class, or within another function. When a function is defined within a module, it is accessible throughout the entire module. When a function is defined within a class, it becomes a method of that class and can only be accessed through an instance of the class. Lastly, a function can also be defined within another function, making it a nested function. This allows the inner function to have access to the variables and scope of the outer function. Therefore, the correct answer is Module, Class, Another function.

    Rate this question:

  • 23. 

    Given a function that does not return any value, What value is thrown by itby default when executed in shell.

    • A.

      Int

    • B.

      Bool

    • C.

      Void

    • D.

      None

    Correct Answer
    D. None
    Explanation
    When a function does not return any value, it is considered to have a return type of "void". In programming languages such as C or C++, "void" is used to indicate that a function does not return a value. However, in shell scripting, the concept of return types is not present. Therefore, when a function that does not return any value is executed in the shell, no specific value is thrown or returned by default. Hence, the answer is "None".

    Rate this question:

  • 24. 

    Which of the following will run without errors(multiple answers possible) ?

    • A.

      Round(45.8)

    • B.

      Round(6352.898,2)

    • C.

      Round()

    • D.

      Round(7463.123,2,1)

    Correct Answer(s)
    A. Round(45.8)
    B. Round(6352.898,2)
    Explanation
    The round() function is used to round a number to a specified number of decimal places. In the given options, round(45.8) will run without errors as it rounds the number 45.8 to the nearest whole number. Similarly, round(6352.898,2) will also run without errors as it rounds the number 6352.898 to 2 decimal places.

    Rate this question:

  • 25. 

    What error occurs when you execute? apple = mango

    • A.

      SyntaxError

    • B.

      NameError

    • C.

      ValueError

    • D.

      TypeError

    Correct Answer
    B. NameError
    Explanation
    The error that occurs when executing the statement "apple = mango" is a NameError. This error is thrown when a name or variable is not found in the current scope. In this case, the variable "mango" is not defined or assigned a value before it is being assigned to the variable "apple". Therefore, a NameError is raised indicating that the name "mango" is not recognized.

    Rate this question:

  • 26. 

    In order to store values in terms of key and value we use what core datatype.

    • A.

      List

    • B.

      Tuple

    • C.

      Class

    • D.

      Dictionary

    Correct Answer
    D. Dictionary
    Explanation
    A dictionary is the core datatype used to store values in terms of key and value. It allows us to associate a unique key with a corresponding value, making it easy to retrieve and manipulate data based on the key. This is different from other core datatypes like lists and tuples, which store values without any specific key-value association. Therefore, a dictionary is the correct choice for storing key-value pairs in Python.

    Rate this question:

  • 27. 

    Which of the following results in a SyntaxError(Multiple answers possible) ?

    • A.

      ‘”Once upon a time…”, she said.’

    • B.

      “He said, “Yes!””

    • C.

      ‘3\’

    • D.

      ”’That’s okay”’

    Correct Answer(s)
    B. “He said, “Yes!””
    C. ‘3\’
    Explanation
    The first option, "He said, "Yes!"" results in a SyntaxError because the double quotes within the string are not properly escaped. The second option, '3\', also results in a SyntaxError because the backslash is not properly escaped.

    Rate this question:

  • 28. 

    What is the return value of trunc() ?

    • A.

      Int

    • B.

      Bool

    • C.

      Float

    • D.

      None

    Correct Answer
    A. Int
    Explanation
    The return value of trunc() is int. The trunc() function is used to remove the decimal part of a number and return the integer part.

    Rate this question:

  • 29. 

    What is the output of the following?i = 1 while True:     if i%0O7 == 0:         break     print(i)     i += 1

    • A.

      1 2 3 4 5 6

    • B.

      1 2 3 4 5 6 7

    • C.

      Error

    • D.

      None of the mentioned

    Correct Answer
    A. 1 2 3 4 5 6
    Explanation
    The given code will output the numbers 1, 2, 3, 4, 5, and 6.

    In the while loop, the condition "True" will always be true, creating an infinite loop. However, the break statement inside the if condition will break out of the loop when the value of i is divisible by 7.

    Since the value of i starts at 1 and increments by 1 in each iteration, the if condition will never be true, and the loop will continue until i reaches 7.

    Therefore, the numbers 1, 2, 3, 4, 5, and 6 will be printed before the loop is terminated.

    Rate this question:

  • 30. 

    What is the output of the following?i = 0 while i < 5:     print(i)     i += 1     if i == 3:         break else:     print(0) 

    • A.

      0 1 2 0

    • B.

      0 1 2

    • C.

      Error

    • D.

      None of the mentioned

    Correct Answer
    B. 0 1 2
    Explanation
    The code uses a while loop to iterate through the values of i from 0 to 4. Inside the loop, the current value of i is printed, and then i is incremented by 1. If i is equal to 3, the loop is exited using the break statement. Otherwise, the value 0 is printed. Therefore, the output of the code will be 0 1 2.

    Rate this question:

  • 31. 

    What is the output of the following?x = "abcdef" i = "a" while i in x:     x = x[:-1]     print(i, end = " ")

    • A.

      I i i i i i

    • B.

      A a a a a a

    • C.

      A a a a a

    • D.

      None of the mentioned

    Correct Answer
    B. A a a a a a
    Explanation
    The output of the code is "a a a a a a".


    The code initializes the variable x with the string "abcdef" and i with the character "a".
    The while loop checks if i is present in x. Since "a" is present in "abcdef", the loop is executed.
    Inside the loop, the last character of x is removed using the slicing operation x[:-1].
    Then, the value of i is printed, followed by a space.
    This process continues until "a" is no longer present in x.
    Since "a" is present 6 times in the original string, it is printed 6 times in the output.

    Rate this question:

  • 32. 

    What is the output of the following?x = "abcdef" i = "a" while i in x[1:]:     print(i, end = " ")

    • A.

      A a a a a a

    • B.

      A

    • C.

      No output

    • D.

      Error

    Correct Answer
    C. No output
    Explanation
    The code initializes a variable x with the string "abcdef" and a variable i with the string "a". It then enters a while loop that checks if the value of i is present in the substring x[1:] (which is "bcdef"). Since the value of i is "a" and "a" is not present in "bcdef", the condition of the while loop is not satisfied and the loop is not executed. Therefore, there is no output.

    Rate this question:

  • 33. 

    What will be the output?
    1. values = [[3, 4, 5, 1], [33, 6, 1, 2]]
    2.  
    3. v = values[0][0]
    4. for lst in values:
    5.     for element in lst:
    6.         if v > element:
    7.             v = element
    8.  
    9. print(v)

    • A.

      1

    • B.

      3

    • C.

      5

    • D.

      6

    Correct Answer
    A. 1
    Explanation
    The code initializes the variable v with the first element of the first sublist in the values list, which is 3. Then, it iterates over each sublist in the values list and checks if the current element is smaller than v. If it is, v is updated with the smaller value. Finally, it prints the value of v, which is the smallest element in the entire values list. In this case, the smallest element is 1, so the output is 1.

    Rate this question:

  • 34. 

    6. What is the output?
    1. matrix = [[1, 2, 3, 4],
    2.        [4, 5, 6, 7],
    3.        [8, 9, 10, 11],
    4.        [12, 13, 14, 15]]
    5.  
    6. for i in range(0, 4):
    7.     print(matrix[i][1], end = " ")

    • A.

      1 2 3 4

    • B.

      4 5 6 7

    • C.

      1 3 8 12

    • D.

      2 5 9 13

    Correct Answer
    D. 2 5 9 13
    Explanation
    The given code prints the second element from each sublist of the matrix. The range function is used to iterate through the rows of the matrix. The index [1] is used to access the second element from each sublist. The end parameter is set to an empty space to print the elements in a single line. Therefore, the output is "2 5 9 13".

    Rate this question:

  • 35. 

    What will be the output?
    1. data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
    2.  
    3. print(data[1][0][0])

    • A.

      1

    • B.

      2

    • C.

      4

    • D.

      5

    Correct Answer
    D. 5
    Explanation
    The given code is accessing the element at index 1 of the outermost list, which is [[5, 6], [7, 8]]. Then it is accessing the element at index 0 of this inner list, which is [5, 6]. Finally, it is accessing the element at index 0 of this innermost list, which is 5. Therefore, the output will be 5.

    Rate this question:

  • 36. 

    What will be the output?
    1. points = [[1, 2], [3, 1.5], [0.5, 0.5]]
    2. points.sort()
    3. print(points)

    • A.

      [[1, 2], [3, 1.5], [0.5, 0.5]]

    • B.

      [[3, 1.5], [1, 2], [0.5, 0.5]]

    • C.

      [[0.5, 0.5], [1, 2], [3, 1.5]]

    • D.

      [[0.5, 0.5], [3, 1.5], [1, 2]]

    Correct Answer
    C. [[0.5, 0.5], [1, 2], [3, 1.5]]
    Explanation
    The given code sorts the list of points in ascending order based on their values. The sort() method arranges the points in ascending order based on the first element of each sublist. If the first elements are the same, it then compares the second elements. Therefore, the output will be [[0.5, 0.5], [1, 2], [3, 1.5]], as it is the sorted version of the original list.

    Rate this question:

  • 37. 

    What is the output of print(math.copysign(3, -1))? 

    • A.

      1

    • B.

      1.0

    • C.

      -3

    • D.

      -3.0

    Correct Answer
    D. -3.0
    Explanation
    The output of the code will be -3.0. The math.copysign() function returns a float value with the magnitude of the first argument and the sign of the second argument. In this case, the magnitude is 3 and the sign is -1, so the output will be -3.0.

    Rate this question:

  • 38. 

    What is the value returned by math.fact(6)? 

    • A.

      720

    • B.

      6

    • C.

      [1, 2, 3, 6]

    • D.

      Error

    Correct Answer
    D. Error
  • 39. 

    What is the output of print(math.factorial(4.5))? 

    • A.

      24

    • B.

      120

    • C.

      Error

    • D.

      None of the mentioned

    Correct Answer
    C. Error
    Explanation
    The output of print(math.factorial(4.5)) is "error" because the factorial function in the math module only accepts integers as input. In this case, 4.5 is a float and not a whole number, so it is not a valid input for the factorial function.

    Rate this question:

  • 40. 

    Which are the two built-in functions to read a line of text from standard input, which by default comes from the keyboard?(Multiple Answers Possible)

    • A.

      Raw_input

    • B.

      Input

    • C.

      Scan

    • D.

      Scanner

    Correct Answer(s)
    A. Raw_input
    B. Input
    Explanation
    The correct answer is Raw_input and Input. Raw_input is a built-in function in Python 2 that reads a line of text from standard input, while Input is a built-in function in Python 3 that performs the same task. Both functions allow the user to input text from the keyboard, which is the default standard input.

    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 22, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Jun 27, 2017
    Quiz Created by
    Examcec.1

Related Topics

Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.