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

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.

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

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

Post by AndaRO »

I don't understand why case 1 : is not executed.

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 »

In the first iteration of the for loop, when i is 0, the switch increments i to 1. When the switch ends, the increment section of the for loop executes and increments i, which makes it 2. So the switch is never executed with a value of 1.
If you like our products and services, please help us by posting your review here.

cccdan
Posts: 1
Joined: Mon Dec 04, 2017 5:09 pm
Contact:

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

Post by cccdan »

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

switch(i++){ //after this line the var i will be 1
Not really. switch doesn't behave in the standard way, as it happens with, say, if (i++)

The thing many people don't understand is that the "case" part from within the switch block is treated as if it's part of the expression inside the switch parantheses. So i will still be zero(or whatever value it has) when it comes to case 0: case 1: and so on... the post increment only gets executed after execution gets past the semicolon of case (if there is a match) or when the method is exited.

it's something like this:

Code: Select all

int i = 0;

switch (i++) { // i is 0
case 0 //i still 0: //now i is 1
}

bomicbon
Posts: 5
Joined: Sun Jan 21, 2018 7:33 pm
Contact:

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

Post by bomicbon »

OK this is why it's not B and then BREAK LOOP:
i = 0;
switch(i++); // 0 is accepted by the switch statement, then incremented immediately afterwards to i = 1
// so none of the cases are accepted in first loop
// proceeds immediately to i=2
// the rest is explained pretty well by enthuware

Post Reply

Who is online

Users browsing this forum: No registered users and 48 guests