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

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

Moderator: admin

ETS User

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

Post by ETS User »

Hi,

Judging from the answer. it looks like the last case statement in this question should read [case 5: System.out.println("F")]. Can you please confirm or reject this? Thanks.

Current code:

Code: Select all

public class ForSwitch{
    public static void main(String args[]){
        char i;
        LOOP: for (i=0;i<5;i++){
            switch(i++){
                case '0': System.out.println("A");
                case 1: System.out.println("B"); break LOOP;
                case 2: System.out.println("C"); break;
                case 3: System.out.println("D"); break;
                case 4: System.out.println("E");
                case 'E' : System.out.println("F");
            }
        }
    }
}

ETS User

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

Post by ETS User »

Apologies - after a bit more investigation, I've realised that there is no big issue here.

ETS User

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

Post by ETS User »

Also... the question asks for select two options (two letters), but in the explanation, it says that, actually, 3 letters were printed. "C", "E" and "F".

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

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

Post by admin »

ETS User wrote:Also... the question asks for select two options (two letters), but in the explanation, it says that, actually, 3 letters were printed. "C", "E" and "F".
That is ok because the one of the letters that is printed is not in the options list. You can still select two options.

You can expect questions in the exam that show only partial output.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Javanaut

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

Post by Javanaut »

I dont see how case:1 is not reached...

a) char is initialized to literal int 0
b) char passes i < 5 condition in loop
c) i is incremented to 1
d) case statement is looked for that matches this...

I run this in Eclipse and get the same answer as shown but I am not seeing how i stays at zero instead of post-incrementing to 1 as the first trip through the switch

:?

Javanaut

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

Post by Javanaut »

I think I get it now...

I thought the post-increment would happen instantly since its almost on its own line besides being inside the switch controller thing...looks like it does post-increment though instead of update its value instantly
:ugeek:

Deepa

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

Post by Deepa »

According to explaination, i didn't understand why
1)case '0' is not selected when i=0
2)when i = 2, "C" is printed and there is a break LOOP; why didn't it come out of for loop instead why did i get incremented?

Please help me in understanding!

Thanks in advance!
Deepa.

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

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

Post by admin »

That is because when i = 2, there is no break LOOP;. There is only break; This means only the switch is broken not for the loop.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Deepa

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

Post by Deepa »

Thanks for the answer! then why case '0' is not selected when i=0?

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

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

Post by admin »

Because 0 is not same as '0'.
If you like our products and services, please help us by posting your review here.

Sweetpin2
Posts: 27
Joined: Thu Feb 07, 2013 9:46 pm
Contact:

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

Post by Sweetpin2 »

We can try below code & see why case '0' will not work

char ch = '0';
System.out.println((int)ch);

The o/p will be 48.

Now if you put switch(48) and view the o/p, it wll only print A & B as break LOOP; will break the labeled for loop.

The_Nick

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

Post by The_Nick »

Ok I thoroughly understand the result of the snippet.
However I would like to know why an integer stored in a char when printed out without typecasting..
Ex.. char a = 10; System.out.println(a);
It does not work it gives out a symbol rather than the int I expected. However the problem does not exist if I typecast the char in int System.out.println((int)a);
In this terms, why the switch would understand which int is i at the moment representing? Would not it represent the char that would print if printed out?

Thanks in advance.

The_Nick

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

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

Post by admin »

There is no difference between a char and an int (other than their size and signs). Both are integral types. The difference is in the way they are interpreted. An integer, when interpreted as an int, is printed as an integer but the same integer when interpreted as a character is printed as the character symbol that it represents. For example, int 97 is 97 but the character symbol for 97 is a, so if you print 97 as a character, it prints a. For the JVM, however, it is still 97. It is only the print out that is different. So a case statement of 'a' will match 97 and vice versa.
If you like our products and services, please help us by posting your review here.

muttley
Posts: 19
Joined: Thu Feb 28, 2013 9:47 am
Contact:

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

Post by muttley »

I didn't understand why in the first loop the B is not printed:

public class ForSwitch{

public static void main(String ... args){
char i;
LOOP: for (i=0;i<5;i++){ //here i = 0
switch(i++){ //after this line the var i will be 1
case '0': System.out.println("A");
case 1: System.out.println("B"); break LOOP; // Thus the B
//should be printed, why it's not happen?
case 2: System.out.println("C"); break;
case 3: System.out.println("D");
case 4: System.out.println("E");
case 'E': System.out.println("F");
}
}
}

}

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

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

Post by admin »

switch(i++){ //after this line the var i will be 1
yes, i will be 1 but it is using post increment therefore, the value used in the switch will be the old value of i i.e. 0. Had it been ++i, you would have been correct.
-Paul.
If you like our products and services, please help us by posting your review here.

Deleted User 1713

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

Post by Deleted User 1713 »

Note that i is incremented in the switch statement even if there is a break statement, while a break statement in the for loop would prevent the increment.

Code: Select all

public class A {

	public static void main(String[] args) {
		int i = 0;

		switch (i++) {
		default:
			break;
		}

		System.out.println(i);

		for (i = 0; i < 10; i++) {
			break;
		}

		System.out.println(i);
	}
}
This will print 1 0

Denn777
Posts: 4
Joined: Sat Mar 14, 2015 5:19 pm
Contact:

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

Post by Denn777 »

Typo in question. According to question, options CF and EF are also correct!
There is nothing said about the order. Actually this app will produce CEF, so two correct options could be: CE, CF or EF.

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

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

Post by admin »

I do not see CF and EF among the options. Had they been there, they would be correct.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Denn777
Posts: 4
Joined: Sat Mar 14, 2015 5:19 pm
Contact:

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

Post by Denn777 »

Yes, you are right. Sorry!

cvsignin
Posts: 4
Joined: Fri Jun 24, 2016 10:19 am
Contact:

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

Post by cvsignin »

Hi Admin,
I have a small question.In case of no match and no default statement shouldn't a fall through occur in the switch statement. I assume a case when there is no for loop lets say.The fall through follows a top down approach until it finds a break.Can you please help me clarify why this didn't happen in this case too.
Maybe i am missing some topic.

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

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

Post by admin »

Not sure what you mean. Can you show what you mean by putting it in code and mentioning what you found unexpected so that we can help?
Paul.
If you like our products and services, please help us by posting your review here.

cvsignin
Posts: 4
Joined: Fri Jun 24, 2016 10:19 am
Contact:

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

Post by cvsignin »

Hi Paul,
I would like to mention the below example.I know although the code in question returns C E F as output but just for clarification i ask below:

Code: Select all

switch (i++) {
			case '0':
				System.out.println("A");
			case 1:
				System.out.println("B");
				break LOOP;
			case 2:
				System.out.println("C");
				break;
			case 3:
				System.out.println("D");
				break;
			case 4:
				System.out.println("E");
			case 'E':
				System.out.println("F");
			}
In the above case if i pass a value of 6(also as the value in switch statement),then as per the explanations of switch case,none of the cases would match and then a default scenario would be looked upon which in the above case is not present,so a fall through should occur. But as the code has been run this was not the case. Is the compiler behavior different in case of casting input values to a Switch?

For the question,if i=0 in the first loop iteration,then if no match is found,then JVM looks for default or follows top down approach till it finds a break. Please clarify me if i am wrong.

Thanks.
Last edited by admin on Mon Jul 25, 2016 8:09 pm, edited 1 time in total.
Reason: Please put code within [code] [/code] so that it is easier to read

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

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

Post by admin »

The code that you've given will not produce any output if i is 6. I am not sure what you mean by "fall through". If by "fall through", you mean, none of the cases is executed, then that is correct. If there is no default and if the value of the switch variable doesn't match any case, then none of the cases will be executed.


>For the question,if i=0 in the first loop iteration,then if no match is found,then JVM looks for default or follows top down approach till it finds a break.

No, it doesn't look for a break i.e. a break is not required in a case block. If a break is there inside a case block, then the next case block will not be executed.So in the given question, when i is 0, none of the cases match, it doesn't go into any of the case blocks. When i is 2, it goes into case 2. There it prints "C", and since there is a break after that, it exits the switch block.
If there is no break after a case block, then it will execute the code in the next case.

You should go through this tutorial to get the basics of switch/case: https://docs.oracle.com/javase/tutorial ... witch.html

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Deleted User 3513

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

Post by Deleted User 3513 »

Just want to clear this one out. It prints C, E, F in that order. i will become 2 and 4. F gets printed too because there's no break in case 4?

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

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

Post by admin »

When i is 2, it will only print C because there is a break in case 2.
When i is 4, it will print E as well as F because there is no break in case 4, so it will go to case 'E' also.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot], marpiva and 31 guests