Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
315 views

Answers To Worksheet - 07. Conditional Constructs in Java

Uploaded by

janudev0205
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
315 views

Answers To Worksheet - 07. Conditional Constructs in Java

Uploaded by

janudev0205
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Unit 7 – Conditional constructs in Java

1. Explain with an example the if-else-if construct. [2] – 2007


Ans: It is a conditional / selection statement used to select alternative. It is also called the if-else-if
ladder, because of its appearance.
Example:
if(a>b && a>c)
max = a;
else if(b>a && b>c)
max = b;
else
max = c;
Here, the expressions are evaluated from top downward. As soon as an expression evaluates
to true, the statement associated with it is executed and the rest of the ladder is bypassed. If
none of the expressions are true, the final else gets executed. If the final else is missing, no
action takes place if all other conditions are false.

2. Explain, with the help of an example, the purpose of default in a switch. [2] – 2005
Ans: The default statement in a switch statement is used to execute a set of statements, if no
matching case is found. The default clause must be the last block, as it executes when none
of the previous case values are matching.
Example:
switch(choice)
{
case 1:
System.out.println(“Monday”);
break;
case 2:
System.out.println(“Tuesday”);
break;
:
:
default:
System.out.println(“Invalid day”);
break;
}

3. Differentiate between if and switch statements. [2] – 2006


Give two differences between the switch and the if-else statement. [2] – 2014
Differentiate between if else if and switch-case statements. [2] – 2019
Ans: if switch
(i) It can work with any type of data. (i) It can work with only int or char.
(ii) It can evaluate all relations. (ii) It can test for equality only.
(iii) It can examine more than one variable. (iii) It can examine only one variable.
(iv) It can match with variables.
(v) It can handle ranges. (iv) It can match with constants
(vi) It can handle floating-point tests. (v) It cannot handle ranges.
(vii) break has no importance with if itself. (vi) It cannot handle floating-point tests.
(viii) There is no fall through (vii) break is used to exit switch.
(viii) Faces fall through in the absence of
break.
4. Rewrite the following using ternary operator:
if(income <= 10000)
tax = 0;
else
tax = 12; [2] – 2007
Ans: tax = (income <= 10000) ? 0 : 12;

5. Rewrite the following using ternary operator:


if(x%2 == 0)
System.out.print(“EVEN”);
else
System.out.print(“ODD”); [2] – 2016
Ans: System.out.print((x%2 == 0) ? “EVEN” : “ODD”);

6. Rewrite the following using ternary operator:


if(bill > 10000)
discount = bill * 10.0/100;
else
discount = bill * 5.0/100; [2] – 2018
Ans: discount = (bill > 10000) ? bill * 10.0/100 : bill * 5.0/100;

7. Rewrite the following using ternary operator:


if(n1 > n2)
r = true;
else
r = false; [2] – 2020
Ans: r = (n1 > n2) ? true : false;

8. Rewrite the following program segment using the if…else statement:


comm = (sale>15000) ? sale*5/100 : 0; [2] – 2013
Ans: if(sale > 15000)
comm = sale * 5/100;
else
comm = 0;

9 Rewrite the following code using the if…else statement:


int m=400;
double ch = (m>300) ? (m / 10.0) * 2 : (m / 20.0) * 2; [2] – 2023
Ans: int m=400;
double ch;
if(m > 300)
ch = (m /10.0) * 2;
else
ch = (m /20.0) * 2;

10. Rewrite the following program segment using if…else statements instead of the ternary
operator:
String grade = (mark>=90) ? “A” : (mark>=80) ? “B” : “C”; [2] – 2014
Ans: String grade;
if(mark >= 90)
grade = “A”;
else if(mark >= 80)
grade = “B”;
else
grade = “C”;

11. Rewrite the following program segment using logical operators:


if(x > 5)
if(x > y)
System.out.println(x+y); [2] – 2020
Ans: if(x > 5 && x > y)
System.out.println(x+y);

12. Convert the following if…else…if construct into switch case


if(var == 1)
System.out.println(“good”);
else if(var == 2)
System.out.println(“better”);
else if(var == 3)
System.out.println(“best”);
else
System.out.println(“invalid”); [2] – 2018
Ans: switch(var)
{
case 1:
System.out.println(“good”);
break;
case 2:
System.out.println(“better”);
break;
case 3:
System.out.println(“best”);
break;
default:
System.out.println(“invalid”);
}

13. Convert the following if…else…if construct into switch case


if(ch == ‘c’ || ch == ‘C’)
System.out.println(“COMPUTER”);
else if(ch == ‘h’ || ch == ‘H’)
System.out.println(“HINDI”);
else
System.out.println(“PHYSICAL EDUCATION”); [2] – 2020
Ans: switch(ch)
{
case ‘c’:
case ‘C’:
System.out.println(“CIOMPUTER”);
break;
case ‘h’:
case ‘H’:
System.out.println(“HINDI”);
break;
default:
System.out.println(“PHYSICAL EDUCATION”);
}

14. Name the method, which terminates the entire program from any stage. [1] – 2020
Ans: exit( )

15. True or False: System.exit(0) terminates the program from any point. [1] – 2022
Ans: True

===========================

You might also like