Python MCQs - Variables And Operators
This section focuses on the "Variables And Operators" of the Python programming. These Multiple Choice Questions (mcq) should be practiced to improve the Python programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Which one of the following is correct way of declaring and initialising a variable, x with value 5?
A. int x
x=5
B. int x=5
C. x=5
D. declare x=5
View Answer
Ans : C
Explanation: One of the following is correct way of declaring and initialising a variable, x with value 5 is x=5.
2. Which of the following is not valid variable name in Python?
A. _var
B. var_name
C. var11
D. 11var
View Answer
Ans : D
Explanation: 11var is not valid variable name in Python.
3. Which one is false regarding local variables?
A. These can be accessed only inside owning function
B. Any changes made to local variables does not reflect outside the function.
C. These remain in memory till the program ends
D. None of the above
View Answer
Ans : C
Explanation: These remain in memory till the program ends is false regarding local variables.
4. Which one is false regarding global variables?
A. Global variables can only be read inside the function declaring the variable as global inside the function.
B. Global variables remain in memory till the end of the program
C. Global variables are those which are declared in global scope.
D. None of the above
View Answer
Ans : A
Explanation: Global variables can only be read inside the function declaring the variable as global inside the function is false regarding local variables.
5. How many local and global variables are there in the following Python code?
var1=5
def fn():
var1=2
var2=var1+5
var1=10
fn()
A. 1 local, 1 global variables
B. 1 local, 2 global variables
C. 2 local, 1 global variables
D. 2 local, 2 global variables
View Answer
Ans : D
Explanation: 2 local, 2 global variables are there in the following Python code
6. What will be the output of the following Python code?
def fn():
global var1
var1=10
var1=var1*5
print(var1)
var1=5
fn()
print(var1)
A. 25
5
B. 50
5
C. 25
25
D. 50
50
View Answer
Ans : D
Explanation: The output of the following code is 50, 50.
7. What will be the output of the following Python code?
x=5
def function1():
global x
y=x+x*2
print(y)
x=7
function1()
print(x)
A. 21
7
B. 15
5
C. 21
5
D. 15
7
View Answer
Ans : A
Explanation: The output of the following code is 21, 7.
8. What will be the value of variable y after execution of following Python code?
x="55"
y=x + str(9)
A. 64
B. 9
C. 559
D. Error
View Answer
Ans : C
Explanation: The output of the following code is 559.
9. Which of the following is incorrect regarding variables in Python?
A. Variable names in Python cannot start with number. However, it can contain number in any other position of variable name.
B. Variable names can start with an underscore.
C. Data type of variable names should not be declared
D. None of the above
View Answer
Ans : D
Explanation: All of the above options are correct regarding variables in Python.
10. Which of the following will give error?
A. a=b=c=1
B. a,b,c=1
C. a,b,c=1, python, 1.5
D. None of the above
View Answer
Ans : B
Explanation: a,b,c=1 give error in Python.
11. How many keywords are there in python 3.7?
A. 32
B. 33
C. 31
D. 30
View Answer
Ans : B
Explanation: There are 33 keywords in Python 3.7 . Keywords are reserved words that can not be used as variables. So, Option B is correct.
12. What is the maximum length of an identifier in python?
A. 32
B. 31
C. 63
D. None of the above
View Answer
Ans : D
Explanation: In pyhton Identifier can be of any length. So, Option D is correct.
13. All keyword in python are in
A. Lowercase
B. Uppercase
C. Both uppercase & Lowercase
D. None of the above
View Answer
Ans : C
Explanation: All keywords in python are in lowercase except True, False and None. So, Option C is correct.
14. Which of the following statment is False?
A. Variable names can be arbitrarily long.
B. They can contain both letters and numbers.
C. Variable name can begin with underscore.
D. Variable name can begin with number.
View Answer
Ans : D
Explanation: Variable name can not begin with number it can begin with letter or underscore. So, Option D is correct.
15. Which of the following is a valid variable?
A. var@
B. 32var
C. class
D. abc_a_c
View Answer
Ans : D
Explanation: Variable name should not be keyword, cannot begin with digit and should not contain special symbol. Hence D is the correct identifier or variable. So, Option D is correct.
16. Why are local variable names beginning with an underscore discouraged?
A. they confuse the interpreter
B. they are used to indicate a private variables of a class
C. they are used to indicate global variables
D. they slow down execution
View Answer
Ans : B
Explanation: local variable names beginning with an underscore discouraged because they are used to indicate a private variables of a class. So, Option B is correct.
17. Which of these are keyword?
A. in
B. is
C. assert
D. All of the above
View Answer
Ans : D
Explanation: All "in", "is" and "assert" are keywords. So, Option D is correct.
18. What is the output of the following code : print 5//2
A. 2
B. 2.5
C. 2.0
D. Error
View Answer
Ans : A
Explanation: Floor Division "//" - The division of operands where the result is the quotient in which the digits after the decimal point are removed. So in this case we get 2 as a answer. So, Option A is correct.
19. Arrange this in order of Precedence.
1. Exponentiation
2. Parentheses
3. Multiplication and Division
4. Addition and Subtraction
A. 1,2,3,4
B. 2,1,3,4
C. 1,2,4,3
D. 2,3,1,4
View Answer
Ans : B
Explanation: Parentheses have the highest precedence then exponentiation and then Division and Multiplication and then Addition and Subtraction. So, Option B is correct.
20. Which of this statment will give an error?
A. a++
B. ++a
C. a+=1
D. Both A and B
View Answer
Ans : A
Explanation: a++ is invalid syntex in python. So, Option A is correct.
21. Which operator is used to calculate power of x raise to y.
A. x^y
B. x*y
C. x**y
D. None of the above
View Answer
Ans : C
Explanation: x**y is used to calculate power of x raise to y. So, Option C is correct.
22. Operators with the same precedence are evaluated from?
A. Left to Right
B. Right to Left
C. Depends on Compiler
D. None of the above
View Answer
Ans : A
Explanation: Operators with the same precedence are evaluated from left to right. So, Option A is correct.
23. "in" is a operator in python?
A. True
B. False
C. Neither true nor false
D. None of the above
View Answer
Ans : A
Explanation: "in" is a python membership operator which evaluates to true if it finds a variable in the specified sequence and false otherwise. For eg a in b if a is member of sequence b then it will return 1. So, Option A is correct.
24. What will be the output of statment 2**2**2**2
A. 16
B. 256
C. 32768
D. 65536
View Answer
Ans : D
Explanation: The statment is equivalent to 2^16. So, Option D is correct.
25. What will be the output of the following code
a = 1
b = ++a
print(b is a)
A. True
B. False
C. Error
D. None of the above
View Answer
Ans : A
Explanation: The output of the above statment will be True. Since the id of both a and b will be same. So, Option A is correct.
Also check :
Discussion