| Author |
Result of the Code
|
RAMA KRISHNA AALLA
Greenhorn
Joined: Aug 10, 2005
Posts: 29
|
|
What is result of the below code for command line-invocation java A 1 2 3 public class A { public static void main(String [] args) { String [][] array = new String[2][2]; int x; array[0]=args; x=array.length; for(int y=0;y<x;y++) { System.out.println(" "+array[0][y]); } } } A. 0 0 B. 1 2 C. 0 0 0 D. 1 2 3 E. Compilation fails F. An Exception is thrown at runtime
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Have you compiled and run the code yourself? If so, then what do you not understand? Oh, yes, one other thing: please tell us where you got this question from. Thanks [ November 20, 2006: Message edited by: Barry Gaunt ]
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
raja kanak
Ranch Hand
Joined: Oct 18, 2006
Posts: 135
|
|
At run time you will get the following exception ArrayIndexOutOfBoundException
|
live
|
 |
RAMA KRISHNA AALLA
Greenhorn
Joined: Aug 10, 2005
Posts: 29
|
|
I got 1 2 as output but expected Runtime exception. The question was from K & B. Thanks, Rama Krishna
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi Guys, What does the statement x = array.length return??? It returns 2 and that is why the o/p 1 2. Is it because array is a two dimensional array, array.length returns 2?? Can anyone explain on this??
|
SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
|
Ok, the array.length returns the no of rows. So that is why the O/P 1 2. I got it. It was worth discussing this topic here.
|
 |
RAMA KRISHNA AALLA
Greenhorn
Joined: Aug 10, 2005
Posts: 29
|
|
What happens when the below statement executes array[0]=args; (Command line Arguments : A 1 2 3) Thanks, Rama Krishna
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
|
It gives 1 2 as the O/P
|
 |
Burkhard Hassel
Ranch Hand
Joined: Aug 25, 2006
Posts: 1274
|
|
Hi ranchers, tricky ! I added only one line to the output and invoked class A from another class: The last line (I don't mean the "}" ) prints [[1, 2, 3], [null, null]] Now what's the length of the array? What's the length of array[0]? What's the length of array[1]? Yours, Bu.
|
all events occur in real time
|
 |
Abdul Rehman
Ranch Hand
Joined: Nov 07, 2006
Posts: 65
|
|
Originally posted by RAMA KRISHNA CHOWDARY: I got 1 2 as output but expected Runtime exception.
That's a common misconception in the mind of those who have worked in C++ or similar languages. In Java, there is no such thing as a two dimensional array; rather, nested arrays are arrays of arrays. In your example, array contains two elements. These elements are actually references to arrays of String i.e. String[]. We can modify the reference contained in the first element of array, to point to any array of String, no matter what its size is. Hope this will help you understand the output. Thanks, Abdul Rehman.
|
 |
RAMA KRISHNA AALLA
Greenhorn
Joined: Aug 10, 2005
Posts: 29
|
|
Thanks Rehman for your explanation. Regards, Rama Krishna
|
 |
 |
|
|
subject: Result of the Code
|
|
|