Python Questions and Answers Part-1

1. All keywords in Python are in _________
a) lower case
b) Capitalized
d) None of the above

Answer: d
Explanation: True, False and None are capitalized while the others are in lower case.

2. Which of the following is an invalid variable?
a) foo
b) 1st_string
c) my_string_1
d) _

Answer: b
Explanation: Variable names should not start with a number.

3. Which of the following is an invalid statement?
a) abc = 1,000,000
b) a_b_c = 1,000,000
c) a b c = 1000 2000 3000
d) a,b,c = 1000, 2000, 3000

Answer: c
Explanation: Spaces are not allowed in variable names.

4. Why are local variable names beginning with an underscore discouraged?
a) they are used to indicate a private variables of a class
b) they are used to indicate global variables
c) they slow down execution
d) they confuse the interpreter

Answer: a
Explanation: As Python has no concept of private variables, leading underscores are used to indicate variables that must not be accessed from outside the class.

5. Which one of the following has the highest precedence in the expression?
a) Addition
b) Multiplication
c) Parenthesis
d) Exponential

Answer: c
Explanation: PEMDAS, i.e , Parenthesis, Exponentiation, Division, Multiplication, Addition, Subtraction. Note that the precedence order of Division and Multiplication is the same. Likewise, the order of Addition and Subtraction is also the same.

6. Which of the following is invalid?
a) _a = 1
b) __str__ = 1
c) __a = 1
d) none of the above

Answer: d
Explanation: All the statements will execute successfully but at the cost of reduced readability.

7. Which of the following is true for variable names in Python?
a) unlimited length
b) underscore and ampersand are the only two special characters allowed
c) all private members must have leading and trailing underscores
d) none of the above

Answer: a
Explanation: Variable names can be of any length.

8. Mathematical operations can be performed on a string.
a) True
b) False

Answer: b
Explanation: You can’t perform mathematical operation on string even if the string is in the form: ‘1234..’.

9. Is Python case sensitive when dealing with identifiers?
a) machine dependent
b) yes
c) no
d) none of the above

Answer: b
Explanation: Case is always significant.

10. Which of the following is not a keyword?
a) eval
b) nonlocal
c) pass
d) assert

Answer: a
Explanation: eval can be used as a variable.