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

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

Moderator: admin

Post Reply
emailgutta
Posts: 1
Joined: Wed Oct 22, 2014 12:24 pm
Contact:

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

Post by emailgutta »

For this question, in the Explanation section it says as below.what does it mean when it says ''A char can be used as an unsigned integer ''

There no unsigned keyword in java! A char can be used as an unsigned integer

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

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

Post by admin »

It means if you want to use an unsigned integer, you can use a char if your number lies between 0 and the max value of a char i.e. 2^16 - 1 because size of char is 2 bytes. A char cannot store negative numbers.

ghostGuiggs
Posts: 1
Joined: Mon Dec 07, 2015 4:51 pm
Contact:

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

Post by ghostGuiggs »

Hello

In my test this question is :

What will the following program print?
public class TestClass{
public static void main(String[] args){
unsigned byte b = 0;
b--;
System.out.println(b);
}
}

I do not agree with the answer given because in my opinion, the right answer should be "It will not compile" because "There are NO unsigned keyword in java". when i saw the answer given as "0" i was so surprised that i checked the code in my NetBeans with JDK 7.
The code indeed did not compile ! when i remove the word "unsigned", it compiles fine and prints "-1" as a result which is normal since byte values are between -128 and 127.

can you explain why the answer in the test prints "0" please.

Thank you in advance.

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

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

Post by admin »

The correct answer is indeed it will not compile.
There is no unsigned keyword in java. If you want to store unsigned integers, you can use a char instead.

HTH,
Paul.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 9 guests