Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

IC208 - Programming for Finance

Seminar 3 – Solutions

Q1) Given the following functionfun1() How to call the function calls?

def fun1(name, age):

 

print(name, age)

 

a)  fun1(name=’Emma’, age=23)

b)  fun1(name=’Emma’, 23)

c)  fun1(‘Emma’, 23)

d)  fun1(text=‘Emma’, age=23)

Ans: a+c


Q2) What is the output of following code?

def outerFun(a, b):

def innerFun(c, d):

return c + d

return innerFun(a, b)

res = outerFun(5, 10)

print(res)


a)   15

b)  Error

c)   15.0

d)  (5,10)

Ans: a

Q3) Select which true for Python function

a)  A function is a code block that only executes when it is called.

b)  Python function can only be called once.

c)  A function only executes when it is called and we can reuse it in a program

d)  Python doesn’t support nested function

Ans: a + c

Q4) What is the output of following code?


def add(a, b):



return a+5, b+5

result = add(3, 2)

print(result)


a)   15

b)  (8,7)

c)   8

d)  Error

Ans: b

Q5) What is the output when following code is executed?


func = lambda x: return x

print(func(2))


a)  2.0

b)  2

c)  x

d)  error


Ans: d we dont use return in lambda the correct code is

func = lambda x: x

print(func(2))


Q6) Define a class named human’ with 03 attributes and 02 methods, which can be applied on the class ‘human’ . Assign Adam as an object of the ‘human’ class and apply the methods on Adam.

Ans:

 

Q7) What is the output when following statement is executed?

a = "Hello ICMA!"

print(a.lower())

Ans:    ‘hello icma!’

Q8) What is the output when following statement is executed?

a = "Hello ICMA!"

print(a.upper())

Ans:    ‘HELLO ICMA!’

Q9) What is the output when following statement is executed?

a = "Hello ICMA!"

print(a.split())

Ans:    [‘Hello’,’ICMA!’]

Q10) What is the output when following statement is executed?

my_list = [‘I’,’love’,’coding’]

def func(x):

return ‘’ .join(x)

func(my_list)


how to write this function in lambda


Ans:    ‘Ilovecoding’

func = lambda x: ''.join(x)

func(my_list)

Q11) What is the output when following statement is executed?

age = 20

txt = "My name is Moso, I am " + age

print(txt)

Ans:

My name is Moso, I am 20

Q12) What is the output when following statement is executed?

age = 20

txt = "My name is Moso, I am {}. I love ICMA.".format(age)

print(txt)

Ans:

My name is Moso, I am 20. I love ICMA.

Q13) What is the output when following statement is executed?

names = [ 'John', 'Moso', 'Harma']

ages = [22, 20, 21]

for i in range(len(names)):

txt = "My name is {}, I am {}. I love ICMA.".format(names[i],ages[i])

print(txt)

Ans:

My name is John, I am 22. I love ICMA.

My name is Moso, I am 20. I love ICMA.

My name is Harma, I am 21. I love ICMA.

Q14) What is the output when following statement is executed?

my_dictionary = {'John': 22, 'Moso': 20, 'Harma': 21}

for key, value in my_dictionary.items():

txt = "My name is {}, I am {}. I love ICMA.".format(key,value)

print(txt)

Ans:

My name is John, I am 22. I love ICMA.

My name is Moso, I am 20. I love ICMA.

My name is Harma, I am 21. I love ICMA.