0% found this document useful (0 votes)
2 views6 pages

Python Mod1 2 Important Questions

The document outlines important questions and answers for the VTU 2025 Scheme Python Programming course, covering Modules 1 and 2. It includes topics such as program errors, iteration, functions, strings, tuples, and lists, along with example programs and explanations. The content is structured according to the official VTU syllabus and model question paper patterns.

Uploaded by

thanvitha02.d
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Python Mod1 2 Important Questions

The document outlines important questions and answers for the VTU 2025 Scheme Python Programming course, covering Modules 1 and 2. It includes topics such as program errors, iteration, functions, strings, tuples, and lists, along with example programs and explanations. The content is structured according to the official VTU syllabus and model question paper patterns.

Uploaded by

thanvitha02.d
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

VTU 2025 Scheme

Python Programming (1BPLC105B / 205B)


Important Questions with Answers
Modules 1 & 2 | 1st / 2nd Semester B.E.
Based on Official VTU Syllabus + Model Question Paper Patterns + Previous SEE Papers

MODULE 1 — The Way of the Program · Variables · Iteration · Functions

Syllabus: The way of the program (errors & debugging) | Variables, expressions, statements, operators, type conversion | Iteration: for loop, while loop, Collatz
sequence, break/continue, nested loops | Functions with arguments and return values

Q1. [8M | Model QP Q1a] What is a program? Explain syntax errors, runtime errors, and semantic errors with suitable examples. ★ Very
Frequently Asked
★ ANSWER:
A program is a sequence of instructions that specifies how to perform a computation. While developing programs, three kinds of errors commonly occur:

1. Syntax Errors — violate the grammar rules of Python. Detected by the


parser before the program runs. Example: missing colon after 'if'.

2. Runtime Errors (Exceptions) — appear only while the program is


executing. Example: dividing a number by zero.

3. Semantic Errors — the program runs without crashing, but does not
do what the programmer intended, due to a logic mistake.

# Syntax error example


if x > 5
print(x) # SyntaxError: expected ':'

# Runtime error example


a = 10
b = 0
print(a / b) # ZeroDivisionError

# Semantic error example (logic mistake, no crash)


length = 5
width = 3
area = length + width # should be length * width
print(area) # prints 8 instead of 15, no error shown

Q2. [6M | Model QP Q1b] Explain the order of operations (operator precedence) in Python with an example program. ★ Very Frequently Asked
★ ANSWER:
Python evaluates expressions using the PEMDAS rule — Parentheses, Exponentiation, Multiplication/Division (left to right), Addition/Subtraction (left to
right). Exponentiation is right-associative.

print(2 + 3 * 4) # multiplication before addition


print((2 + 3) * 4) # parentheses evaluated first
print(2 ** 3 ** 2) # right-to-left: 2 ** (3 ** 2)
print(10 - 4 + 2) # left to right for same precedence

# Output:
# 14
# 20
# 512
# 8

Q3. [8M | Model QP Q2a] Develop a program to generate the Fibonacci sequence of length N (read N from the console). ★ Very Frequently
Asked
★ ANSWER:
The Fibonacci sequence starts with 0 and 1, and each subsequent term is the sum of the previous two terms.

n = int(input("Enter the length: "))


a, b = 0, 1
for i in range(n):
print(a, end=' ')
a, b = b, a + b

# Sample Output:
# Enter the length: 7
# 0 1 1 2 3 5 8

Q4. [6M | Repeated] Explain the Collatz 3n+1 sequence. Write a Python program to illustrate it. ★ Frequently Asked
★ ANSWER:
For any positive integer n: if n is even, divide it by 2; if n is odd, multiply it by 3 and add 1. Repeating this process eventually reaches 1.

n = int(input("Enter n: "))
while n != 1:
print(n, end=' ')
if n % 2 == 0:
n = n // 2
else:
n = 3 * n + 1
print(1)

# Sample Output:
# Enter n: 6
# 6 3 10 5 16 8 4 2 1

Q5. [6M | Model QP Q2b] Differentiate between the for loop and while loop in Python with examples. ★ Frequently Asked
★ ANSWER:

for loop while loop

Used when the number of iterations is known/fixed Used when the number of iterations depends on a condition

Iterates directly over a sequence (range, string, list) Repeats as long as a condition evaluates to True

Example: for i in range(5): Example: while x > 0:

for i in range(5): # for loop


print(i, end=' ')

n = 5
while n > 0: # while loop
print(n, end=' ')
n -= 1

Q6. [4M | Repeated] What is the role of break and continue statements? Illustrate with an example. ★ Frequently Asked
★ ANSWER:
break terminates the nearest enclosing loop immediately. continue skips the remaining statements in the current iteration only and moves to the next
iteration.

for i in range(1, 6):


if i == 3:
continue # skip printing 3
if i == 5:
break # stop the loop at 5
print(i)

# Output:
# 1
# 2
# 4

Q7. [8M | Model QP Q2c] Explain functions with arguments and return values. Write a function to find the maximum of two numbers. ★
Very Frequently Asked
★ ANSWER:
A function is a named sequence of statements that performs a computation. Arguments are the input values passed to a function; the return statement
sends a result back to the caller, ending the function's execution.

def maximum(a, b):


if a > b:
return a
else:
return b

result = maximum(10, 25)


print("Maximum is:", result)

# Output:
# Maximum is: 25

Q8. [4M | Repeated] What is the modulus operator? Write a program to check whether a number is even or odd. ★ Frequently Asked
★ ANSWER:
The modulus operator (%) returns the remainder of integer division and is commonly used to test divisibility.

num = int(input("Enter a number: "))


if num % 2 == 0:
print("Even")
else:
print("Odd")

Q9. [6M | Repeated ×2] Write a program to print a multiplication table using nested loops (2D table). ★ Occasionally Asked
★ ANSWER:

for row in range(1, 6):


for col in range(1, 6):
print(row * col, end='\t')
print()

Q10. [6M | Model QP Q1c] Explain local and global scope with suitable examples. ★ Frequently Asked
★ ANSWER:
A variable defined inside a function has local scope and exists only within that function. A variable defined outside all functions has global scope and is
accessible (read-only by default) from inside functions; the global keyword allows modification.

x = 10 # global variable

def show():
x = 5 # local variable, separate from global x
print("Local:", x)

show()
print("Global:", x)

count = 0
def increment():
global count
count += 1

increment()
print("After increment:", count)

# Output:
# Local: 5
# Global: 10
# After increment: 1
MODULE 2 — Strings · Tuples · Lists

Syllabus: Strings: length, traversal, slices, comparison, immutability, find/split/format methods | Tuples: assignment, return values, composability | Lists:
operations, slicing, mutability, aliasing/cloning, methods, nested lists

Q1. [8M | Model QP Q3a] Explain different list operations with examples — insert, remove, append, length, pop, and clear. ★ Very Frequently
Asked (Lab PYQ)
★ ANSWER:
append(x) adds x to the end of the list. insert(i, x) inserts x at index i. remove(x) deletes the first matching value. pop(i) removes and returns the item at
index i. len(list) returns the count of elements. clear() removes all elements.

fruits = ["apple", "banana"]


[Link](1, "mango")
[Link]("grape")
print("Length:", len(fruits))
[Link]("banana")
print("Popped:", [Link]())
print(fruits)
[Link]()
print(fruits)

# Output:
# Length: 3
# Popped: grape
# ['apple', 'mango']
# []

Q2. [8M | Model QP Q3b] What is slicing? Explain string slices and list slices with examples. ★ Very Frequently Asked
★ ANSWER:
Slicing extracts a portion of a sequence using sequence[start:end:step]. The start index is included, the end index is excluded. Omitting start or end uses
the beginning or end of the sequence by default.

s = "Programming"
print(s[2:7]) # ogram
print(s[:4]) # Prog
print(s[-3:]) # ing

lst = [10, 20, 30, 40, 50]


print(lst[1:4]) # [20, 30, 40]
print(lst[::2]) # [10, 30, 50]
print(lst[::-1]) # [50, 40, 30, 20, 10]

Q3. [6M | Model QP Q4a] Explain how strings are immutable, with an example. How does this differ from lists? ★ Frequently Asked
★ ANSWER:
Strings cannot be modified after creation — assigning to an index raises a TypeError. Lists are mutable, so elements can be reassigned directly.

s = "cat"
# s[0] = 'b' # TypeError: 'str' object does not support item assignment
s = "b" + s[1:] # correct way: build a new string
print(s)

lst = ["c", "a", "t"]


lst[0] = "b" # allowed, lists are mutable
print(lst)

# Output:
# bat
# ['b', 'a', 't']

Q4. [8M | Repeated ×2] Explain aliasing and cloning of lists with an example. Why is cloning needed? ★ Very Frequently Asked
★ ANSWER:
Aliasing occurs when two variables refer to the same list object — a change through either name affects both. Cloning creates an independent copy using
a full slice [:], list(), or [Link]() for nested lists, so the original remains unaffected by later changes.

original = [1, 2, 3]
alias = original
clone = original[:]

[Link](4)
[Link](99)

print("original:", original)
print("alias:", alias)
print("clone:", clone)

# Output:
# original: [1, 2, 3, 4]
# alias: [1, 2, 3, 4]
# clone: [1, 2, 3, 99]

Q5. [8M | Model QP Q4b] Read N numbers from the console, create a list, and print the mean, variance, and standard deviation. ★ Very
Frequently Asked (Lab PYQ)
★ ANSWER:
Mean is the average of the values; variance is the average squared distance from the mean; standard deviation is the square root of the variance.

n = int(input("How many numbers? "))


nums = [float(input(f"Enter number {i+1}: ")) for i in range(n)]

mean = sum(nums) / n
variance = sum((x - mean) ** 2 for x in nums) / n
std_dev = variance ** 0.5

print("Mean:", mean)
print("Variance:", variance)
print("Standard Deviation:", std_dev)

Q6. [6M | Model QP Q4c] What are tuples? How do they differ from lists? Explain tuple assignment with an example. ★ Frequently Asked
★ ANSWER:

Tuple List

Immutable — cannot change after creation Mutable — elements can be changed

Written using ( ) Written using [ ]

Used for fixed collections of data Used for collections that change over time

point = (3, 4)
x, y = point # tuple assignment / unpacking
print(x, y)

a, b = 5, 10
a, b = b, a # swap without a temp variable
print(a, b)

# Output:
# 3 4
# 10 5

Q7. [6M | Repeated] Explain the built-in find() and split() string methods with examples. ★ Frequently Asked
★ ANSWER:

text = "VTU Python Programming"


print([Link]("Python")) # returns starting index
print([Link]("Java")) # returns -1, not found

csv_line = "10,20,30,40"
values = csv_line.split(",")
print(values)

line = " Python is fun "


print([Link]()) # removes leading/trailing whitespace

# Output:
# 4
# -1
# ['10', '20', '30', '40']
# Python is fun

Q8. [8M | Model QP Q3c] Develop a function named find_letter_count that takes a word and a letter, and counts how many times the
letter appears in the word. ★ Frequently Asked
★ ANSWER:

def find_letter_count(word, letter):


count = 0
for ch in word:
if ch == letter:
count += 1
return count

print(find_letter_count("mississippi", "s"))

# Output:
# 4

Q9. [6M | Repeated] Explain nested lists with an example. How are elements of a 2D list (matrix) accessed? ★ Occasionally Asked
★ ANSWER:

matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[2][1]) # row index 2, column index 1 -> 8
for row in matrix:
for val in row:
print(val, end=' ')
print()

# Output:
# 8
# 1 2 3
# 4 5 6
# 7 8 9

Q10. [4M | Repeated] Differentiate between pure functions and modifier functions with respect to lists. ★ Occasionally Asked
★ ANSWER:
A pure function does not alter its argument; it computes and returns a new value. A modifier changes the object passed to it in place and typically returns
None.

original = [3, 1, 2]
new_list = sorted(original) # pure function — original unchanged
print(original, new_list)

[Link]() # modifier — changes original directly


print(original)

# Output:
# [3, 1, 2] [1, 2, 3]
# [1, 2, 3]

VTU 2025 Scheme · 1BPLC105B/205B Python Programming · Modules 1 & 2 · Important Questions with Answers
Based on Official VTU Syllabus + Model Question Paper Patterns + Previous SEE Papers

You might also like