COMPUTER SCIENCE: Python Programming (CSCSEC-151T)
FYUG Even Semester Exam, 2024

Course No: CSCSEC-151T | Full Marks: 50 | Time: 2 Hours

Subject: Computer Science (Skill Enhancement Course)

Semester: 2nd Semester (FYUG)

Exam Name: Even Semester Exam, 2024


SECTION-A (Answer any fifteen) 1 × 15 = 15 Marks

1. Why is Python used widely?

Python is widely used due to its simple syntax that mimics natural language, its vast library support (like NumPy and Pandas), and its versatility across domains such as Web Development, Data Science, and AI.

2. Write an application of Python.

One major application is Data Science and Machine Learning, where Python is used for data analysis, visualization, and building predictive models.

3. How to write comments in Python?

Single-line comments are written using the hash symbol (#). Multi-line comments can be created using triple quotes (''' or """).

4. Write the output of: print(2**3+(5+6)**(1+1))

Calculation: 23 + 112 = 8 + 121 = 129.

5. What is if-elif-else statement in Python?

It is a conditional branching statement used to execute different blocks of code based on multiple conditions. elif stands for "else if".

6. What is indentation in Python?

Indentation refers to the spaces or tabs at the beginning of a code line. In Python, it is used to define the scope or block of code (like in loops or functions), whereas other languages use curly braces.

8. Output of: str = 'Hello World!'; print(str[2:5])

This is a string slicing operation. It takes characters from index 2 to 4 (5 is excluded). Output: llo.

9. Define list in Python.

A list is an ordered, mutable collection of items that can be of different data types, enclosed in square brackets [].

12. Output of: a=[1,2,3]; a=tuple(a); a[0]=2; print(a)

Output: TypeError. Explanation: The list a is converted to a tuple. Tuples are immutable, so assigning a[0]=2 is not allowed.

15. What is recursion?

Recursion is a programming technique where a function calls itself directly or indirectly to solve a smaller sub-problem of the same type.

16. Output of variable swap code:

a=3; b=1
print(a, b) # Output: 3 1
a, b = b, a
print(a, b) # Output: 1 3
    

19. Which command is used to open a file in Python?

The open() function is used to open a file. Syntax: file_object = open("filename", "mode").

20. Output of: Day=["Sun", "Mon", "Tue", "Wed"]; del Day[2]; print(Day)

Output: ['Sunday', 'Monday', 'Wednesday']. (The item at index 2, "Tuesday", is deleted) .


SECTION-B (Answer any five) 2 × 5 = 10 Marks

22. What are tokens in Python? Give example. [2]

Tokens are the smallest individual units in a Python program. Examples: Keywords (if), Identifiers (variable_name), Literals (10), and Operators (+).

23. Differences between conditional flow and iterative flow. [2]

  • Conditional Flow: Executes a block of code once if a condition is true (e.g., if).
  • Iterative Flow: Executes a block of code repeatedly as long as a condition is true (e.g., while, for).

25. What do you mean by mutability of a dictionary? [2]

Mutability means the content can be changed after creation. In a dictionary, you can add, modify, or delete key-value pairs.

Example: d = {'a': 1}; d['a'] = 2 changes the value to 2.

28. Types of functions available in Python. [2]

  1. Built-in Functions: Pre-defined functions like print(), len(), max().
  2. User-defined Functions: Functions created by the programmer using the def keyword.

29. How to plot a graph in Python using matplotlib? [2]

Use the pyplot module from matplotlib.

import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
    


SECTION-C (Answer any five) 5 × 5 = 25 Marks

34. Write a program script to find the factorial of a positive number. [1+4=5]

num = int(input("Enter a number: "))
factorial = 1
if num < 0:
    print("Factorial does not exist for negative numbers")
elif num == 0:
    print("The factorial of 0 is 1")
else:
    for i in range(1, num + 1):
        factorial = factorial * i
    print("The factorial of", num, "is", factorial)
    

37. Write a Python script to generate Fibonacci series. [5]

nterms = int(input("How many terms? "))
n1, n2 = 0, 1
count = 0
if nterms <= 0:
    print("Please enter a positive integer")
elif nterms == 1:
    print("Fibonacci sequence:", n1)
else:
    while count < nterms:
        print(n1, end=" ")
        nth = n1 + n2
        n1 = n2
        n2 = nth
        count += 1
    

39. Python script to compare three numbers and print the largest one. [5]

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))

if (a >= b) and (a >= c):
    largest = a
elif (b >= a) and (b >= c):
    largest = b
else:
    largest = c

print("The largest number is", largest)
    

40. Explain various file operations available in Python. [5]

File handling involves four primary steps:

  • Open: Using open() with modes like 'r' (read), 'w' (write), 'a' (append).
  • Read/Write: Using read(), readline() to extract data, or write() to add data.
  • Close: Using close() to free up system resources.