0% found this document useful (0 votes)
7 views

Python Test With Answer

The document covers various aspects of Python programming, including the interactive mode of the interpreter, key features of Python, and examples of Python programs for tasks such as finding maximum numbers, calculating averages, and checking for prime numbers. It also explains the differences between compilers and interpreters, variable classification, and conditional statements. Additionally, it provides code snippets for practical programming exercises.

Uploaded by

prasathg214
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python Test With Answer

The document covers various aspects of Python programming, including the interactive mode of the interpreter, key features of Python, and examples of Python programs for tasks such as finding maximum numbers, calculating averages, and checking for prime numbers. It also explains the differences between compilers and interpreters, variable classification, and conditional statements. Additionally, it provides code snippets for practical programming exercises.

Uploaded by

prasathg214
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Test 1

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.

2)List the key features of Python?


1. Easy to Learn: Python has a simple syntax and is relatively easy to learn,
making it a great language for beginners.

2. High-Level Language: Python is a high-level language, meaning it abstracts


away many low-level details, allowing developers to focus on the logic of their
program.
3. Interpreted Language: Python is an interpreted language, meaning that code
is executed line by line, without the need for compilation.

4. Object-Oriented: Python is an object-oriented language, supporting the


concepts of classes, objects, inheritance, polymorphism, and encapsulation.

3)Write a python program to retrieve strings starting with m and having 5


characters
# List of strings
strings = ["mouse", "money", "major", "master", "mobile", "mainly", "matrix"]

# Retrieve strings starting with 'm' and having 5 characters


result = [s for s in strings if len(s) == 5 and s.startswith('m')]
# Print the result
print(result)

Output:
['mouse', 'money', 'major']

4)Differentiate Compiler and Interpreter?


Compiler:
1. Translation: Translates the entire source code into machine code at once.
2. Compilation: Occurs before the execution of the program.
3. Machine Code: Generates machine code that can be executed directly by the
computer's processor.
4. Error Detection: Detects errors during the compilation process, before the
program is executed.
5. Examples: C, C++, Fortran.
Interpreter:

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)Classify a variable and represent the rules for naming a variable.


Variables can be classified into several types based on their characteristics:
1. Integer Variable: A variable that stores whole numbers, e.g., int x = 10;
2. Floating-Point Variable: A variable that stores decimal numbers, e.g., float pi
= 3.14;
3. Character Variable: A variable that stores a single character, e.g., char initial =
'A';
4. String Variable: A variable that stores a sequence of characters, e.g., string
name = "John Doe";
5. Boolean Variable: A variable that stores a true or false value, e.g., bool
isAdmin = true;
• It cannot be a reserved python keyword.
• It should not contain white space.
• It can be a combination of A-Z, a-z, 0-9, or underscore.
• It should start with an alphabet character or an underscore ( _ ).
• It should not contain any special character other than an underscore ( _ ).

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]

# Use built-in max function to find the maximum number


max_num = max(series)

# Print the result


print("Maximum number in the series:", max_num)

2)Determine the average of five subject marks by using python program.


ANS:
# Input marks for five subjects
subject1 = float(input("Enter marks for Subject 1: "))
subject2 = float(input("Enter marks for Subject 2: "))
subject3 = float(input("Enter marks for Subject 3: "))
subject4 = float(input("Enter marks for Subject 4: "))
subject5 = float(input("Enter marks for Subject 5: "))

# Calculate the sum of marks


total_marks = subject1 + subject2 + subject3 + subject4 + subject5

# Calculate the average


average_marks = total_marks / 5

# Print the result


print("Average marks: ", average_marks)

3)Define conditional statement with example.


ANS:
A conditional statement is a statement that executes a specific block of code if a
certain condition is true. It allows the program to make decisions based on
conditions.

Types of Conditional Statements:


1. If Statement: Used to execute a block of code if a condition is true.
2. If-Else Statement: Used to execute a block of code if a condition is true and
another block of code if the condition is false.
3. If-Elif-Else Statement: Used to check multiple conditions and execute
different blocks of code.

Example 1: If Statement
x = 10
if x > 5:
print("x is greater than 5")

Example 2: If-Else Statement


x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
Example 3: If-Elif-Else Statement
x = 10
if x > 15:
print("x is greater than 15")
elif x > 5:
print("x is greater than 5 but less than or equal to 15")
else:
print("x is less than or equal to 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

5)Build a program to print the number given by user. If number is even


print ‘Even’, else print ‘Odd’.
ANS: # Get the number from the user
num = int(input("Enter a number: "))
# Check if the number is even
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")
6)Use Python program to check the number is armstrong or not.
ANS: # Function to check if a number is Armstrong
def is_armstrong(num):
# Convert the number to string to find its length
num_str = str(num)
length = len(num_str)
# Initialize sum
total = 0
# Calculate the sum of each digit raised to the power of the length
for digit in num_str:
total += int(digit) ** length
# Check if the sum is equal to the original number
if total == num:
return True
else:
return False
# Get the number from the user
num = int(input("Enter a number: "))
# Check if the number is Armstrong
if is_armstrong(num):
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
Example Output:
Enter a number: 371
371 is an Armstrong number
Enter a number: 123
123 is not an Armstrong number.

7)Using Python Program, determine whether the given number is positive


or negative using if-elif and else statements.
ANS: # Get the number from the user
num = float(input("Enter a number: "))
# Check if the number is positive, negative, or zero
if num > 0:
print(num, "is a positive number")
elif num < 0:
print(num, "is a negative number")
else:
print(num, "is zero")
Example Output:
Enter a number: 10
10.0 is a positive number
Enter a number: -5
-5.0 is a negative number
Enter a number: 0
0.0 is zero

8)Demonstrate a program which prints all the divisors of a number.


ANS: # Get the number from the user
num = int(input("Enter a number: "))
# Print all the divisors of the number
print("Divisors of", num, "are:")
for i in range(1, num + 1):
if num % i == 0:
print(i)

Example Output:
Enter a number: 12
Divisors of 12 are:
1
2
3
4
6
12

9)Build a program to check the input number is prime number or not.


ANS: # Function to check if a number is prime
def is_prime(num):
if num <= 1:
return False
for i in range(2, num):
if num % i == 0:
return False
return True
# Get the number from the user
num = int(input("Enter a number: "))
# Check if the number is prime
if is_prime(num):
print(num, "is a prime number")
else:
print(num, "is not a prime number")
Example Output:
Enter a number: 25
25 is not a prime number
Enter a number: 23
23 is a prime number

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
...

You might also like