Result of the Code
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
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.
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
At run time you will get the following exception
ArrayIndexOutOfBoundException
ArrayIndexOutOfBoundException
live
RAMA KRISHNA AALLA
Greenhorn
Posts: 29
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I got 1 2 as output but expected Runtime exception.
The question was from K & B.
Thanks,
Rama Krishna
The question was from K & B.
Thanks,
Rama Krishna
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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??
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, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
RAMA KRISHNA AALLA
Greenhorn
Posts: 29
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
What happens when the below statement executes
array[0]=args; (Command line Arguments : A 1 2 3)
Thanks,
Rama Krishna
array[0]=args; (Command line Arguments : A 1 2 3)
Thanks,
Rama Krishna
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
It gives 1 2 as the O/P
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
posted 19 years ago
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
Posts: 29
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Rehman for your explanation.
Regards,
Rama Krishna
Regards,
Rama Krishna
| You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |










