• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a question about arrays

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte [] arry1, array2[];
byte array3 [][];
byte[][] array4;
If each array has been initialized, which statement will cause a compiler error?
A. array2 = array1;
B. array2 = array3;
C. array2 = array4;
D. both A and B
E. both A and C
F. both B and C
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A
 
Simo Hayha
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but the answer is F,can you explain why you select A?
thanks.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is A. Compile it and see.
The variable arry1 is a one-dimensional array. All of the others are two-dimensional arrays.
[ July 02, 2002: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
arry1 is a single array, all other arrays are array of arrays, or two dimensional in this case.
the assignment at A produces a compiler error because they are not compatible. incompatible types!
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which mock exam is this question from?

Could the question be:
If each array has been initialized, which statement will not cause a compiler error?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer should be F <BR>
because both array1 and array2 <BR>
are single dimentional arrays, so cast is ok<BR>
when we try to cast two dimentional arrays
<BR>to single dimentional arrays, like with
<BR> the choice B, and C, the compilation
<BR> error occured.

to verify, check javadoc utility<BR>
A Java Student
Saif Ullah
SCJP2
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Saif Ullah:
The answer should be F because both array1 and array2 are single dimentional arrays, so cast is ok


No, they aren't. array1 is a one dimensional array and array2 is a two dimensinal array. From the JLS, §10.2 Array Variables:


The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both, as in this example:

This declaration is equivalent to:


Corey
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the two two-dimentional arrays do not have equal number of elements in each dimention,how does the assigment happen?
thanks.
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The assignment has nothing to do with the size of the array. you only copying the reference. after the assignment, the two references point to the same array object.
 
andy lau
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic