Python variable MCQ : Set 1

Python Variable MCQ

1). Which is the correct way to get the type of the variable, say for variable x that holds any kind of value?

  a) typedef (x)
  b) pytype (x)
  c) type (x)
  d) None of these

Correct answer is c).
Explanation: Type is a built-in function in Python that returns the class type of the object.
For example: If x is a number (integer), then type(x) will return “<class ‘int’>”.

2). Which is true for the variable names in Python?
  a) underscore is not allowed
  b) only names and special characters are allowed
  c) unlimited length
  d) none of these

Correct answer is c).
Explanation: There is no limit of the variable name length in the python.

3). What is __name__ in python?
  a) this is not a built-in variable
  b) this is a built-in variable
  c) None of these

Correct answer is b).
Explanation:  __name__ is a special or built-in variable that gives the name of the current module.

4). What is a ++ in Python?
  a) this is an increment operator like Java or C++
  b) this is not allowed in Python
  c) this is used as a global variable
  d) none of these

Correct answer is b).
Explanation: There is no ++ incremental operator in Python.

5). What are nonlocal variables in Python?
  a) Those are the same as the global variable
  b) Those are declared within nested functions
  c) There is no way to create nonlocal variables in Python
  d) None of these

Correct answer is b).
Explanation: Variables that are declared within a nested function are called nonlocal variables.

6). What are local variables in Python?
  a) Those are declared inside the function
  b) Those are defined within the local scope
  c) Not accessible outside of the function
  d) All of the these

Correct answer is d).
Explanation: Local variables are defined within a local scope, declared inside the function, and can not be accessed outside of the function.

7). Which is an invalid statement?
  a) testvar = 1000
  b) test var = 1000 2000
  c) test,var = 1000, 2000
  d) test_var = 1000

Correct answer is b).
Explanation: Whitespace is not allowed in the variable name.

8). What will be the value of variable b after the below code execution?
       a = “10”
       b = a + str (5)

  a) 15
  b) 5
  c) 105
  d) Syntax error

Correct answer is c).
Explanation: In this code, we are not adding 5 with 10 arithmetically but, we are adding two strings namely “10” and “5”.

9). Which is not a correct way to assign value to the variable?
  a) x=y=z=10
  b) x,y,z=10
  c) x,y,z=7.5,word,5
  d) None of these

Correct answer is b).
Explanation: Assigning a single value to multiple variables is not possible.

10). What will be the output of :
num = 2.789
print(round(num))
  a) 2.0
  b) 2.79
  c) 3.0
  d) 3

Correct answer is c)
Explanation: The round() function in Python rounds a number to the nearest whole number. In this case, num is 2.789, which is closer to 3 than to 2. Therefore, when you use round(num), it will round up to 3.0.

11). Python Variable must be declared before it is assigned a value:

  a) True
  b) False

Correct answer is b).
Explanation: No need to declare a variable before it is assigned a value

12). What are the things inside the list called?
  a) variables
  b) identifiers
  c) elements
  d) items

Correct answer is c).
Explanation: The things inside the list are called elements.

13). Identify the correct key value pair:
dict = {“Name”:”Rahul”, “Age”:24, “Occupation”:”Lawyer”}
  a) Key is Rahul, Value is Name
  b) Key is Name, Value is 24
  c) Key is Name, Value is Rahul
  d) Key is Name, Value is Age

Correct answer is c).
Explanation: Here, we are asked to identify the correct key value PAIR, so it will be option c).

14). Choose the correct option of the below code:

        def num():
             global a
             a = 20
             a = a+10
             print(a)

        a=10
        num()
        print(a)

  a) 30
       10

  b) 30
       30

  c) 20
       10

  d) 20
       20

Correct answer is b).
Explanation: Inside function num(), variable “a” is initially declared as global and assigned value as 20. Value of “a” will be printed when num() is called.
Variable inside the function will be given preference and hence the output of a will be a = a+10 = 20+10 = 30.
As we have declared “a” as global so when the function ends, outside print(a) will also output 30. So the correct answer is b).

15). What will be the output of the following code:
print(20//7)
  a) 2
  b) 2.0
  c) 2.857142857142857
  d) 3

Correct answer is a).
Explanation: The ‘//’ operator is called the floor division operator. This operator carries out the division and rounds off the value into the nearest whole number.

16). Which of the following is a core data type?
  a) boolean
  b) class
  c) def
  d) none of these

Correct answer is a).
Explanation: Boolean is a core data type.

17). Which of the following is an immutable data type?
  a) Sets
  b) Lists
  c) Strings
  d) Dictionary

Correct answer is c).
Explanation: Strings are an immutable data type as their values cannot be updated.

18). Which of the following is not a data type?
  a) String
  b) Character
  c) Integer
  d) None of these

Correct answer is b).
Explanation: Character is not a data type.

19). Which of the following function does not return any value?
  a) type
  b) int
  c) bool
  d) None

Correct answer is d).
Explanation: type, int, and bool return some value whereas the None function does not.

20). What will be the output of the following Python code?
type(“abcd”)
  a) string
  b) char
  c) str
  d) int

Correct answer is c).
Explanation: “abcd” is stored in a string format so it will return str.

21). What will be the output of the following code?
list1 = [1,2,3,4]
tup1 = tuple(list1)
print(tup1)
  a) [1,2,3,4]
  b) (1,2,3,4)
  c) 1 2 3 4
  d) The code will give an error while running

Correct answer is b).
Explanation: In this code, we are converting a list into a tuple and then printing it, so the output that we will get will be in tuple format.

22). In which of the following data type is stored in “Key – Value” pair format?
  a) Lists
  b) Dictionary
  c) Tuples
  d) None of these

Correct answer is b).
Explanation: In dictionaries, data is stored in key-value pair format.

23). Which of the following cannot be a variable?
  a) pass
  b) fail
  c) Both a and b
  d) None of these

Correct answer is a).
Explanation: pass is a keyword in Python. So, it can’t be a variable.

24). What will be the output of the following Python code?
type(range(10))
  a) <class ‘range’>
  b) int
  c) dict
  d) None of these

Correct answer is a).
Explanation: Range allows to generation a series of numbers within the mentioned range. If it’s used with type(), then it returns <class ‘range’>.

25). What will be the output of the following Python code?
type(‘5’)
  a) <class ‘str’>
  b) <class ‘int’>
  c) <class ‘float’>
  d) None of these

Correct answer is a).
Explanation: As ‘5’ is stored in a string format i.e. inside the colons (”), it will give the output as <class ‘str’>.

Leave a Comment