0% found this document useful (0 votes)
12 views8 pages

Cycle Test 2 QB

The document is a question bank for Cycle Test-2 for the Department of Aeronautical Engineering at Nehru Institute of Technology, covering the course U23GE201 – Problem Solving and Python Programming. It includes various questions on Python programming concepts such as functions, variables, lists, recursion, and string methods. The document is structured into parts with multiple-choice questions, short answer questions, and detailed explanations for programming concepts.

Uploaded by

jersonm06xx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Cycle Test 2 QB

The document is a question bank for Cycle Test-2 for the Department of Aeronautical Engineering at Nehru Institute of Technology, covering the course U23GE201 – Problem Solving and Python Programming. It includes various questions on Python programming concepts such as functions, variables, lists, recursion, and string methods. The document is structured into parts with multiple-choice questions, short answer questions, and detailed explanations for programming concepts.

Uploaded by

jersonm06xx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

NEHRUINSTITUTEOF TECHNOLOGY

(Autonomous)
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC with Section 2(f), Accredited by NAAC with A+, NBA Accredited – Aero & CSE
COIMBATORE - 641105

CYCLE TEST-2 Question Bank, April 2025


Department : Department of Aeronautical Engineering
Course Code & Title : U23GE201–Problem Solving and Python Programming
Answer all the questions
Part A 5 x 1 = 5 Marks
1 What is the purpose of the return statement in a function? CO4 L1
a) To define the input parameters of a b) To specify what value the function
function. should output.
c) To end the function definition. d) To call another function from within the
current function.
2 Which scope of a variable declared inside a function?
CO4 L1
a) Global scope b) Local scope.

c) Both local and global scope. d) It depends on the value of the variable.

3
Choose the best description about recursion. CO4 L1
a)A function calling itself directly or b) A function with no parameters
indirectly.
c) A function with a large number of return d) A function that cannot be called more
statements. than once.
4 Given the string s = "hello world", what will s[0:5] return?
CO3 L1
a) "world” b) "hello"

c) "hello world” d) "h"

5 How does aliasing a list in Python works?


CO5 L1
a) Creating a copy of a list with a different b) Referring to the same list object using
name. different names.

c) Creating a new list with identical values. d) Assigning a function as an element of a


list.

Part B 5 x 2 = 10 Marks
6 What is the significance of fruitful functions in Python? CO4 L2

Fruitful functions are those that perform computations and return a value using the
return statement. They are significant because:
●​ They make programs modular and reusable by allowing the returned value to
be utilized in other parts of the code.
●​ They reduce code duplication, enhancing readability and efficiency.
●​ For example, a function to calculate the area of a circle can be called
multiple times with different inputs, and its returned value can be directly
used in other calculations.
7 Differentiate local and global variables. CO4 L3
Difference Between Local and Global Variables

●​ Local Variables:
o​ Defined inside a function or block and are accessible only within that
scope.
o​ Created when the function starts and destroyed when the function
ends.
o​ Example:

def my_function():

local_var = 10 # Local variable

print(local_var)

my_function()

# print(local_var) # Will raise an error

●​ Global Variables:
o​ Defined outside all functions and accessible across the entire
program, including within functions (unless shadowed).
o​ They persist throughout the program’s lifecycle.
o​ Example:

global_var = 20 # Global variable

def my_function():

print(global_var)

my_function()

print(global_var)

8 Define Python list. CO5 L2


A list in Python is a mutable (modifiable) and ordered collection of elements that
can store items of various data types, including integers, strings, floats, and even
other lists. Lists are defined using square brackets ([]) and elements are separated
by commas.

Example:
my_list = [1, "apple", True, 3.14]
print(my_list[0]) # Access the first element (1)
9 How to slice a list in Python? CO5 L2
List slicing allows you to extract a part of a list by using the syntax
list[start:end:step]:
●​ start: Index where slicing begins (inclusive).
●​ end: Index where slicing stops (exclusive).
●​ step: Interval between elements (default is 1).
Example:
my_list = [0, 1, 2, 3, 4, 5]
print(my_list[1:4]) # [1, 2, 3]
print(my_list[:3]) # [0, 1, 2]
print(my_list[::2]) # [0, 2, 4]
print(my_list[::-1]) # [5, 4, 3, 2, 1, 0] (reversed)

10 List the different types of arguments / parameters used in CO4 L3


Function.

Python functions can have different types of arguments to allow flexibility:

1.​ Positional Arguments:


o​ Passed based on their position in the function definition.
o​ Example:

def greet(name, age):


print(f"My name is {name} and I am {age} years old.")
greet("Alice", 25) # Positional order matters
2.​ Default Arguments:
o​ Have predefined values, used when an argument is not provided in
the function call.
o​ Example:

def greet(name, age=18):


print(f"My name is {name} and I am {age} years old.")
greet("Bob") # Outputs: My name is Bob and I am 18 years old.
3.​ Keyword Arguments:
o​ Passed using parameter names, allowing arguments to be passed in
any order.
o​ Example:

greet(age=30, name="Charlie")

# Outputs: My name is Charlie and I am 30 years old.

4.​ Variable-Length Arguments:


o​ Accepts arbitrary numbers of arguments.
o​ *args for positional arguments:

def sum_all(*args):

return sum(args)
print(sum_all(1, 2, 3, 4)) # Outputs: 10

o​ **kwargs for keyword arguments:

def print_info(**kwargs):

for key, value in kwargs.items():

print(f"{key}: {value}")

print_info(name="Eve", age=27, city="NYC")

Part C2 x 10 = 20 Marks
1 What is function? How a function is defined and called in python? Explain with a CO4 L2
simple program.

In programming, a function is a block of reusable code designed to perform a


specific task. Functions help organize code into smaller, manageable pieces, making
it easier to debug, reuse, and read.

How functions are defined and called in Python

1.​ Definition: Functions are defined using the def keyword followed by the
function name and parentheses (). Optionally, you can include parameters
inside the parentheses. The body of the function contains the code to
execute, which is indented.
2.​ Calling: To execute a function, you "call" it by writing its name followed by
parentheses. If the function accepts parameters, you pass the required
arguments inside the parentheses.

Here's a simple example to explain:

Example Program: Defining and Calling a Function

# Define a function

def greet(name): # 'name' is the parameter

print(f"Hello, {name}! Welcome to Python programming.")

# Call the function

greet("Alice") # Passing the argument 'Alice'

greet("Bob") # Passing the argument 'Bob'

Explanation of the Code

1.​ Function Definition:


o​ The function greet is defined with one parameter, name.
o​ The print statement inside the function uses an f-string to display a
personalized greeting.
2.​ Function Call:
o​ greet("Alice") and greet("Bob") call the function. The arguments
"Alice" and "Bob" are passed to the name parameter, resulting in
different greetings.

Output

Hello, Alice! Welcome to Python programming.

Hello, Bob! Welcome to Python programming.


2 Describe in detail about recursive function with an example program. CO4 L2

A recursive function is a function that calls itself, either directly or indirectly, in its
definition. Recursion allows a problem to be broken down into smaller subproblems
of the same type, making it an efficient approach for solving problems that can be
defined in terms of themselves.

Key Features of Recursive Functions

1.​ Base Case: This is a condition that stops the recursion. Without a base case,
the function would call itself indefinitely, leading to a stack overflow error.
2.​ Recursive Case: This is the part of the function where it calls itself with a
modified parameter, gradually reducing the problem size.

Example: Factorial Calculation

The factorial of a non-negative integer ( n ) (denoted as ( n! )) is defined as: [ n! = n


\times (n-1) \times (n-2) \times \ldots \times 1 ] For example, ( 5! = 5 \times 4 \times
3 \times 2 \times 1 = 120 ).

This problem is naturally recursive because: [ n! = n \times (n-1)! ] and the base
case is: [ 0! = 1 ]

Recursive Function to Calculate Factorial

# Recursive function to calculate factorial

def factorial(n):

# Base case

if n == 0 or n == 1:

return 1 # Recursive case

return n * factorial(n - 1)

# Test the function


print(factorial(5)) # Output: 120

How the Function Works

1.​ Initial Call:


o​ factorial(5) starts the process.
2.​ Recursive Calls:
o​ 5 * factorial(4) is evaluated. This calls factorial(4).
o​ 4 * factorial(3) is evaluated. This calls factorial(3).
o​ This continues until the base case is reached.
3.​ Base Case:
o​ When factorial(1) is called, it returns 1.
4.​ Unwinding the Stack:
o​ As the calls return, the multiplication is performed:
▪​ 2 * 1 = 2
▪​ 3 * 2 = 6
▪​ 4 * 6 = 24
▪​ 5 * 24 = 120

Output

120

3 Explain in detail about creating the list, accessing values in the lists,updating the CO5 L2
lists and deleting the list elements .

1. Creating a List

A list is created using square brackets []. It can store items of mixed types, such as
integers, strings, or even other lists.

Example:
# Creating lists
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "banana", "cherry"]
mixed = [1, "hello", 3.5, [10, 20]]
print(numbers)
print(fruits)
print(mixed)

2. Accessing Values in a List

You can access items in a list using their index, which starts from 0 for the first
element.

Example:
# Accessing values
numbers = [1, 2, 3, 4, 5]
print(numbers[0]) # First element (1)
print(numbers[2]) # Third element (3)
# Accessing nested elements
mixed = [1, "hello", 3.5, [10, 20]]
print(mixed[3][1])

3. Updating a List

You can update list elements by assigning a new value to an existing index or by
adding elements to the list.

Example:
# Updating existing elements
numbers = [1, 2, 3, 4, 5]
numbers[1] = 10 # Update the second element
print(numbers) # Output: [1, 10, 3, 4, 5] # Adding elements
numbers.append(6) # Append a single element
numbers.extend([7, 8]) # Add multiple elements
print(numbers) # Output: [1, 10, 3, 4, 5, 6, 7, 8]

4. Deleting List Elements

You can remove elements using methods like del, remove(), pop(), or clear().

Example:
# Deleting elements
numbers = [1, 2, 3, 4, 5]
# Using 'del' to remove by index
del numbers[0] # Deletes the first element
print(numbers) # Output: [2, 3, 4, 5]
# Using 'remove()' to delete by value
numbers.remove(3) # Removes the element with value 3
print(numbers) # Output: [2, 4, 5]
# Using 'pop()' to remove and return an element by index
last_element = numbers.pop(-1) # Removes the last element
print(last_element) # Output:5
print (numbers) #Output:[2,4]
# Using 'clear()' to delete all elements
numbers.clear()
print(numbers) #output:[]
4 Define methods in a string and write an example program using at least 5 methods. CO3 L2

In programming, methods in a string are operations or functions that manipulate or


analyze strings. Most programming languages, like Python, Java, or C#, provide
built-in methods to work with strings. For this example, I'll use Python to
demonstrate several string methods and write a program utilizing at least five of
them. Here's how it looks:

# Simple program to demonstrate string methods

def simple_string_methods():
text = "Welcome to Python programming"

print("Original Text:", text)

# 1. Convert to lowercase

print("Lowercase:", text.lower())

# 2. Convert to uppercase

print("Uppercase:", text.upper())

# 3. Count occurrences of a word

print("Count of 'Python':", text.count("Python"))

# 4. Check if the string starts with a word

print("Starts with 'Welcome':", text.startswith("Welcome"))

# 5. Replace a word in the text

print("After replacing 'Python' with 'Coding':", text.replace("Python", "Coding"))

# Call the function simple_string_methods()

What this program does:

1.​ lower(): Converts the string to all lowercase letters.


2.​ upper(): Converts the string to all uppercase letters.
3.​ count(): Counts occurrences of a specific word or character in the string.
4.​ startswith(): Checks whether the string starts with a specific word or
substring.
5.​ replace(): Replaces a word or part of the string with another word.

​ ​ ​ ​ ​ ​

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy