Page 282 - ComputerScience_Class_11
P. 282
F. Case study-based questions.
Ramesh is developing a Java program for a small online shopping platform used by local vendors. The program calculates the final bill
amount for customers based on the price of items and quantity purchased. Since the calculation logic is required at multiple places
in the program, Ramesh creates a user-defined method to compute the total cost.
The main method sends the item price and quantity to the calculation method. Inside the method, these values are received using
formal parameters and the total amount is calculated and returned to the calling method.
Later, Ramesh extends the program by creating multiple methods with the same name to calculate bills for single items and bulk
orders by changing the number of parameters. He also adds a constructor to initialise default discount values whenever an object of
the shopping class is created.
Based on the given case, answer the following questions:
1. Why did Ramesh use a user-defined method for bill calculation?
a. To increase program length b. To avoid repetition of calculation code
c. To restrict access to variables d. To create multiple objects
2. What are the values passed from the main method to the calculation method called?
a. Formal parameters b. Actual parameters
c. Local variables d. Instance variables
3. Which feature allows Ramesh to use the same method name for different bill calculations?
a. Method overriding b. Constructor overloading
c. Method overloading d. Encapsulation
4. What is the role of the return statement in this program?
a. To display output on the screen b. To terminate the program
c. To send the calculated value back to the calling method d. To define parameters
5. Why is a constructor used in the shopping class?
a. To perform calculations b. To initialise instance variables automatically
c. To call other methods d. To overload methods
Answers
1. b 2. b 3. c 4. c 5. b
G. Identify the error in the given codes and rewrite the correct code:
1. class SumAverage {
void calculate(int m, int n) {
int sum = m + n;
double average;
average = sum/2;
System.out.println("Sum: "+sum+" Average: "+average);
}
void main(String[] args) {
calculate(10, 20);
}
}
Ans. class SumAverage {
void calculate(int m, int n) {
int sum = m + n;
double average;
average = sum/2.0;
System.out.println("Sum: "+sum+” Average: "+average);
}
public static void main(String[] args) {
280 Touchpad Computer Science (Ver. 3.0)-XI

