O_Level | Python | MCQ By. Shivansh Sir

 PYTHON MULTIPLE CHOICE QUESTIONS

PYTHON | MCQ | SET - 1

1. Who developed Python Programming Language?

Ans. Guido van Rossum

(Summary- Python language is designed by a Dutch programmer Guido van Rossum in the Netherlands.)

2. Which type of Programming does Python support?

Ans. All of the Above

(Summary - Python is an interpreted programming language, which supports object-oriented, structured, and functional programming.) 

3. Is Python case sensitive when dealing with identifiers?

Ans. Yes

(Summary - Case is always significant while dealing with identifiers in python.)

4. Which of the following is the correct extension of the Python file?

Ans. .py

(Summary - ‘.py’ is the correct extension of the Python file. Python programs can be written in any text editor. To save these programs we need to save in files with file extension ‘.py’.)

5. Is Python code compiled or interpreted?

Ans. Python code is both compiled and interpreted

(Summary - Many languages have been implemented using both compilers and interpreters, including C, Pascal, and Python.)

6. All keywords in Python are in _________

Ans. Not Mentioned

(Summary - True, False and None are capitalized while the others are in lower case.)

7. What will be the value of the following Python expression?

4 + 3 % 5

Ans. 7

(Summary - The order of precedence is: %, +. Hence the expression above, on simplification results in 4 + 3 = 7. Hence the result is 7.)

8. Which of the following is used to define a block of code in Python language?

Ans. Indentation

(Summary - In Python, to define a block of code we use indentation. Indentation refers to whitespaces at the beginning of the line.)

9. Which keyword is used for function in Python language?

Ans. def

(Summary - The def keyword is used to create, (or define) a function in python.)

10. Which of the following character is used to give single-line comments in Python?

Ans. #

(Summary - To write single-line comments in Python use the Hash character (#) at the beginning of the line. It is also called number sign or pound sign. To write multi-line comments, close the text between triple quotes.)

11. What will be the output of the following Python code?

i = 1

while True:

    if i%3 == 0:

        break

    print(i)

     i + = 1

Ans. Error

(Summary - SyntaxError, there shouldn’t be a space between + and = in +=.)

12. Which of the following functions can help us to find the version of python that we are currently working on?

Ans. sys.version

(Summary - The function sys.version can help us to find the version of python that we are currently working on. It also contains information on the build number and compiler used. For example, 3.5.2, 2.7.3 etc. this function also returns the current date, time, bits etc along with the version.)

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

Ans. lambda

(Summary - Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called lambda. Lambda functions are restricted to a single expression. They can be used wherever normal functions can be used.)

14. What is the order of precedence in python?

Ans. Parentheses, Exponential, Multiplication, Division, Addition, Subtraction

(Summary- For order of precedence, just remember this PEMDAS (similar to BODMAS).)

15. What will be the output of the following Python code snippet if x=1?

x<<2

Ans. 4

(Summary - The binary form of 1 is 0001. The expression x<<2 implies we are performing bitwise left shift on x. This shift yields the value: 0100, which is the binary form of the number 4.)

16. What does pip stand for python?

Ans. Preferred Installer Program

(Summary - pip is a package manager for python. Which is also called Preferred Installer Program.)

17. Which of the following is true for variable names in Python?

Ans. unlimited length

(Summary - Variable names can be of any length.)

18. What are the values of the following Python expressions?

2**(3**2)

 (2**3)**2

 2**3**2

Ans. 512, 64, 512

(Summary - Expression 1 is evaluated as: 2**9, which is equal to 512. Expression 2 is evaluated as 8**2, which is equal to 64. The last expression is evaluated as 2**(3**2). This is because the associativity of ** operator is from right to left. Hence the result of the third expression is 512.)

19. Which of the following is the truncation division operator in Python?

Ans. //

(Summary - // is the operator for truncation division. It is called so because it returns only the integer part of the quotient, truncating the decimal part. For example: 20//3 = 6.)

20. What will be the output of the following Python code?

l=[1, 0, 2, 0, 'hello', '', []]

list(filter(bool, l))

Ans. [1, 2, ‘hello’]

(Summary - The code shown above returns a new list containing only those elements of the list l which do not amount to zero. Hence the output is: [1, 2, ‘hello’].)

21. Which of the following functions is a built-in function in python?

Ans.  print()

(Summary -  The print function is a built-in function which prints a value directly to the system output.)

22. The following python program can work with ____ parameters.

def f(x):
    def f1(*args, **comp):
           print("Computer")
           return x(*args, **comp)
    return f1

Ans. 0

(Summary :- The code shown above shows a general decorator which can work with any number of arguments.)

23. What will be the output of the following Python function?

min(max(False,-3,-4), 2,7)

Ans. False

(Summary :- The function max() is being used to find the maximum value from among -3, -4 and false. Since false amounts to the value zero, hence we are left with min(0, 2, 7) Hence the output is 0 (false).)

24. Which of the following is not a core data type in Python programming?

Ans. Class

(Summary ;- Class is a user-defined data type.)

25. What will be the output of the following Python expression if x=56.236?

print("%.2f"%x)

Ans. 56.24

(Summary :- The expression shown above rounds off the given number to the number of decimal places specified. Since the expression given specifies rounding off to two decimal places, the output of this expression will be 56.24. Had the value been x=56.234 (last digit being any number less than 5), the output would have been 56.23.)


PYTHON | MCQ | SET - 2 

1. What is the definition for packages in Python?

Ans. A folder of python modules

(Summary :- A folder of python programs is called as a package of modules.)

2.What will be the output of the following Python function?

len(["hello",2, 4, 6])

Ans. 4

(Summary :- The function len() returns the length of the number of elements in the iterable. Therefore the output of the function shown above is 4.)

3. What will be the output of the following Python code?

x = 'abcd'

for i in x:

    print(i.upper())

Ans. A

        B

        C

        D

(Summary :- The instance of the string returned by upper() is being printed)

4. What is the order of namespaces in which Python looks for an identifier?

Ans. Python first searches the local namespace, then the global namespace and finally the built-in namespace

5. What will be the output of the following Python code snippet?

for i in [1, 2, 3, 4][::-1]:

    print (i)

Ans. 4 3 2 1

(Summary :- [::-1] reverses the list.)

6. What will be the output of the following Python program?

def foo(x):

    x[0] = ['def']

    x[1] = ['abc']

    return id(x)

q = ['abc', 'def']

print(id(q) == foo(q))

Ans. True

(Summary :- The same object is modified in the function.)

7. What will be the output of the following Python statement?

>>>"a"+"bc"

Ans. abc

(Summary :- + operator is concatenation operator.)

8. Which function is called when the following Python program is executed?

f = foo()

format(f)

Ans. _str_()

(Summary :- Both str(f) and format(f) call f._str_().)

9. Which one of the following is not a keyword in Python language?

Ans. eval

(Summary :- eval can be used as a variable.)

10.What will be the output of the following Python code?

class tester:

        def _init_(self, id):

            self.id = str(id)

            id="224"

>>>temp = tester(12)

>>>print(temp.id)

Ans. 12

(Summary :- Id in this case will be the attribute of the instance.)

11.Which module in the python standard library parses options received from the command line?

Ans. getopt

(Summary :- getopt parses options received from the command line.)

12. What arithmetic operators cannot be used with strings in Python?

Ans. ( – )

(Summary :- + is used to concatenate and * is used to multiply strings.)

13. Which of the following statements is used to create an empty set in Python?

Ans. set()

(Summary :- { } creates a dictionary not a set. Only set() creates an empty set.)

14. What will be the value of ‘result’ in following Python program?

list1 = [1,2,3,4]

list2 = [2,4,5,6]

list3 = [2,6,7,8]

result = list()

result.extend(i for i in list1 if i not in (list2+list3) and i not in result)

result.extend(i for i in list2 if i not in (list1+list3) and i not in result)

result.extend(i for i in list3 if i not in (list1+list2) and i not

Ans. [1, 3, 5, 7, 8] 

(Summary :- Here, ‘result’ is a list which is extending three times. When first time ‘extend’ function is called for ‘result’, the inner code generates a generator object, which is further used in ‘extend’ function. This generator object contains the values which are in ‘list1’ only (not in ‘list2’ and ‘list3’). Same is happening in second and third call of ‘extend’ function in these generator object contains values only in ‘list2’ and ‘list3’ respectively.

So, ‘result’ variable will contain elements which are only in one list (not more than 1 list).)

15. To add a new element to a list we use which Python command?

Ans. list1.append(5)

(Summary :- We use the function append to add an element to the list.)

16. Which one of the following is the use of function in python?

Ans. Functions are reusable pieces of programs

(Summary :- Functions are reusable pieces of programs. They allow you to give a name to a block of statements, allowing you to run that block using the specified name anywhere in your program and any number of times.)

17. What is the maximum possible length of an identifier in Python?

Ans. No Limit

(Summary :- Identifiers can be of any length.)

18. What will be the output of the following Python code?

x = 'abcd'

for i in range(len(x)):

    print(i)

Ans. 0 1 2 3

(Summary :- i takes values 0, 1, 2 and 3.)

19. What will be the output of the following Python code snippet?

Ans. True

(Summary :- The code shown above is used to check whether a particular item is a part of a given set or not. Since ‘a’ is a part of the set z, the output is true. Note that this code would result in an error in the absence of the quotes.)

20. What will be the output of the following Python expression?

round(4.576)

Ans. 5

(Summary :- This is a built-in function which rounds a number to give precision in decimal digits. In the above case, since the number of decimal places has not been specified, the decimal number is rounded off to a whole number. Hence the output will be 5.)


For Any Doubt Clear on Telegram Discussion Group

Join Us On Social Media
For Any Query WhatsApp Now

Comments