Conditional Statements notes
Conditional Statements notes
Question 1
In a switch case, when the switch value does not respond to any case then the
execution transfers to:
1. a break statement
2. a default case ✓
3. a loop
4. none
Question 2
1. p=Integer.parseInt(in.readLine());
2. c=++a;
3. if(a>b) a++; b- - ; ✓
4. a=4;
Question 3
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. All ✓
Question 4
Question 5
if(a>b)
c=a;
else
c=b;
It can be written as:
1. c= (b>a)?a:b;
2. c= (a!=b)?a:b;
3. c= (a>b)?b:a;
4. None ✓
Question 6
If a, b and c are the sides of a triangle then which of the following statement is true
for: if(a!=b && a!=c && b!=c)?
1. Equilateral triangle
2. Scalene triangle ✓
3. Isosceles triangle
4. All of the above
Question 7
1. Arithmetic operator
2. Relational operator ✓
3. Ternary operator
4. None
Question 8
Question 9
1. for statement
2. switch statement ✓
3. if-else
4. none
Question 10
A Java program executes but doesn't give the desired output. It is due to:
Question 1
Answer
Question 2
if-else statement
Answer
switch statement
Question 3
(a) nested if
Answer
We can write an if-else statement within another if-else statement. We call this
nested if. It has the following syntax:
if (condition 1) {
if (condition 2) {
Statement a;
Statement b;
..
}
else {
Statement c;
Statement d;
..
}
}
else {
if (condition 3) {
Statement e;
Statement f;
..
}
else {
Statement g;
Statement h;
..
}
}
(b) if - else
Answer
if - else statement is used to execute one set of statements when the condition is
true and another set of statements when the condition is false. It has the
following syntax:
if (condition 1) {
Statement a;
Statement b;
..
}
else {
Statement c;
Statement d;
..
}
(c) if - else - if
Answer
if - else - if ladder construct is used to test multiple conditions and then take a
decision. It provides multiple branching of control. It has the following
syntax:
if (condition)
statement;
else if (condition)
statement;
else if (condition)
statement;
..
..
else
statement;
Question 4
Answer
1. switch can only test for equality whereas if can test for any Boolean
expression.
2. switch tests the same expression against constant values while if-else-if
ladder can use different expression involving unrelated variables.
3. switch expression must only evaluate to byte, short, int, char.
4. A switch statement will run much faster than the equivalent program
written using the if-else-if ladder
Question 5
Answer
Question 6
Explain with the help of an example, the purpose of default in a switch statement.
Answer
When none of the case values are equal to the expression of switch statement
then default case is executed. In the example below, value of number is 4 so
case 0, case 1 and case 2 are not equal to number. Hence
System.out.println of default case will get executed printing "Value of number
is greater than two" to the console.
int number = 4;
switch(number) {
case 0:
System.out.println("Value of number is zero");
break;
case 1:
System.out.println("Value of number is one");
break;
case 2:
System.out.println("Value of number is two");
break;
default:
System.out.println("Value of number is greater than two");
break;
}
Question 7
Answer
Question 8
Answer
Answer
if (a < b) {
/*
* All statements within this set of braces
* form the compound statement
*/
Question 10
Answer
if - else - if ladder construct is used to test multiple conditions and then take a
decision. It provides multiple branching of control. Below is an example of if -
else - if:
Question 11
Answer
Question 12
Give two differences between the switch statement and the if-else statement.
Answer
Switch if-else
switch can only test if the expression is if-else can test for any boolean expression like
equal to any of its case constants less than, greater than, equal to, not equal to, etc.
Question 1
int a=1,b=1,m=10,n=5;
if((a==1)&&(b==0))
{
System.out.println((m+n));
System.out.println((m—n));
}
if((a==1)&&(b==1))
{
System.out.println((m*n));
System. out.println((m%n));
}
Output
50
First if condition is false, second if condition is true. Statements inside the code
block of second if condition are executed.
m*n => 10 * 5 => 50
m%n => 10 % 5 => 0
Question 2
int x=1,y=1;
if(n>0)
{
x=x+1;
y=y+1;
}
What will be the value of x and y, if n assumes a value (i) 1 (ii) 0?
Output
(i) n = 1
x=2
y=2
(ii) n = 0
x=1
y=1
Explanation
When n = 1, if condition is true, its code block is executed adding 1 to both x and
y. When n = 0, if condition is false so x and y retain their original values.
Question 3
int b=3,k,r;
float a=15.15,c=0;
if(k==1)
{
r=(int)a/b;
System.out.println(r);
}
else
{
c=a/b;
System.out.println(c);
}
Output
Explanation
Question 4
switch (opn)
{
case 'a':
System.out.println("Platform Independent");
break;
case 'b':
System.out.println("Object Oriented");
case 'c':
System.out.println("Robust and Secure");
break;
default:
System.out.println("Wrong Input");
}
When (i) opn = 'b' (ii) opn = 'x' (iii) opn = 'a'
Output
Object Oriented
Explanation
case 'b' is matched, "Object Oriented" gets printed to the console. As there is no
case statement in case 'b', program control falls through to case 'c' printing "Robust
and Secure" to the console. case 'c' has a break statement which transfers the
program control outside switch statement.
Wrong Input
Explanation
Platform Independent
Explanation
case 'a' is matched, "Platform Independent" gets printed to the console. break
statement in case 'a' transfers the program control outside switch statement.
Correct the errors in the given programs
Question 1
class public
{
public static void main(String args{})
{
int a=45,b=70,c=65.45;
sum=a+b;
diff=c-b;
System.out.println(sum,diff);
}
}
Explanation
Corrected Program
class Square
{
public static void main(String args[])
{
int n=289,r;
r=sqrt(n);
if(n==r)
System.out.println("Perfect Square");
else
System.out.println("Not a Perfect Square");
}
}
Explanation
Corrected Program
class Square
{
public static void main(String args[])
{
int n=289;
double r=Math.sqrt(n); //1st & 2nd correction
if(n==r)
System.out.println("Perfect Square");
else
System.out.println("Not a Perfect Square");
}
}
Question 3
class Simplify
{
public static void main(String args[])
{
int a,b,c,d;
a=10,b=5,c=1,d=2;
c=a2+b2;
d=(a+b)2;
p=c/d;
System.out.println(c + " "+ " "+d+ " "+p);
}
}
Explanation
Corrected Program
class Simplify
{
public static void main(String args[])
{
int a=10,b=5,c=1,d=2; //1st correction
c = (int)(Math.pow(a, 2) + Math.pow(b, 2)); //2nd correction
d = (int)Math.pow((a+b), 2); //3rd correction
int p=c/d; //4th correction
System.out.println(c + " "+ " "+d+ " "+p);
}
}
Question 4
class Sample
{
public static void main(String args[])
{
int n,p;
float k,r;
n=25;p=12;
if(n=25)
{
k=pow(p,2)
System.out.println("The value of"+p+ " = "+k);
}
else
{
r=Math.square root(n);
System.out.println("The value of"+n+ " = "+r);
}
}
}
Explanation
Corrected Program
class Sample
{
public static void main(String args[])
{
int n,p;
float k,r;
n=25;p=12;
if(n==25) //1st correction
{
k=(float)Math.pow(p,2); //2nd correction
System.out.println("The value of"+p+ " = "+k);
}
else
{
r=(float)Math.sqrt(n); //3rd correction
System.out.println("The value of"+n+ " = "+r);
}
}
}