MCQs on Variables and Data Types | Python programming

1. Variables and Constants

  1. Which of the following is a valid variable name in Python?
    • a) 2variable
    • b) my_variable
    • c) @variable
    • d) variable-1
  2. In Python, variables are:
    • a) Type-bound
    • b) Object-bound
    • c) Value-bound
    • d) Constant-bound
  3. What will be the result of the following code: x = 5; x = 10?
    • a) Syntax error
    • b) 10
    • c) 5
    • d) Both 5 and 10
  4. Which of the following is used to declare a constant in Python?
    • a) const keyword
    • b) readonly keyword
    • c) All caps variable names by convention
    • d) No such concept in Python
  5. Which of the following data types are immutable in Python?
    • a) List
    • b) Tuple
    • c) Dictionary
    • d) Set

2. Data Types in Python

  1. Which of the following is not a built-in data type in Python?
    • a) List
    • b) Set
    • c) Array
    • d) Dictionary
  2. What is the default value of a variable if it is not initialized in Python?
    • a) None
    • b) Null
    • c) Undefined
    • d) 0
  3. Which of the following is a mutable data type in Python?
    • a) String
    • b) Tuple
    • c) List
    • d) Integer
  4. What type of object is returned when performing division in Python 3?
    • a) Integer
    • b) Float
    • c) Complex
    • d) String
  5. What is the output of the following code: type("Hello World")?
    • a) str
    • b) string
    • c) type
    • d) text

3. Type Casting and Conversion

  1. Which of the following functions is used to convert a string to an integer in Python?
    • a) int()
    • b) str()
    • c) float()
    • d) convert()
  2. What is the result of the expression int("10") + 2.5?
    • a) 12
    • b) 12.5
    • c) TypeError
    • d) 10.5
  3. Which of the following functions is used to convert an integer to a string in Python?
    • a) str()
    • b) float()
    • c) int()
    • d) convert()
  4. What will the following code print? print(float("3.14") + 2)
    • a) 5.14
    • b) 5
    • c) 3.14
    • d) 2.14
  5. Which function is used to convert a value to a boolean in Python?
    • a) bool()
    • b) int()
    • c) float()
    • d) str()

4. Dynamic Typing

  1. What is meant by dynamic typing in Python?
    • a) Variables are explicitly typed at declaration
    • b) Variables can hold any data type at runtime
    • c) The type of a variable is defined by its value
    • d) None of the above
  2. Which of the following statements about dynamic typing in Python is true?
    • a) Variables must be declared with their type
    • b) Type information is associated with variables
    • c) Variables can change their type during execution
    • d) All variables are of type None
  3. In Python, which of the following will raise a TypeError?
    • a) Adding an integer to a string
    • b) Adding two integers
    • c) Adding two floats
    • d) Adding a string to a string
  4. Which of the following does Python allow in terms of data types?
    • a) Strongly typed
    • b) Weakly typed
    • c) Explicitly typed
    • d) None of the above
  5. What is the result of the following code: a = 5; a = "Hello"; print(a)?
    • a) 5
    • b) Hello
    • c) SyntaxError
    • d) TypeError

5. Input and Output

  1. Which function is used to take user input in Python?
    • a) read()
    • b) input()
    • c) get_input()
    • d) scan()
  2. What will be the result of the following code: input("Enter your name: ")?
    • a) Displays a prompt and returns a string entered by the user
    • b) Returns the name directly
    • c) Raises an exception
    • d) Returns nothing
  3. How would you print the result of an addition operation in Python?
    • a) print(5 + 5)
    • b) display(5 + 5)
    • c) echo(5 + 5)
    • d) output(5 + 5)
  4. How do you format strings in Python 3.6 and later?
    • a) f"Hello, {name}"
    • b) "Hello, {name}".format()
    • c) %s operator
    • d) str.format()
  5. What is the output of the following code: print("Value of x is:", 5)?
    • a) Value of x is: 5
    • b) Value of x is
    • c) 5
    • d) Value of x is: 5.0

6. Advanced Input and Output

  1. Which function is used to read all lines from a file in Python?
    • a) read()
    • b) readlines()
    • c) input()
    • d) getlines()
  2. In Python, which method can be used to print text without a newline at the end?
    • a) print("text", end="")
    • b) echo("text")
    • c) output("text")
    • d) display("text")
  3. How do you write data to a file in Python?
    • a) write()
    • b) writelines()
    • c) output()
    • d) Both a and b
  4. What is the function used to convert input from the user into an integer?
    • a) int()
    • b) input()
    • c) str()
    • d) float()
  5. Which of the following is a correct way to display the value of a variable x with formatted output in Python?
    • a) print(f"x = {x}")
    • b) print("x = x")
    • c) print("x:", x)
    • d) All of the above

Answer Table:

QnoAnswer
1b) my_variable
2b) Object-bound
3b) 10
4c) All caps variable names by convention
5b) Tuple
6c) Array
7a) None
8c) List
9b) Float
10a) str
11a) int()
12b) 12.5
13a) str()
14a) 5.14
15a) bool()
16b) Variables can hold any data type at runtime
17c) Variables can change their type during execution
18a) Adding an integer to a string
19b) Weakly typed
20b) Hello
21b) input()
22a) Displays a prompt and returns a string entered by the user
23a) print(5 + 5)
24a) f"Hello, {name}"
25a) Value of x is: 5
26b) readlines()
27a) print("text", end="")
28d) Both a and b
29a) int()
30d) All of the above

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top