MCQs on Python Variable Names

Test Your Knowledge: MCQs on Python Variable Names

Test your knowledge of Python variable names with this MCQ quiz! Challenge yourself with 15 questions that cover a range of topics, from valid variable names to reserved keywords and data types. See how well you know Python variable naming conventions and take your skills to the next level.

1. Which of the following is a valid Python variable name?

a) my_variable

b) 1variable

c) my variable

d) my-variable

View Answer

Answer: a

Variable names in Python can only contain letters, numbers, and underscores (_), and they cannot start with a number.

2. Which of the following is a valid way to declare a variable in Python?

a) variable name = value

b) name_variable = value

c) name variable = value

d) var-name = value

View Answer

Answer: a

In Python, variables are declared by assigning a value to a variable name using the = operator.

3. Which of the following is a reserved word in Python?

a) variable

b) for

c) value

d) number

View Answer

Answer: b

Reserved words, also known as keywords, are words that are already used by Python for specific purposes and cannot be used as variable names. "for" is one such keyword.

4. What is the convention for naming constants in Python?

a) All caps with underscores

b) Camel case

c) Snake case

d) Pascal case

View Answer

Answer: a

Constants in Python are typically named using all capital letters with underscores (_) separating words.

5. Which of the following is not a valid Python variable name?

a) MY_VARIABLE

b) MyVariable

c) My-Variable

d) my_variable_2

View Answer

Answer: c

Variable names in Python cannot contain hyphens (-).

6. What is the convention for naming private variables in Python?

a) Camel case

b) Pascal case

c) Snake case with a leading underscore

d) Snake case with a trailing underscore

View Answer

Answer: c

In Python, variables that are intended to be private (i.e., only accessible within the class or object that defines them) are typically named using snake case with a leading underscore (_).

7. What is the maximum length of a Python variable name?

a) 32 characters

b) 64 characters

c) 128 characters

d) There is no maximum length

View Answer

Answer: d

There is no maximum length for Python variable names.

8. Which of the following is not a valid Python built-in function?

a) print

b) input

c) len

d) my_function

View Answer

Answer: d

"my_function" is not a valid built-in function in Python. It would need to be defined before it could be used.

9. Which of the following is not a valid way to assign a value to a variable in Python?

a) variable_name = "value"

b) variable name = "value"

c) variable_name = 5

d) variable_name = True

View Answer

Answer: b

Variable names cannot contain spaces in Python, so "variable name" is not a valid variable name.

10. Which of the following is a valid way to delete a variable in Python?

a) delete variable_name

b) variable_name = None

c) del variable_name

d) variable_name.delete()

View Answer

Answer: c

The "del" keyword can be used to delete a variable in Python.

11. Which of the following is a valid variable name in Python?

a) my_variable

b) 123variable

c) variable-name

d) @variable

View Answer

Answer: a

Variable names in Python can only contain letters, numbers, and underscores (_). They cannot start with a number or contain special characters like dashes or @ symbols.

12. What is the convention for naming constants in Python?

a) All caps with underscores

b) CamelCase

c) snake_case

d) PascalCase

View Answer

Answer: a

Constants in Python are typically named using all caps with underscores separating words (e.g., MY_CONSTANT).

13. Which of the following is a reserved keyword in Python?

a) main

b) class

c) import

d) function

View Answer

Answer: b

"class" is a reserved keyword in Python and cannot be used as a variable name.

14. Which of the following is a valid way to check the data type of a variable in Python?

a) print(type(variable_name))

b) print(variable_name.type())

c) print(variable_name.type)

d) print(typeof(variable_name))

View Answer

Answer: a

The "type()" function can be used to check the data type of a variable in Python. Option a is the correct syntax.

15. Which of the following is not a valid way to concatenate two strings in Python?

a) string1 + string2

b) string1 . string2

c) "{} {}".format(string1, string2)

d) f"{string1} {string2}"

View Answer

Answer: b

To concatenate two strings in Python, you can use the "+" operator (option a), the "{} {}".format()" method (option c), or f-strings (option d). Option b is not a valid way to concatenate strings in Python.

Popular posts from this blog

MCQs on Python Core Data Types

MCQs on Python Basic Operators

MCQs on Python Numeric Types