Page 153 - CA_Blue( J )_Class10
P. 153
8. Consider the following String array and give the output: [2018]
String arr[] = {"DELHI", "CHENNAI", "MUMBAI", "LUCKNOW", "JAIPUR"};
System.out.println(arr[0].length() > arr[3].length());
System.out.print(arr[4].substring(0, 3));
Ans. false
JAI
9. Rewrite the following using ternary operator: [2018]
if(bill > 10000)
discount = bill * 10.0 / 100;
else
discount = bill * 5.0 / 100;
Ans. discount = (bill > 10000)? bill * 10.0 / 100: bill * 5.0 / 100;
10. Rewrite the following using ternary operator: [2016]
if(x % 2 == 0)
System.out.print("EVEN");
else
System.out.print("ODD");
Ans. System.out.print(((x % 2 == 0)? "EVEN":"ODD"));
11. Evaluate the value of n if value of p = 5, q = 19. [2015]
int n = (q - p) > (p - q)? (q - p) : (p - q);
Ans. 14 [Explanation: n = (19 - 5)>(5-19) = 14>-14 which is true.]
SECTION B
16. Design a class named ShowRoom with the following description: [2018]
Instance variables/data members:
String name : to store the name of the customer.
long mobno : to store the mobile number of the customer.
double cost : to store the cost of the items purchased.
double dis : to store the discount amount.
double amount : to store the amount to be paid after discount.
Member methods:
ShowRoom() : default constructor to initialize data members.
void input() : to input customer name, mobile number, cost.
void calculate() : to calculate discount on the cost of purchased items, based on the following criteria:
Cost Discount (in percentage)
Less than or equal to Rs. 10000 5%
More than Rs. 10000 and less than or equal to Rs. 20000 10%
More than Rs. 20000 and less than or equal to Rs. 35000 15%
More than Rs. 35000 20%
void display() : to display customer name, mobile number, amount to be paid after discount.
Write a main() method to create an object of the class and call the above member methods.
Ans. import java.util.*;
class ShowRoom
{
String name;
long mobno;
double cost,dis,amount;
151
Conditional Constructs in Java 151

