7. Additional control statements Flashcards

1
Q

What would be the result of running the following code when noOfBedrooms has the value 3?

// noOfBedrooms is assumed to be non-negative.
switch (noOfBedrooms) {
  case 0:
    System.out.println("I hope you have a car!");
    break;
  case 1:
    System.out.println("Nice and cosy.");
    break;
  case 2: case 3: case 4:
    System.out.println("Room for a family...");
    break;
  case 5:
    System.out.println("You should get a lodger!");
    break;
  default:
    System.out.println("I hope you have a car!");
    break;
} // switch
A

ss

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Consider the following fragment of code.

int count = Integer.parseInt(args[0]);
switch (count) {
  case 5: System.out.println("Got 5");
  case 4: System.out.println("Got 4");
  case 3: System.out.println("Got 3");
  case 2: System.out.println("Got 2");
  case 1: System.out.println("Got 1");
  case 0: System.out.println("Got 0");
} // switch

Why are there five lines output of the first command line argument is 4?

A

ss

How well did you know this?
1
Not at all
2
3
4
5
Perfectly