MAULANA ABUL KALAM AZAD UNIVERSITY OF TECHNOLOGY, WEST BENGAL
Paper Code: BCAC301
Python Programming
UPID: 3000070
Time Allotted: 3 Hours
Full Marks: 70
The figures in the margin indicate full marks.
Candidates are required to give their answers in their own words as far as practicable.
Group-A (Very Short Answer Type Questions)
Answer any ten of the following: (1 × 10 = 10)
Which command creates a list?
a)
list1 = list()b)
list1 = []c)
list1 = list([1, 2, 3])d) All of the mentioned ✓
Python tuple example:
Correct answer: None of the options (tuples use
( ), e.g.,(1, 2, 3)).
False statement about dictionaries:
b) "The keys of a dictionary can be accessed using values" (False).
Open file for writing:
outfile = open("c:\\scores.txt", "w")(escape backslash or use raw string).
Python creator:
c) Guido van Rossum ✓
Python keywords:
b) lower case (e.g.,
if,def).
Single-line comment symbol:
#(None of the options match; typo in the question).
String slicing output (
str1[::-1]):a)
"dlrowolleh"(reversed string).
Function definition keyword:
c)
def✓
Shuffle a list:
c)
random.shuffle(fruit)(requiresimport random).
Set slicing output (
t[1:-1]):Error (sets are unordered; slicing not supported).
Arithmetic operators with strings:
c)
-(cannot be used).
Group-B (Short Answer Type Questions)
Answer any three of the following: (5 × 3 = 15)
2. Read/Write Text File in Python
Reading:
with open("file.txt", "r") as file: content = file.read()
Writing:
with open("file.txt", "w") as file: file.write("Hello, World!")
3. Dictionary Example
student = {"name": "Alice", "age": 21, "grade": "A"} print(student["name"]) # Output: Alice
4. Identifier Token
Rules:
Start with
[a-zA-Z_], followed by[a-zA-Z0-9_].Case-sensitive (
var ≠ Var).Cannot use keywords (e.g.,
if,while).
5. OOP Features
Encapsulation (e.g., classes).
Inheritance (e.g.,
class Child(Parent)).Polymorphism (e.g., method overriding).
Abstraction (e.g., abstract classes).
6. for vs. while Loops
for: Iterates over sequences.for i in range(5): print(i) # 0,1,2,3,4
while: Runs until condition isFalse.i = 0 while i < 5: print(i); i += 1 # 0,1,2,3,4
Group-C (Long Answer Type Questions)
Answer any three of the following: (15 × 3 = 45)
7. String Operations
(a) Slicing:
text = "Python" print(text[1:4]) # "yth"
(b) String Methods:
upper(),split(),replace(),strip().
(c) Strings in Python:Immutable, support indexing/slicing, versatile methods.
8. Functions
(a) Definition:
def greet(name): print(f"Hello, {name}!")
(b) Multiplication Table:
def table(n): for i in range(1, 11): print(f"{n} x {i} = {n*i}")
(c) Fruitful Functions:
Return values (e.g.,
def add(a, b): return a + b).
9. Lists vs. Tuples
(a) Differences:
Lists: Mutable,
[ ].Tuples: Immutable,
( ).
(b)append()vs.extend():append(): Adds single element.extend(): Adds multiple elements.
(c) List Methods:sort(),reverse(),pop(),insert().
10. Tuples
(a) Definition:
coordinates = (10, 20)
(b) Tuple Assignment:
x, y = (10, 20) # x=10, y=20
(c) Immutability:
Ensures data integrity (cannot modify after creation).
11. Python Features & Comparisons
(a) Python Features:
Easy syntax, dynamic typing, cross-platform.
(b) Python vs. C:Python: Interpreted, no pointers.
C: Compiled, manual memory management.
(c) Python vs. Java Data Types:Python: Dynamic (e.g.,
list).Java: Static (e.g.,
ArrayList<String>).
Group-A (Very Short Answer Type Questions)
Solutions (Any 10):
List Creation:
Correct Option: d) All of the mentioned (
list(),[], andlist([1, 2, 3])are valid).
Python Tuple:
Correction: The options are incorrect (tuples use parentheses, e.g.,
(1, 2, 3)).
False Dictionary Statement:
Answer: b) "The keys of a dictionary can be accessed using values" (False; keys are unique and used to access values).
File Opening:
Answer:
outfile = open("c:\\scores.txt", "w")(escape backslashes or use raw string:r"c:\scores.txt").
Python Creator:
Answer: c) Guido van Rossum.
Keywords Case:
Answer: b) lower case (e.g.,
if,def).
Single-line Comment:
Correction: Python uses
#(options are incorrect).
String Slicing (
str1[::-1]):Answer: Reverses the string →
"dlrowolleh".
Function Definition:
Answer: c)
def.
List Shuffling:
Answer: c)
random.shuffle(fruit)(requiresimport random).
Set Slicing:
Answer: Error (sets are unordered; slicing not supported).
String Arithmetic:
Answer: c)
-(cannot be used with strings).
Group-B (Short Answer Type Questions)
2. Reading/Writing Text Files
Solution:
# Writing to a file with open("example.txt", "w") as file: file.write("Hello, Python!") # Reading from a file with open("example.txt", "r") as file: content = file.read() print(content) # Output: Hello, Python!
3. Dictionary Example
Solution:
student = {"name": "Alice", "age": 21, "grade": "A"} print(student["name"]) # Output: Alice
4. Identifier Token Rules
Key Points:
Must start with
[a-zA-Z_].Can include
[a-zA-Z0-9_]thereafter.Case-sensitive (
myVar ≠ myvar).Cannot use keywords (e.g.,
for,while).
5. OOP Features
Solution:
Encapsulation: Bundling data/methods (e.g., classes).
Inheritance:
class Child(Parent):.Polymorphism: Method overriding (
len()works for strings/lists).Abstraction: Hiding complexity (e.g., abstract classes).
6. for vs. while Loops
Solution:
for: Iterates over sequences (lists, strings).for i in range(3): print(i) # 0, 1, 2
while: Runs until condition isFalse.i = 0 while i < 3: print(i); i += 1 # 0, 1, 2
Group-C (Long Answer Type Questions)
7. String Operations
(a) Slicing:
text = "Python" print(text[1:4]) # "yth"
(b) String Methods:
upper()→ Converts to uppercase.split()→ Splits into a list.replace("old", "new")→ Replaces substrings.strip()→ Removes whitespace.
(c) String Properties:
Immutable (cannot modify after creation).
Support indexing (
text[0]) and slicing (text[1:3]).
8. Functions
(a) Definition:
def greet(name): print(f"Hello, {name}!")
(b) Multiplication Table:
def print_table(n): for i in range(1, 11): print(f"{n} x {i} = {n*i}")
(c) Fruitful Functions:
Return values (e.g.,
return a + b).Example:
def add(a, b): return a + b result = add(3, 5) # 8
9. Lists vs. Tuples
(a) Differences:
Lists ([]) | Tuples (()) |
|---|---|
| Mutable | Immutable |
| Slower iteration | Faster iteration |
| More memory | Less memory |
(b) append() vs. extend():
append(): Adds a single element.lst = [1, 2]; lst.append(3) # [1, 2, 3]
extend(): Adds multiple elements.lst.extend([4, 5]) # [1, 2, 3, 4, 5]
(c) List Methods:
sort(): Sorts in-place.reverse(): Reverses the list.pop(): Removes by index.
10. Tuples
(a) Definition:
coordinates = (10, 20)
(b) Tuple Assignment:
x, y = (10, 20) # x=10, y=20
(c) Immutability:
Ensures data integrity (e.g., for dictionary keys).
11. Python Features & Comparisons
(a) Python Features:
Dynamic typing, easy syntax, cross-platform.
(b) Python vs. C:Python: Interpreted, no manual memory management.
C: Compiled, supports pointers.
(c) Python vs. Java Data Types:Python:
list(dynamic),int(unbounded).Java:
ArrayList<String>(static),int(32-bit).

No comments:
Post a Comment