About Question enthuware.ocajp.i.v7.2.1119 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

About Question enthuware.ocajp.i.v7.2.1119 :

Post by Ambiorix »

Code: Select all

public class Switcher{
 
   public static void main(String[] args){
       switch(Integer.parseInt(args[1]))  //1
       {
          case 0 :
             boolean b = false;
             break;
     
          case 1 :
             b = true; //2
             break;
       }
       
       if(b) System.out.println(args[2]);
   }
}
I was surprised that there is no compilation error at //2 since b isn't declared as a boolean.

While it is declared in case 0:, these lines won't execute if args[1] is 1, so how does the application know that b should be a boolean where it matches case 1:?

I tried commenting out the problematic print line and declaring boolean b = true; at //2, but the compiler complained that b is already defined.

Can I assume that any declarations in the first case are visible to all the other cases?

Also, what happens if I declare a variable in one of the later cases? Is this visible from anywhere in the switch, or only in the cases that follow it?

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1119 :

Post by admin »

Ambiorix wrote:Can I assume that any declarations in the first case are visible to all the other cases?
Yes.
Ambiorix wrote: Also, what happens if I declare a variable in one of the later cases? Is this visible from anywhere in the switch, or only in the cases that follow it?
[/quote]
Only the cases that follow. You might want to try it out as well.

javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1119 :

Post by javaman »

java Switcher 1 2 3...
Isn't Switcher called here with 5 arguments: "1"," ","2"," " and "3"??? There are spaces between the numbers.

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1119 :

Post by admin »

No, spaces are not considered arguments. If you want to give space as an argument, you need to put it in quotes, for example: java Switcher " " 1 2 3 <- here, the first argument is a space.

HTH,
Paul.

Shortrope
Posts: 15
Joined: Sun Jun 01, 2014 8:27 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1119 :

Post by Shortrope »

Here is another oddity:

if case 0 is not selected, it seems the variable b in case 0 is created but not initialized !

Code: Select all

switch(x) {
    case 0 :
        boolean b = false;
        System.out.println("Case 0\tb = " + b);
        break;
    case 1 :
        b = true;
        System.out.println("Case 1\tb = " + b);
        break;
}
This code runs fine for either 'case'
But if you comment out //b = true in case 1, ... you get a compile time error:

Code: Select all

Class.java:19: error: variable b might not have been initialized
                    System.out.println("Case 1\tb = " + b);
                                                        ^
You would think if the variable b was created from the statement in case 0, it would have been assigned false as well.

codecode
Posts: 2
Joined: Wed Apr 22, 2020 5:27 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1119 :

Post by codecode »

any ideas about this valid observation "You would think if the variable b was created from the statement in case 0, it would have been assigned false as well.", anyone?

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1119 :

Post by admin »

Well, what happened when you printed it out at that point?

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests