Python Multiple Choice Questions and Answers: Our team took some time to write down these online Python MCQ quizzes for you. This is to help you check your learning progress as well as to test your Python knowledge. These python MCQ are popular and the are frequently asked questions in exams as well as job interviews. So, lets start practicing now for exams, online tests, quizzes & interviews!

Python MCQ Questions for Quiz & Online Test 2022/2023

1) Which of the following statements is/are TRUE in respect of the Python programming language?

Statement 1: Python is an interpreted, high-level, general-purpose programming language.

Statement 2: Python provides high-level data structures along with dynamic binding and typing for Rapid Application Development and deployment.

Statement 3: Python is a Statically typed Programming language.

Options:

  1. Only Statement 1
  2. Statement 1 and 2
  3. Statement 1 and 3
  4. All Statements are Correct

Answer: B: Statement 1 and 2

Explanation: Python is an interpreted, high-level, general-purpose programming language. Being an interpreted language, it executes every block of code line by line, and thus type-checking is done while executing the code. Hence, it is a dynamically typed language.

Moreover, Python offers high-level data structures, together with dynamic binding and typing, allows a large community of developers for Rapid Application Development and Deployment.

2) What is the full form of PEP?

Options:

  1. Python Enhancement Proposal
  2. Python Enchantment Proposal
  3. Programming Enhancement Proposition
  4. Python Enrichment Program

Answer: A: Python Enhancement Proposal

Explanation: A PEP, also known as Python Enhancement Proposal, is an official design document offering information to the community of Python developers or depicting a new feature for Python or its methods.

3) Which of the following statements is/are NOT correct regarding Memory Management in Python?

Statement 1: Python Memory Manager handles the management regarding memory in Python

Statement 2: Python uses CMS (Concurrent Mark Sweep) approach as its Garbage Collection technique.

Statement 3: Python offers a core garbage collection to recycle the vacant memory for the private heap space.

Options:

  1. Only Statement 3
  2. Statement 1 and 3
  3. Statement 2 and 3
  4. Only Statement 2

Answer: D: Only Statement 2

Explanation: Python employs the Reference Counting algorithm as its Garbage Collection technique. This technique is highly efficient and straightforward; however, it can’t detect the reference cycle. Hence, Python has an additional algorithm known as Generational Cyclic (GC), which only deals with the reference cycle.

4) Which of the following statements is/are NOT correct in respect to Python namespaces?

Statement 1: Python implements the namespace in the form of Array.

Statement 2: Python namespaces are classified into three types – local, global and built-in.

Statement 3: A Python namespace ensures that the names of the objects in a program are unique and can be utilized, deprived of any inconsistency.

Options:

  1. Only Statement 1
  2. Only Statement 3
  3. Statement 1 and 2
  4. Statement 1 and 3

Answer: A: Only Statement 1

Explanation: The namespaces in Python are implemented in the form of Dictionaries where ‘key’ denotes the ‘name’, mapped to a corresponding ‘value’ denoting the ‘object’.

5) Which of the following is invalid in terms of Variable Names?

Options:

  1. _mystr = “Hello World!”
  2. __mystr = “Hello World!”
  3. __mystr__ = “Hello World!”
  4. None of the mentioned

Answer: D: None of the mentioned

Explanation: All Statements will be executed successfully. However, the code readability will also be reduced.

6) In respect to the scope in Python, which of the following statements is/are TRUE?

Statement 1: A variable created within a function belongs to the local scope and can be used outside that function.

Statement 2: A local scope is referred to the object present through the execution of code since its inception.

Statement 3: A local scope is referred to the local object present in the current function.

Options:

  1. Only Statement 2
  2. Statement 1 and 3
  3. Only Statement 3
  4. All Statements are True

Answer: C: Only Statement 3

Explanation: Local scope, also known as function scope, is the block of code or body of any function in Python. This scope consists of the names that we define within the function. These names will only be observable from the function code. It is created at the function call, not at the definition of the function, so that we will have as many distinct local scopes as the calls in function. This is true even when we call the same function more than one time or recursively. Every call will return a new local scope.

7) What will the following snippet of code print?

Code:

# assigning a variable  
myint = 10  
  
# defining a function  
def myfunction():  
    # reassigning a variable  
    myint = 20  
  
# calling the function  
myfunction()  
  
# printing the value of the variable  
print(myint)  

Options:

  1. 10
  2. 20
  3. 0
  4. Traceback Error

Answer: A: 10

Explanation: 10 is printed.

Whenever we reallocate a global variable in the local scope of a function, the relocation only holds inside the local scope. The variable goes back to the global value when the code returns the global scope.

8) What will the following snippet of code yield?

Code:

# assigning a variable  
myint = 21  
  
# using if False statement  
if False:  
    # reassigning a variable  
    myint = 34  
  
# defining a function  
def myfunction():  
    if True:  
        # reassigning a variable  
        myint = 65  
  
# calling the function  
myfunction()  
  
# printing the value of the variable  
print(myint)  

Options:

  1. 65
  2. Traceback Error
  3. 21
  4. 34

Answer: C: 21

Explanation: 21 is printed.

The variable, myint, is initially assigned to 21. The if False statement estimates to False, so the myint = 34 reassigned never occurs. The reassignment within the myfunction() function does occur inside the local scope, not the global one. When we try to access the variable myint in the global scope, the variable’s value remains unchanged at 21.

9) Among the following statements based on the difference between lists and tuples, which one statement is TRUE?

Statement 1: List is a sequence data structure, whereas Tuple is not.

Statement 2: Lists are immutable; however, Tuples are mutable.

Statement 3: Tuple is a sequence data structure, whereas List is not.

Statement 4: Tuples are immutable; however, Lists are mutable.

Options:

  1. Statement 1
  2. Statement 4
  3. Statement 2
  4. Statement 3

Answer: B: Statement 4

Explanation: Apart from many similarities, one of the significant differences between the two is that the Lists are mutable, whereas Tuples are immutable. This statement implies that we can modify or change the values of a list; however, we can’t alter the values of a Tuple.

10) What will be the output of the following snippet of code?

Code:

# defining a list  
my_list = [7, 9, 8, 2, 5, 0, 1, 3, 6]  
  
# using the pop() function  
my_list.pop(2)  
  
# printing the final list  
print(my_list)  

Options:

  1. [7, 9, 2, 5, 0, 1, 3, 6]
  2. [7, 9, 8, 2, 5, 0, 3, 6]
  3. [7, 8, 2, 5, 0, 1, 3, 6]
  4. [7, 9, 8, 2, 5, 0, 1, 6]

Answer: A: [7, 9, 2, 5, 0, 1, 3, 6]

Explanation: The pop() function is used to remove the element from the list. The function’s parameter is the index value, n, of the data element to be removed from the list.

Thus, the pop(n) function removes the nth element from the list. As in the above case, the index number is 2. Hence, the pop(2) function removes the second element from my_list, i.e., 8.

Similar Topics

  • Download python quiz questions and answers pdf
  • Samples of python MCQ online test
  • Test python multiple choice questions code
  • List of python 3 quiz and answers
  • Complete python interview questions
  • Sanfoundry python questions
  • Python functions quiz
  • What will be the output of the following python code

11) What is self in Python?

Self is an instance or an object of a class. In Python, this is explicitly included as the first parameter. However, this is not the case in Java where it’s optional. It helps to differentiate between the methods and attributes of a class with local variables.

The self-variable in the init method refers to the newly created object while in other methods, it refers to the object whose method was called.

12) How can you generate random numbers in Python?

Random module is the standard module that is used to generate a random number. The method is defined as:

import random  
random.random  

The statement random.random() method return the floating point number that is in the range of [0, 1). The function generates random float numbers. The methods that are used with the random class are the bound methods of the hidden instances. The instances of the Random can be done to show the multi-threading programs that creates a different instance of individual threads. The other random generators that are used in this are:

randrange(a, b): it chooses an integer and define the range in-between [a, b). It returns the elements by selecting it randomly from the range that is specified. It doesn’t build a range object.

uniform(a, b): it chooses a floating point number that is defined in the range of [a,b).Iyt returns the floating point number

normalvariate(mean, sdev): it is used for the normal distribution where the mu is a mean and the sdev is a sigma that is used for standard deviation.

The Random class that is used and instantiated creates independent multiple random number generators.

13) What is PYTHONPATH?

PYTHONPATH is an environment variable which is used when a module is imported. Whenever a module is imported, PYTHONPATH is also looked up to check for the presence of the imported modules in various directories. The interpreter uses it to determine which module to load.

14) What are python modules? Name some commonly used built-in modules in Python?

Python modules are files containing Python code. This code can either be functions classes or variables. A Python module is a .py file containing executable code.

Some of the commonly used built-in modules are:

  • os
  • sys
  • math
  • random
  • data time
  • JSON

15) What is the difference between range & xrange?

For the most part, xrange and range are the exact same in terms of functionality. They both provide a way to generate a list of integers for you to use, however you please. The only difference is that range returns a Python list object and x range returns an xrange object.

This means that xrange doesn’t actually generate a static list at run-time like range does. It creates the values as you need them with a special technique called yielding. This technique is used with a type of object known as generators. That means that if you have a really gigantic range you’d like to generate a list for, say one billion, xrange is the function to use.

This is especially true if you have a really memory sensitive system such as a cell phone that you are working with, as range will use as much memory as it can to create your array of integers, which can result in a Memory Error and crash your program. It’s a memory hungry beast.

16) What advantages do NumPy arrays offer over (nested) Python lists?

  • Python’s lists are efficient general-purpose containers. They support (fairly) efficient insertion, deletion, appending, and concatenation, and Python’s list comprehensions make them easy to construct and manipulate.
  • They have certain limitations: they don’t support “vectorized” operations like elementwise addition and multiplication, and the fact that they can contain objects of differing types mean that Python must store type information for every element, and must execute type dispatching code when operating on each element.
  • NumPy is not just more efficient; it is also more convenient. We get a lot of vector and matrix operations for free, which sometimes allow one to avoid unnecessary work. And they are also efficiently implemented.
  • NumPy array is faster and we get a lot built in with NumPy, FFTs, convolutions, fast searching, basic statistics, linear algebra, histograms, etc.

17) Mention what the Django templates consist of.

The template is a simple text file. It can create any text-based format like XML, CSV, HTML, etc. A template contains variables that get replaced with values when the template is evaluated and tags (% tag %) that control the logic of the template.

Django templates
Django templates

18) Explain the use of session in Django framework?

Django provides a session that lets the user store and retrieve data on a per-site-visitor basis. Django abstracts the process of sending and receiving cookies, by placing a session ID cookie on the client side, and storing all the related data on the server side.

Django framework
Django framework

So, the data itself is not stored client side. This is good from a security perspective.

In summary, there are several other Python Basics (Multiple Choice QuestionsMCQ – Python Interview objective questions. You can learn more Python Quiz from the articles below.