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

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

Moderator: admin

Post Reply
kashyapa
Posts: 23
Joined: Thu May 08, 2014 5:27 am
Contact:

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

Post by kashyapa »

If we change the switch expression to char and the case labels in to int, it will not compile because of every int values cannot be assignable to char.
Last edited by kashyapa on Wed Jul 16, 2014 7:55 am, edited 1 time in total.

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

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

Post by admin »

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

kashyapa
Posts: 23
Joined: Thu May 08, 2014 5:27 am
Contact:

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

Post by kashyapa »

try it you self ;)

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

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

Post by admin »

We try to help as much as we can but if we start spoon feeding, we won't be able to help anyone. So please help us help you and post the details of your efforts.
If you like our products and services, please help us by posting your review here.

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

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

Post by admin »

I see that you've updated your post and removed the question.
If you like our products and services, please help us by posting your review here.

kashyapa
Posts: 23
Joined: Thu May 08, 2014 5:27 am
Contact:

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

Post by kashyapa »

Yape… actually what you do in here is precious, we should appreciate that. I got it and apologize. :| I have already done the OCPJP and I think that’s why I got lazy like this.

Tony.Singarayar
Posts: 3
Joined: Sat Oct 25, 2014 2:17 pm
Contact:

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

Post by Tony.Singarayar »

Hi,
I still dont understand how "switch expression of type int and case label value of type char" is correct.
What if we pass the vale of int to be -655454. Will there be any char value that can take it...

Please help.
Thanks,
Tony Singarayar

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

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

Post by admin »

Yes, you can pass any value. If it doesn't match any label, the control will go to default.
If you like our products and services, please help us by posting your review here.

Tony.Singarayar
Posts: 3
Joined: Sat Oct 25, 2014 2:17 pm
Contact:

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

Post by Tony.Singarayar »

Hi,
How can the char value be -655454 when char is unsigned?

Can you please explain.

THanks,
Tony Singarayar

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

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

Post by admin »

You asked what if you pass an int, not char. Int can be compared to char labels.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon »

Hi,

I got this question right, still have couple of doubt regarding this question, Please correct me if I am wrong, I just little help that, how to face such questions in the real exam, I know that case value must be "Compile time constant", hence following program compiles fine,

Code: Select all

public static void main(String args[])
	{
		final byte b=10;
		char ch='c';
		byte cb='b';
		
		switch(ch)
		{
		case'a':System.out.println("a");break;
		case b:System.out.println("b");break;
		}
	}
Then, how can I prove option 4 is wrong?

Secondly, in your explanation for option 4 you have given case value as -1, which is integer compile time constant isn't it? If yes, then it is nothing to do with byte. I do know that in the above program cb=ch or ch=cb is not possible, as per your explanation. Just wanted to know how should we answer such questions in the exam. One last thing, if the option include
"switch expression of type char and case label value of type int".
will be true or false? as,

Code: Select all

switch(ch)
{
case 10:break;//fine
case -20://compilation  fails
}
Regards,
GPAR

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

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

Post by admin »

In your first code, case b:System.out.println("b");break; should not compile.

Any negative number less than 127 and greater than -128 can be considered a byte if it is a compile time constant. It can also be an int. But that doesn't make the example for option 4 invalid. The fact is byte can take negative values but a char cannot. So, if your switch variable is of type char, your case labels cannot be those bytes/ints that fall outside the range of a char.

The basic idea is that case labels should be assignable to the switch variable. That's all. So if an option makes a general statement that a switch variable of type char and case labels of type ints is good, then it is not valid because as you can see, it will not work for all ints.

Having said that, I would say you need not worry too much about this. If you understand the concept, you will be fine.

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

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon »

Sorry, for raising the doubts again, I respect your software, also have great respect towards you as well, now this code of mine, does compile. I have no idea how to prove it. Secondly there are compile time constant as bytes? I dint know this. can you please provide me the link where, this is mentioned?
Any negative number less than 127 and greater than -128 can be considered a byte if it is a compile time constant. It can also be an int.
As these are the questions which I often get wrong, a easy ones for some one, is tough ones for me. BTW thanks, I got the answer for my last query(i.e switch(char){case:int}).

Thanks for your kind help,
GPAR

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

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

Post by admin »

A raw number doesn't have a type. A variable has a type. So if you just write some number, it could very well be a byte, integer, char, or short, etc. assuming that it is within the range of that type.
For example, byte b = 120; will compile and so will int b = 120; but byte b = 129; will not. So what is 120? is it a byte or is it an int?

case labels are like that. They are just values. Their validity depends on what you are assigning them to i.e. the switch variable.

I am sorry, I don't have any link for this. You may need to google.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon »

Hi,

Thanks for your detailed explanation, after your last post I did google, but could not find anything, but this reply of yours is sufficient to understand the concept.

With respect,
GPAR

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon »

Hi Paul,

According to your explanation on compile time constants in your last post, I wrote this program,

Code: Select all

public class OverLoadingSample {
	
	public static void main(String args[])
	{
		call(10);
	}
	/*static void call(int i)
	{
		System.out.println("Int primitive");
	}*/
	static void call(short s)
	{
		System.out.println("short primitive");
	}
	static void call(byte b)
	{
		System.out.println("byte primitive");
	}
	
}
If the method call(int) is uncommented then, that method is called else compile time error, if 10 which can fit into short/byte, and can be a compile time constant of type short/byte as well as int, then why is this program behaves like this? why cant it call method call(byte), which is more specific!

With respect,
GPAR

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

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

Post by admin »

Because as per Section 5.3 of JLS 7, narrowing conversions are not allowed in method calls.
If you like our products and services, please help us by posting your review here.

levijatanus
Posts: 3
Joined: Sat Apr 16, 2016 7:53 am
Contact:

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

Post by levijatanus »

Can you explain how come this is ok and prints

One
One

when run

Code: Select all

		final int b1 = 1; 
		final byte i1 = 1;
		switch(i1){
		case 0: System.out.println("Zero");
		case b1: System.out.println("One");
		}
		switch(b1){
		case 0: System.out.println("Zero");
		case i1: System.out.println("One");
		}
Last edited by levijatanus on Wed Apr 20, 2016 1:52 am, edited 1 time in total.

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

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

Post by admin »

Why do you think it should not be ok?
Paul.
If you like our products and services, please help us by posting your review here.

levijatanus
Posts: 3
Joined: Sat Apr 16, 2016 7:53 am
Contact:

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

Post by levijatanus »

I have thought that before case and switch are evaluated some kind of conversion is applied – that variable in case would be casted to type of variable in switch ?
Correlation would be that you can assign byte to int but not vice versa.
In explanation stands:

Code: Select all

This will not work in all cases because a byte may have negative values which cannot be assigned to a char.
So if assigning is happening how come cast is not needed?

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

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

Post by admin »

Cast is not needed here because the variables are being declared as final. When a variable is declared final the compiler knows that it is never going to change (it is a constant, actually). It looks at the actual value being assigned and determines whether the value is within the limit of the variable or not. If it is, then it accepts the assignment.

Try changing the value being assigned to b1 to (for example) 255 and see what happens.
-Paul.
If you like our products and services, please help us by posting your review here.

zoharch
Posts: 4
Joined: Fri Sep 29, 2017 3:34 pm
Contact:

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

Post by zoharch »

As I understand, switch expression and case labels can be compiled if:
1.Expression is byte, short,char,int or String.
2. You can convert the syntax to assignment to see if it compiled.
For example:
Switch(ing variable), case with char variable or value like 'c' compiles if also
Int variable = 'c' compiles. And it compiles.
But char k = 100000 will not compile so the following switch will not compile too :
char k;
switch(k) {
case 100000: DOSOMETHING;
}

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

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

Post by Meghana »

With respect to option 4: "switch expression of type char and case label value of type byte", is this only in a switch case or can we not even cast a char to int? I tried to google about it. But I couldn't really understand the concept of "special conversion".

Thank you.

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

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

Post by admin »

What happened when you tried it out...assigning a char value to an int? This is actually a very basic thing and you should've been able to answer it right away without writing code to test it. If you are not, you should go through a good book before attempting mock exams.
Hint - char data type (16 bits) is smaller than int (32 bits).
If you like our products and services, please help us by posting your review here.

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

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

Post by Meghana »

I got it. Thank you. :) Will run and check.
It was basic! just got carried away by the "negative sign"

Post Reply

Who is online

Users browsing this forum: dvc1190 and 2 guests