DMS I Test 2023
DMS I Test 2023
)
Bapuji Institute of Engineering and Technology, Davangere–577 004
Department of Master of Computer Applications
Mathematical Foundation for
Course Title Course Code 22MCA11
Computer Applications
Course
Roopa R Semester I
Coordinator
CIE No. 1st Max. Marks 20
9.00 am to
Date: 20-03-2023 Time
10.00am
OR
2 a) Explain in detail loop control statements while and while with else
break and continue.
The while Loop
With the while loop we can execute a set of statements as long as a con- 2
dition is true.
i=1
while i< 6:
print(i)
i += 1 4
While with else clause will be executed if control comes out of the loop
because of the loop condition evaluating to False.
Continue is used to skip the part of the loop. This statement exe-
2
Q. Marks Total
Solution
No. Marks
Part A
cutes the loop to continue the next iteration.
In python, the break and continue statements are jump state-
ments. The break statement is used to terminate the loop while in
the case of the continue statement it is used to continue the next
iterative in the loop.
b) Explain for loop, break and continue statements with syntax and
examples.
Python For Loop is used for iterating over a sequence (that is either a
list, a tuple, a dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and
works more like an iterator method as found in other object-orientated
programming languages.
With the for loop we can execute a set of statements, once for each item
in a list, tuple, set etc.
Example 3
Print each fruit in a fruit list:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x) 6
The break Statement
With the break statement we can stop the loop even if the while condition
is true:
Example 3
Exit the loop when i is 3:
i=1
while i < 6:
print(i)
if i == 3:
break
i += 1
The continue Statement
With the continue statement we can stop the current iteration, and con-
tinue with the next:
Example
Continue to the next iteration if i is 3:
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
Part B
3 a) Define Function. Explain both built in and user defined functions with 4
examples.
A function is a block of code which only runs when it is called. 2
You can pass data, known as parameters, into a function.
A function can return data as a result.
Q. Marks Total
Solution
No. Marks
Part A
Built in Function already defined in compiler.
Ex: abs(),bool()
Creating a Function
In Python a function is defined using the def keyword:
Ex: def my_function():
print("Hello from a function") 2
Calling a Function
To call a function, use the function name followed by parenthesis:
Ex: def my_function():
print("Hello from a function")
my_function()
b.Discuss scope and lifetime of a variable, *args and **kwargs with
example.
Scope and Lifetime of a variable:
Arbitrary Arguments, *args
If you do not know how many arguments that will be passed into your 3
function, add a * before the parameter name in the function definition.
This way the function will receive a tuple of arguments, and can access
the items accordingly:
If the number of arguments is unknown, add a * before the parameter
name:
def my_function(*kids):
print("The youngest child is "+ kids[2]) 6