Tuesday, April 29, 2025

BCA PAPER CODE BCAC403 SEM -4 [2024]

 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:

  1. What will be the output of the following Python code?

    python
    Copy
    Download
    a = {5, 4}  
    b = {1, 2, 4, 5}  
    a < b  
  2. What is the extension of the Python file?

  3. What will be the output of the following Python program?

    python
    Copy
    Download
    i = 0  
    while i < 5:  
        print(i)  
        i += 1  
        if i == 3:  
            break  
    else:  
        print(0)  
  4. Suppose list1 is [2, 33, 222, 14, 25]. What is list1[:-1]?

  5. What will be the output of the following Python code?

    python
    Copy
    Download
    my_tuple = (1, 2, 3, 4)  
    my_tuple.append((5, 6, 7))  
    print(len(my_tuple))  
  6. What will be the value of the following Python expression?

    python
    Copy
    Download
    4 + 3 % 5  
  7. What will be the output of the following Python code?

    python
    Copy
    Download
    print("Hello {0[0]} and {0[1]}".format({'foo', 'bin'}))  
  8. What will be the output of the following Python code?

    python
    Copy
    Download
    a = [13, 56, 17]  
    a.append([87])  
    a.extend([45, 67])  
    print(a)  
  9. What will be the output of the following Python code?

    python
    Copy
    Download
    a = {4, 5, 6}  
    b = {2, 8, 6}  
    a + b  
  10. What is the order of precedence in Python?

  11. The process of pickling in Python includes ______.

  12. What will be the output of the following Python code?

    python
    Copy
    Download
    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:

  1. Define a property that must have the same value for every class instance (object).

  2. Define variable in Python.

  3. Is elif a nested if in Python?

  4. What are functions in Python?

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

  1. Define functions in Python.

  2. Write a Python function that takes a number as a parameter and checks whether the number is prime or not.

  3. What are the advantages and disadvantages of using a for loop in Python?

  4. How can I use a break statement in a for loop in Python?

  5. Explain inheritance in Python with an example.

  6. How are classes created in Python?

  7. Write a Python function student_data() that will take as argument student_name and student_class from the user and print the student name and class.

  8. How to comment multiple lines in Python?

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

python
Copy
Download
a = {5, 4}  
b = {1, 2, 4, 5}  
a < b  

Solution:

  • a < b checks if a is a proper subset of b.

  • 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:

python
Copy
Download
i = 0  
while i < 5:  
    print(i)  
    i += 1  
    if i == 3:  
        break  
else:  
    print(0)  

Solution:

  • The loop breaks when i == 3.

  • The else block does not execute because the loop was terminated by break.
    Output:

Copy
Download
0  
1  
2  

4. Slicing list1 = [2, 33, 222, 14, 25]

Code:

python
Copy
Download
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:

python
Copy
Download
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 = 3

  • 4 + 3 = 7
    Output: 7


7. Output of the given Python code (String Formatting Error)

Code:

python
Copy
Download
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:

python
Copy
Download
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]) → Adds 45 and 67 as separate elements.
    Output:
    [13, 56, 17, [87], 45, 67]


9. Output of the given Python code (Set Addition Error)

Code:

python
Copy
Download
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:

    1. Parentheses ()

    2. Exponentiation **

    3. Unary +x, -x, ~x

    4. Multiplication *, Division /, Floor //, Modulus %

    5. Addition +, Subtraction -

    6. Bitwise shifts <<, >>

    7. Bitwise AND &

    8. Bitwise XOR ^

    9. Bitwise OR |

    10. Comparison ==, !=, >, <, >=, <=

    11. Logical NOT not

    12. Logical AND and

    13. Logical 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 pickle module.


12. Output of the given Python code (List Comprehension on Tuple)

Code:

python
Copy
Download
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:

python
Copy
Download
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:

  • variable is a named reference to a value stored in memory.

  • No explicit declaration needed (dynamic typing).
    Example:

python
Copy
Download
x = 10          # Integer  
name = "Alice"  # String  

3. Is elif a nested if in Python?

Solution:

  • Noelif is not a nested if.

  • It is a shortcut for else if and avoids deep nesting.
    Example:

python
Copy
Download
if x > 10:  
    print("Greater")  
elif x == 10:  
    print("Equal")  
else:  
    print("Smaller")  

4. What are functions in Python?

Solution:

  • function is a reusable block of code that performs a specific task.

  • Defined using def.
    Example:

python
Copy
Download
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:

python
Copy
Download
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:

    python
    Copy
    Download
    def function_name(parameters):  
        # Function body  
        return result  
  • Example:

    python
    Copy
    Download
    def square(x):  
        return x ** 2  
    
    print(square(5))  # Output: 25  

2. Write a Python function to check if a number is prime.

Solution:

python
Copy
Download
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:

AdvantagesDisadvantages
Easy to read and writeSlower than while for some cases
No manual counter neededNot 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:

  • break exits the loop immediately.
    Example:

python
Copy
Download
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:

python
Copy
Download
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 class keyword.
    Example:

python
Copy
Download
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:

python
Copy
Download
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:

python
Copy
Download
'''  
This is a  
multi-line  
comment.  
'''  

9. What is a dictionary in Python? Explain with an example.

Solution:

  • dictionary is a key-value pair collection.

  • Example:

python
Copy
Download
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