• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sun's Sample Q&A. Java 1.1 Question 5.

 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the Sun certification site (see link in mock exam list) there are two sets of sample questions. The first set of 7 questions is for Java 1.2. The second set of six questions is for Java 1.1.
The answer to the following question is given as B.
5. Which correctly creates an array of five empty Strings (select all that apply)?
A. String a [ ] = new String [5]; for (int i = 0; i < 5; a[i++] = "");
B. String a [ ] = {"", "", "", "", ""};
C. String a [5];
D. String [ ] a = new String [5]; for (int i = 0; i < 5; a[i++] = null);
E. String [5] a;
However, I believe A would work as well.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I didn't check the answer in Sun's site. It takes a lot of time to get to sun's site. Since you had checked already, I am moving this thread to 'Mock Exam Errata' forum. See whose qstn is coming to out Errata Forum.
regds
maha anna
[This message has been edited by maha anna (edited April 10, 2000).]
 
Don Zur
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer A does not work. I replaced "" with "a" so the results would be easier to see. To me, this seems like a pretty rotten question.
public class Test{
public static void main (String args[]){
String a [ ] = new String [5];
for (int i = 0; i < 5; a[i++] = "a")
{
System.out.println(a[i]);
}
String b[] = {"a","a","a","a","a"};
for (int i =0; i < 5; i++)
{
System.out.println(b[i]);
}
}
}
Result
C:\WINDOWS\Desktop>java Test
null
null
null
null
null
a
a
a
a
a
 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don,
I don't see your point. The results that I get show that A is no different from B. Run this code:

The result is:

(The editor for this post appears to be removing the 5 blank lines after each "Empty Array X" line above, but the code actually does produce 5 blanks lines after each.)

[This message has been edited by Betty Reynolds (edited April 16, 2000).]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don- the problem is this loop:
<code><pre>for (int i = 0; i < 5; a[i++] = "a")
{
System.out.println(a);
}</pre></code>
The statement <code>a[i++] = "a"</code> is executed [I]after
the print statement in each case, at the end of each loop iteration. Try this instead:
<code><pre>for (int i = 0; i < 5; i++)
{
a[i] = "a";
System.out.println(a[i]);
}</pre></code>
 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4/21/2000
I think I should point out that there were at least two sites that had exam objectives and only one of them had the error.
This one, which has the I/O objectives, was the one in error.
This was probably always correct.
The good news is that Sun appears to have corrected the problem. Hopefully, the mock exam authors will do the same.

[This message has been edited by Betty Reynolds (edited April 21, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic