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

Python Function Test1_answer

The document contains a series of programming questions and answers related to Python, covering topics such as functions, variable scope, and list manipulation. Each question includes a specific coding task or theoretical question, followed by the correct answer. The document serves as an assessment tool for evaluating understanding of Python programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Python Function Test1_answer

The document contains a series of programming questions and answers related to Python, covering topics such as functions, variable scope, and list manipulation. Each question includes a specific coding task or theoretical question, followed by the correct answer. The document serves as an assessment tool for evaluating understanding of Python programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Roll No : Name of School : Time - MM- 91

Date : Name of Assessment :


Subject :
Class :

1 Find the output: 3

Ans :
False False False

2 Answer the following questions based on the given script/code. 3

(a) Name the variable used as parameter.


(b) Name the variable used as an argument
(c) Write function header statement.
(d) Which statement will return a value?
(e) Name a local variable(s).
(f) Name a global variable(s).
(g) Write function call statement.
(h) Variable 'a' will return the value to variable ______________.
(i) Name formal parameter

(a) a
(b) b
(c) def show(a) :
(d) return a
(e) a
(f) b
(g) res=show(b)
(h) res
Ans : (i) a

3 Write the output of the following script/code 3


(a) Bye
(b) Hello
Ans : Bye

4 Write a user defined function arrangelements(X),that accepts a list X of integers and 3


sets all the negative elements to the left and all positive elements to the right of the
list.
For example:
if L =[1,-2,3,4,-5,7] ,
Then the output should be: [-2,-5,3,4,7]

Ans :

5 Write a menu driven python program using function to do the following: 1. Reverse a 4
Number, 2. Check whether a number is Palindrome or not and 3. To check whether it is
Armstrong or not and 4 to exit. Program should continue till the user says 'no'.
Ans :
6 Find the output of the following code 5
P=100
def changer(P,Q=10):
P=P/Q
Q=P%Q
print(P,"#",Q)
return P
A=200
B=20
A=changer(A,B)
print(A,"$",B)
B=changer(B)
print(A,"$",B)
Ans : 10.0 # 10.0 10.0 $ 20 2.0 # 2.0 10.0 $ 2.0

7 Write a Function Filter() which will take a list as parameter and made the different 5
lists Even[] and Odd[]. And then combine the two list and return the main list i.e.
nested list.
Ans : def filter(L): even=[] odd=[] master=[] for i in L: if i%2==0: even.append(i)
else: odd.append(i) master.append(even) master.append(odd) return master

8 Write a function Common () to accept lists as argument and return the list with 5
common elements from both the lists.
Ans : def common(L1,L2): c=[] for i in L1: if i in L2: c.append(i) return c

9 WAF to take a list and inserting element as parameter and insert the new element at 5
begining of the list.
Ans : def insertbeg(list,n): # Inserting n in the list list = [n] + list[0:]
print("New List",list) # Main li=eval(input("Enter the List"))
ele=int(input("Enter the new element to be inserted")) insertbeg(li,ele)

10 Write a Python program to push all zeros to the end of a given list a. The order of the 5
elements should not change.
Input: Elements of the list with each element
separated by a space.
Output: Elements of the modified list with each
element separated by a space. After the last
element, there should not be any space.
Example:
Input:0 2 3 4 6 7 0 1
Output:2 3 4 6 7 1 0 0
Ans : def pushZerosToEnd(arr, n): count = 0 for i in range(n): if arr[i] != 0:
arr[count] = arr[i] count+=1 while count < n: arr[count] = 0 count += 1 #Main
arr = [1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9] n = len(arr) pushZerosToEnd(arr, n)
print("Array after pushing all zeros to end of array:") print(arr)

11 What will be the output of the following code? 1


import math
abs(math.sqrt(25))
(a) 5
(b) 5.0
(c) error
(d) No output
Ans : (b)

12 What is the output of the functions shown below? 1


min(max(False, -3,-4),2,7)
(a) –3
(b) –4
(c) True
(d) False
Ans : (c)

13 Which type of functions are defined in external files? 1


(a) User-defined functions
(b) Built-in functions
(c) Module functions
(d) None of the above

Ans : (c)

14 Which of the following is not correct in context of scope of variables? [CBSE 2021] 1
(a) global keyword is used to change value of a global variable in a local scope
(b) local keyword is used to change value of a local variable in a global scope
(c) global variables can be accessed without using the global keyword in a local
scope
(d) local variables cannot be used outside its scope
Ans : (b)

15 For a function header as follows: [CBSE 2021] 1


def Calc(X, Y=20)
Which of the following function calls will give an Error?
(a) Calc(15, 25)
(b) Calc(X=15, Y=25)
(c) Calc(Y=25)
(d) Calc(X=25)

Ans : (c)

16 Which of the following is not correct in context of Positional and Default parameters 1
in Python functions? [CBSE 2021]
(a) Default parameters must occur to the right of Positional parameters
(b) Positional parameters must occur to the right of Default parameters
(c) Positional parameters must occur to the left of Default parameters
(d) All parameters to the right of a Default parameter must also have Default values.
Ans : (b)

17 Which of the following is not a function/method of the random module in Python 1


[CBSE 2021]
(a) randfloat()
(b) randint()
(c) random()
(d) randrange()
Ans : (a)

18 What will be the output of the following Python code? [CBSE 2021] 1
V = 25
def Fun (Ch):
V=50
print (V, end=Ch)
print (V, end="*")
Fun ("!")
print (V)
(a) 25*50!25
(b) 50*100!100!100
(c) 25*50!100!100
(d) Error
Ans : (a)

19 What is the output for the following Python code? 1


def ListChange():
for i in range (len(L)):
if L[i]%2 == 0:
L[i]=L[i]*2
if L[i]%3 == 0:
L[i]=L[i]*3
else:
L[i]=L[i]*5
L = [2,6,9,10]
ListChange()
for i in L:
print(i,end="#")
(a) 4#12#27#20#
(b) 6#18#27#50#
(c) 20#36#27#100#
(d) Error

Ans : (c)

20 In the following questions, a statement of assertion (A) is followed by a statement of 1


reason (R). Choose the correct answer out of the following choices.
Assertion (A): A variable declared outside a function has global scope in Python.
Reason (R): This means that it can be accessed and modified from inside any function.
(a) Both Assertion (A) and Reason (R) are true and Reason (R) is the correct
explanation of Assertion (A)
(b) Both Assertion (A) and Reason (R) are true and Reason (R) is not the correct
explanation of Assertion (A)
(c) Assertion (A) is true but Reason (R) is false
(d) Assertion (A) is false but Reason (R) is true
Ans : (a)

21 What is the syntax for creating a function with no parameters or return value? 1
(a) def function_name():
(b) def function_name(parameter):
(c) def function_name() -> return:
(d) def function_name() -> parameter:
Ans : (a)

22 What will be the output of the following Python code? [CBSE 2021] 1
def FunStr(S):
T=""
for i in S:
If i.isdigit():
T=T + i
return T
X = "PYTHON 3.9"
Y=FunStr(X)
print(X, Y, sep="*")
(a) PYTHON 3.9
(b) PYTHON 3.9*3.9
(c) PYTHON 3.9*39
(d) Error
Ans : (a)

23 What will be the output of the following Python code? [CBSE 2021] 1
v = 50
def Change (N):
global V
V, N = N, V
print(V, N, sep="#",end="@")
Change(20)
print(V)
(a) 20#50@20
(b) 50@20#50
(c) 50#50#50
(d) 20@50#20
Ans : (a)

24 In the following questions, a statement of assertion (A) is followed by a statement of 1


reason (R). Choose the correct answer out of the following choices.
Assertion (A): A function in Python can have only one return statement.
Reason (R): (a) This ensures that the function always returns a value.
(b) Having multiple return statements can lead to confusion and errors.
(a) Both Assertion (A) and Reason (R) are true and Reason (R) is the correct
explanation of Assertion (A)
(b) Both Assertion (A) and Reason (R) are true and Reason (R) is not the correct
explanation of Assertion (A)
(c) Assertion (A) is true but Reason (R) is false
(d) Assertion (A) is false but Reason (R) is true
Ans : (b)

25 In the following questions, a statement of assertion (A) is followed by a statement of 1


reason (R). Choose the correct answer out of the following choices.
Assertion (A): A function in Python can modify the value of a global variable without
using the global keyword.
Reason (R): (a) This is a recommended practice to avoid name clashes.
(b) This can lead to unexpected behavior and bugs.
(a) Both Assertion (A) and Reason (R) are true and Reason (R) is the correct
explanation of Assertion (A)
(b) Both Assertion (A) and Reason (R) are true and Reason (R) is not the correct
explanation of Assertion (A)
(c) Assertion (A) is true but Reason (R) is false
(d) Assertion (A) is false but Reason (R) is true
Ans : (d)

26Indian Law Organization is planning to computerized their application form. For 5


registration, organization has setup the rules imposed by Indian Law. Citizen can
check their eligibility criteria by providing Nationality, Gender and Age.

On the basis of above code, choose the best appropriate option.


(a) Which statement will be executed if Nationality is Indian, Gender is Male and
Age is 19.
(i) Statement 1 (ii) Statement 2
(iii) Statement 3 (iv) Statement 4
(b) Which Statement will be executed, if Nationality is American, Gender is Female
and Age is 27.
(i) Statement 1 (ii) Statement 2
(iii) Statement 4 (iv) Statement 5
(c) Which Statement is executed, if Nationality is Indian, Age is 25 and Gender if
Female.
(i) Statement 1 (ii) Statement 2
(iii) Statement 3 (iv) Statement 4
(d) Which Statement is executed, if Nationality is Indian, Age is 17 and Gender is
Female.
(i) Statement 1 (ii) Statement 2
(iii) Statement 3 (iv) Statement 4
(e) Which statement will be executed if Nationality is Indian, Gender is Male and
Age is 29.
(i) Statement 1 (ii) Statement 2
(iii) Statement 3 (iv) Statement 4

Ans : (a) (ii) Statement 2 (b) (iv) Statement 5


(c) (iii) Statement 3 (d) (iv) Statement 4
(e) (i) Statement 1

27 Find the output 2

Ans : 100 # 40 40 # 100

28 Rewrite the following python code after removing any/ all syntactical errors with 2
each correction underlined:

Ans :

29 Find and write the output of the following python code: 2


def Call(P=40,Q=20):
P=P+Q
Q=P-Q
print(P,'@',Q)
return P
R=200
S=100
R=Call(R,S)
print (R,'@',S)
S=Call(S)
print(R,'@',S)
Ans : 300 @ 200 300 @ 100 120 @ 100 300 @ 120

30 Find and write the output of the following python code: 2


def ChangeVal(M,N):
for i in range(N):
if M[i]%5 == 0 :
M[i] //= 5
if M[i]%3 == 0 :
M[i] //= 3
L=[ 25,8,75,12]
ChangeVal(L,4)
for i in L :
print(i, end='#')
Ans : 5#8#5#4#

31 What will be the output of the following Python code? 2


def func(a, b=5, c=10):
function addition.txt
print('a is', a, 'and b is', b, 'and c is', c)
func(3, 7)
func(25, c = 24) func(c = 50, a = 100)

Ans : 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

32 What will be the output of the following Python code? 2


x=12
def f1(a,b=x): print(a,b)
x=15
f1(4)
Ans : 4 12

33 Write the output of the following Python code 2


Ans : 0 @6 @7 @8 @9 @25 @11 @ 0 @6 @7 @8 @9 @ 0 @6 @7 @

34 Write the output of the following Python code: 2

Ans : 110 120 120 20 10 30 50 50 20 10

35 Write the output of the given code: 2

Ans : New string is : iNdiA%****

36 Write the output of the given code: 2


Ans : x = 15 def change(): #using a global keyword global x
# increment value of a by 5 x = x + 5 print("Value of x inside a function :", x)
change() print("Value of x outside a function :", x)
# Python program to modify a global # value inside a function x = 15 def change():
#using a global keyword global x # increment value of a by 5 x = x + 5
print("Value of x inside a function :", x)
change() print("Value of x outside a function :", x) Value of x inside a function : 20
Value of x outside a function : 20

37 Write the output of the given code 2


def Check(K, L=70):
if(K>L):
K-=L
else:
K+=L
M=100
N=40
Check(M,N)
print(M,"#",N)
Check(M)
print(N,"#",M)
Ans : 100 # 40 40 # 100

38Write the definition of a function Reverse(X) in Python, to display the elements in 2


reverse order such that each displayed element is the twice of the original element
(element * 2) of the List X in the following manner:
Example:
If List X contains 7 integers is as follows:
X[0] X[1 X[2] X[3 X[4] X[5] X[6]
4 8 7 5 6 2 10
After executing the function, the array content should be displayed as follows:
Ans : #Filename:Ch-datas-P_Q9 def Reverse(X): for i in range(len(X)-1,-1,-1):
print(X[i]*2)

39 Write definition of a method ZeroEnding(SCORES) to add all those values in the list 2
of SCORES, which are ending with zero (0) and display the sum.
For example,
If the SCORES contain [200,456,300,100,234,678] The sum should be displayed as
600.
Ans : #Filename:Ch-datas-P_Q10 def ZeroEnding(SCORES): s=0
for i in SCORES: if i%10==0: s=s+i print (s)

40 Write definition of a Method COUNTNOW(PLACES) to find and display those place 2


names, in which there are more than 5 characters. For example:

Ans : #Filename:Ch-datas-P_Q11 def COUNTNOW(PLACES): for P in PLACES:


if len(P)>5: print (P)

41 Write definition of a Method AFIND(CITIES) to display all the city names from a list 2
of CITIES, which are starting with alphabet A.
For example:
If the list CITIES contains
[“AHMEDABAD”,”CHENNAI”,”NEW
DELHI”,”AMRITSAR”,”AGRA”]
The following should get displayed
AHEMDABAD
AMRITSAR
AGRA
Ans : #Filename:Ch-datas-P_Q16 def AFIND(CITIES): for i in CITIES:
if i[0]=='A': print (i)

You might also like