Q:

Java find output programs (switch case) | set 1

belongs to collection: Java find output programs

0

Find the output of Java programs | switch case | Set 1: Enhance the knowledge of Java switch case concepts by solving and finding the output of some Java programs.

Question 1:

public class Main {
  public static void main(String[] args) {
    boolean num;

    num = (null == 0);

    switch (num) {
    case true:
      System.out.println("www.includehelp.com");
      break;
    case false:
      System.out.println("www.google.com");
      break;
    }
  }
}

Question 2:

public class Main {
  public static void main(String[] args) {
    float num = 3.14F;
    int i = 0;

    switch (num) {
    case 3.14:
      System.out.println("www.includehelp.com");
      break;
    case 3.14F:
      System.out.println("www.google.com");
      break;
    }
  }
}

Question 3:

public class Main {
  public static void main(String[] args) {
    float num = 3.14F;
    int i = 0;

    switch ((int) Math.floor(num)) {
    case 3:
      System.out.println("www.includehelp.com");
      break;
    case 4:
      System.out.println("www.google.com");
      break;

    default:
      System.out.println("Wrong option");
      break;
    }
  }
}

Question 4:

public class Main {
  public static void main(String[] args) {
    int num = 5;

    switch (num) {
    case 1 to 5:
      System.out.println("www.includehelp.com");
      break;
    case 6 to 10:
      System.out.println("www.google.com");
      break;
    default:
      System.out.println("Wrong option");
      break;
    }
  }
}

Question 5:

public class Main {
  public static void main(String[] args) {
    int num = -4;

    switch (num) {
    case 1 - 5 : System.out.println("www.includehelp.com");
      break;
    case 6 - 15 : System.out.println("www.google.com");
      break;
    default:
      System.out.println("Wrong option");
      break;
    }
  }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Question 1:

Output:

Main.java:5: error: incomparable types:  and int
    num = (null == 0);
                ^
Main.java:7: error: incompatible types: boolean cannot be converted to int
    switch (num) {
           ^
2 errors

Explanation:

The above program will generate syntax errors because we cannot compare null with an integer value and we cannot use the boolean value in the switch block.

Answer Question 2:

Output:

Main.java:6: error: incompatible types: possible lossy conversion from float to int
    switch (num) {
           ^
1 error

Explanation:

The above program will generate an error because we cannot use the floating-point value in the switch in Java.

Answer Question 3:

Output:

www.includehelp.com

Explanation:

In the above program, we created a class Main that contains the main() method. In the main() method we created two local variables num and i initialized with 3.14F and 0 respectively.

switch ((int)Math.floor(num))

In the above switch statement, we used floor() method of Math class, it will convert value 3.14F in 3.0F then we typecast it into 3. Then the "case 3" block will execute and "www.includehelp.com" will be printed on the console screen.

Answer Question 4:

Output:

Main.java:6: error: : expected
    case 1 to 5:
          ^
Main.java:6: error: not a statement
    case 1 to 5:
           ^
Main.java:6: error: ';' expected
    case 1 to 5:
             ^
Main.java:9: error: : expected
    case 6 to 10:
          ^
Main.java:9: error: not a statement
    case 6 to 10:
           ^
Main.java:9: error: ';' expected
    case 6 to 10:
             ^
6 errors

Explanation:

The above program will generate syntax errors because we cannot use range using 'to' in the switch case in Java.

Answer Question 5:

Output:

www.includehelp.com

Explanation:

In the above program, we created a class Main that contains the main() method. In the main() method we created a local variable num initialized with -4.

case 1-5:
    System.out.println("www.includehelp.com");
    break;

The above case will be '-4' and the value of variable num is -4 that's why "www.includehelp.com" will print on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java find output programs (switch case) | set 2... >>
<< Java find output programs (if else) | set 3...