Python Variable MCQ : Set 3

Python Variable MCQ

1). What will be the value of the following variable?
a = 10
a += 5
a -= 3

a) 5
b) 7.5
c) 10
d) 12

Correct answer is d)
Explanation: The code performs arithmetic operations on the variable “a”. The final value of “a” will be 12.

2). What is the scope of a local variable in Python?
a) Restricted to the function where it is defined
b) Accessible from any part of the program
c) Restricted to the class where it is defined
d) None of these

Correct answer is a)
Explanation: A local variable is accessible only within the function or block of code where it is defined.

3). Which of the following statements is true about global variables in Python?
a) They are accessible only within a specific function
b) They can be accessed from any part of the program
c) They can only store numeric values
d) None of these

Correct answer is b)
Explanation: Global variables can be accessed from any part of the program, including functions.

4). What will be the output of the given code?
a = 7
b = 3
print(x // y)

a) 2.5
b) 2
c) 3
d) 2.0

Correct answer is b)
Explanation: The double slash (//) is the floor division operator, which performs integer division and returns the quotient. In this case, 5 // 2 is equal to 2.

5). What is the data type of the following variable?
x = 5.0

a) int
b) float
c) double
d) None of these

Correct answer is b)
Explanation: The variable “x” is assigned a floating-point value, so its data type is float.

6). What will be the output of the given code?
x = “2”
y = 3
print(x * y)

a) 23
b) 6
c) 222
d) TypeError

Correct answer is c)
Explanation: When a string is multiplied by an integer, it repeats the string multiple times. In this case, “2” * 3 results in “222”.

7). What will be the output of the given code?
x = 10
y = “20”
print(x + int(y))

a) 30
b) 1020
c) 102010
d) TypeError

Correct answer is a)
Explanation: The code converts the string “20” to an integer using the int() function and performs addition. The result is 30.

8). Which of the following is an invalid variable name in Python?
a) my-var
b) my_var
c) 123var
d) _var123

Correct answer is a)
Explanation: Variable names cannot contain hyphens (-) in Python. Underscores (_) are allowed.

9). What will be the value of the variable “x” after the execution of the following code?
x = 5
x = x + “2”

a) 7
b) 52
c) TypeError
d) None of these

Correct answer is c)
Explanation: You cannot concatenate a string and an integer using the + operator. It raises a TypeError.

10). Which of the following is the correct way to check the type of a variable “x”?
a) typeof(x)
b) type(x)
c) x.type()
d) None of these

Correct answer is b)
Explanation: The type() function is used to check the type of a variable in Python.

11). What is the output of the following code?
x = 3.8
print(int(x))

a) 3
b) 4
c) 3.0
d) 4.0

Correct answer is a)
Explanation: The int() function converts a floating-point number to an integer by truncating the decimal part. In this case, int(3.8) is equal to 3.

12). What will be the output of the following code?
x = “Hello, World!”
print(x[7:12])

a) Hello
b) World
c) , Wor
d) None of these

Correct answer is b)
Explanation: The code uses slicing to extract the characters from index 7 to 11 (exclusive). This results in the string “World”.

13). Which of the following is true about constant variables in Python?
a) Their values cannot be changed after the assignment
b) They can only store integer values
c) They are declared using the const keyword
d) None of these

Correct answer is d)
Explanation: Python does not have a built-in concept of constant variables. Variables can be reassigned with different values.

14). What will be the output of the following code?
x = 2
y = 3
print(x ** y)

a) 6
b) 8
c) 9
d) 6.0

Correct answer is c)
Explanation: The double asterisk (**) is the exponentiation operator in Python. In this case, 2 ** 3 is equal to 8.

15). Which of the following is an immutable data type in Python?
a) list
b) tuple
c) dictionary
d) set

Correct answer is b)
Explanation: Tuples are immutable, meaning their elements cannot be changed once assigned. Lists, dictionaries, and sets are mutable.

16). What is the output of the following code?
x = “SQATOOLS”
print(x[-3:])

a) OLS
b) LS
c) SQA
d) OOLS

Correct answer is a)
Explanation: Negative indexing starts from the end of the string. x[-3:] extracts the last three characters, resulting in “OLS”.

17). What will be the value of the variable “x” in the given code?
x = 2
x = x * x

a) 2
b) 4
c) 8
d) None of these

Correct answer is b)
Explanation: The code squares the value of “x” and assigns the result back to “x”. So the value of “x” is 4.

18). Which of the following is a valid way to concatenate two strings in Python?
a) str1.add(str2)
b) str1 + str2
c) str1.concat(str2)
d) None of these

Correct answer is b)
Explanation: The + operator in Python is used to concatenate two strings in Python.

19). What is the output of the given code?
x = “Python”
print(x[1:4])

a) Pyt
b) yth
c) thon
d) None of these

Correct answer is b)
Explanation: The code uses slicing to extract the characters from index 1 to 3 (exclusive). This results in the string “yth”.

20). What will be the output of the given code?
x = 5
y = 2
print(x % y)

a) 2.5
b) 2
c) 1
d) 2.0

Correct answer is c)
Explanation: The % operator is the modulus operator, which returns the remainder of the division. In this case, 5 % 2 is equal to 1.

21). Which of the following is a valid way to convert an integer to a string in Python?
a) int_to_str()
b) str()
c) convert_str()
d) None of these

Correct answer is b)
Explanation: The str() function is used to convert an object to its string representation.

22). What is the output of the following code?
x = 2.7
print(round(x))

a) 2
b) 3
c) 2.0
d) 3.0

Correct answer is a)
Explanation: The round() function rounds the given number to the nearest integer. In this case, round(2.7) is equal to 2.

23). What will be the value of the variable “x” after the execution of the following code?
x = [1, 2, 3]
y = x
y[0] = 10

a) [1, 2, 3]
b) [10, 2, 3]
c) [10, 2, 3, 1]
d) None of these

Correct answer is b)
Explanation: Both variables “x” and “y” refer to the same list object. Modifying “y” will also modify “x” since they point to the same memory location.

24). Which of the following is a valid way to convert a string to an integer in Python?
a) int_to_str()
b) str()
c) int()
d) None of these

Correct answer is c)
Explanation: The int() function is used to convert a string to an integer in Python.

25). What is the output of the following code?
x = “Hello”
y = “World”
print(x + y)

a) HelloWorld
b) Hello World
c) Hello+World
d) None of these

Correct answer is a)
Explanation: The + operator concatenates two strings. In this case, “Hello” + “World” results in “HelloWorld”.

Leave a Comment