• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaCaps Self Review Question -Section 1 Q6

 
Karen Leoh
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there..
I hope someone can explain this to me.
6. What is the output when you compile and run the following code?
public class StatAarray {
static char staticArray[]=new char[5];
public static void main(String args[]) {
System.out.println(staticArray[1]);
}
}
[a] Prints 0 to 4 on the screen
[b] Compile time and run time error
[c] Prints \u0000 on the screen
[d] Compile time error
[e] Prints nothing on the screen
The answer given is E.
Based on my understanding, when an array is constructed, it's elements are automatically initialized to the default values, if there is no explicit initialization.
As such, the intial value for char is '\u0000' ( unicode representation ) So, why isn't the answer C.
I tried compiling and running this code.
My output was :[]. Does it means it prints nothing?
Thanks in advance.
 
La Vish
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I,too, tried this yesterday and found that for char the output would be just a blank but for all other types the output printed would be their default values.
 
Karen Leoh
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vasu,
Thanks a lot. Just wanted to make sure..
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\u0000 is a non-printing character. It's priting it, but there's no character that maps to that value, so the behavoior is to print a square character indicating "no character glyph for this unicode value".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic