Python Test With Answer
Python Test With Answer
2 marks
1)What is meant by interactive mode of the interpreter?
Interactive mode of the interpreter refers to a way of executing Python code
where the user can interactively enter and execute Python statements one at a
time, and the interpreter responds immediately with the output.
In interactive mode:
- The interpreter prompts the user to enter a statement.
- The user enters a statement, and the interpreter executes it immediately.
- The interpreter displays the output of the statement.
- The user can enter another statement, and the process repeats.
Interactive mode is useful for:
- Testing small code snippets
- Exploring Python syntax and features
- Debugging code
To enter interactive mode, you can:
- Open a terminal or command prompt and type python
- Use an Integrated Development Environment (IDE) that supports interactive
mode, such as IDLE, PyCharm, or Visual Studio Code.
Output:
['mouse', 'money', 'major']
1. Translation: Translates the source code into machine code line by line.
2. Interpretation: Occurs during the execution of the program.
3. No Machine Code: Does not generate machine code that can be executed
directly by the processor.
4. Error Detection: Detects errors during the execution of the program.
5. Examples: Python, JavaScript, Ruby.
Key differences:
- Compilation vs. Interpretation: Compiler compiles the entire source code
before execution, while an interpreter interprets the source code line by line
during execution.
- Machine Code Generation: Compiler generates machine code, while an
interpreter does not.
- Error Detection: Compiler detects errors before execution, while an
interpreter detects errors during execution.
5 marks
1)Build a Program to find maximum number from the given series (1, 10, -
5, 15, 20, 17, 4, 7, -8, 25)
ANS: # Given series
series = [1, 10, -5, 15, 20, 17, 4, 7, -8, 25]
Example 1: If Statement
x = 10
if x > 5:
print("x is greater than 5")
4)Express the syntax of the following statements [3+3+3+3] using for loop.
ANS: result = 0
for i in range(4):
result += 3
print(result) # Output: 12
Example Output:
Enter a number: 12
Divisors of 12 are:
1
2
3
4
6
12
10)Demonstrate a program that prints the numbers from 1 to 100. But for
multiples of three print “Fizz” instead of the number and for the multiples
of five print “Buzz”. For numbers which are multiples of both three and
five print “FizzBuzz”.
ANS: # Loop through numbers from 1 to 100
for i in range (1, 101):
# Check if the number is a multiple of both 3 and 5
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
# Check if the number is a multiple of 3
elif i % 3 == 0:
print("Fizz")
# Check if the number is a multiple of 5
elif i % 5 == 0:
print("Buzz")
# If none of the above conditions are met, print the number
else:
print(i)
Example Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
...