Variables, Types & Operators questions and answers

Dynamic typing, numbers, operators, precedence and truthiness. Practise variables, types & operators questions from the python section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.

Sample variables, types & operators questions with worked answers

Question 1

Which of the following is a valid Python variable name?

  1. 2nd_rank
  2. total-marks
  3. class
  4. _score2

Answer: D. _score2

Explanation: A name may contain letters, digits and underscores but cannot start with a digit, so _score2 is valid. 2nd_rank starts with a digit, total-marks contains a minus sign, and class is a reserved keyword.

Question 2

Which statement about the operators == and is is correct?

  1. == compares the values of two objects, while is checks whether both names refer to the same object
  2. is compares the values of two objects, while == checks whether both names refer to the same object
  3. Both give the same result for any two lists that have equal contents
  4. is can be used only with numbers, while == works with every type

Answer: A. == compares the values of two objects, while is checks whether both names refer to the same object

Explanation: == asks whether two objects are equal in value; is asks whether they are one and the same object in memory. Two separately built lists with equal contents are == but not is.

Question 3

Booleans are used in arithmetic here. What does the snippet print?

print(True + True * 2, isinstance(True, int))
  1. 4 True
  2. 3 True
  3. 3 False
  4. TypeError

Answer: B. 3 True

Explanation: bool is a subclass of int, with True equal to 1, so isinstance(True, int) is True. Multiplication comes first: True * 2 = 2, then True + 2 = 3.

All 20 variables, types & operators questions with answers

Variables, Types & Operators: common questions

What are variables, types & operators questions?

Dynamic typing, numbers, operators, precedence and truthiness. They appear in the python section of many placement tests.

Where can I practise variables, types & operators questions with answers for free?

Right here on LogicWiz Exams. Every variables, types & operators question has a worked explanation after you answer. Practice is free and needs no sign-up.

How should I practise variables, types & operators?

Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.

Start practising

On this page you can start a fresh set of variables, types & operators questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: Python practice.

More python topics

Other subjects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep