Python MCQs with explanation

Which of the following declarations is incorrect?

  1. _x = 2
  2.  __x = 3
  3. __xyz__ = 5
  4. None of these



Answer: 
Option d: None of these

In Python, variables can start with an underscore or double underscore without any issues. The single underscore prefix is commonly used as a convention to indicate that a variable is intended for internal use or as a placeholder.

The double underscore prefix is used for name mangling, primarily to avoid naming conflicts in subclasses.

The line of code _ _x_ _ = 3 in Python indicates that the variable _ _ _x__ is assigned the value 3. The double underscores (__) before and after the variable name indicate that it is a private variable. Private variables are not accessible outside of the class or module in which they are defined.

Post a Comment

0 Comments