Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sarga mockexam

 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
26) A sample question provided by Sun
Which correctly create an array of five empty Strings?
A.String a [] = new String [5];
for (int i = 0; i < 5; a[i++] = "");
B.String a [] = {"", "", "", "", ""};
C.String a [5];
D.String [5] a;
E.String [] a = new String [5];
for (int i = 0; i < 5; a[i++] = null);
According to me the ans is only b but it is given both a and b.
in optA for (int i=0; i<5; a[i++]="") means it is omitting the zeroth element and thus returns only 4 elements?
Can anybody explain???
Thank you.
 
Pat Barrett
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
i++ is in post-fix notation. What this means is that the value, in this case i, will be evaluated in its current condition, and then incremented by 1. Since i is initially 0, the first a[i++] will initialize the 0th element of the array.
Hope this helps,
Pat B.
 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You are right. I totally forgot about post incrementing . Thank you very much for reminding me about That.
Madhuri.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic