Python Variable MCQ : Set 2

Python Variable MCQ

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

Correct answer is a).
Explanation: The value 10.0 is float, so the output of the given code will be <class ‘float’>.

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

Correct answer is b).
Explanation: Here, we are not finding the arithmetic value of 10/5, instead we need the data type of 10/5 (i.e. 2.0). So the output will be float.

3). What will be the output of the following Python code?
type(()) is <class ‘tuple’>
  a) True
  b) False

Correct answer is a).
Explanation: Tuples are stored inside round brackets () and the parameter inside type() is an empty tuple. So, the given code on execution will give the output as True.

4). What will be the output of the following Python code?
type([])
  a) <class ‘array’>
  b) <class ‘dict’>
  c) <class ‘list’>
  d) Code will generate an error

Correct answer is c).
Explanation: Lists are stored inside square brackets [] and the parameter inside type() is an empty list. So, the given code on execution will give the output as <class ‘list’>.

5). What will be the output of the following Python code?
type({}) is <class ‘set’>

  a) True
  b) False

Correct answer is b).
Explanation: type({}) produces a dictionary and not a set so the correct answer is b).

6). What will be the output of the following Python code?

bool(-5)
  a) True
  b) False

Correct answer is a).
Explanation: Bool is used to get a truth value of an expression.

7). A variable should be assigned a value before it is declared.
  a) True
  b) False

Correct answer is b).
Explanation: Python determines the type of a variable during runtime based on the value that is assigned to it. So, not necessary to assign a value during its declaration.

8). Which of the following function gives a unique number assigned to an object?
  a) ref()
  b) reference()
  c) id()
  d) type()

Correct answer is c).
Explanation: id() function returns a unique id for the object.

9). How do we define a block of code in Python?
  a) Using curly braces
  b) Indentation
  c) Using Brackets
  d) None of these

Correct answer is b).
Explanation: Identation is used to define a block of code in Python. Curly braces {} are used to define a block of code in other languages like C, C++,.

10). Variable names are case-sensitive in Python.
  a) True
  b) False

Correct answer is a).
Explanation: Variable names are case-sensitive in Python. So, variable1 is different than VARIABLE1 in Python.

11). How do we comment in a single line in Python?
  a) /
  b) \
  c) #
  d) ” “

Correct answer is c).
Explanation: Hash (#) is used to comment the entire line.

12). The following way is correct to assign multiple variables to a single value
      a = b = c = 5
  a) True
  b) False

Correct answer is a).
Explanation: This is a compact way to assign the same value to multiple variables. Here, a, b, and c will have a value of 5.

13). The following way is correct to assign multiple variables to multiple values
a, b, c = “Bikes”, “Cars”, “Trucks”
  a) True
  b) False

Correct answer is a).
Explanation: This is a compact way to assign values to multiple variables. Here, a, b, and c will have “Bikes”, “Cars”, and “Trucks” respectively.

14). What will be the output of the following Python code?
type(4j)
  a) alphanum
  b) long
  c) complex
  d) Error

Correct answer is c).
Explanation: j suffix to 4 (e.g., 4j) indicates the imaginary part. Type for such value returns complex.

15). What will be the output of the following Python code?
type(4l)
  a) alphanum
  b) long
  c) complex
  d) Error

Correct answer is d)
Explanation: Output will produce an error for this Python code. It varies on Python versions. This is valid in Python 2.7.5 and returns “long”.

16). Python supports the conversion of integer variables to complex.
  a) True
  b) False

Correct answer is a).
Explanation: Conversion of integer say x to complex is supported in Python using complex (x) function.

17). Python supports the conversion of complex variables to integers.
  a) True
  b) False

Correct answer is b).
Explanation: Conversion of complex numbers to integers is not supported in Python.

18). What will be the output of the following Python code?
var = “Hello”
int(var)
  a) Hello
  b) H,e,l,l,o
  c) Code will generate an error
  d) None of these

Correct answer is c).
Explanation: This is not supported in Python, and the code will generate an error: invalid literal for int() with base 10.

19). What will be the output of the following Python code?
var = “1234”
int(var)
  a) 1234
  b) 1,2,3,4
  c) Code will generate an error
  d) None of these

Correct answer is a).
Explanation: This will work fine in Python, and the code will produce output 1234 (correct option a).

20). What will be the output of the following Python code?
var = str(1234)
print(var)
  a) 1234
  b) 1,2,3,4
  c) ‘1234’
  d) Code will generate an error

Correct answer is a).
Explanation: This will work fine in Python, and the code will produce output 1234 (correct option a).

21). What will be the output of the following Python code?
str(1234)
  a) 1234
  b) 1,2,3,4
  c) ‘1234’
  d) Code will generate an error

The correct answer is c).
Explanation: This will work fine in Python, and the code will produce output ‘1234’ (correct option c).

22). What is a variable in Python Programming language?
a) A container to store data
b) A function to perform calculations
c) A loop control structure
d) None of these

Correct answer is a)
Explanation: A variable in Python is a name that refers to a value or data stored in the computer’s memory.

23). Which of the following is a valid variable name in Python?
a) 123var
b) var123
c) _var
d) None of these

Correct answer is b)
Explanation: Variable names in Python can start with a letter or an underscore (_), but not with a digit.

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

a) 5Hello
b) Hello5
c) TypeError
d) None of these

Correct answer is c)
Explanation: You cannot perform arithmetic operations between different data types. Adding an integer and a string raises a TypeError.

25). Which of the following is not a valid variable type in Python?
a) int
b) float
c) string
d) char

Correct answer is d)
Explanation: Python does not have a separate data type for characters. Characters are represented as strings of length 1.

Leave a Comment