Python Variable MCQ : Set 4

Python Variable MCQ

1). What is the output of the following code?
x = [1, 2, 3]
y = x.copy()
y[0] = 10
print(x)

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

Correct answer is a)
Explanation: The copy() method creates a shallow copy of the list. Modifying the copy does not affect the original list.

2). What is the output of the following code?
x = “Hello”
print(len(x))

a) 5
b) 6
c) 10
d) None of these

Correct answer is a)
Explanation: The len() function in Python returns the length of a string. In this case, len(“Hello”) is equal to 5.

3). Which of the following is the correct way to declare a global variable “x” inside a function?
a) global x
b) x = global
c) x.global()
d) None of these

Correct answer is a)
Explanation: The “global” keyword is used to declare a variable as global inside a function.

4). What is the output of the following code?
x = “Python”
print(x[-2])

a) t
b) o
c) n
d) None of these

Correct answer is a)
Explanation: Negative indexing starts from the end of the string. x[-2] returns the second-to-last character, which is “t”.

5). What is the output of the following code?
x = “Hello”
print(x[::-1])

a) Hello
b) olleH
c) ello
d) None of these

Correct answer is b)
Explanation: The slicing [::-1] reverses the string. In this case, “Hello” reversed becomes “olleH”.

6). Which of the following is a valid way to check if a variable “x” is of type float?
a) type(x) == float
b) x.type() == float
c) typeof(x) == float
d) None of these

Correct answer is a)
Explanation: The type() function is used to check the type of a variable. The comparison “type(x) == float” checks if “x” is of type float.

7). What will be the value of the variable “x” after the execution of the following code?
x = [1, 2, 3]
y = x
y.append(4)

a) [1, 2, 3]
b) [1, 2, 3, 4]
c) [4, 3, 2, 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.

8). What is the output of the following code?
x = “Hello”
print(x[1])

a) H
b) e
c) l
d) None of these

Correct answer is b)
Explanation: Indexing starts from 0. x[1] returns the second character, which is “e”.

9). Which of the following is true about local variables in Python?
a) They can be accessed from any part of the program
b) Their scope is limited to the function or block where they are defined
c) They are declared using the local keyword
d) None of these

Correct answer is b)
Explanation: Local variables are only accessible within the function or block where they are defined.

10). What is the output of the following code?
x = 2
y = 3
print(x < y)

a) True
b) False
c) 2
d) 3

Correct answer is a)
Explanation: The < operator checks if the left operand is less than the right operand. In this case, 2 < 3 is True.

11). What is the output of the following code?
x = 5
y = 2
z = x % y
print(z)

a) 1
b) 2
c) 0
d) None of these

Correct answer is a)
Explanation: The % operator calculates the remainder of the division. In this case, 5 % 2 is equal to 1.

12). Which of the following is a valid variable name in Python?
a) 1_variable
b) variable_1
c) variable-1
d) variable 1

Correct answer is b)
Explanation: Variable names in Python can contain letters, numbers, and underscores, but they cannot start with a number or contain spaces or hyphens.

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

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

Correct answer is b)
Explanation: Slicing extracts a portion of a string. In this case, x[1:4] returns the characters from index 1 to index 3, excluding the character at index 4.

14). Which of the following is the correct way to convert a floating-point number to an integer in Python?
a) int()
b) float_to_int()
c) str()
d) None of these

Correct answer is a)
Explanation: The int() function can be used to convert a floating-point number to an integer in Python.

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

a) 5
b) 7
c) 2
d) None of these

Correct answer is a)
Explanation: Assigning the value of “x” to “y” creates a copy of the value. Modifying “y” does not affect the original value of “x”.

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

a) Python
b) ython
c) P
d) None of these

Correct answer is b)
Explanation: Slicing with an omitted end index returns the substring from the start index to the end of the string. In this case, x[1:] returns “ython”.

17). Which of the following is the correct way to declare a constant variable in Python?
a) Use the const keyword
b) Prefix the variable name with an underscore (e.g., _PI)
c) Use all uppercase letters for the variable name (e.g., PI)
d) None of these

Correct answer is c)
Explanation: Although Python does not have built-in support for constant variables, using all uppercase letters for the variable name is a common convention to indicate that it should be treated as a constant.

18). What is the output of the following code?
x = [1, 2, 3]
y = x
y = [4, 5, 6]
print(x)

a) [1, 2, 3]
b) [4, 5, 6]
c) [1, 2, 3, 4, 5, 6]
d) None of these

Correct answer is a)
Explanation: Assigning a new list [4, 5, 6] to “y” does not modify the original list object referred to by “x”.

19). Which of the following is a valid way to concatenate two strings in Python?
a) str.add()
b) str.join()
c) str.append()
d) None of these

Correct answer is b)
Explanation: The str.join() method can be used to concatenate two strings in Python.

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

a) True
b) False
c) 2
d) 3

Correct answer is b)
Explanation: The == operator checks if the left operand is equal to the right operand. In this case, 2 is not equal to 3, so the expression evaluates to False.

21). What is the output of the following code?
x = 2
x = x * 3
x = x + 1
print(x)

a) 5
b) 7
c) 8
d) 9

Correct answer is d)
Explanation: The code multiplies the value of “x” by 3, adds 1 to it, and assigns the result back to “x”. Therefore, the value of “x” becomes 7, then 8.

22). What will be the value of the variable “y” after the execution of the following code?
x = [1, 2, 3]
y = x
x.append(4)
print(y)

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

Correct answer is b)
Explanation: When “y” is assigned the value of “x”, it refers to the same list object in memory. Therefore, any changes made to the list through either variable will be reflected in both. The code appends the value 4 to the list, so the value of “y” becomes [1, 2, 3, 4].

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] = 4
print(x)

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

Correct answer is b)
Explanation: Since “x” and “y” refer to the same list object, modifying the list through one variable will affect the other. The code changes the value at index 0 to 4, so the value of “x” becomes [4, 2, 3].

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

a) “Hello World”
b) “HelloWorld”
c) “WorldHello”
d) “HWeolrllod”

Correct answer is a)
Explanation: The + operator in Python can be used to concatenate two strings in Python. In this case, the variable “z” contains the concatenation of “Hello” and “World”, resulting in the string “Hello World”.

25). Which of the following statements is true regarding variable names in Python?
a) Variable names can contain spaces.
b) Variable names can start with a number.
c) Variable names are case-insensitive.
d) Variable names can contain special characters like @ or #.

Correct answer is b)
Explanation: Variable names in Python cannot start with a number. They can only start with a letter or an underscore.

Leave a Comment