Page 1 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE Ph: 9336061496, , ASSIGNMENT 3, SWITCH CASE -1, Question 1:, What will be the output of the following program., class WeekDays, {, public static void main( ), {, int day = 4;, switch(day), {, case 1:, System.out.println("Monday");, case 2:, System.out.println("Tuesday");, case 3:, System.out.println("Wednesday");, case 4:, System.out.println("Thursday");, case 5:, System.out.println("Friday");, default:, System.out.println("Weekend");, break;, }, }, }, Question 2:, What will be the output of the following program., class Directions, {, public static void main( ), {, char direction = 'N';, char west = 'W';, switch(direction), {, case 'N':, System.out.println("North");, break;, case 'E':, System.out.println("East");
Page 2 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE Ph: 9336061496, break;, case west:, System.out.println("West");, break;, case 'S':, System.out.println("South");, }, }, }, Question 3:, Arrange the following such that the program output is 4 1 2., int input = 4;, switch(input), {, case 1:, System.out.print("1 ");, }, case 2:, System.out.print("2 ");, default:, System.out.print(input + " ");, break;, Question 4:, What will be the output of the following program., class HappyNewYear, {, public static void main( ), {, int code = 3;, switch(code), {, case 1:, System.out.println("Wish");, case 2:, System.out.println("You");, default:, System.out.println("A");, case 3:, System.out.println("Happy");, case 4:, System.out.println("New");, case 5:
Page 3 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE Ph: 9336061496, System.out.println("Year");, }, }, }, Question 5:, Which of the following two types are acceptable as expressions for, the switch statement? The expression in the below example is expr., switch (expr), {, default:, System.out.println("Hello");, }, 1. byte 2. long 3. char 4. float 5. double 6. boolean, Question 6:, What will be the output of the following program?, public class Jerusalem {, public static void main( ) {, int i = 1, j = 0;, switch (i) {, case 2 :, j += 6;, case 4 :, j += 1;, default :, j += 2;, case 0 :, j += 4;, }, System.out.println("j = " + j);, }, }, Question 7:, What will be the output of the following program?, class DemoOnSwitch, {, public static void main( ), {, int firstVariable, secondVariable = 1;, firstVariable = (secondVariable > 1) ? 2 : 1;
Page 4 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE Ph: 9336061496, switch (firstVariable), {, case 0 :, System.out.print(0);, break;, case 1 :, System.out.print(1);, case 2 :, System.out.print(2);, break;, case 3 :, System.out.print(3);, break;, }, }, }, Question 8:, What will be the output of the following program?, public class DemoOnSwitchCase, {, public static void main( ), {, int variable = 7;, switch (variable++), {, case 7 :, switch (variable--), {, default :, System.out.print(variable);, case 7 :, }, default :, System.out.print("wonders");, }, }, }, Question 9:, What will be the output of the following program?
Page 5 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE Ph: 9336061496, public class ScopeOfVariables {, public static void main( ) {, int x = 10;, int y = 20;, {, System.out.print(x + ", " + y);, }, {, x = 15;, System.out.print(" - " + x + ", " + y);, }, System.out.print(" - " + x + ", " + y);, }, }, Question 10:, int a=1,b=2,c=3;, switch(p), {, case 1: a++;, case 2: ++b;, break;, case 3: c--;, }, System.out.println(a + ",", + b + "," +c);, (i) p = 1, (ii) p = 3, Question 11:, Convert the following constructs as directed, switch case construct into if-else-if :, switch (n), {, case 1:, s=a+b;, System.out.println("Sum="+s);, break;, case 2:, d=a-b;, System.out.println("Difference="+d);, break:, case 3:, p=a*b;, System.out.println("Product="+p);
Page 6 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE Ph: 9336061496, break;, default:, System.out.println("Wrong Choice!");, }, Question 12:, if-else-if construct into switch case:, if(var==1), System.out.println("Distinction");, else if(var==2), System.out.println("First Division");, else if(var==3), System.out.println("Second Division");, else, System.out.println("invalid");