Python Test: Trivia Questions! Quiz

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 Examcec.1
E
Examcec.1
Community Contributor
Quizzes Created: 1 | Total Attempts: 192
| Attempts: 192 | Questions: 40
Please wait...
Question 1 / 40
0 %
0/100
Score 0/100
1. What will be the output of the following code: 6 * 3 + 7 * 4

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.

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

Dive into Python with our 'Python Test: Trivia Questions!' Quiz, designed to challenge and enhance your understanding of Python programming. From basic syntax to complex logic, test your... see moreskills and refine your coding prowess in an engaging and educational way. see less

2. Which keyword is use for function?

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.

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

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.

Submit
4. Which of the following refers to mathematical function?

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.

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

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

Submit
6. Which of the following refers to mathematical function?

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.

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

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.

Submit
8. Which of the following cannot be a variable?

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.

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

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.

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

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

Submit
11. Which of the following is not a keyword?

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.

Submit
12. All keywords in Python are in

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.

Submit
13. What are the two types of functions?

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.

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

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.

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

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.

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

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.

Submit
17. Which function is used to read single line from file?

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.

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

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.

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

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.

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

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.

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

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.

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

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

Submit
23. What is the return value of trunc() ?

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.

Submit
24. 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 = " ")

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

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

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.

Submit
26. Which function is used to close a file in python?

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.

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

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

Submit
28. Which of the following is an invalid variable?

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.

Submit
29. What is the maximum possible length of an identifier?

Explanation

not-available-via-ai

Submit
30. What error occurs when you execute? apple = mango

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.

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

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

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

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.

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

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.

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

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.

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

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.

Submit
36. Where is function defined?

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.

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

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.

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

Explanation

not-available-via-ai

Submit
39. What is called when a function is defined inside a class?

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.

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

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.

Submit
View My Results

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

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
Cancel
  • All
    All (40)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What will be the output of the following code: 6 * 3 + 7 * 4
Which keyword is use for function?
What is the use of “a” in file handling?
Which of the following refers to mathematical function?
What is the output of below program?...
Which of the following refers to mathematical function?
Which of the following operator is used to find remainder in Python?
Which of the following cannot be a variable?
Which are the two built-in functions to read a line of text from...
What gets printed?x = Truey = Falsez = False if x or y and...
Which of the following is not a keyword?
All keywords in Python are in
What are the two types of functions?
In order to store values in terms of key and value we use what core...
What will be the output?points = [[1, 2], [3, 1.5], [0.5,...
What is the output of the following?i = 1 while...
Which function is used to read single line from file?
What is the output of the following?i = 0 while i <...
What is the output of the following?x = "abcdef" i =...
Which Of The Following Represents A Template, Blueprint, Or Contract...
What is the output of the below program?...
What is the output of the below program ?...
What is the return value of trunc() ?
6. What is the output?matrix = [[1, 2, 3,...
What is the output of print(math.factorial(4.5))? 
Which function is used to close a file in python?
To read two characters from a file object infile, we use
Which of the following is an invalid variable?
What is the maximum possible length of an identifier?
What error occurs when you execute? apple = mango
Given a function that does not return any value, What value is thrown...
What will be the output?values = [[3, 4, 5, 1], [33, 6, 1, 2]] v...
What is the output of print(math.copysign(3, -1))? 
Which of the following will run without errors(multiple answers...
What will be the output?data = [[[1, 2], [3, 4]], [[5, 6], [7,...
Where is function defined?
Which of the following results in a SyntaxError(Multiple answers...
What is the value returned by math.fact(6)? 
What is called when a function is defined inside a class?
What is the output of the following?x = "abcdef" i =...
Alert!

Advertisement