File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JavaCaps Self Review Question -Section 1 Q6 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JavaCaps Self Review Question -Section 1 Q6" Watch "JavaCaps Self Review Question -Section 1 Q6" New topic

JavaCaps Self Review Question -Section 1 Q6

Karen Leoh
Ranch Hand

Joined: Dec 03, 2001
Posts: 40
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[]) {
[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.

--------------------<p>Karen Leoh<br />Sun Certified Programmer for Java™ 2 Platform
La Vish
Ranch Hand

Joined: Apr 17, 2002
Posts: 161
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.

La Vish
SCJP 1.4, President 60s Club
Karen Leoh
Ranch Hand

Joined: Dec 03, 2001
Posts: 40
Hi Vasu,
Thanks a lot. Just wanted to make sure..
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
\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".

SCJP 1.4
I agree. Here's the link:
subject: JavaCaps Self Review Question -Section 1 Q6
It's not a secret anymore!