Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
152 views

Gu 5th sem bca python programming solved question paper 2023

Gu 5th sem bca python programming solved question paper 2023 pandu College Guwahati University

Uploaded by

Skilless Ninja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

Gu 5th sem bca python programming solved question paper 2023

Gu 5th sem bca python programming solved question paper 2023 pandu College Guwahati University

Uploaded by

Skilless Ninja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Answer:(a)

Core data types in Python include:

1. Numeric Types:

o int (integer): Whole numbers (e.g., 5, -10).

o float (floating-point): Decimal numbers (e.g., 3.14, -2.71).

o complex: Complex numbers (e.g., 2+3j).

2. Sequence Types:

o str (string): Text data (e.g., "Hello").


o list: Ordered and mutable collection (e.g., [1, 2, 3]).

o tuple: Ordered but immutable collection (e.g., (1, 2, 3)).

3. Mapping Type:

o dict: Key-value pairs (e.g., {"key": "value"}).

4. Set Types:

o set: Unordered collection of unique elements (e.g., {1, 2, 3}).

o frozenset: Immutable version of a set.

5. Boolean Type:

o bool: True or False.

6. NoneType:

o None: Represents the absence of a value.

(b) What are command line arguments?

Answer:
Command-line arguments are inputs passed to a Python script during its
execution via the command line. These arguments are stored in the
sys.argv list, where:

 sys.argv[0] contains the script name.

 sys.argv[1:] contains the arguments passed to the script.

Example:

python

Copy code

import sys

print(sys.argv)

If the script is executed as:

Copy code

python script.py arg1 arg2

The output will be:


css

Copy code

['script.py', 'arg1', 'arg2']

(c) Define class with a proper example.

Answer:
A class in Python is a blueprint for creating objects. It encapsulates data
(attributes) and methods (functions).

Example:

python

Copy code

class Person:

def __init__(self, name, age):

self.name = name

self.age = age

def greet(self):

print(f"Hello, my name is {self.name} and I am {self.age} years


old.")

# Create an object of the class

person1 = Person("Alice", 25)

person1.greet()

Output:

csharp

Copy code

Hello, my name is Alice and I am 25 years old.

(d) What is Recursion? Give example.


Answer:
Recursion is a programming technique where a function calls itself directly
or indirectly to solve a problem.

Example: Calculating Factorial Using Recursion

python

Copy code

def factorial(n):

if n == 1: # Base case

return 1

else:

return n * factorial(n - 1) # Recursive case

print(factorial(5)) # Output: 120

(e) What do you mean by the scope of variables in Python?

Answer:
The scope of a variable refers to the context in which the variable is
accessible:

1. Local Scope: Variables defined inside a function are accessible only


within that function.

2. Global Scope: Variables defined outside any function are


accessible throughout the program.

3. Nonlocal Scope: Variables defined in an enclosing function (not


global) can be accessed using the nonlocal keyword in nested
functions.

Example:

python

Copy code

x = 10 # Global variable

def outer():

y = 20 # Enclosing variable
def inner():

nonlocal y

y += 10

print(y)

inner()

outer()

(f) What are built-in functions in Python? Give example.

Answer:
Built-in functions in Python are predefined functions that can be used
without importing any module. Examples include:

 len(): Returns the length of an object.

 sum(): Returns the sum of elements in an iterable.

 type(): Returns the type of an object.

 print(): Prints output.

Example:

python

Copy code

nums = [1, 2, 3, 4]

print(len(nums)) # Output: 4

print(sum(nums)) # Output: 10

2. (a) What is string slicing in Python? Explain with a proper


example.

Answer:
String slicing allows extracting a portion of a string by specifying a range
of indices. The syntax is:
string[start:end:step]

 start: Starting index (inclusive, default is 0).


 end: Ending index (exclusive).

 step: Step value (optional, default is 1).

Example:

python

Copy code

text = "Hello, World!"

print(text[0:5]) # Output: Hello (characters 0 to 4)

print(text[7:]) # Output: World! (from index 7 to the end)

print(text[::-1]) # Output: !dlroW ,olleH (reversed string)

(b) Define Decision table.

Answer:
A Decision Table is a tabular representation of decision logic. It lists all
possible conditions and actions for a particular problem. It helps in
systematic decision-making by identifying all possible scenarios.

Structure Example:

Action Action
Conditions
1 2

Condition 1:
Yes No
True

Condition 2:
No Yes
False

(c) What is a list variable in Python? Give example.

Answer:
A list in Python is a mutable, ordered collection of elements. Lists can
store elements of different data types and allow duplicate values.

Example:

python

Copy code

my_list = [1, 2, 3, "Python", 4.5]


print(my_list) # Output: [1, 2, 3, 'Python', 4.5]

my_list.append(6) # Adds an element

print(my_list) # Output: [1, 2, 3, 'Python', 4.5, 6]

3. (a) What are the important characteristics of Python


programming? What are identifiers and keywords?

Answer:
Characteristics of Python Programming:

1. Easy to Learn and Use: Simple syntax and readability.

2. Interpreted Language: Executes line-by-line without compilation.

3. Dynamically Typed: No need to declare variable types.

4. Extensive Libraries: Rich standard libraries for various


applications.

5. Portability: Works on multiple platforms (Windows, Linux, etc.).

Identifiers:

 Names used to identify variables, functions, or classes.

 Rules:

o Must start with a letter or underscore.

o Cannot be a keyword or contain special characters.

Keywords:

 Reserved words in Python with specific meanings (e.g., if, else, for,
while).

(b) Write a program to implement linear search on a list.

Answer:

python

Copy code

def linear_search(arr, target):

for i in range(len(arr)):

if arr[i] == target:
return i

return -1

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

target = 30

result = linear_search(numbers, target)

if result != -1:

print(f"Element found at index {result}")

else:

print("Element not found")

4. (a) Explain Relational and Logical operators of Python


programming with proper example.

Answer:
Relational Operators: Compare values and return a boolean.

 <, >, <=, >=, ==, !=.

Example:

python

Copy code

x, y = 5, 10

print(x < y) # Output: True

print(x == y) # Output: False

Logical Operators: Combine conditional statements.

 and, or, not.

Example:

python

Copy code

x, y = True, False

print(x and y) # Output: False

print(x or y) # Output: True


print(not x) # Output: False

(b) What is the structure of a Python program? Discuss.

Answer:
A Python program typically consists of:

1. Imports: Import necessary libraries or modules.

2. Global Variables: Define variables to be used globally.

3. Functions: Define reusable blocks of code.

4. Main Execution: Entry point of the program.

Example:

python

Copy code

# Import

import math

# Global variable

PI = 3.14

# Function

def area_circle(radius):

return PI * radius ** 2

# Main execution

if __name__ == "__main__":

print(area_circle(5)) # Output: 78.5

5. (a) What are the major programming methodologies? Explain.

Answer:
Major programming methodologies include:

1. Procedural Programming: Uses procedures or routines (e.g., C).


2. Object-Oriented Programming (OOP): Based on objects and
classes (e.g., Python, Java).

3. Functional Programming: Focuses on functions and immutability


(e.g., Haskell).

4. Logic Programming: Based on formal logic (e.g., Prolog).

(b) Write a program to generate Fibonacci series.

Answer:

python

Copy code

def fibonacci(n):

a, b = 0, 1

for _ in range(n):

print(a, end=" ")

a, b = b, a + b

fibonacci(10) # Output: 0 1 1 2 3 5 8 13 21 34

6. Answer any two:

(a) Write a program to sort a list using insertion sort.


Answer:

python

Copy code

def insertion_sort(arr):

for i in range(1, len(arr)):

key = arr[i]

j=i-1

while j >= 0 and key < arr[j]:

arr[j + 1] = arr[j]

j -= 1
arr[j + 1] = key

numbers = [12, 11, 13, 5, 6]

insertion_sort(numbers)

print(numbers) # Output: [5, 6, 11, 12, 13]

(b) Write a program to create Stack class and implement all its
methods.

Answer:

python

Copy code

class Stack:

def __init__(self):

self.items = []

def push(self, item):

self.items.append(item)

def pop(self):

if not self.is_empty():

return self.items.pop()

else:

return "Stack is empty"

def peek(self):

if not self.is_empty():

return self.items[-1]

else:
return "Stack is empty"

def is_empty(self):

return len(self.items) == 0

def size(self):

return len(self.items)

# Example Usage

stack = Stack()

stack.push(10)

stack.push(20)

print(stack.peek()) # Output: 20

print(stack.pop()) # Output: 20

print(stack.pop()) # Output: 10

print(stack.pop()) # Output: Stack is empty

(c) Write a program to check whether a number is even or odd.

Answer:

python

Copy code

def check_even_odd(number):

if number % 2 == 0:

return "Even"

else:

return "Odd"

# Example Usage

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


print(f"The number {num} is {check_even_odd(num)}.")

(d) Write a program to reverse a number.

Answer:

python

Copy code

def reverse_number(number):

reversed_num = int(str(number)[::-1])

return reversed_num

# Example Usage

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

print(f"The reversed number is: {reverse_number(num)}")

(b) Stack Class Program Output

Code:

python

Copy code

stack = Stack()

stack.push(10)

stack.push(20)

print(stack.peek()) # Output: 20

print(stack.pop()) # Output: 20

print(stack.pop()) # Output: 10

print(stack.pop()) # Output: Stack is empty

Output:

csharp

Copy code
20

20

10

Stack is empty

(c) Even or Odd Program Output

Code:

python

Copy code

num = 7 # Example Input

print(f"The number {num} is {check_even_odd(num)}.")

Output (if input is 7):

csharp

Copy code

The number 7 is Odd.

Code (if input is 4):

python

Copy code

num = 4 # Example Input

print(f"The number {num} is {check_even_odd(num)}.")

Output (if input is 4):

csharp

Copy code

The number 4 is Even.

(d) Reverse Number Program Output

Code:

python

Copy code
num = 12345 # Example Input

print(f"The reversed number is: {reverse_number(num)}")

Output:

python

Copy code

The reversed number is: 54321

Code (if input is 9876):

python

Copy code

num = 9876 # Example Input

print(f"The reversed number is: {reverse_number(num)}")

Output:

python

Copy code

The reversed number is: 6789

THANK YOU

You might also like