Python Programming MCQ - Variables And Operators
13. 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.
14. 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.
15. 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
16. 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.
17. 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.
18. 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.
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