Dr.
H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
PYTHON PROGRAMMING
MODULE 1 – QUESTION BANK WITH ANSWERS
I Semester | 2025 Batch
College Dr. H N National College of Engineering, Jayanagar, Bangalore – 560070
Subject Python Programming
Semester I Semester (2025 Batch)
Module Module 1
Total Questions 22 Questions (5 & 6 Marks)
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 1
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
MODULE 1 – ALL QUESTIONS & ANSWERS
Q1. Define the term 'Program' and explain high-level and
6 Marks
low-level languages.
■ Answer:
A program is a sequence of instructions written in a programming language that tells the computer what to do. It
consists of Data (variables/values) and Instructions (operations on data).
Low-Level Languages:
• Closest to machine hardware; written in binary (0s and 1s) or assembly mnemonics.
• Machine Language (1GL): Binary code – e.g., 10110000 01100001
• Assembly Language (2GL): Mnemonics like MOV AX, 01H; needs an assembler.
• Advantages: Fast execution, direct hardware control.
• Disadvantage: Difficult to write, machine-dependent.
High-Level Languages:
• Human-readable syntax, close to English; e.g., Python, C, Java.
• Needs a compiler or interpreter to translate to machine code.
• Example (Python): print("Hello, World!")
• Advantages: Easy to write, portable, maintainable.
• Disadvantage: Slightly slower than low-level languages.
# Example Python program (High-level)
length = 10
breadth = 5
area = length * breadth
print('Area is', area)
Q2. List and explain the essential components of a program. 6 Marks
■ Answer:
Every program consists of the following essential components:
1. Data (Variables and Values):
• Stores information used by the program.
• Example: length = 10, breadth = 5
2. Instructions (Statements):
• Operations that process or manipulate data.
• Example: area = length * breadth
3. Input:
• Receiving data from the user or external source.
• Example: length = int(input('Enter length: '))
4. Output:
• Displaying results to the user.
• Example: print('Area is', area)
5. Control Flow:
• Conditions and loops that control the order of execution.
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 2
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
# Complete example program with all components
length = int(input('Enter length: ')) # Input
breadth = int(input('Enter breadth: ')) # Input
area = length * breadth # Instruction
print('Area of rectangle:', area) # Output
Q3. With examples explain the following types of errors: a.
6 Marks
Syntax Error b. Runtime Error c. Semantic Error
■ Answer:
a. Syntax Error:
Occurs when the code violates Python's grammatical rules. The interpreter cannot parse the code before
execution begins.
# Syntax Error Example
print(Hello) # Missing quotes → SyntaxError
class = 42 # 'class' is a keyword → SyntaxError
a = 3
b = 5
result=a**2+b**2+2*a*b
Print(result) # Wrong case → SyntaxError
b. Runtime Error:
Occurs when code is syntactically valid but encounters an unexpected condition during execution.
# Runtime Error Examples
a, b = 10, 0
result = a / b # ZeroDivisionError
print(result)
# NameError
print(undefined_var) # Variable not declared
# TypeError
a = '10'
b = 5
sum = a + b # Cannot add str and int
c. Semantic Error (Logic Error):
Occurs when code runs without crashing but produces wrong results because the logic doesn't match the
programmer's intention.
# Semantic Error Example
score1, score2, score3 = 85, 92, 78
# Wrong: division applies only to score3
average = score1 + score2 + score3 / 3
print(average) # Output: 203.0 (Wrong!)
# Correct:
average = (score1 + score2 + score3) / 3
print(average) # Output: 85.0 (Correct)
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 3
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
Summary Table:
Syntax Error: Code doesn't run at all. Runtime Error: Code runs but crashes. Semantic Error: Code runs but
gives wrong results.
Q4. List the different data types available in Python and explain
6 Marks
each one with an example.
■ Answer:
Python supports the following built-in data types:
1. Integer (int):
• Represents whole numbers (positive, negative, zero).
• Example: a = 10, b = -5, c = 0
2. Float (float):
• Represents decimal (floating-point) numbers.
• Example: pi = 3.14, weight = 65.5
3. String (str):
• Represents a sequence of characters enclosed in quotes.
• Example: name = 'Ram', city = "Bangalore"
4. Boolean (bool):
• Represents True or False values.
• Example: is_valid = True, flag = False
5. Complex (complex):
• Represents numbers with real and imaginary parts.
• Example: z = 4 + 7j
# Data type examples
a = 123 # int
b = 3.14 # float
c = 'Hello' # str
d = True # bool
e = 4 + 7j # complex
print(type(a), type(b), type(c), type(d), type(e))
Q5. With one example program explain: a. Variable and
5 Marks
assignment operator b. Overwriting on the variable.
■ Answer:
a. Variable and Assignment Operator:
A variable is a named memory location used to store data. The assignment operator (=) assigns a value to a
variable.
age = 30 # 'age' is variable, 30 is data, = is assignment
name = 'Ram'
height = 5.9
print(age, name, height) # Output: 30 Ram 5.9
Multiple declarations on one line:
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 4
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
name, age, height = 'Ram', 30, 6.2
b. Overwriting a Variable:
In Python (dynamically typed), a variable can be reassigned to a new value of a different type.
a = 5 # a is int, value = 5
print(a) # Output: 5
a = 8.8 # Overwritten with float
print(a) # Output: 8.8
a = 'Ram' # Overwritten with string
print(a) # Output: Ram
This is possible because Python is dynamically typed – the type of a variable is determined at runtime.
Q6. With suitable examples explain the rules for writing
5 Marks
variable names in Python.
■ Answer:
Variable names in Python must follow these rules:
Rules:
• 1. Must start with a letter (a–z, A–Z) or underscore (_).
• 2. Can contain alphanumeric characters (a–z, A–Z, 0–9) and underscores.
• 3. Cannot start with a digit.
• 4. Cannot be a Python keyword (reserved word).
• 5. Case-sensitive: Name and name are different.
• 6. Should be meaningful and descriptive.
# Valid variable names
price = 12
cust_name = 'Ram'
_address = 'Jayanagar'
address1 = 'Bangalore'
# Invalid variable names
1address = 'x' # Starts with digit – Error
cust-name = 'x' # Hyphen not allowed – Error
cust name = 'x' # Space not allowed – Error
none = 'x' # 'none' is a keyword – Error
Python Keywords (cannot be used as variable names):
False, None, True, and, as, assert, async, await, break, class, continue, def, del,
elif, else, except, finally, for, from, global, if, import, in, is, lambda,
nonlocal, not, or, pass, raise, return, try, while, with, yield
Q7. Citing examples, explain the concept of statically typed
6 Marks
and dynamically typed programming languages.
■ Answer:
Statically Typed Languages:
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 5
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
The data type of a variable is declared explicitly at compile time and cannot change. Examples: C, C++, Java.
// C Example (Statically Typed)
int a = 5;
float b = 4.2;
char c = 'A';
// a = 4.2; // Error! Cannot assign float to int
Dynamically Typed Languages:
The data type is determined at runtime. The same variable can hold values of different types. Examples: Python,
JavaScript.
# Python Example (Dynamically Typed)
a = 5 # int
print(type(a)) #
a = 8.8 # Now float – overwriting
print(type(a)) #
a = 'Ram' # Now string
print(type(a)) #
Key Differences:
• Statically typed: Type checking at compile time; faster execution.
• Dynamically typed: Type checking at runtime; more flexible.
• Python's dynamic typing allows overwriting variables with different types.
Q8. Explain the concept of type conversion in Python.
Differentiate between implicit and explicit conversion with 6 Marks
examples.
■ Answer:
Type Conversion:
Type conversion (type casting) is the process of converting one data type into another in Python.
1. Implicit Type Conversion (Automatic):
Python automatically converts one data type to another without programmer intervention. It happens when
combining compatible types.
# Implicit Conversion
a = 10 # int
b = 3.5 # float
result = a + b
print(result) # Output: 13.5
print(type(result)) # Output:
Python promoted int to float automatically to avoid data loss.
2. Explicit Type Conversion (Type Casting):
The programmer manually converts a data type using built-in functions: int(), float(), str(), bool().
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 6
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
# Explicit Conversion
print(int(3.14)) # Output: 3
print(float(17)) # Output: 17.0
print(str(17)) # Output: '17'
print(int('50')) # Output: 50
# Practical use with input()
age = int(input('Enter age: '))
marks = float(input('Enter marks: '))
Key Rule:
• Implicit: int → float → complex (no data loss).
• Explicit: Programmer controls; may cause data loss (e.g., float → int truncates decimal).
Q9. Develop a Python program with a while loop to display the
6 Marks
Fibonacci sequence up to n terms entered by the user.
■ Answer:
The Fibonacci sequence is a series where each number is the sum of the two preceding numbers: 0, 1, 1, 2, 3,
5, 8, 13, …
Algorithm:
• 1. Read n from user.
• 2. Initialize first = 0, second = 1.
• 3. Loop n times: print current term, update terms.
# Fibonacci Sequence using while loop
n = int(input('Enter number of terms: '))
first = 0
second = 1
count = 0
print('Fibonacci Sequence:')
while count < n:
print(first, end=' ')
temp = first + second
first = second
second = temp
count = count + 1
Sample Output (n = 8):
Enter number of terms: 8
Fibonacci Sequence:
0 1 1 2 3 5 8 13
Explanation:
• first and second track consecutive Fibonacci numbers.
• temp stores the next value; variables are shifted forward each iteration.
• The while loop runs exactly n times controlled by the counter.
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 7
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
Q10. Describe the Collatz 3n+1 sequence and explain how
iteration and conditional statements are used in its 6 Marks
implementation.
■ Answer:
The Collatz Conjecture (3n+1 Problem):
The Collatz function is defined as:
• If n is even: divide by 2 → n = n / 2
• If n is odd: multiply by 3 and add 1 → n = 3n + 1
Repeat until n = 1. It is conjectured (but unproven) that this always reaches 1.
Example trace for n = 6:
6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1
Python Implementation:
# Collatz 3n+1 sequence
n = int(input('Enter a positive number: '))
while n != 1:
print(n, end=' ')
if n % 2 == 0: # Even condition
n = n // 2
else: # Odd condition
n = 3 * n + 1
print(n) # Print final 1
Sample Output (n = 6):
Enter a positive number: 6
6 3 10 5 16 8 4 2 1
Role of Iteration and Conditional Statements:
• while loop (Iteration): Repeats until n becomes 1; number of iterations is unpredictable.
• if-else (Conditional): Decides whether to halve (even) or apply 3n+1 (odd).
• The combination demonstrates how control flow directs algorithm behaviour.
Q11. Explain the different data types supported in Python with
6 Marks
syntax and examples.
■ Answer:
Python is dynamically typed; data types are assigned at runtime.
1. Integer (int):
a = 10
b = -5
print(type(a)) #
2. Float (float):
pi = 3.14159
weight = -0.7
print(type(pi)) #
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 8
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
3. String (str):
name = 'Python'
greeting = "Hello World"
print(type(name)) #
4. Boolean (bool):
flag = True
is_valid = False
print(type(flag)) #
5. Complex (complex):
z = 4 + 7j
print([Link], [Link]) # 4.0 7.0
print(type(z)) #
Type Checking:
print(type(42)) # int
print(type(3.14)) # float
print(type('Hi')) # str
print(type(True)) # bool
print(type(2+3j)) # complex
Q12. Develop a Python program to find the factorial of a
5 Marks
number.
■ Answer:
The factorial of a non-negative integer n (written as n!) is the product of all positive integers from 1 to n.
n! = n × (n-1) × (n-2) × … × 1 (and 0! = 1 by definition)
Algorithm:
• 1. Read n from user.
• 2. Initialize factorial = 1, i = 1.
• 3. Multiply factorial by i in each iteration; increment i.
• 4. Loop until i > n; print result.
# Factorial using while loop
n = int(input('Enter a number: '))
factorial = 1
i = 1
while i <= n:
factorial = factorial * i
i = i + 1
print('Factorial of', n, 'is', factorial)
Sample Output:
Enter a number: 5
Factorial of 5 is 120
Dry Run for n = 5:
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 9
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
i=1: factorial = 1×1 = 1
i=2: factorial = 1×2 = 2
i=3: factorial = 2×3 = 6
i=4: factorial = 6×4 = 24
i=5: factorial = 24×5 = 120
Q13. Explain the difference between = and == operator. 5 Marks
■ Answer:
Assignment Operator (=):
Used to assign a value to a variable. It stores data into a memory location.
# Assignment Operator
age = 25 # Assigns 25 to variable age
name = 'Ram' # Assigns 'Ram' to name
a, b = 10, 20 # Multiple assignment
Equality Operator (==):
Used to compare two values. Returns True if both values are equal, False otherwise. It is a relational operator.
# Equality Operator
print(5 == 5) # Output: True
print(5 == 6) # Output: False
print('a' == 'a') # Output: True
# Practical use in conditions
x = 10
if x == 10:
print('x is ten') # Output: x is ten
Key Differences:
• = is an assignment operator; it does not return a boolean.
• == is a comparison operator; always returns True or False.
• Using = in a condition (if x = 5) causes a SyntaxError in Python.
• Using == for assignment (5 == a) does not assign any value.
Q14. Explain flow control statements using if, else, and elif. 6 Marks
■ Answer:
Flow control determines which statements run and in what order. Python's conditional statements are if, else,
and elif.
1. if Statement:
Executes a block of code only when the condition is True.
# if statement
marks = 75
if marks >= 50:
print('Pass') # Executes since 75 >= 50
2. if-else Statement:
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 10
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
Executes one block if condition is True, another block if False.
# if-else
marks = 40
if marks >= 50:
print('Pass')
else:
print('Fail') # Executes since 40 < 50
3. elif (else-if) Statement:
Checks multiple conditions in sequence. Used when there are more than two possible outcomes.
# elif ladder – Grade assignment
marks = int(input('Enter marks: '))
if marks >= 90:
print('Grade: A')
elif marks >= 75:
print('Grade: B')
elif marks >= 60:
print('Grade: C')
elif marks >= 50:
print('Grade: D')
else:
print('Grade: F')
Syntax Summary:
• if : – Primary condition.
• elif : – Additional condition (can be chained).
• else: – Executes when all above conditions are False.
Q15. Develop a Python program to read the name and year of
birth of a person and display whether the person is a senior 5 Marks
citizen or not.
■ Answer:
A person is considered a senior citizen if their age is 60 or more. Age = Current Year – Year of Birth.
Algorithm:
• 1. Input: name and year of birth from user.
• 2. Calculate age = 2025 − year_of_birth.
• 3. If age >= 60: print 'Senior Citizen'; else: print 'Not a Senior Citizen'.
# Senior Citizen Check
name = input('Enter your name: ')
year_of_birth = int(input('Enter your year of birth: '))
current_year = 2025
age = current_year - year_of_birth
print('Name:', name)
print('Age:', age)
if age >= 60:
print(name, 'is a Senior Citizen')
else:
print(name, 'is NOT a Senior Citizen')
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 11
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
Sample Output:
Enter your name: Ramesh
Enter your year of birth: 1958
Name: Ramesh
Age: 67
Ramesh is a Senior Citizen
Q16. Explain the following with an example: a. print() b. len() c.
6 Marks
input()
■ Answer:
a. print():
Used to display output on the screen. Can print strings, numbers, variables, and expressions.
print('Hello, World!') # Output: Hello, World!
print('Sum:', 10 + 5) # Output: Sum: 15
name = 'Ram'
print('Name:', name) # Output: Name: Ram
print('a', 'b', 'c', sep='-') # Output: a-b-c
print('Line1', end=' ') # Stays on same line
b. len():
Returns the number of characters in a string (or items in a sequence).
name = 'Python'
print(len(name)) # Output: 6
city = 'Bangalore'
print(len(city)) # Output: 9
print(len('')) # Output: 0 (empty string)
c. input():
Reads a string of text from the keyboard. Always returns a string; use type conversion to get int/float.
name = input('Enter your name: ')
print('Hello', name)
age = int(input('Enter age: '))
marks = float(input('Enter marks: '))
print('Age:', age, '| Marks:', marks)
Q17. For the following expression explain the order of
5 Marks
operation and write the final output: x = (((5+2)*(6-4))/2)
■ Answer:
Python follows PEMDAS order of operations: Parentheses → Exponentiation → Multiplication/Division →
Addition/Subtraction.
Step-by-Step Evaluation:
x = (((5+2) * (6-4)) / 2)
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 12
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
• Step 1: Innermost parentheses – (5+2) = 7
• Step 2: Next parentheses – (6-4) = 2
• Step 3: Multiplication – 7 × 2 = 14
• Step 4: Division – 14 / 2 = 7.0
# Python evaluation
x = (((5+2) * (6-4)) / 2)
print(x) # Output: 7.0
Operator Precedence Table (High to Low):
• 1. () – Parentheses
• 2. ** – Exponentiation
• 3. *, /, //, % – Multiplication, Division, Floor Division, Modulus
• 4. +, - – Addition, Subtraction
Final Output: x = 7.0
Q18. Explain string replication and string concatenation with
5 Marks
suitable examples.
■ Answer:
1. String Concatenation (+):
When the + operator is used on two string values, it joins (concatenates) them into one string.
# String Concatenation
first = 'Python'
last = 'Programming'
result = first + ' ' + last
print(result) # Output: Python Programming
name = 'Alice'
greeting = 'Hello, ' + name + '!'
print(greeting) # Output: Hello, Alice!
2. String Replication (*):
When the * operator is used on one string and one integer, it repeats the string that many times.
# String Replication
name = 'Alice'
result = name * 5
print(result) # Output: AliceAliceAliceAliceAlice
separator = '-' * 20
print(separator) # Output: --------------------
Key Rules:
• + for concatenation: both operands must be strings.
• * for replication: one operand must be a string, other must be an integer.
• 'Hello' + 5 → TypeError; '5' + 5 → TypeError.
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 13
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
Q19. Receiving the input from the keyboard, write a program to
5 Marks
check if the input number is odd or even.
■ Answer:
A number is even if it is divisible by 2 (remainder = 0), and odd if the remainder is 1.
Algorithm:
• 1. Read integer n from user.
• 2. Compute n % 2.
• 3. If remainder is 0: print 'Even'; else print 'Odd'.
# Odd or Even Check
n = int(input('Enter a Number: '))
if n % 2 == 0:
print(n, 'is Even')
else:
print(n, 'is Odd')
Sample Output:
Enter a Number: 7
7 is Odd
Enter a Number: 12
12 is Even
Extended version using abs() for negative numbers:
n = int(input('Enter a Number: '))
if abs(n) % 2 == 0:
print('Even')
else:
print('Odd')
Q20. Write a Python program to print the multiplication table of
5 Marks
5.
■ Answer:
A multiplication table displays the product of a number with integers from 1 to 10.
Using while loop:
# Multiplication table of 5 using while loop
n = 5
i = 1
while i <= 10:
print(n, 'x', i, '=', n * i)
i = i + 1
Sample Output:
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 14
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Generalised version (for any number):
n = int(input('Enter a number: '))
i = 1
while i <= 10:
print(n, 'x', i, '=', n * i)
i += 1
Q21. Write a Python program to receive marks for Physics,
Chemistry, and Mathematics. For a passing mark of 50, check 6 Marks
if the student has passed in all three subjects.
■ Answer:
A student passes only if marks in all three subjects are greater than or equal to 50.
Algorithm:
• 1. Input marks for Physics, Chemistry, Mathematics.
• 2. Check if all three marks >= 50.
• 3. If yes: display 'Result Passed'; else: display 'Better luck next time'.
# Pass/Fail in Three Subjects
phy = int(input('Enter Physics marks: '))
chem = int(input('Enter Chemistry marks: '))
math = int(input('Enter Mathematics marks: '))
if phy >= 50 and chem >= 50 and math >= 50:
print('Result Passed')
else:
print('Better luck next time')
Sample Output – Pass:
Enter Physics marks: 65
Enter Chemistry marks: 72
Enter Mathematics marks: 58
Result Passed
Sample Output – Fail:
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 15
Dr. H N National College of Engineering | Python Programming – Module 1 Question Bank & Answers
Enter Physics marks: 45
Enter Chemistry marks: 80
Enter Mathematics marks: 90
Better luck next time
Note:
The and logical operator ensures all three conditions must be True simultaneously for the student to pass.
Q22. Receiving the input from the keyboard, write a program to
5 Marks
check if the input number is positive or negative.
■ Answer:
A number is positive if it is greater than 0, negative if less than 0, and zero if equal to 0.
Algorithm:
• 1. Read integer n from user.
• 2. Check n > 0: Positive.
• 3. Check n < 0: Negative.
• 4. Else: Zero.
# Positive, Negative or Zero
n = int(input('Enter a number: '))
if n > 0:
print(n, 'is a Positive number')
elif n < 0:
print(n, 'is a Negative number')
else:
print('The number is Zero')
Sample Outputs:
Enter a number: 15
15 is a Positive number
Enter a number: -8
-8 is a Negative number
Enter a number: 0
The number is Zero
Note:
The elif ladder is ideal here as there are three mutually exclusive outcomes. Only one block executes per run.
I Semester – 2025 Batch | Python Programming | Module 1 Question Bank Page 16