Docsity

Computer Science – Functions in Python, Exams of Computer science

Toggle sidebar
bg1
Name of the Chapter-FUNCTIONS IN PYTHON
Q
NO.
QUESTION
LEARNING OUTCOME
1
Study the following program and select the
possible output :
Import random
X=random.random()
Y=random.randint(0,4)
print(int(x),”:”,y+int(x))
a) -1:0 b)1:6 c)2:4 d)0:3
Function defined in module
2
Select output given by the following code
a={i:i*I for I in range(6)}
print(a)
a) Dictionary comprehension doesn’t exist
b) {0:0,1:1,2:4,3:9,4:16,5:25,6:36}
c) {0:0,1:1,4:4,9:9,16:16,25:25}
d) {0:0,1:1,2:4,3:9,4:16,5:25}
Built in function range
3
Choose the correct output of the following Python
code:
def makenew(mystr):
newstr=””
count=0
for x in mystr:
if count%2!=0:
newstr=newstr+str(count)
else:
if x.islower():
newstr=newstr+x.upper()
else:
newstr=newstr+x
count+=1
newstr=newstr+mystr[:1]
print(“The new string is:”,newstr)
makenew(“sTUdeNT”)
a) ST11111s
b) StU1234s
c) sT12345s
d) sT111111
User defined function
4
Choose the correct output of the following Python
code:
def ChangeList():
l=[]
l1=[]
l2=[]
for I in range(1,10):
User defined function
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30

Partial preview of the text

Download Computer Science – Functions in Python and more Exams Computer science in PDF only on Docsity!

Name of the Chapter-FUNCTIONS IN PYTHON Q NO.

QUESTION LEARNING OUTCOME

Study the following program and select the possible output : Import random X=random.random() Y=random.randint(0,4) print(int(x),”:”,y+int(x)) a) -1:0 b)1:6 c)2:4 d)0: Function defined in module 2 Select output given by the following code a={i:i*I for I in range(6)} print(a) a) Dictionary comprehension doesn’t exist b) {0:0,1:1,2:4,3:9,4:16,5:25,6:36} c) {0:0,1:1,4:4,9:9,16:16,25:25} d) {0:0,1:1,2:4,3:9,4:16,5:25} Built in function range 3 Choose the correct output of the following Python code: def makenew(mystr): newstr=”” count= for x in mystr: if count%2!=0: newstr=newstr+str(count) else: if x.islower(): newstr=newstr+x.upper() else: newstr=newstr+x count+= newstr=newstr+mystr[:1] print(“The new string is:”,newstr) makenew(“sTUdeNT”) a) ST11111s b) StU1234s c) sT12345s d) sT User defined function 4 Choose the correct output of the following Python code: def ChangeList(): l=[] l1=[] l2=[] for I in range(1,10): User defined function

l.append(i) for i in range(0,1,-2): l1.append(i) for i in range (len(l1)): l2.append(l1[i]+l[i]) l2.append(len(l)-len(l1)) print(l2) ChangeList() a)[11,10,9,8,7,4] b)[11,10,9,8,7,6] c)[11,10,9,8,7] d)[10,9,8,7,6,5] 5 Choose the correct output of the following Python code: def Findoutput(): L=”earn” X=”” L1=[] Count= for I in L: if i in [‘a’,’e’,’I’,’o’,’u’]: X=X+i.swapcase() else: if(count%2!=0): X=X+str(len(L[:count])) else: X=X+i Count=count+ Print(X) Findoutput() a)EArn b)EA3n c)EA 5)EARN User Defined Function and Function defined in module 6 Which output is not expected from the following program: import random X= N=random.randint(1,x) for i in range(N): print (I,”#”,i+1) a)0# b) 1# c)2# d)3# Function defined in module 7 What will be the output of the following Python code?

  1. def printMax(a, b):
  2. if a > b: User Defined Function

a is 3 and b is 7 and c is 10 a is 25 and b is 5 and c is 24 a is 100 and b is 5 and c is 50 d) None of the mentioned 10 What will be the output of the following Python code?

  1. def maximum(x, y):
  2. if x > y:
  3. return x
  4. elif x == y:
  5. return 'The numbers are equal'
  6. else:
  7. return y
  8. print(maximum(2, 3)) a) 2 b) 3 c) The numbers are equal d) None of the mentioned User defined function 11 What will be the output of the following Python code? def change(i = 1, j = 2): i = i + j j = j + 1 print(i, j) change(j = 1, i = 2) a) An exception is thrown because of conflicting values b) 1 2 c) 3 3 d) 3 2 User defined function 12 What will be the output of the following Python code? def change(one, *two): print(type(two)) change(1,2,3,4) a) Integer b) Tuple c) Dictionary d) An exception is thrown Built in function 13 If a function doesn’t have a return statement, which of the following does the function return? a)int b) null c) None d) An exception is thrown without the return statement Return value of a function

14 What will be the output of the following Python code? def display(b, n): while n > 0: print(b,end="") n=n- display('z',3) a) zzz b) zz c) An exception is executed d) Infinite loop User defined function 15 What will be the output of the following Python code? def find(a, **b): print(type(b)) find('letters',A='1',B='2') a) String b) Tuple c) Dictionary d) An exception is thrown Built in function 16 Python passes arguments to function by

  1. Reference
  2. Value
  3. Both
  4. None of the above User defined function 17 ____________ is a blank space in the beginning of a statement within a block. a. Indention b. Space c. Body d. None of the above User defined function 18 These are predefined functions that are always available for use. For using them we don’t need to import any module. a. Built function b. Predefined function c. User defined d. None of the above Built in function 19 A function is said to be___________ if it calls itself a. Built function b. Pre defined function c. recursive function d. None of the above User defined function

27 If the return statement is not used in the function then which type of value will be returned by the function? a) int b) str c) float d) None User defined function 28 Divya wants to print the identity of the object used in the function. Which of the following functions is used to print the same? a) identity() b) ide() c) id() d) idy() in-built functions 29 Function sqrt() is available under which module of python? a. random b. math c. statistics d. None of the above 30 The default value for a parameter is defined in function .....................? a. Definition b. Return statement c. Header d. None of the above 31 Function randint() is available under which module of python? a. random b. math c. statistics d. None of the above 32 ANSWERS Question No Answer (^1) d (^2) d (^3) a (^4) a (^5) b (^6) d (^7) c

(^8) a (^9) c (^10) b (^11) d (^12) b (^13) c (^14) a (^15) c (^16) b.)python passes arguments to function by value (^17) a)Indentation is the blank space in the beginning of a statement within a block (^18) a)Built in functions are predefined functions that are always available for use.For using them we do not need to import any module. For example:len(),type(),int(),input(). (^19) c) recursive function is a function which calls itself (^20) c) scope of a variable is the area of the program where it may be referenced (^21) b) Stack (^22) 2) at end (^23) 2) void function (^24) b)functions (^25) a) (^26) d) (^27) d) (^28) c) (^29) b) (^30) (c) (^31) (a) 32 MCQ BASED QUESTIONS 1 A ______________ is a block of code that will execute only when it is called. a. Function b. Sub Program c. Block d. All of the above Define a function. 2 ________________ is the blank space in the beginning of a statement within a block. a. Indention b. Space c. Body d. None of the above How to write function.

8 By default if you return multiple value separated by comma, then it is returned as a. List b. Tuples c. String d. None of the above. Function returning values 9 Find the output of following code : x= def study(x): global x x= print(“Value of x is :”, x) a. 100 b. 50 c. Error d. None of the above Local and global variables. 10 The values being passed through function-call statement are called ________________. a. arguments b. parameter c. values d. none Function passing values. 11 The values received in function definition/header statement are called ______________. a. arguments b. parameter c. values d. none Function passing values. 12 What will be the output of the following code: def calculate(a, b): return a+b, a-b res = calculate(7, 7) print(res) a. (14,0) b. [14, 0] c. 14 d. 0 How to return a value by a function.

13 Which of the following function header is correct :

a. def discount(rate=7,qty,dis=5)

b. def discount(rate=7,qty,dis)

c. def discount(rate,qty,dis=5)

d. def discount(qty,rate=7,dis)

14 Select which of the following is NOT TRUE for Python function:

a. A function only executes when it is called and we can

reuse it in a program

b. In a python function a keyword argument can not be

followed by a positional argument

c. A Python function can return multiple values

d. Python function doesn’t return anything unless and until

you add a return statement

15 Select the correct function call for the following given function

fun1():

a. fun1(name='Emma', age=23,'Female')

b. fun1(age='23', name='Emma', gender='female')

c. fun1('Emma', age=23, Gender='Female')

d. func1('Emma', 23, 'Female' )

16 Q12. What is the output of the following function call?

a. 8-

b. Syntax Error

c. -

d. 5

17 What will be the output of the following code?

def fun(x=10, y=20):

x+=

y = y - 3

return x*y

print(fun(5),fun())

a. 20, 200

12 (a) (14, 0) 13 (c) 14 (d) 15 (b) 16 (d) 17 (b) 18 (c) Name of the vetter- INDER PAL SINGH Name of the KV- KV RBNM SALBONI Region-KOLKATA Mobile No- E-mail ID [email protected]

Name of the Chapter-FUNCTIONS IN PYTHON 1 Assertion: The function header ‘def read (a=2, b=5, c):’ is not correct. Reason: Non default arguments can’t follow default arguments. Please choose the correct choice out of the four options given below: a. Both Assertion and reason are true and reason is the correct explanation of assertion. b. Assertion and reason both are true but reason is not the correct explanation of assertion. c. Assertion is true, reason is false. d. Assertion is flase, reason is true. Defining a default argument. 2 A function code is given as follows: def study (num = 5): print(num + 5) Assertion: We can call the above function either by statement ‘study(7)’ or ‘study( )’. Reason: As the function contains default arguments, it depends on the caller that the above function can be called with or without the value. Please choose the correct choice out of the four options given below: a. Both Assertion and reason are true and reason is the correct explanation of assertion. b. Assertion and reason both are true but reason is not the correct explanation of assertion. c. Assertion is true, reason is false. d. Assertion is false, reason is true. Calling a function having default argument. 3 Assertion: len( ), type( ), int( ), input( ) are the functions that are always available for use. Reason: Built in functions are predefined functions that are always available for use. For using them we don’t need to import any module. Please choose the correct choice out of the four options given below: Built in functions.

Statement B: Default arguments can be used to combine similar

functions into one

a. Statement A is correct

b. Statement B is correct

c. Both are correct

d. Both are incorrect

7 Assertion (A) : Built in function are predefined in the language

that are used directly

Reason (R) : print() and input() are built in functions

a. Both A and R are true and R is the correct explanation for

A.

b. Both A and R are true and R is not the correct explanation

for A.

c. A is true but R is false

d. A is false but R is true

ANSWERS 1 (a) 2 (a) 3 (a) 4 (c) 5 (c) 6 (c) 7 (b)

TYPE ARQ(5)

DIRECTIONS: In the following questions, A statement of Assertion (A) is followed by a statement of Reason (R). Mark the correct choice as: (A) Both A and R are true and R is the correct explanation for A (B) Both A and R are true and R is not correct explanation for A (C) A is true but R is false (D) A is false but R is true QNO ASSERTION - REASONING LEARNING OUTCOME

ANSWER

  1. Assertion(A): Built in function are predefined in the language that are used directly Reason(R): print() and input() are built in functions Built in function

B

  1. Assertion(A):Keyword arguments are related to the function calls Reason(R): When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name Argument passing

A

  1. Assertion(A): A function is a block of organized and reusable code that is used to perform a single, related action Reason(R): Function provides better modularity by your application and a high degree of code reusability. User defined function

A

4 Assertion(A): It is not possible to delete/remove the elements from a list Reason(R): The pop(), remove(), del() functions are used to remove/delete the elements from the list. Built in function

D

5 Assertion(A): There are various built in functions/methods to manipulate the dictionaries Reason(R): Built in functions does not work on dictionaries Built in function

C

Name of the vetter- INDER PAL SINGH Name of the KV- KV RBNM SALBONI Region-KOLKATA Mobile No- E-mail ID [email protected]

4 Aman wants to write a function in python. But he doesn’t know how to start with it! Select the keyword used to start a function out of the following: a) function b) start c) def d) fun ANSWER CBQ NO Answer

1 A) b

1 B) a

2 a

3 b

4 c

SECTION - A

MCQ (Multiple Choice Questions) Sl. No Question Learning objectionv 1 A ______________ is a block of code that will execute only when it is called. a. Function b. Sub Program c. Block d. All of the above Define a function. 2 ________________ is the blank space in the beginning of a statement within a block. How to write function.

a. Indention b. Space c. Body d. None of the above 3 Every function return a value if function don’t explicitly return a value, then it will return __________. a. return b. None c. Value d. All How function return values. 4 There are predefined functions that are available in module. For using them we need to import module a. built-in functions b. predefined functions c. user defined functions d. function defined in module Built in functions. 5 The ______________ of a variable is the area of the program where it may be referred. a. external b. global c. scope d. local Local and Global Scope of a variable. 6 Find the output of following code : x= def study(x): x= print(“Value of x is :”, x) a. 100 b. 50 Local and Global Variable