• 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

Can you tell me how to answer such question?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
When I do the self test,I find I'm not good at doing the questions below:


Give the following,
public class Test{
public static void main(Sting [] args){
byte [][] big= new byte[7][7];
byte [][] b = new byte [2][1];
byte b3 = 5;
byte b2=[][][]= new byte[2][3][1][2];
}
}
Which of the following lines of code could be inserted at line 7,and still allow the code to compile?(Choose four that would work.)
A.b2[0][1]=b;
B.b[0][0]=b3;
C.b2[1][1][0]=b[0]p0];
D.b2[1][2][0]=b;
E.b2[0][1][0][0]=b[0][0];
F.b2[0][1]=big;
The answer is A,B,E and F
Can you tell me how to answer such question? Thank you very much.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Hi y xl,

your declaration of array b2 is wrong, it needs to be corrected.
If you compile this code then it will not compiled.

I think the declaration of b2 should be like this


Choices are :-

A.b2[0][1]=b;
B.b[0][0]=b3;
C.b2[1][1][0]=b[0][0];
D.b2[1][2][0]=b;
E.b2[0][1][0][0]=b[0][0];
F.b2[0][1]=big;


The answer is A,B,E and F

As far as my understanding with Arrays says that B & E are right.

Well I have no idea how A and F are also the answers.

Please someone put light on this question.

Thanks
Kaps
[ July 17, 2004: Message edited by: kapil munjal ]
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off: The main() parameter is spelled wrong: String not Sting (I like Police too ;-)

Find how many brackets the left value removes from the original declaration. And find out if the value assigned has the same number of brackets.

A. b2[0][1] = b. You are assigning an array of array of bytes (2 brackets) to an array of array of bytes (removed 2 brackets on the left) GOOD
B b[0][0]=b3. You are assigning a byte to a byte (removed 2 brackets). GOOD
C. b2[1][1][0]=b[0][0]. You are assigning a byte (removed 2 brackets) to an array of bytes (removed one.) // WRONG
D. b2[1][2][0]=b. You are assigning an array of array of bytes to an array of bytes (removed one.) // WRONG
E. b2[0][1][0][0]=b[0][0] You are assigning a byte to a byte. // GOOD
F. b2[0][1]=big You are assigning an array of array of bytes (2 brackets) to an array of array of bytes (removed 2 brackets on the left) // GOOD same as A

The most time consuming part is making sure that the bounds of the arrays on both sides are respected.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Next Stepguy and y xl,

Welcome to Javaranch

We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic