• 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

Result of the Code

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At run time you will get the following exception
ArrayIndexOutOfBoundException
 
RAMA KRISHNA AALLA
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got 1 2 as output but expected Runtime exception.

The question was from K & B.

Thanks,

Rama Krishna
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when the below statement executes

array[0]=args; (Command line Arguments : A 1 2 3)


Thanks,

Rama Krishna
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It gives 1 2 as the O/P
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rehman for your explanation.

Regards,

Rama Krishna
 
It's never done THAT before. Explain it to me tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic