0% found this document useful (0 votes)
446 views

MCQ On Python

This document contains a question bank with 25 multiple choice questions about Python concepts like variables, data types, operators, and control flow. It also includes 5 additional practice questions. The questions cover topics such as variable naming rules, data types like strings, lists, tuples, and dictionaries, operators and precedence, and while loops. The answers provided explain the reasoning behind each multiple choice response.

Uploaded by

S Gayatri Dora
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
446 views

MCQ On Python

This document contains a question bank with 25 multiple choice questions about Python concepts like variables, data types, operators, and control flow. It also includes 5 additional practice questions. The questions cover topics such as variable naming rules, data types like strings, lists, tuples, and dictionaries, operators and precedence, and while loops. The answers provided explain the reasoning behind each multiple choice response.

Uploaded by

S Gayatri Dora
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Question Bank

* MCQ Questions
1. Is Python case sensitive when dealing with identifiers?
a) yes
b) no
c) machine dependent
d) none of the mentioned
Answer: a
Explanation: Case is always significant.

2. What is the maximum possible length of an identifier?


a) 31 characters
b) 63 characters
c) 79 characters
d) none of the mentioned
Answer: d
Explanation: Identifiers can be of any length.

3. Which of the following is invalid?


a) _a = 1
b) a = 1
c) str = 1
d) none of the mentioned
Answer: d
Explanation: All the statements will execute successfully but at the cost of reduced
readability.

4. Which of the following is an invalid variable?


a) my_string_1
b) 1st_string
c) foo
d) _
Answer: b
Explanation: Variable names should not start with a number.

5. Why are local variable names beginning with an underscore discouraged?


a) they are used to indicate a private variables of a class
b) they confuse the interpreter
c) they are used to indicate global variables
d) they slow down execution
Answer: a
Explanation: As Python has no concept of private variables, leading underscores are
used to indicate variables that must not be accessed from outside the class.
6. Which of the following is not a keyword?
a) val
b) int
c) continue
d) pass
Answer: a
Explanation: eval can be used as a variable.
7. All keywords in Python are in
a) lower case
b) UPPER CASE
c) Capitalized
d) None of the mentioned
Answer: d
Explanation: True, False and None are capitalized while the others are in lower case.

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


a) unlimited length
b) all private members must have leading and trailing underscores
c) underscore and ampersand are the only two special characters allowed
d) none of the mentioned
Answer: a
Explanation: Variable names can be of any length.

9. Which of the following is an invalid statement?


a) abc = 1,000,000
b) a b c = 1000 2000 3000
c) a,b,c = 1000, 2000, 3000
d) a_b_c = 1,000,000
Answer: b
Explanation: Spaces are not allowed in variable names.

10. Which of the following cannot be a variable?


a) init
b) in
c) it
d) on
Answer: b
Explanation: in is a keyword.

11. What is the output of print 0.1 + 0.2 == 0.3?


a) True
b) False
c) Machine dependent
d) Error
View Answer
Answer: b
Explanation: Neither of 0.1, 0.2 and 0.3 can be represented accurately in binary. The
Round off errors from 0.1 and 0.2 accumulate and hence there is a difference
of 5.5511e-17 between (0.1 + 0.2) and 0.3.

12. Which of the following is not a complex number?


a) k = 2 + 3j
b) k = complex(2, 3)
c) k = 2 + 3l
d) k = 2 + 3J
Answer: c
Explanation: l (or L) stands for long.
13. What is the type of inf?
a) Boolean
b) Integer
c) Float
d) Complex
Answer: c
Explanation: Infinity is a special case of floating point numbers. It can be obtained by
float(‘inf’).

14. Which of the following statements create a dictionary?


a) d = {}
b) d = {“john”:40, “peter”:45}
c) d = {40:”john”, 45:”peter”}
d) All of the mentioned
Answer: d
Explanation: Dictionaries are created by specifying keys and values.

15. What will be the output of the following Python code snippet?
d = {"john":40, "peter":45}
a) “john”, 40, 45, and “peter”
b) “john” and “peter”
c) 40 and 45
d) d = (40:”john”, 45:”peter”)
Answer: b
Explanation: Dictionaries appear in the form of keys and values.

16. What will be the output of the following Python code snippet?
d = {"john":40, "peter":45}
"john" in d

a) True
b) False
c) None
d) Error

Answer: a
Explanation: In can be used to check if the key is int dictionary.

17. What will be the output of the following Python code snippet?
d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 == d2
a) True
b) False
c) None
d) Error
Answer: b
Explanation: If d2 was initialized as d2 = d1 the answer would be true.
18. What will be the output of the following Python code snippet?
d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 > d2
a) True
b) False
c) Error
d) None
Answer: c
Explanation: Arithmetic > operator cannot be used with dictionaries.

19. Which of the following is a Python tuple?


a) [1, 2, 3]
b) (1, 2, 3)
c) {1, 2, 3}
d) {}
Answer: b
Explanation: Tuples are represented with round brackets.

20. Suppose t = (1, 2, 4, 3), which of the following is incorrect?


a) print(t[3])
b) t[3] = 45
c) print(max(t))
d) print(len(t))
Answer: b
Explanation: Values cannot be modified in the case of tuple, that is, tuple is
immutable.

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


>>>t=(1,2,4,3)
>>>t[1:3]
a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
Answer: c
Explanation: Slicing in tuples takes place just as it does in strings.

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


>>>t=(1,2,4,3)
>>>t[1:-1]
a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
Answer: c
Explanation: Slicing in tuples takes place just as it does in strings.
23. What will be the output of the following Python code?
>>>t = (1, 2, 4, 3, 8, 9)
>>>[t[i] for i in range(0, len(t), 2)]

a) [2, 3, 9]
b) [1, 2, 4, 3, 8, 9]
c) [1, 4, 8]
d) (1, 4, 8)

Answer: c
Explanation: Execute in the shell to verify.

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


d = {"john":40, "peter":45}
d["john"]

a) 40
b) 45
c) “john”
d) “peter”
Answer: a
Explanation: Execute in the shell to verify.

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


>>>t = (1, 2)
>>>2 * t

a) (1, 2, 1, 2)
b) [1, 2, 1, 2]
c) (1, 1, 2, 2)
d) [1, 1, 2, 2]
Answer: a
Explanation: * operator concatenates tuple.
EXTRA QUESTIONS:
Q. Who developed the Python language?
1. Zim Den
2. Guido van Rossum
3. Niene Stom
4. Wick van Rossum
Answer: (b) Guido van Rossum
Explanation: Python language was developed by Guido van Rossum in the Netherlands.
Q. Which one of the following is the correct extension of the Python file?
1. .py
2. .python
3. .p
4. None of these
Answer: (a) .py
Explanation: ".py" is the correct extension of the Python file.
Q. What do we use to define a block of code in Python language?
1. Key
2. Brackets
3. Indentation
4. None of these
Answer: (c) Indentation
Explanation: Python uses indentation to define blocks of code. Indentations are simply
spaces or tabs used as an indicator that is part of the indent code child. As used in curly
braces C, C++, and Java.
Q. Which character is used in Python to make a single line comment?
1. /
2. //
3. #
4. !
Answer: (c) #
Explanation: "#" character is used in Python to make a single-line comment.
Q. Which of the following is not a keyword in Python language?
1. val
2. raise
3. try
4. with
Answer: (a) val
Explanation: "val" is not a keyword in python language.
Q. Which of the following operators is the correct option for power(ab)?
1. a ^ b
2. a**b
3. a ^ ^ b
4. a ^ * b
Answer: (b) a**b
Explanation: The power operator in python is a**b, i.e., 2**3=8.
Q. Which of the following precedence order is correct in Python?
1. Parentheses, Exponential, Multiplication, Division, Addition, Subtraction
2. Multiplication, Division, Addition, Subtraction, Parentheses, Exponential
3. Division, Multiplication, Addition, Subtraction, Parentheses, Exponential
4. Exponential, Parentheses, Multiplication, Division, Addition, Subtraction
Answer: (a) Parentheses, Exponential, Multiplication, Division, Addition, Subtraction
Explanation: PEMDAS (similar to BODMAS).
Q. Which one of the following has the same precedence level?
1. Division, Power, Multiplication, Addition and Subtraction
2. Division and Multiplication
3. Subtraction and Division
4. Power and Division
Answer: (b) Division and Multiplication
Explanation: None
Q.Which of the following functions is a built-in function in python language?
1. val()
2. print()
3. print()
4. None of these
Answer: (b) print()
Q. Study the following program:
x = 1  
while True:  
    if x % 5 = = 0:  
        break  
    print(x)   
    x + = 1  
What will be the output of this code?
1. error
2. 2 1
3. 0 3 1
4. None of these
Answer: (a) error
Explanation: Syntax error, there should not be a space between + and =.
Q. Study the following statement:
>>>"a"+"bc"  
What will be the output of this statement?
1. a+bc
2. abc
3. a bc
4. a
Answer: (b) abc
Explanation: In Python, the "+" operator acts as a concatenation operator between two
strings.
Q. Study the following program:
i = 1:  
while True:  
    if i%3 == 0:  
        break  
    print(i)  
Which of the following is the correct output of this program?
1. 1 2 3
2. 3 2 1
3. 1 2
4. Invalid syntax
Answer: (d) Invalid syntax
Explanation: Invalid syntax, because this declaration (i = 1:) is wrong.
Q. Study the following program:
a = 1  
while True:  
    if a % 7 = = 0:  
        break  
    print(a)  
    a += 1  
Which of the following is correct output of this program?
1. 1 2 3 4 5
2. 1 2 3 4 5 6
3. 1 2 3 4 5 6 7
4. Invalid syntax
Answer: (b) 1 2 3 4 5 6
Explanation: None
Q. Study the following program:
i = 0  
while i < 5:  
    print(i)  
    i += 1  
    if i == 3:  
        break  
else:  
    print(0)  
What will be the output of this statement?
1. 1 2 3
2. 0 1 2 3
3. 0 1 2
4. 3 2 1
Answer: (c) 0 1 2
Explanation: None
Q. Study the following program:
i = 0  
while i < 3:  
    print(i)  
    i += 1  
else:  
    print(0)  
What will be the output of this statement?
1. 0 1
2. 0 1 2
3. 0 1 2 0
4. 0 1 2 3
Answer: (c) 0 1 2 0
Explanation: None
Q. Study the following program:
z = "xyz"  
j = "j"  
while j in z:  
    print(j, end=" ")  
What will be the output of this statement?
1. xyz
2. No output
3. x y z
4. j j j j j j j..
Answer: (b) No output
Explanation: "j" is not in "xyz".
Q. Study the following program:
x = 'pqrs'  
for i in range(len(x)):  
    x[i].upper()  
print (x)  
Which of the following is the correct output of this program?
1. PQRS
2. pqrs
3. qrs
4. None of these
Answer: (b) pqrs
Explanation: None
Q. Study the following program:
d = {0: 'a', 1: 'b', 2: 'c'}  
for i in d:  
    print(i)  
What will be the output of this statement?
1. a b c
2. 0 1 2
3. 0 a   1 b   2 c
4. None of these above
Answer: (b) 0 1 2
Explanation: None
Q. Study the following program:
d = {0, 1, 2}  
for x in d:  
    print(x)  
What will be the output of this statement?
1. {0, 1, 2} {0, 1, 2} {0, 1, 2}
2. 0 1 2
3. Syntax_Error
4. None of these above

Q. Which of the following option is not a core data type in the python language?
5. Dictionary
6. Lists
7. Class
8. All of the above
Answer: (c) Class
Explanation: Class is not a core data type because it is a user-defined data type.

You might also like