This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

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[]) {
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.


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

Joined: Apr 17, 2002
Posts: 158
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
Bartender

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".


Rob
SCJP 1.4
 
jQuery in Action, 2nd edition
 
subject: JavaCaps Self Review Question -Section 1 Q6
 
Similar Threads
final modifier
interface and initialization
question on unicode
Doubt on static
javacaps mock 2 Q:8