ADVERTISEMENT
ADVERTISEMENT

Java find output programs (switch case) | set 1

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.
Submitted by Nidhi, on January 30, 2021

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;
    }
  }
}

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.

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;
    }
  }
}

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.

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;
    }
  }
}

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.

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;
    }
  }
}

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.

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;
    }
  }
}

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.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.