*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question on char array 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 "question on char array" Watch "question on char array" New topic
Author

question on char array

Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
After the declaration:

char[] c = new char[100];

what is the value of c[50]?

a) 50
b) 49
c) '\u0000'
d) '\u0020'
e) " "
f) cannot be determined
g) always null until a value is assigned

I chose the answer as e " ". When I added the code to a program and SOP
C[50] I got spaces as well, but the answer in the answer key(MINDQ mock exam) has been given as c '\u0000'. Does anyone one know how, is it because I am having a different version of JAVA.

Cheers,
JP


Cheers,
Ram
Joseph A Alexander
Greenhorn

Joined: Oct 31, 2003
Posts: 23
" " is a String literal. char cannot hold it. '\u0000' is unicode for space.

You can try this
public class test {
public static void main(String [] args) {
System.out.println("-"+ '\u0000' + "-");
}
}


Regards,<br />Joe, SCEA
Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
If it cannot hold spaces, how is it I am getting spaces when I do a SOP, please execute the below code...and you will see for urself..

class Test1 {
public static void main(String[] args) {
char[] c = new char[100];
System.out.println("-"+ c[50] + "-");
}
}

Am I missing something ..

Cheers,
JP
Usha Hanumolu
Greenhorn

Joined: Sep 16, 2005
Posts: 18
Hi Ravi,

You are getting space because the char array will be initialized with '\u0000'(unicode for space) but not with " ". So when u try to print c[50] it will print space in place of '\u0000'.

Hope you got it..

Regards,
Usha.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You may also want to try this:

System.out.println((int)c[50]);

This will print out the numerical Unicode value of the char.

Layne


Java API Documentation
The Java Tutorial
Usha Hanumolu
Greenhorn

Joined: Sep 16, 2005
Posts: 18
Originally posted by JP Ravi:
If it cannot hold spaces, how is it I am getting spaces when I do a SOP,


char array can hold spaces but not String literals such as " "
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on char array
 
Similar Threads
What is the correct answer?
Character Array
char array
'\u000' != " " true or false
char default value ?