This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple test questions, need help

 
Michael Coogan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following statements are correct in java?

int Size;
Size = 12.22;
Size = 12;
char startWith = ‘S’;
char initial=”j”;
float num;
num=16.0;
String name = “Jane”;
Boolean isHot;
isHot = ‘true’;
isHot = false;
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

We don't give out answers to that sort of question. And we require (for copyright reasons) that all such questions have a source quoted.

Please tell us what you think the answers are, and we shall see whether we agree with you .
 
Kalabaw moo
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are lots of tutorials on the net regarding rules on declaring variables and lots about data types. The question that was provided to you is very very very easy.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subject: simple test questions, need help


Dear Michael Coogan
As you mentioned in subject line its simple then why you are asking ....? Before asking question please go through the link
Link provided
 
Michael Coogan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks guys

here are my anwsers, just wanted some confirmation

int Size; correct
Size = 12.22; wrong - dont think you can use decimals
Size = 12; correct
char startWith = ‘S’; correct
char initial=”j”; correct
float num; correct
num=16.0; correct
String name = “Jane”; correct
Boolean isHot; correct
isHot = ‘true’; wrong - dont think you can use the quotes
isHot = false; correct
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
9 correct, 2 wrong.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got 9; I thought at first 10 but that was mistaken.
I presume you have been told about the difference between boolean and Boolean, and that Size is regarded as incorrect style.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code appears to have been edited on a word processor because you have smart quotes ‘’ and “”. Don't use a word processor; use a text editor like jEdit, Notepad++, Notepad2, kate (Linux/Unix only) or gedit (Linux/Unix only). You find word processors do things like insert control characters or smart quotes and the compiler can't cope with them.
 
Michael Coogan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks campbell,

int Size; correct
Size = 12.22; wrong - dont think you can use decimals
Size = 12; correct - i take it Size shoudl be in lower case also
char startWith = ‘S’; correct
char initial=”j”; correct
float num; correct - i dont like the look of this statement
num=16.0; correct
String name = “Jane”; correct
Boolean isHot; wrong - boolean should not have a capital
isHot = ‘true’; wrong - dont think you can use the quotes
isHot = false; correct
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for your information this will not compile.
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Coogan wrote:char initial=”j”; correct

Wrong; but can you tell us why?

float num; correct - i dont like the look of this statement

That's because it's not a statement but a declaration. It is quite common to declare variables and later assign them.

num=16.0; correct

Wrong; removed reason
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Clark, please don't give out that sort of answer. It doesn't do anybody any good to be told answers like that. Look at this FAQ. Let him work it out for himself. I have pulled rank and deleted your answer. Please don't be annoyed with me.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, you have also given a straight answer
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Sean Clark, please don't give out that sort of answer. It doesn't do anybody any good to be told answers like that. Look at this FAQ. Let him work it out for himself. I have pulled rank and deleted your answer. Please don't be annoyed with me.


haha, ok fair enough, but I think the other guy above gave more info than I did, there is a difference between giving them the answer and pointing them in the right direction.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, sorry about that. I have PM-ed Rob about it. Too late to delete the answer now
 
Michael Coogan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks fellas, i am a greenhorn and will learn my lesson for future posts......

as this is incorrect

char initial=”j”;

should it be in single quotes when assigning a value for the primitive type char?

 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not too late at all. Still, I feel right in telling Michael which answers he has wrong. If nobody did he could be searching for the wrong answers until pigs fly.

The why's he has to figure out for himself.
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Coogan wrote:char initial=”j”;

should it be in single quotes when assigning a value for the primitive type char?

Yes.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, we got there in the end. Sorry for the confusion. Thank you for removing the explanations, Rob.

You can have Boolean and boolean. They are both permissible. But they mean something different. With a small b it is a keyword, meaning a primitive with two possible values, false and true. You were right first time about quotes around true, Boolean with a capital B is a class and isHot refers to an object of the Boolean class. These class objects come in two kinds, corresponding to the boolean (small b) values true and false, and (capital B) provide additional functionality over and above the boolean (small b) values.

There you go, I have added even more confusion You can read more about Boolean (capital B) here. It's not that interesting.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you see Rob's explanation about 16.0? If not, have you worked out why the line with 16.0 causes problems?
 
Michael Coogan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, so Size = 12.22; is incorrect & num=16.0; is also incorrect.

i think it has something to do with the size of bytes availablel on a float and a double. can a float only have one place after the decimal point i.e 16.0 and a double can have multiple places, 12.22? also what is a floting point? does a float have to have an f after the value, i.e float f = 56.3?

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15288
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael, welcome to JavaRanch.

When you copy a question from a book, website or other source, please quote your sources - please tell us where you copied this from.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said earlier you got 9 answers in your first posting. The only two you didn't get were "j" and 16.0. Let's go back to your earlier list, and try not to cause more confusion than I have already I'll use for when you got it right and for the two you got wrong.
Michael Coogan wrote:ok thanks guys

here are my anwsers, just wanted some confirmation

int Size; correct Although technically not a statement, this is legal Java code. "size" is better style than "Size"
Size = 12.22; wrong - dont think you can use decimals You cannot put decimals into an "int"
Size = 12; correct
char startWith = ‘S’; correct
char initial=”j”; correct As you said earlier it should be single quotes
float num; correct
num=16.0; correct See below
String name = “Jane”; correct
Boolean isHot; correct There are Booleans and booleans with and without capital B but both would be permissible Java code.
isHot = ‘true’; wrong - dont think you can use the quotes The only place you can use quotes around several characters is inside a String.
isHot = false; correct
There are three well-known kinds of floating point number in Java: BigDecimal, which we aren't using here, double and float. Your variable is declared as a float, so it can accept a floating-point number in 32 bits. That can have any number of decimal places, but is only precise up to about 7 sig fig (in decimal). If you write a float literal, you need to have a letter f on it. You can write any number of places after the decimal point, but you won't get more than about 7 sig fig out of it. You can legally write 0.123456789101112131415161718192021 and put it into a float with an f, but the best you will get is 0.12345679.
If you write a decimal number without an f then the compiler assumes it is a double which takes up 64 bits in memory and gives about 15 or 16 sig fig precision. If you try the same number it prints out as 0.12345678910111213 as a double.
Now you can't fit 64 bits into a 32 bit space in memory. So when you wrote 16.0 the compiler would assume you meant 64 bits and when you wrote float num that only allocated 32 bits, so the two won't work together. Compiler error. The cast (float) tells the compiler to change the double to the nearest available float. That will work. The f tells the compiler the number was a float all along. That will work. Probably better solution than the cast. You can use f or F. One of the few places in Java where capitals and small letters are equivalent.
 
Kalabaw moo
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never thought you'll give the answer
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kalabaw moo wrote:I never thought you'll give the answer
He got the answer about number of bytes available. I simply explained his answer for him.
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides, we're bartenders. We can do anything we want
*hides from Ulf, Bear and Jeanne*
 
Kalabaw moo
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have nothing against with what you can do... I didn't mean anything to what I said in my first post...
 
salvin francis
Bartender
Posts: 1274
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is valid only in Java 6 right :

Boolean myVariable = true;
Integer myInt=10;

I think its some technology called Tiger (correct me if wrong)
does it compile for Java v1.4 and v1.5 too?
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:This is valid only in Java 6 right :

Boolean myVariable = true;
Integer myInt=10;

I think its some technology called Tiger (correct me if wrong)
does it compile for Java v1.4 and v1.5 too?

It works since Java 5.0 (1.5). In Java 1.4 and before, you would need to convert the primitive manually:
 
salvin francis
Bartender
Posts: 1274
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Tiger, J2SE 5.0
Java 2 Platform Standard Edition 5.0, was launched by Sun on September 30th 2004. J2SE 5.0 (known internally as J2SE 1.5.0 or informally as 'Tiger') has a number of key developments and new features, among them some changes to the fundamentals of the Java language.


Quoted from site: http://www.javatiger.com/
 
Nishant rty
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:As I said earlier you got 9 answers in your first posting. The only two you didn't get were "j" and 16.0. Let's go back to your earlier list, and try not to cause more confusion than I have already I'll use for when you got it right and for the two you got wrong.
Michael Coogan wrote:ok thanks guys

here are my anwsers, just wanted some confirmation

int Size; correct Although technically not a statement, this is legal Java code. "size" is better style than "Size"
Size = 12.22; wrong - dont think you can use decimals You cannot put decimals into an "int"
Size = 12; correct
char startWith = ‘S’; correct
char initial=”j”; correct As you said earlier it should be single quotes
float num; correct
num=16.0; correct See below
String name = “Jane”; correct
Boolean isHot; correct There are Booleans and booleans with and without capital B but both would be permissible Java code.
isHot = ‘true’; wrong - dont think you can use the quotes The only place you can use quotes around several characters is inside a String.
isHot = false; correct
There are three well-known kinds of floating point number in Java: BigDecimal, which we aren't using here, double and float. Your variable is declared as a float, so it can accept a floating-point number in 32 bits. That can have any number of decimal places, but is only precise up to about 7 sig fig (in decimal). If you write a float literal, you need to have a letter f on it. You can write any number of places after the decimal point, but you won't get more than about 7 sig fig out of it. You can legally write 0.123456789101112131415161718192021 and put it into a float with an f, but the best you will get is 0.12345679.
If you write a decimal number without an f then the compiler assumes it is a double which takes up 64 bits in memory and gives about 15 or 16 sig fig precision. If you try the same number it prints out as 0.12345678910111213 as a double.
Now you can't fit 64 bits into a 32 bit space in memory. So when you wrote 16.0 the compiler would assume you meant 64 bits and when you wrote float num that only allocated 32 bits, so the two won't work together. Compiler error. The cast (float) tells the compiler to change the double to the nearest available float. That will work. The f tells the compiler the number was a float all along. That will work. Probably better solution than the cast. You can use f or F. One of the few places in Java where capitals and small letters are equivalent.



The last option is the correct one in all cases. I couldn't agree more.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic