You are on page 1of 5

Worksheet

2022-2023

Name: Arnav Ravinder


Subject: Computer Science
Grade: 8B Topic: User-Defined Functions
Date: 17-02-2023

Compare the programs given below and write the similarities and differences between them
Question 1:
Code # 1 Code # 2

Similarities :
● Both programs define a function called add() that takes three arguments a, b, and c.
● Both programs prompt the user to enter three integer values for a, b, and c using the input() function.
● Both programs calculate the average of the three values using the add() function.
● Both programs print the result to the console using the print() function.

Differences :
● In Code #1, the add() function returns the sum of a, b, and c, while in Code #2, the add() function
calculates the average of a, b, and c.
● In Code #1, the average is calculated by dividing the sum of a, b, and c by 3 outside of the add()
function, while in Code #2, the add() function returns the average directly.
Worksheet
2022-2023
Question 2
Code # 1 Code # 2

Similarities :
● Both programs define a function called count() that takes two arguments a and b.
● Both programs prompt the user to enter two integer values for a and b using the input() function.
● Both programs call the count() function to calculate the number of integers between a and b (inclusive)
that are divisible by 3.
● Both programs print the result to the console using the print() function.

Differences :
● In Code #1, the user is prompted to enter two sets of values for a and b in a loop that runs twice,
while in Code #2, the user is prompted to enter two sets of values for a and b separately outside of
any loop.
● Code #2 uses more variables (a2, b2, x2) than Code #1.
Worksheet
2022-2023
Question 3:

A function compare() takes two strings as parameters and checks if they have the same starting and ending
characters. It returns 0 if they have the same starting and ending characters, and -1 if they don’t. There are
some errors in the code. Identify the line numbers and rectify the error.
Example:
The inputs “Hello” and “Hi” return -1 as they have the same starting but different ending characters
The inputs “India” and “Indiana” return 0 as they have the same starting and ending characters

Corrected Code:

Errors:
● In line 2, the and condition only checks for the second character instead of the last character. It should
be checking for S1[-1] == S2[-1] instead of SI[1] == S2[1].
● In line 3 and 5, the print statement is missing parentheses, causing a syntax error.
● In line 5, the vertical bar character should be a hyphen to indicate a negative return value.
● In line 8, the function is called with two arguments, but the function definition only has one parameter.
Also, the print statement should be outside the function.
Worksheet
2022-2023
Question 4:

Given below is a function check() that takes a list called Price as a parameter. The list Price has the cost of
10 items. The function counts how many items have a price of more than 500, returns the result and prints it
at the end. There are five errors in the code. Identify the line numbers and rectify the error.

Corrected Code:

Errors:
● In line 5, C is assigned to 0 inside the loop, causing it to always return 0. It should be incremented by 1
instead.
● In line 9, Price.append() doesn't append anything to the list. It should append the input value P instead.
● The function is called in line 10, but the return value is not stored in a variable. It should be stored in a
variable called result.
● The print statement should be outside the function and should display the value of result.
Worksheet
2022-2023

You might also like