Page 151 - ComputerScience_Class_11
P. 151
3. Assertion (A): The + operator in Java can be used for both addition and string concatenation.
Reason (R): In Java, the + operator behaves differently depending on the data types of the operands; it performs addition for
numeric types and concatenation for strings.
Ans. a. Both A and R are true and R is the correct explanation of A.
F. Case study-based questions.
Ankur, a student in Grade 11, is learning Java programming and wants to develop a simple budget calculator for his monthly expenses.
He decides to use different types of literals and arithmetic operators to calculate his total expenses.
Each month, Ravi tracks his spending on various categories: food, entertainment, transport and utilities. He uses the following
approach:
1. For food, he spends ₹2,500.
2. For entertainment, he spends ₹1,200.
3. For transport, he spends ₹800.
4. For utilities, he spends ₹1,000.
Ravi wants to calculate the total expenses for the month. To do this, he declares variables for each category and uses the addition
operator to calculate the total. Ravi also plans to save a percentage of his total expenses for savings, so he uses the multiplication
operator to calculate how much he will save based on a savings rate of 10%.
Based on the given case, answer the following questions:
1. Which type of literal is used for storing the value ₹2,500 in Ravi's budget calculator?
a. Integer literal b. Floating-point literal
c. Character literal d. String literal
2. What data type would be most appropriate for storing the savings rate (10%) in Ravi’s budget calculator?
a. int b. double
c. boolean d. char
3. Which arithmetic operator is used to calculate the savings based on the total expenses in the case study?
a. + (Addition) b. - (Subtraction)
c. * (Multiplication) d. / (Division)
4. In the case study, if Ravi were to use String savings = "₹500" instead of a numeric value, what type of operation would not work
correctly?
a. Addition of expenses b. Calculating savings as a percentage
c. Printing the total expenses d. Subtracting transport expenses
Answers
1. a 2. b 3. c 4. b
G. Identify the error in the given codes and rewrite the correct code:
1. class Display {
public static void main(String[] args) {
int num = 5;
Integer obj = num;
System.out.println("Value of obj: " obj);
}
}
Ans. class Display {
public static void main(String[] args) {
int num = 5;
Integer obj = num;
System.out.println("Value of obj: " + obj);
}
}
Primitive Values, Wrapper Classes, Types and Casting 149

