Q:

Java find output programs (switch case) | set 2

belongs to collection: Java find output programs

0

Find the output of Java programs | switch case | Set 2: 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) {
    int num = -4;

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

Question 2:

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

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

Question 3:

public class Main {
  public static void main(String[] args) {
    switch (14 % 4) {
    case 1:
      System.out.println("www.includehelp.com");
      break;
    case 2:
      System.out.println("www.fb.com");
    case 3:
      System.out.println("www.google.com");
      break;
    default:
      System.out.println("wrong choice");
      break;
    }
  }
}

Question 4:

public class Main {
  public static void main(String[] args) {
    switch (30 * 30 / 30) {
    case 10:
      System.out.println("www.includehelp.com");
      break;
    case 20:
      System.out.println("www.fb.com");
      break;
    case 30:
      System.out.println("www.google.com");
      break;
    default:
      System.out.println("wrong choice");
      break:
    }
  }
}

Question 5:

public class Main {
  public static void main(String[] args) {
    String STR = "hello";
    switch (STR) {
    case "HELLO":
      System.out.println("@@@@@@@@@@@@@@@");
      break;
    case "hello":
      System.out.println("$$$$$$$$$$$$$$$");
      break;
    case "STR":
      System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!");
      break;
    default:
      System.out.println("###############");
      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:8: error: duplicate case label
    case 15 - 11 : System.out.println("www.google.com");
    ^
1 error

Explanation:

The above program will generate a syntax error because we cannot use duplicate cases in the switch block.

Answer Question 2:

Output:

Wrong option
www.includehelp.com

Explanation:

In the above program, we create a local variable initialized with -4. Here we did not use the break statement in default case that's why it will execute default case as well as "case 5-1".

Answer Question 3:

Output:

www.fb.com
www.google.com

Explanation:

In the above program, we used "14%4" in switch condition, then it will return 2 then "case 2" will execute but we did not use break statement in "case 2" that’s why "case 3" will also execute.

Answer Question 4:

Output:

Main.java:15: error: ';' expected
      break:
           ^
1 error

Explanation:

The above program will generate a syntax error because we below statement.

default:
    System.out.println("wrong choice");
    break:

Here we used colon ":" instead of ";" in above break statement.

Answer Question 5:

Output:

$$$$$$$$$$$$$$$

Explanation:

In the above program, we created a class Main that contains a main() method, in the main() method we created a string variable STR, which is initialized with "hello". Then (case "hello") will execute and 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 3... >>
<< Java find output programs (switch case) | set 1...