Board Q&A · Class 12 Computer Science

Working with Functions in Python: Class 12 CBSE Previous-Year Questions with Answers

CBSE Question Papers →

36 questions from CBSE board, sample and compartment papers (2024–2027), 1 to 3 marks, each with a model answer. Questions on defining and calling functions, positional and default parameters, return values, local and global variables (the global keyword), possible outputs with the random module, finding and correcting errors in given code, and writing short functions. Programming answers open in the Python Lab.

Showing 36 of 295 questions
Group

Working with Python Function

36 Questions
Working with Python Function· 1 Mark· Q1· 2024 Board Paper
State True or False: While defining a function in Python, the positional parameters in the function header must always be written after the default parameters.
Working with Python Function· 1 Mark· Q4· 2024 Board Paper Python
What possible output from the given options is expected to be displayed when the following Python code is executed? import random Signal = ['RED', 'YELLOW', 'GREEN'] for K in range(2, 0, -1): R = random.randrange(K) print(Signal[R], end = '#')
  1. (A)YELLOW # RED #
  2. (B)RED # GREEN #
  3. (C)GREEN # RED #
  4. (D)YELLOW # GREEN #
Working with Python Function· 1 Mark· Q12· 2024 Board Paper Python
Observe the given Python code carefully: a=20 def convert(a): b=20 a=a+b convert(10) print(a) Select the correct output from the given options:
  1. (A)10
  2. (B)20
  3. (C)30
  4. (D)Error
Working with Python Function· 2 Marks· Q20· 2024 Board Paper
The code given below accepts five numbers and displays whether they are even or odd: Observe the following code carefully and rewrite it after removing all syntax and logical errors. Underline all the corrections made. def EvenOdd() for i in range(5): num=int(input("Enter a number") if num/2==0: print("Even") else: print("Odd") EvenOdd()
Working with Python Function· 2 Marks· Q21· 2024 Board Paper Python
(A) Write a user defined function in Python named showGrades(S) which takes the dictionary S as an argument. S contains Name: [Eng, Math, Science] as key:value pairs. The function displays the corresponding grade obtained by the students according to the following grading rules: Average >= 90: Grade A < 90 but >= 60: Grade B < 60: Grade C For example: S={"AMIT": [92,86,64], "NAGMA": [65,42,43], "DAVID": [92,90,88]} Output should be: AMIT - B NAGMA - C DAVID - A OR (B) Write a user defined function in Python named Puzzle(W,N) which takes the argument W as an English word and N as an integer and returns the string where every Nth alphabet of the word W is replaced with an underscore ("_"). For example: if W contains "TELEVISION" and N is 3, then the function should return "TE_EV_SI_N". If N is 4, then return "TEL_VIS_ON".
Working with Python Function· 2 Marks· Q25· 2024 Board Paper Python
Predict the output of the following code: def callon(b=20,a=10): b=b+a a=b-a print(b,"#",a) return b x=100 y=200 x=callon(x,y) print(x,"@",y) y=callon(y) print(x,"@",y)
Working with Python Function· 1 Mark· Q10· 2025 Board Paper Python
Consider the statements given below and then choose the correct output from the given options: def Change(N): N=N+10 print(N,end='$$') N=15 Change(N) print(N)
  1. (A)25$$15
  2. (B)15$$25
  3. (C)25$$25
  4. (D)2525$$
Working with Python Function· 2 Marks· Q22· 2025 Board Paper Python
What does the return statement do in a function? Explain with the help of an example.
Working with Python Function· 2 Marks· Q25· 2025 Board Paper
What possible output from the given options is expected to be displayed when the following code is executed? import random Cards=["Heart","Spade","Club","Diamond"] for i in range(2): print(Cards[random.randint(1,i+2)],end="#")
  1. (A)Spade#Diamond#
  2. (B)Spade#Heart#
  3. (C)Diamond#Club#
  4. (D)Heart#Spade#
Working with Python Function· 2 Marks· Q26· 2025 Board Paper Python
The code given below accepts N as an integer argument and returns the sum of all integers from 1 to N. Observe the following code carefully and rewrite it after removing all syntax and logical errors. Underline all the corrections made. def Sum(N) for I in range(N): S=S+I return S print(Sum(10)
Working with Python Function· 1 Mark· Q9· 2026 Board Paper Python
What will be the output of the following code? def f1(a, b=1): print(a+b, end='-') c = f1(1, 2) print(c, sep='*')
  1. (A)3-2
  2. (B)3-2*
  3. (C)3-None
  4. (D)3*None-
Working with Python Function· 2 Marks· Q22· 2026 Board Paper Python
What is the difference between default parameters and positional parameters in Python? Also give an example of a function header which uses both.
Working with Python Function· 2 Marks· Q25· 2026 Board Paper Python
What possible output(s) from the given options will NOT be displayed when the following code is executed? Also, mention, for how many iterations the for loop in the given code will run? import random a = [1, 2, 3, 4, 5, 6] for i in range(4): j = random.randrange(i, 5) print(a[j], end='-') print() Options: (A) 3-4-5-4- (B) 2-2-4-5- (C) 4-3-3-5- (D) 5-1-2-4-
Working with Python Function· 2 Marks· Q26· 2026 Board Paper Python
The function given below is written to accept a string s as a parameter and return the number of vowels appearing in the string. The code has certain errors. Rewrite it after removing all the logical and syntax errors. def CountVowels(s): c = 0 for ch in range(s): if 'aeiouAEIOU' in ch: c =+ 1 return (ch)
Working with Python Function· 1 Mark· Q12· 2025 Compartment Paper Python
What will be the output of the following code segment? a = 5 def func_1(b=10): global a a = b - 10 b += a print(a, b) func_1(a)
  1. (A)0 5
  2. (B)5 0
  3. (C)0 -5
  4. (D)-5 0
Working with Python Function· 2 Marks· Q25· 2025 Compartment Paper Python
Identify the correct possible output(s) of the following code segment. Also write the minimum and the maximum possible values of the variable b. import random s="War and Peace" a = len(s)//2 for i in range(4): b = random.randrange(i, a) print(s[b], end='+') (A) n+P+d+a+ (B) W+r+n+n+ (C) e+r+W+a+ (D) a+P+e+r+
Working with Python Function· 1 Mark· Q10· 2024 Sample Paper Python
What possible outputs(s) will be obtained when the following code is executed? import random myNumber=random.randint(0,3) COLOR=["YELLOW","WHITE","BLACK","RED"] for I in range(1,myNumber): print(COLOR[I],end="*") print()
  1. (A)a. RED* WHITE* BLACK*
  2. (B)b. WHITE* BLACK*
  3. (C)c. WHITE* WHITE* BLACK* BLACK*
  4. (D)d. YELLOW* WHITE*WHITE* BLACK* BLACK* BLACK*
Working with Python Function· 1 Mark· Q12· 2024 Sample Paper Python
Consider the code given below: b=100 def test(a): # missing statement b=b+a print(a,b) test(10) print(b) Which of the following statements should be given in the blank for #Missing Statement, if the output produced is 110?
  1. (A)a. global a
  2. (B)b. global b=100
  3. (C)c. global b
  4. (D)d. global a=100
Working with Python Function· 1 Mark· Q18· 2024 Sample Paper
Assertion(A): Python Standard Library consists of various modules. Reasoning(R): A function in a module is used to simplify the code and avoids repetition. (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
  1. (A)(a) Both A and R are true and R is the correct explanation for A
  2. (B)(b) Both A and R are true and R is not the correct explanation for A
  3. (C)(c) A is True but R is False
  4. (D)(d) A is false but R is True
Working with Python Function· 2 Marks· Q20· 2024 Sample Paper Python
The code given below accepts a number as an argument and returns the reverse number. Observe the following code carefully and rewrite it after removing all syntax and logical errors. Underline all the corrections made. define revNumber(num) : rev = 0 rem = 0 While num > 0: rem ==num %10 rev = rev*10 + rem num = num//10 return rev print(revNumber(1234))
Working with Python Function· 2 Marks· Q21· 2024 Sample Paper Python
Write a function countNow(PLACES) in Python, that takes the dictionary, PLACES as an argument and displays the names (in uppercase)of the places whose names are longer than 5 characters. For example, Consider the following dictionary PLACES={1:"Delhi",2:"London",3:"Paris",4:"New York",5:"Doha"} The output should be: LONDON NEW YORK OR Write a function, lenWords(STRING), that takes a string as an argument and returns a tuple containing length of each word of a string. For example, if the string is "Come let us have some fun", the tuple will have (4, 3, 2, 4, 4, 3)
Working with Python Function· 2 Marks· Q25· 2024 Sample Paper Python
Predict the output of the following code: def Changer(P,Q=10): P=P/Q Q=P%Q return P A=200 B=20 A=Changer(A,B) print(A,B,sep='$') B=Changer(B) print(A,B,sep='$',end='###')
Working with Python Function· 1 Mark· Q12· 2025 Sample Paper Python
What will be the output of the following code? c = 10 def add(): global c c = c + 2 print(c,end='#') add() c=15 print(c,end='%')
  1. (A)12%15#
  2. (B)15#12%
  3. (C)12#15%
  4. (D)12%15#
Working with Python Function· 1 Mark· Q20· 2025 Sample Paper
Assertion (A): Positional arguments in Python functions must be passed in the exact order in which they are defined in the function signature. Reasoning (R): This is because Python functions automatically assign default values to positional arguments.
  1. (A)Both A and R are True and R is the correct explanation for A
  2. (B)Both A and R are True and R is not the correct explanation for A
  3. (C)A is True but R is False
  4. (D)A is False but R is True
Working with Python Function· 2 Marks· Q25· 2025 Sample Paper
Identify the correct output(s) of the following code. Also write the minimum and the maximum possible values of the variable b. import random a="Wisdom" b=random.randint(1,6) for i in range(0,b,2): print(a[i],end='#')
  1. (A)W#
  2. (B)W#i#
  3. (C)W#s#
  4. (D)W#i#s#
Working with Python Function· 2 Marks· Q26· 2025 Sample Paper Python
The code provided below is intended to swap the first and last elements of a given tuple. However, there are syntax and logical errors in the code. Rewrite it after removing all errors. Underline all the corrections made. def swap_first_last(tup) if len(tup) < 2: return tup new_tup = (tup[-1],) + tup[1:-1] + (tup[0]) return new_tup result = swap_first_last((1, 2, 3, 4)) print("Swapped tuple: " result)
Working with Python Function· 1 Mark· Q1· 2026 Sample Paper
State if the following statement is True or False: Using the statistics module, the output of the below statements will be 20: import statistics statistics.median([10, 20, 10, 30, 10, 20, 30])
Working with Python Function· 1 Mark· Q11· 2026 Sample Paper Python
What possible output is expected to be displayed on the screen at the time of execution of the Python program from the following code? import random L=[10,30,50,70] Lower=random.randint(2,2) Upper=random.randint(2,3) for K in range(Lower, Upper+1): print(L[K], end="@")
  1. (A)50@70@
  2. (B)90@
  3. (C)10@30@50@
  4. (D)10@30@50@70@
Working with Python Function· 1 Mark· Q12· 2026 Sample Paper Python
What will be the output of the following Python code? i = 5 print(i,end='@@') def add(): global i i = i+7 print(i,end='##') add() print(i)
  1. (A)5@@12##15
  2. (B)5@@5##12
  3. (C)5@@12##12
  4. (D)12@@12##12
Working with Python Function· 2 Marks· Q23· 2026 Sample Paper Python
The code provided below is intended to remove the first and last characters of a given string and return the resulting string. However, there are syntax and logical errors in the code. Rewrite it after removing all the errors. Also, underline all the corrections made. define remove_first_last(str): if len(str) < 2: return str new_str = str[1:-2] return new_str result = remove_first_last("Hello") Print("Resulting string: " result)
Working with Python Function· 2 Marks· Q25· 2026 Sample Paper Python
A. Write a function remove_element() in Python that accepts a list L and a number n. If the number n exists in the list, it should be removed. If it does not exist, print a message saying "Element not found". OR B. Write a Python function add_contact() that accepts a dictionary phone_book, a name, and a phone number. The function should add the name and phone number to the dictionary. If the name already exists, print "Contact already exists" instead of updating it.
Working with Python Function· 1 Mark· Q6· 2027 Sample Paper Python
What possible output is expected to be displayed on the screen at the time of execution of the Python program from the following code? import random games = {0: "Chess",1: "Scrabble",2: "Sudoku",3: "Tic-Tac-Toe",4: "Puzzle" } start_key = random.randint(1, 2) end_key = random.randrange(3, 4) for k in range(start_key, end_key + 1): print(games[k], end="@@")
  1. (A)Sudoku@@Tic-Tac-Toe@@Puzzle@@
  2. (B)Scrabble@@Sudoku@@Tic-Tac-Toe@@
  3. (C)Chess@@Scrabble@@Tic-Tac-Toe@@
  4. (D)Scrabble@@Tic-Tac-Toe@@
Working with Python Function· 1 Mark· Q16· 2027 Sample Paper
What will be the output of the following Python code? y = 8 def update(): y = y + 2 print(y) update()
  1. (A)10
  2. (B)8
  3. (C)Error
  4. (D)None
Working with Python Function· 2 Marks· Q22· 2027 Sample Paper
A. Define global variable and local variable with a suitable example. OR B. Write the difference between the seek() and tell() methods in Python text file handling with a suitable example.
Working with Python Function· 2 Marks· Q24· 2027 Sample Paper Python
A. Write a Python function capitalize_words(words) that takes a list of strings words and returns a new list with each word capitalize. Example: If the input is ["python", "computer", "science"] The output should be ["Python", "Computer", "Science"]. OR B. Write a Python function sum_positive(list1) that takes a list of numbers and prints the sum of all positive numbers in the list. Example: If the input is [10, -5, 20, -3, 7] The output should be 37.
Working with Python Function· 3 Marks· Q31· 2027 Sample Paper Python
A. Write a Python function avg_len( ) segment that adds up the lengths of all the words in a list and then prints the average (mean) length. OR B. Consider the tuple pairs = ((2,5),(4,2),(9,8),(12,10)). Write a Python function cnt_even( ) to count the number of pairs (a, b) where both a and b are even.