MAULANA ABUL KALAM AZAD UNIVERSITY OF TECHNOLOGY, WEST BENGAL
Paper Code: BCAC403 Python Programming
UPID: 400088
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 Question)
Answer any ten of the following:
What will be the output of the following Python code?
a = {5, 4} b = {1, 2, 4, 5} a < b
What is the extension of the Python file?
What will be the output of the following Python program?
i = 0 while i < 5: print(i) i += 1 if i == 3: break else: print(0)
Suppose
list1is[2, 33, 222, 14, 25]. What islist1[:-1]?What will be the output of the following Python code?
my_tuple = (1, 2, 3, 4) my_tuple.append((5, 6, 7)) print(len(my_tuple))
What will be the value of the following Python expression?
4 + 3 % 5
What will be the output of the following Python code?
print("Hello {0[0]} and {0[1]}".format({'foo', 'bin'}))
What will be the output of the following Python code?
a = [13, 56, 17] a.append([87]) a.extend([45, 67]) print(a)
What will be the output of the following Python code?
a = {4, 5, 6} b = {2, 8, 6} a + b
What is the order of precedence in Python?
The process of pickling in Python includes ______.
What will be the output of the following Python code?
t = (1, 2, 4, 3, 8, 9) [t[i] for i in range(0, len(t), 2)]
Group-B (Short Answer Type Question)
Answer any three of the following:
Define a property that must have the same value for every class instance (object).
Define variable in Python.
Is
elifa nestedifin Python?What are functions in Python?
Write a Python class to get all possible unique subsets from a set of distinct integers.
Group-C (Long Answer Type Question)
Answer any three of the following:
Define functions in Python.
Write a Python function that takes a number as a parameter and checks whether the number is prime or not.
What are the advantages and disadvantages of using a
forloop in Python?How can I use a
breakstatement in aforloop in Python?Explain inheritance in Python with an example.
How are classes created in Python?
Write a Python function
student_data()that will take as argumentstudent_nameandstudent_classfrom the user and print the student name and class.How to comment multiple lines in Python?
What is a dictionary in Python? Explain with an example.
*****************************END OF PAPER*********************
Solutions
********************************************************************
Group-A (Very Short Answer Type Questions)
Answer any ten of the following:
1. Output of the given Python code:
Code:
a = {5, 4} b = {1, 2, 4, 5} a < b
Solution:
a < bchecks ifais a proper subset ofb.Since
{5, 4}is entirely contained in{1, 2, 4, 5}, the output is:True
2. Extension of a Python file
Solution:
Python files typically have the extension:
.py
3. Output of the given Python program:
Code:
i = 0 while i < 5: print(i) i += 1 if i == 3: break else: print(0)
Solution:
The loop breaks when
i == 3.The
elseblock does not execute because the loop was terminated bybreak.
Output:
0 1 2
4. Slicing list1 = [2, 33, 222, 14, 25]
Code:
list1[:-1]
Solution:
[:-1]returns all elements except the last one.
Output:[2, 33, 222, 14]
5. Output of the given Python code (Tuple Append Error)
Code:
my_tuple = (1, 2, 3, 4) my_tuple.append((5, 6, 7)) print(len(my_tuple))
Solution:
Tuples are immutable, so
append()is not allowed.This code will raise an
AttributeError.
6. Value of the Python expression 4 + 3 % 5
Solution:
%(modulus) has higher precedence than+.3 % 5 = 34 + 3 = 7
Output:7
7. Output of the given Python code (String Formatting Error)
Code:
print("Hello {0[0]} and {0[1]}".format({'foo', 'bin'}))
Solution:
Sets are unordered, so indexing (
{0[0]}) is not possible.This will raise a
TypeError.
8. Output of the given Python code (List Operations)
Code:
a = [13, 56, 17] a.append([87]) a.extend([45, 67]) print(a)
Solution:
append([87])→ Adds[87]as a single element.extend([45, 67])→ Adds45and67as separate elements.
Output:[13, 56, 17, [87], 45, 67]
9. Output of the given Python code (Set Addition Error)
Code:
a = {4, 5, 6} b = {2, 8, 6} a + b
Solution:
Sets do not support
+(concatenation).This will raise a
TypeError.
10. Order of Precedence in Python
Solution:
From highest to lowest precedence:
Parentheses
()Exponentiation
**Unary
+x, -x, ~xMultiplication
*, Division/, Floor//, Modulus%Addition
+, Subtraction-Bitwise shifts
<<, >>Bitwise AND
&Bitwise XOR
^Bitwise OR
|Comparison
==, !=, >, <, >=, <=Logical NOT
notLogical AND
andLogical OR
or
11. Process of Pickling in Python
Solution:
Pickling is the process of serializing (converting Python objects into a byte stream).
Unpickling reverses the process.
Uses the
picklemodule.
12. Output of the given Python code (List Comprehension on Tuple)
Code:
t = (1, 2, 4, 3, 8, 9) [t[i] for i in range(0, len(t), 2)]
Solution:
Selects elements at even indices (
0, 2, 4).
Output:[1, 4, 8]
Group-B (Short Answer Type Questions)
Answer any three of the following:
1. Define a property that must have the same value for every class instance.
Solution:
Use a class variable (shared across all instances).
Example:
class MyClass: common_value = "Same for all instances" obj1 = MyClass() obj2 = MyClass() print(obj1.common_value) # Output: "Same for all instances" print(obj2.common_value) # Output: "Same for all instances"
2. Define a variable in Python.
Solution:
A variable is a named reference to a value stored in memory.
No explicit declaration needed (dynamic typing).
Example:
x = 10 # Integer name = "Alice" # String
3. Is elif a nested if in Python?
Solution:
No,
elifis not a nestedif.It is a shortcut for
else ifand avoids deep nesting.
Example:
if x > 10: print("Greater") elif x == 10: print("Equal") else: print("Smaller")
4. What are functions in Python?
Solution:
A function is a reusable block of code that performs a specific task.
Defined using
def.
Example:
def greet(name): return f"Hello, {name}!" print(greet("Alice")) # Output: "Hello, Alice!"
5. Write a Python class to get all unique subsets from a set of distinct integers.
Solution:
Use backtracking or itertools.
Example:
from itertools import combinations class Subsets: def get_subsets(self, nums): subsets = [] for i in range(len(nums) + 1): subsets.extend(combinations(nums, i)) return subsets s = Subsets() print(s.get_subsets({1, 2, 3}))
Group-C (Long Answer Type Questions)
Answer any three of the following:
1. Define functions in Python.
Solution:
A function is a block of reusable code that performs a specific task.
Syntax:
def function_name(parameters): # Function body return result
Example:
def square(x): return x ** 2 print(square(5)) # Output: 25
2. Write a Python function to check if a number is prime.
Solution:
def is_prime(n): if n <= 1: return False for i in range(2, int(n ** 0.5) + 1): if n % i == 0: return False return True print(is_prime(7)) # Output: True print(is_prime(4)) # Output: False
3. Advantages and Disadvantages of for Loop in Python
Solution:
| Advantages | Disadvantages |
|---|---|
| Easy to read and write | Slower than while for some cases |
| No manual counter needed | Not suitable for complex conditions |
| Works with iterables (lists, tuples, etc.) | Cannot modify the loop variable directly |
4. How to use break in a for loop?
Solution:
breakexits the loop immediately.
Example:
for i in range(10): if i == 5: break print(i) # Output: 0, 1, 2, 3, 4
5. Explain inheritance in Python with an example.
Solution:
Inheritance allows a class (child) to inherit attributes/methods from another class (parent).
Example:
class Animal: def speak(self): print("Animal sound") class Dog(Animal): def speak(self): print("Bark!") dog = Dog() dog.speak() # Output: "Bark!"
6. How are classes created in Python?
Solution:
Use the
classkeyword.
Example:
class MyClass: def __init__(self, name): self.name = name obj = MyClass("Alice") print(obj.name) # Output: "Alice"
7. Write a function student_data() to print student details.
Solution:
def student_data(student_name, student_class): print(f"Name: {student_name}, Class: {student_class}") student_data("Alice", "BCA") # Output: "Name: Alice, Class: BCA"
8. How to comment multiple lines in Python?
Solution:
Use
'''(triple quotes) or#for each line.
Example:
'''
This is a
multi-line
comment.
''' 9. What is a dictionary in Python? Explain with an example.
Solution:
A dictionary is a key-value pair collection.
Example:
student = {"name": "Alice", "age": 20} print(student["name"]) # Output: "Alice"
Conclusion
Group-A: Mostly tests basic syntax, data structures, and operations.
Group-B: Covers OOP, functions, and control structures.
Group-C: Requires detailed explanations and coding solutions.
This analysis provides complete solutions to all questions in the exam paper. 🚀
Thanks
SK Institute

No comments:
Post a Comment