aspose file tools*
The moose likes Beginning Java and the fly likes simple test questions, need help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple test questions, need help" Watch "simple test questions, need help" New topic
Author

simple test questions, need help

Michael Coogan
Greenhorn

Joined: Jun 30, 2009
Posts: 5
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

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: May 26, 2009
Posts: 61
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

Joined: Apr 06, 2009
Posts: 296
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


Creativity is nothing but Breaking Rules
Michael Coogan
Greenhorn

Joined: Jun 30, 2009
Posts: 5
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

Joined: Oct 13, 2005
Posts: 38412
    
  23
9 correct, 2 wrong.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: Jun 30, 2009
Posts: 5
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

Joined: Jul 15, 2009
Posts: 377

Just for your information this will not compile.


I love this place!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: Oct 13, 2005
Posts: 38412
    
  23
Rob, you have also given a straight answer
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

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

Joined: Oct 13, 2005
Posts: 38412
    
  23
Yes, sorry about that. I have PM-ed Rob about it. Too late to delete the answer now
Michael Coogan
Greenhorn

Joined: Jun 30, 2009
Posts: 5
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

Joined: Oct 27, 2005
Posts: 19672
    
  18

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

Joined: Oct 27, 2005
Posts: 19672
    
  18

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

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: Jun 30, 2009
Posts: 5
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

Joined: Aug 16, 2005
Posts: 14114
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: May 26, 2009
Posts: 61
I never thought you'll give the answer
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: Oct 27, 2005
Posts: 19672
    
  18

Besides, we're bartenders. We can do anything we want
*hides from Ulf, Bear and Jeanne*
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
I have nothing against with what you can do... I didn't mean anything to what I said in my first post...
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

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?


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

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
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

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

Joined: Jul 17, 2009
Posts: 14
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple test questions, need help