Python Programming MCQ Trivia 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 PriyangaGT
P
PriyangaGT
Community Contributor
Quizzes Created: 1 | Total Attempts: 3,339
| Attempts: 3,339 | Questions: 40
Please wait...
Question 1 / 40
0 %
0/100
Score 0/100
1.  Operators with the same precedence are evaluated in which manner?

Explanation

Operators with the same precedence are evaluated from left to right. This means that when there are multiple operators with the same level of precedence in an expression, they are evaluated in the order they appear from left to right. This ensures that the mathematical operations are performed in a consistent and predictable manner.

Submit
Please wait...
About This Quiz
Python Programming MCQ Trivia Quiz - Quiz

If you're a Python programmer or want to be one, you should definitely play this Python programming MCQ trivia quiz and evaluate how good you're at the language.... see moreIt is one of the most demanding and popular programming languages today. Python is a powerful general-purpose and high-level programming language. If someone wants to learn this language easily, they need to take this quiz as it contains every type of question-related to Python. So, guys, what do you need more? Go ahead and try the quiz now.
see less

2.  What sequence of numbers is printed? values = [2, 3, 2, 4] def my_transformation(num): return num ** 2 for i in map(my_transformation, values): print i

Explanation

The given code defines a function called my_transformation that squares a given number. The map function is then used to apply this transformation to each element in the values list. The resulting sequence of numbers that is printed is the squared values of the original list, which are 4, 9, 4, and 16.

Submit
3. Python supports the creation of anonymous functions at runtime, using a construct called __________

Explanation

Python supports the creation of anonymous functions at runtime using a construct called "Lambda". Lambda functions are small, one-line functions that do not have a name and can be used wherever function objects are required. They are commonly used in scenarios where a small function is needed for a short period of time and does not need to be defined separately.

Submit
4. What is the output of this program?   y = 6   z = lambda x: x * y print z(8)

Explanation

The program defines a lambda function called "z" that takes an argument "x" and multiplies it by the value of "y", which is 6. The program then calls the lambda function with an argument of 8, resulting in 8 * 6 = 48. Therefore, the output of the program is 48.

Submit
5. Which of the following statements is NOT true about Python?

Explanation

Python's syntax is not much like PHP. Python has its own unique syntax and is known for its simplicity and readability. While both Python and PHP are popular programming languages, they have distinct syntax and are used for different purposes.

Submit
6. Which of the following data structures can be used with the "in" operator to check if an item is in the data structure?

Explanation

The "in" operator can be used with all of the given data structures (list, set, and dictionary) to check if an item is present in them. In a list, the "in" operator checks if the item is one of the elements in the list. In a set, it checks if the item is one of the values in the set. In a dictionary, it checks if the item is one of the keys in the dictionary. Therefore, all of the given data structures can be used with the "in" operator to perform the desired check.

Submit
7. What is the output of below program? def writer():    title = 'Sir'    name = (lambda x:title + ' ' + x)    return name who = writer()who('Arthur')

Explanation

The program defines a function named "writer" which returns a lambda function. This lambda function takes an argument "x" and concatenates it with the variable "title" (which is set to "Sir"). The function "writer" is then called and assigned to the variable "who". Finally, the lambda function stored in "who" is called with the argument "Arthur". Therefore, the output of the program is "Sir Arthur".

Submit
8. What is output of print(math.pow(3, 2))?

Explanation

The output of the given code is 9.0. The math.pow() function in Python is used to calculate the power of a number. In this case, it calculates 3 raised to the power of 2, which is 9. The output is a floating-point number because the math.pow() function always returns a float value.

Submit
9. What is the output of the below program? a = [1,2,3,None,(),[],] print len(a)

Explanation

The program creates a list "a" with 6 elements: 1, 2, 3, None, (), and []. The "len(a)" function returns the number of elements in the list, which is 6.

Submit
10. What is the output of the below program? def C2F(c):    return c * 9/5 + 32print C2F(100)print C2F(0)

Explanation

The given program defines a function named C2F that converts a temperature in Celsius to Fahrenheit. The function takes a parameter c, which represents the temperature in Celsius. It then applies the formula c * 9/5 + 32 to convert the temperature to Fahrenheit.

In the first print statement, the function is called with an argument of 100, so it will calculate 100 * 9/5 + 32, which equals 212. Therefore, the output of the first print statement is 212.

In the second print statement, the function is called with an argument of 0, so it will calculate 0 * 9/5 + 32, which equals 32. Therefore, the output of the second print statement is 32.

Submit
11. What does the following code do?   def a(b, c, d): pass

Explanation

The given code defines a function named "a" with three parameters "b", "c", and "d". However, the function body is empty, indicated by the "pass" statement. Therefore, the function does nothing when called.

Submit
12.  What gets printed? x = 4.5 y = 2 print x//y

Explanation

The given code snippet calculates the floor division of x by y and prints the result. Floor division is a division operation that rounds the quotient down to the nearest integer. In this case, x is 4.5 and y is 2. When we perform floor division (//) on 4.5 and 2, the result is 2.0 because 4.5 divided by 2 is 2.25, and floor division rounds it down to the nearest integer which is 2. Therefore, the output of the code is 2.0.

Submit
13. What gets printed? x = True y = False z = 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 evaluating the logical operators in a specific order. Since the "and" operator has higher precedence than the "or" operator, it will be evaluated first. In this case, y and z both evaluate to False, so the expression y and z is False. Then, the x or False expression evaluates to True. Therefore, the if statement condition is True, and the code will print "yes".

Submit
14. If you have a variable "example", how do you check to see what type of variable you are working with?

Explanation

To check the type of variable "example", the correct way is to use the "type()" function. The "type(example)" statement will return the type of the variable "example".

Submit
15. What is the difference between r+ and w+ modes?

Explanation

The difference between r+ and w+ modes is that in r+ mode, the pointer is initially placed at the beginning of the file, allowing both reading and writing operations. On the other hand, in w+ mode, the pointer is also initially placed at the beginning of the file, but it is set to the end of the file as soon as any write operation is performed. Therefore, in w+ mode, only writing operations are allowed.

Submit
16. What does the code below do? sys.path.append('/root/mods')

Explanation

The code `sys.path.append('/root/mods')` adds a new directory to search for Python modules that are imported.

Submit
17. What is the output of the below program?   def power(x, y=2):     r = 1     for i in range(y):        r = r * x     return r print power(3) print power(3, 3)

Explanation

The program defines a function called "power" that takes two parameters, "x" and "y". The default value for "y" is 2. Inside the function, it initializes a variable "r" to 1. It then enters a loop that iterates "y" times. In each iteration, it multiplies "r" by "x". Finally, it returns the value of "r".

In the first print statement, the function is called with the argument 3. Since the default value of "y" is 2, the function calculates 3^2 which is 9.

In the second print statement, the function is called with the arguments 3 and 3. The function calculates 3^3 which is 27.

Therefore, the output of the program is 9 and 27.

Submit
18. Which of the following will print True?
a = foo(2)
b = foo(3)
print(a < b)

Explanation

The correct answer is the class foo with the __lt__ method that returns True if self.x is less than other.x, and False otherwise. This is because the expression a

Submit
19. Can one block of except statements handle multiple exception?

Explanation

Yes, one block of except statements can handle multiple exceptions by listing them within the parentheses after the "except" keyword. In this case, the exceptions being handled are TypeError and SyntaxError. This means that if either of these exceptions occur in the code, the block of code within the except statement will be executed.

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

Explanation

When a function does not return any value, it is considered to have a return type of "void". In programming languages like C, C++, and Java, "void" is used to indicate that a function does not return any value. However, in a shell environment, when a function with a void return type is executed, it does not throw any specific value. Therefore, the default value thrown by a void function when executed in a shell is "None".

Submit
21.  Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?

Explanation

When print(A + B) is executed, the __add__() function is called to perform the addition of objects A and B. Then, the __str__() function is called to convert the result of the addition into a string representation, which is then printed.

Submit
22. What gets printed? class parent: def __init__(self, param): self.v1 = param class child(parent): def __init__(self, param): self.v2 = param obj = child(11) print "%d %d" % (obj.v1, obj.v2)

Explanation

The given code will generate an error because the child class does not call the constructor of the parent class. As a result, the parent class's attributes, including v1, are not initialized. Therefore, when trying to print obj.v1, it will result in an error.

Submit
23. How do you get the current position within the file?

Explanation

The correct answer is fp.tell(). The fp.tell() function is used to get the current position within the file. It returns an integer representing the current position in the file, which can be used for various file operations such as reading or writing data at a specific position. This function is commonly used in file handling operations to keep track of the file pointer's position.

Submit
24. Which of the following returns a string that represents the present working directory?

Explanation

The correct answer is os.getcwd(). This function is used in the os module in Python to get the current working directory as a string. It returns a string that represents the present working directory. The other options, os.cwd(), os.getpwd(), and os.pwd(), do not exist in the os module and therefore are not valid functions to use for getting the current working directory.

Submit
25. If PYTHONPATH is set in the environment, which directories are searched for modules? A) PYTHONPATH directory B) current directory C) home directory D) installation dependent default path

Explanation

When PYTHONPATH is set in the environment, the directories that are searched for modules are the PYTHONPATH directory, the current directory, and the installation dependent default path. This means that when importing modules, Python will first look in the directory specified by the PYTHONPATH variable, then in the current directory, and finally in the default path that is determined by the Python installation.

Submit
26. If you had a statement like, "f = open("test.txt","w")", what would happen to the file as soon as that statement is executed?

Explanation

When the statement "f = open("test.txt","w")" is executed, it opens the file "test.txt" in write mode. In write mode, if the file already exists, it will be truncated, meaning its contents will be erased. Therefore, the correct answer is that the file's contents will be erased.

Submit
27. What gets printed? print r"\nwoow"

Explanation

The given answer is incorrect. The correct answer is "the text like exactly like this: woow". This is because the string "r"woow"" is printed exactly as it is, including the characters "r", "", and "woow".

Submit
28. Assuming the filename for the code below is /usr/lib/python/person.py and the program is run as:  python /usr/lib/python/person.py  What gets printed? class Person: def __init__(self): pass def getAge(self): print __name__ p = Person() p.getAge()

Explanation

When the program is run, it creates an instance of the Person class and calls the getAge method on that instance. Inside the getAge method, it prints the value of __name__. In this case, since the program is being run directly as the main module, the value of __name__ is "__main__". Therefore, when the program is run, "__main__" gets printed.

Submit
29. How many except statements can a try-except block have?

Explanation

A try-except block can have more than zero except statements. The purpose of a try-except block is to handle exceptions that may occur within the try block. Each except statement specifies the type of exception it can handle. By having multiple except statements, we can handle different types of exceptions separately and provide appropriate error handling for each. This allows for more robust and specific exception handling within the try-except block.

Submit
30. What gets printed? class A: def __init__(self, a, b, c): self.x = a + b + c a = A(1,2,3) b = getattr(a, 'x') setattr(a, 'x', b+1) print a.x

Explanation

The code defines a class A with an initializer that takes three arguments and assigns their sum to the instance variable x. Then, an instance of class A is created with arguments 1, 2, and 3. The variable b is assigned the value of the attribute x of the instance a. Then, the attribute x of the instance a is set to the value of b plus 1. Finally, the value of the attribute x of the instance a is printed, which is 7.

Submit
31. Is the following code valid?   try:     # Do something except:     # Do something finally:     # Do something

Explanation

The given code is not valid because the "finally" block cannot be used together with the "except" block. The "finally" block is used to specify code that will be executed regardless of whether an exception occurs or not. On the other hand, the "except" block is used to handle specific exceptions that may occur within the try block. These two blocks cannot be used together in the same try-except statement.

Submit
32. In python 2.6 or earlier, the code will print error type 1 if access secure system raises an exception of either AccessError type or SecurityError type try:   accessSecureSystem() except AccessError, SecurityError:   print "error type 1" continueWork()

Explanation

In Python 2.6 or earlier, the code will not print "error type 1" if accessSecureSystem() raises an exception of either AccessError type or SecurityError type. The correct syntax to catch multiple exceptions in Python is to enclose them in parentheses and separate them with commas. So, the correct code would be:

try:
accessSecureSystem()
except (AccessError, SecurityError):
print "error type 1"

continueWork()

Since the exceptions are not caught correctly in the given code, the correct answer is False.

Submit
33. What gets printed? print "\x48\x49!"

Explanation

The given code prints "HI!" because "\x48" represents the ASCII value for the letter 'H' and "\x49" represents the ASCII value for the letter 'I'. Therefore, when these values are printed together, it forms the string "HI!".

Submit
34. What is returned by math.expm1(p)?

Explanation

The expression math.expm1(p) returns the value of e raised to the power of p, subtracted by 1. This is equivalent to (math.e ** p) - 1.

Submit
35. All keywords in Python are in:

Explanation

In Python, keywords are predefined reserved words that cannot be used as identifiers (variable names, function names, etc.). These keywords are written in lowercase, and there are no keywords that are written in uppercase or capitalized. Therefore, the correct answer is "none."

Submit
36. What happens if no arguments are passed to the seek function?

Explanation

If no arguments are passed to the seek function, an error occurs. This is because the seek function requires at least one argument to specify the position in the file where the file pointer should be moved. Without any arguments, the function does not know where to move the file pointer, resulting in an error.

Submit
37. Which of the following will run without errors?

Explanation

The first option, round(45.8), will run without errors because it is using the round() function with only one argument, which is the number to be rounded. The second option, round(6352.894,2), will also run without errors because it is using the round() function with two arguments, the number to be rounded and the number of decimal places to round to.

Submit
38. What is the order of precedence in python? i) Parentheses ii) Exponential iii) Division iv) Multiplication v) Addition vi) Subtraction

Explanation

The order of precedence in Python determines the sequence in which different operators are evaluated in an expression. In this case, the correct answer is "i,ii,iv,iii,v,vi". This means that parentheses have the highest precedence, followed by exponential operations, then multiplication and division, and finally addition and subtraction. This order ensures that calculations are performed correctly according to the rules of mathematics.

Submit
39. Which of the following is incorrect?

Explanation

The given statement x = 03964 is incorrect because the leading zero indicates an octal number in Python. However, the octal number system only uses digits 0-7. Therefore, the digit 9 is not valid in octal, making the statement incorrect.

Submit
40. Which of the following results in a SyntaxError?

Explanation

The given answer '3\',"He said, "Yes!""' results in a SyntaxError because it contains multiple quotation marks within the string without escaping them properly. In Python, when using quotation marks within a string, they need to be escaped by using a backslash (\) before each quotation mark. In this case, the quotation marks around "Yes!" are not escaped, causing a syntax error. Additionally, the backslash before the comma (,) is not necessary and also contributes to the syntax error.

Submit
View My Results

Quiz Review Timeline (Updated): Jul 1, 2023 +

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

  • Current Version
  • Jul 01, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Apr 15, 2016
    Quiz Created by
    PriyangaGT
Cancel
  • All
    All (40)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
 Operators with the same precedence are evaluated in which...
 What sequence of numbers is printed?...
Python supports the creation of anonymous functions at runtime, using...
What is the output of this program?...
Which of the following statements is NOT true about Python?
Which of the following data structures can be used with the...
What is the output of below program? def writer():  ...
What is output of print(math.pow(3, 2))?
What is the output of the below program?...
What is the output of the below program? def C2F(c):   ...
What does the following code do?   def a(b, c, d): pass
 What gets printed? x = 4.5 y = 2 print x//y
What gets printed? ...
If you have a variable "example", how do you check to see...
What is the difference between r+ and w+ modes?
What does the code below do? sys.path.append('/root/mods')
What is the output of the below program?...
Which of the following will print True? a = foo(2) ...
Can one block of except statements handle multiple exception?
Given a function that does not return any value, What value is thrown...
 Let A and B be objects of class Foo. Which functions are called...
What gets printed?...
How do you get the current position within the file?
Which of the following returns a string that represents the present...
If PYTHONPATH is set in the environment, which directories are...
If you had a statement like, "f =...
What gets printed? print r"\nwoow"
Assuming the filename for the code below is /usr/lib/python/person.py...
How many except statements can a try-except block have?
What gets printed?...
Is the following code valid?...
In python 2.6 or earlier, the code will print error type 1 if access...
What gets printed? print "\x48\x49!"
What is returned by math.expm1(p)?
All keywords in Python are in:
What happens if no arguments are passed to the seek function?
Which of the following will run without errors?
What is the order of precedence in python?...
Which of the following is incorrect?
Which of the following results in a SyntaxError?
Alert!

Advertisement