Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Conditional Constructs in Java

Chapter 8

Conditional Constructs in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Assignment Questions and Answers


Question 1

What is sequential flow of control? Explain with an example.

Answer

In sequential flow of control, the statements of a program are executed from top to bottom in order in which they are written including method calls. During execution, the control is transferred to the method called. The method is then executed in a sequential order and upon its completion, the control is transferred back to the calling method and execution of statements continues sequentially. For example, the following program will be executed in a sequential manner:

public class SequentialExample {

    static void showGreeting() {

        System.out.println("Welcome to KnowledgeBoat");

    }

     public static void main(String args[]) {

        System.out.println("Sequential Execution Program Example");

        showGreeting();

    }

}

Question 2

What is conditional flow of control? Explain with an example.

Answer

By default, the statements of a program are executed sequentially from top to bottom in order in which they are written. But most of the times our programs require to alter this top to bottom control flow based on some condition. When the flow of control of a program is changed based on some condition using control statements, it is termed conditional flow of control. For example, in the below program we achieve conditional flow of control using the if-else statement.

public class ConditionalExample {

    public static void main(String args[]) {

        int x = -10;

        if (x >= 0) {

            System.out.println("Positive Number");

        }

        else {

            System.out.println("Negative Number");

        }

    }

}

Question 3

Explain the significance of break statement in the switch statement.

Answer

The break statement is used inside the switch statement to terminate a statement block. It brings the program control out of the switch statement.

Question 4

What is a fall through? Give an example.

Answer

break statement at the end of case is optional. Omitting break leads to program execution continuing into the next case and onwards till a break statement is encountered or end of switch is reached. This is termed as fall through. For example, consider the below program:

public class FallThroughExample {

    public static void main(String args[]) {

        int number = 1;

        switch(number) {

            case 0:

                System.out.println("Value of number is zero");

            case 1:

                System.out.println("Value of number is one");

            case 2:

                System.out.println("Value of number is two");

            default:

                System.out.println("Value of number is greater than two");

        }

        System.out.println("End of switch");

    }

}

Its output when executed will be:

Value of number is one

Value of number is two

Value of number is greater than two

End of switch

Question 5

Explain the significance of the default label in the 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 println statement 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;

}

Output

Value of number is greater than two

Question 6

Explain the use of System.exit(n) method in Java.

Answer

The currently running program can be terminated with the help of the exit() method of the System class - System.exit(n). The argument n serves as a status code. A non-zero status code indicates abnormal termination, and a zero status code indicates a normal termination.

Question 7

Format the following if statements with indentation:

i. if (x == y) if (x == z) x = 1; else y = 1; else z = 1;

Answer

if (x == y)

    if (x == z)

        x = 1;

    else

        y = 1;

else

    z = 1;

ii. if (x == y) {if (y == z) x = 1; y = 2; } else z = 1;

Answer

if (x == y)

{

    if (y == z)

        x = 1;

    y = 2;

}

else

    z = 1;

iii. if (num1 != num2) {
    if (num2 >= num3) x = 1; y = 2; }
    else {x = 1; if (num1 == num2) z = 3;}

Answer

if (num1 != num2)

{

    if (num2 >= num3)

        x = 1;

    y = 2;

}

else

{

    x = 1;

    if (num1 == num2)

        z = 3;

}

Question 8

Rewrite the following if statement, using the switch statement:

if (choice == 1)

  System.out.println("You selected One");

else if (choice == 2)

  System.out.println("You selected Two");

else if (choice == 3)

  System.out.println("You selected Three");

else if (choice == 4)

  System.out.println("You selected Four");

else if (choice == 5)

  System.out.println("You selected Five");

else if (choice == 6)

  System.out.println("You selected Six");

else

  System.out.println("Invalid choice");

Answer

switch (choice) {

  case 1:

    System.out.println("You selected One");

    break;

   case 2:

    System.out.println("You selected Two");

    break;

   case 3:

    System.out.println("You selected Three");

    break;

   case 4:

    System.out.println("You selected Four");

    break;

   case 5:

    System.out.println("You selected Five");

    break;

   case 6:

    System.out.println("You selected Six");

    break;

   default:

    System.out.println("Invalid choice");

}

Question 9

Write the following switch statement by using nested if statements:

switch (choice)

{

case 0:

case 1:

x = 111;

y = 222;

 break;

case 2:

x = 333;

y = 444;

 break;

case 3:

x = -11;

y = -22;

 break;

default:

y = 555;

}

Answer

 

if (choice == 0 || choice == 1)

{

  x = 111;

  y = 222;

}

else

{

  if (choice == 2)

  {

    x = 333;

    y = 444;

  }

  else

  {

    if (choice == 3)

    {

      x = -11;

      y = -22;

    }

    else

    {

      y = 555;

    }

  }

}

Question 10

Write an if statement to find the smallest of the three given integers using the min() method of the Math class.

Answer

 

if (a < Math.min(b, c))

  System.out.println(a);

else

  System.out.println(Math.min(b, c));

Question 11

Find the error in the given statement:

int x = (a => b) ? "a" : "b";

Answer

  1. => is an invalid operator. it should be >=
  2. Type of x should be String.
Corrected Statement

String x = (a >= b) ? "a" : "b";

 

Question 12

Find the error, if any, in the following code. Write the correct statement.

 

int a=5, b=10;

int x = (a>b)>true:false;

Answer

  1. Ternary operator is written incorrectly.
  2. Type of x should be boolean.
Corrected Statement
 

int a=5, b=10;

boolean x = (a>b) ? true:false;

Question 13

Rewrite the following statement using if else:

 

int max=215, min=323;

String str= (max>min) ? "Max is greater than Min" : "Min is Greater than Max";

Answer

 

int max=215, min=323;

String str="";

if (max > min)

  str = "Max is greater than Min";

else

  str = "Min is Greater than Max";

Question 14

What will be the value of 'n' after the execution of the code given below?

 

int x=2, m=1, c=-1;

int n = x + c;

n = n - c + x;

System.out.println(n);

Answer

n will be 4 after execution of the code.

First n = x + c is executed ⇒ n = 2 + (-1) = 1.
Next, n = n - c + x is executed.
    n = n - c + x
⇒ n = 1 - (-1) + 2
⇒ n = 4
So final value of n is 4.

Question 15

What will be the output of the following code?

 

int x=2,y=5,a=0;

a=x;

x=y;

y=a;

System.out.println("x=" + x + " y=" + y);

Answer

Output is:
x=5 y=2

This code is swapping the values of x and y.

 

Question 16

Find the errors in the following code and rewrite the correct version:

 

char m="A";

Switch ("A");

{

Case 'a';

     System.out.println("A");

     break;

Case 'b';

     System.out.println("B");

     break;

Default:

     System.out.println("Not a valid option");

}

Answer

The code has the following errors:

  1. char variable m is assigned a String literal.
  2. S in Switch is capital.
  3. There should be no semicolon (;) after switch statement.
  4. Expression of switch statement is a String literal "A".
  5. C in Case is capital.
  6. Semicolon (;) of case statements should be replaced with colon (:).
  7. D in Default is capital.

Corrected Version:

 

char m = 'A';

switch (m)

{

  case 'a':

    System.out.println("A");

    break;

  case 'b':

    System.out.println("B");

    break;

  default:

    System.out.println("Not a valid option");

}