Selection Statement – if else and switch in Java Important Questions Answer

Selection Statement – Question Answer in Java

Java Important Question Answer

Selection (if else & switch) Statement in Java

800+Questions XII Information Technology Code 802
Buy Chapter wise Question bank Class 12 Information Technology 802

800+Questions XII Information Technology Code 802

Que 1. What do you mean by Control flow?

Answer: Control flow is a Cursor/control which executes the statement/instruction.

Que 2. How many types of control structures are in Java?

Answer: There are three types of Control structures. These are

  • (a) Sequential Structure
  • (b) Selection Structure
  • (c) Iteration Structure

Que 3. What is Sequential Structure?

Answer: When Java executes the instructions in sequential order i.e. one after the other, called Sequential structure.

Que 4. What is Selection Structure?

Answer: When Java executes the instructions only if the condition holds true or another set of instructions when the condition is false, called the Selection structure.

Que 5. How many types of Selection statements are in Java?

Answer: There are two types of selection statements in Java.

(a) if else statement           and (b) switch statement

Que 6. What is an if statement in Java?

Answer: The if statement in Java lets us execute a block of code depending upon whether an expression evaluates to true. If the condition is true then execute the block of if, otherwise not.

The structure of the Java if statement is as below:

            if (expression or condition) {

                        Statements

            }

The expression given in the round brackets after the if keyword, is a condition – an expression constructed using relational and logical operators. If the condition evaluates to true, the block of code following if is executed, otherwise not.

Que 7. What is the use of curly brackets with if statement in Java?

Answer: The curly brackets is generally used to represent a block of if statement. All statements i.e. body of if statement is written inside the curly bracket.

If there is only one statement in the block after the if, the curly braces are optional.

Que 8. Write a Java program to input percentage marks. If percentage secured by the student is greater than or equal to 40%, declare a result PASSED otherwise declare a result FAILED. Using if statement only.

Answer:

            percentage = Float.parseFloat(jTextFiled1.getText());

            if (percentage >= 40 ){

                        jTextField2.setText(“PASSED”)

            }

            if (percentage < 40 )

                        jTextField2.setText(“FAILED”);

Que 9. What is if else statement in Java?

Answer: The if statement in Java allows to execute block of if when condition evaluates to true, while else statement allows to execute a block on condition evaluate false.

else statement is the optional part of if. It is only required when you want to execute different set of statements on true condition and false condition.

The structure of the Java if else statement is as below:

            if (expression or condition) {

                        Statements

            }

            else {

                        Statements

            }

The block of code following if is executed if the conditional expression evaluates to true. The block of code following the else is executed if the conditional expression evaluates to false.

Que 10. Write a Java program to input percentage marks. If percentage secured by the student is greater than or equal to 40%, declare a result PASSED otherwise declare a result FAILED. Using if else statement only.

Answer:

            percentage = Float.parseFloat(jTextFiled1.getText());

            if (percentage >= 40 )

                        jTextField2.setText(“PASSED”)

            else

                        jTextField2.setText(“FAILED”);

800+Questions XII Information Technology Code 802
Buy Chapter wise Question bank Class 12 Information Technology 802

800+Questions XII Information Technology Code 802

Que 11. Which operators are used to combine two or more conditional expressions.

Answer: Logical operators – && (Logical AND) and || (Logical OR) are used to combine two or more conditional expressions in java.

Que 12. Write a conditional expression to check the percentage is between 40 and 60.

Answer:  In this case we will use the && (Logical AND) Operator.

  • percentage >= 40 && percentage <= 60
  • percentage > 39 && percentage < 61

Que 13. What do you mean by nested if?

Answer:  If written inside the another if, is called Nested if.

if (expression) {

            Statement

            if (expression) {

                        statement

            }

}

Que 14. What do you mean by nested if-else?

Answer:  if-else statement is written either inside the if or else or both, called nested if-else.

Syntax: Method – 1

if (condition1)

{

            if (condition2) {

                        statement1

            }

            else {

                        statement2

            }

}

else {

            statement 3

}

Syntax: Method – 2

if (condition1)

{

            Statement1

}

else {

            if (condition) {     

                        statement2

            }

            else {

                        statement3

            }

}

Syntax: Method – 3

if (condition1)

{

            if (condition2) {

                        statement1

            }

            else {

                        statement2

            }

}

else {

            if (condition2) {

                        statement3

            }

            else {

                        statement4

            }

}

Que 15. What do you mean by if-else if?

Answer:  else if is use to check the condition with else statement.

Syntax:

            if (condition1){

                        statements

            }

            else if (condition2){

                        statements

            }

            else{

                        statements

            }

Que 16. Write a JAVA program to input a number. Check the value of number and print the following message as per given criteria.

number >= 10Super
Number >= 5 && number < 10Ok
Number >= 3 && number < 5Average
OtherwiseBelow Average

Answer:

int number;

number = Integer.parseInt(jTextField1.getText());

if (number >= 10){

            jTextField2.setText(“Super”);

}

else{

            if (number >= 5 && number < 10) {

                        jTextField2.setText(“Ok”);

            }

            else{

                        if(number >= 3 && number < 5 ){

                                    jTextField2.setText(“Average”);

                        }

                        else{

                                    jTextField2.setText(“Below Average”);

                        }

            }

}

Que 17. Write a JAVA program to input a number. If the number is even then Display its square otherwise its cube. [SQP]

Answer:

int a, result;

a = Integer.parseInt(jTextField1.getText());

if(a%2 == 0)

result=a*a;

else if(a%2!=0)

                        result=a*a*a;

System.out.println(“ “ +result);

Que 18. Write a JAVA program to input a number. Check the given number is even number or odd number [SQP]

Answer:

int a, result;

a = Integer.parseInt(jTextField1.getText());

if(a%2 == 0)

System.out.println(“Even Number”);

else                           

System.out.println(“Odd Number“);

Que 19. Write a JAVA program to input percentage marks and print the appropriate grade.

PercentageGrade
>= 80A
>=60 and < 80B
>=40 and < 60C
OtherwiseD

Answer:

int percentage;

String Grade

percentage = Integer.parseInt(jTextField1.getText());

if(percentage >= 80)

            Grade = “A”;

else if (percentage >= 60 && percentage < 80)

            Grade = “B”;

else if (percentage >= 40 and percentage < 60)

            Grade = “C”;

else

            Grade = “D”;

System.out.println(“Your Grade is “, +Grade);

Que 20. What will be the value of n after the execution of the following selection statement:  [CBSE SQP]

int n = 0;

if (6 > 7 II 5 > 4) {

     n = 5;

}

else

     n = 7;

            System.out.println(“n = ”, +n);

Answer:  n = 5

800+Questions XII Information Technology Code 802
Buy Chapter wise Question bank Class 12 Information Technology 802

800+Questions XII Information Technology Code 802

Que 21. A group of statements which get executed based on a condition in java is called _______.

(a) Selection             (b) Sequential          (c) Iteration             (d) None of these

Answer: (a) Selection

Que 22. What is the switch statement in Java?

Answer: The switch statement is used to execute a block of code matching one value out of many possible values.

The structure of the Java switch statement is as follows:

switch (expression) {

case constant_1 :   statements;

                                                break;

case constant_2 :   statements;

                                                break;

            …

default :                    statements;

break;

            }

Que 23. What is the use of case in switch statement?

Answer: Within the switch statement, there can be many case statements.

switch (expression) {

case constant_1 :   statements;

                                                break;

case constant_2 :   statements;

                                                break;

            …

default :                    statements;

break;

            }

The expression is compared with the constants in each case group and the matching case group is executed. If the expression evaluates to some constant = constant_1, the statements in the case group constant_1 are executed.

Similarly, if the expression evaluates to constant_2, the statements in the case group constant_2 are executed.

Que 24. What is the purpose of using default in a switch statement?

Answer: If there is no match for the expression with any case group, the statements in the

default part is executed.

For example:

{

switch(day) {

case 6: day = “Saturday”;

                          break;

            case 7: day = “Sunday”;

                       break;

            default: day = “Incorrect Day!”

                       break;

    }

System.out.println (day);

}

if the value of day is other that 6 or 7 default statement will be executed.

Que 25. What is the use of break statement in switch statement?

Answer: The break statement after each case group terminates the switch and causes execution to continue to the statements after the switch.

Que 26. What happen, if break statement is not used in switch statement?

Answer: The break statement after each case group terminates the switch and causes execution to continue to the statements after the switch.

If break statement is not written after any case, and that case is true, then after execution of respective case, the control moves to the another case, it assume that case is also true, and their body will execute. This is known as fall through.

The execution will continue either the break statement encounter or reach to the end of the switch.

Que 27. What is fall through condition in Java?

Answer: This condition occurs in the switch control statement when there is no break keyword mention for the particular case in the switch statement and cause execution of the cases till no break statement occurs or exit from the switch statement.

Example:

class FallThrough {

public static void main(String[] args) {

int value = 1;

switch (value ){

  case 1:  

  System.out.println(“Number 1”);

case 2:

System.out.println(“Number 2”);

case 3:

System.out.println(“Number 3”);

default :

System.out.println(“This is default case”);

}

}

    }

Output

          Number 1

          Number 2

          Number 3

          This is default case

Que 28. What will be the value of rem after the execution of the following code snippet? Give reason. [CBSE SQP]

code = 2;

switch(code) {

     case 1: rem = 8;

     case 2: rem = 9;

     case 3: rem = 10;

               break;

      case 4: rem= 11;

            break;

}

Answer:  rem = 10

Reason: case 2 will be executed and rem will become 9 but because of absence of break statement the control will move down (fall through) and case 3 will be executed which will make rem as 10 now break will terminate the switch case statement.

Que 29. Consider the following code. What will be the output of following program and why? [CBSE SQP]

public class SwitchDemo {

public static void main (String[ ] args)

{

int day = 6;

String dayname = ‘‘ ’’;

switch (day)

{

case 1 : dayname = ‘‘Monday’’;

break;

case 2 : dayname = ‘‘Tuesday’’;

break;

case 3 : dayname = ‘‘Wednesday’’;

break;

case 4 : dayname = ‘‘Thursday’’;

break;

case 5 : dayname = ‘‘Friday’’;

break;

case 6 : dayname = ‘‘Saturday’’;

break;

case 7 : dayname = ‘‘Sunday’’;

break;

default : dayname = ‘‘Invalid day’’;

}

System.out.println(dayname);

}

}

Answer:  Saturday

Reason: The value of day is 6, which matched with the case 6 and it assign Saturday to dayname, after case 6, break statement will send the control outside the switch. Print statement print the result.

Que 30. What will be the output of following Code?

int Number = 20;

switch (Number)

{

case 10:

System.out.println(“Ten Thousand”);

break;

case 20:

System.out.println (“Twenty Thousand”);

case 30:

System.out.println (“Thirty Thousand”);

break;

case 40:

System.out.println (“Forty Thousand”);

default:

System.out.println (“Not enough!!!”);

}

Answer:  Output is

Twenty Thousand

Thirty Thousand

Reason: case 20 will be executed and Twenty Thousand will be printed but because of absence of break statement the control will move down (fall through) and case 30 will be executed which will print Thirty Thousand now break will terminate the switch case statement.

Que 31. What types of value (expression) you can compare in switch statement in java?

Answer: The value (expression) in the switch statement must evaluate to byte, short, int, or char.

Que 32. Differentiate between if-else and switch.

Answer:

if –elseswitch
(i) Any types of value can be compared.(i) Only byte, short, int or char type values are compared
(ii) All relational operators are allowed in conditional expression. > , <, ==, !=, >=, <=(ii) Only equal to or not equal to will be compared in switch.
800+Questions XII Information Technology Code 802
Buy Chapter wise Question bank Class 12 Information Technology 802

800+Questions XII Information Technology Code 802

You cannot copy content of this page

Scroll to Top