Online Coding Lessons Python Coding Basics 2 — Questions and Answers
Question 1: What is the output of: print(2 ** 3) in Python?
- 6
- 8 (Correct answer)
- 9
- 5
Correct answer: 8
The ** operator in Python performs exponentiation, so 2 ** 3 equals 2 raised to the power of 3, which is 8.
Question 2: Which loop is used to iterate over a sequence in Python?
- while loop
- for loop (Correct answer)
- do-while loop
- repeat loop
Correct answer: for loop
The 'for' loop in Python is used to iterate over sequences like lists, strings, or ranges.
Question 3: What does 'input()' do in Python?
- Prints text to the screen
- Reads user input from the keyboard (Correct answer)
- Opens a file
- Imports a module
Correct answer: Reads user input from the keyboard
The input() function pauses program execution and waits for the user to type something, then returns the entered text as a string.
Question 4: Which of the following is a valid Python variable name?
- 2myVar
- my-var
- my_var (Correct answer)
- my var
Correct answer: my_var
Python variable names can contain letters, numbers, and underscores, but cannot start with a number or contain hyphens or spaces.
Question 5: What is the result of: type(3.14) in Python?
- <class 'int'>
- <class 'str'>
- <class 'float'> (Correct answer)
- <class 'num'>
Correct answer: <class 'float'>
The value 3.14 is a decimal number, so Python classifies it as a 'float' (floating-point number).
Question 6: What keyword is used to handle exceptions in Python?
- catch
- error
- except (Correct answer)
- handle
Correct answer: except
Python uses the 'except' keyword in a try-except block to catch and handle exceptions.
What is the output of: print(2 ** 3) in Python?