jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Language Fundamentals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Language Fundamentals" Watch "Language Fundamentals" New topic

Language Fundamentals

Sri Sridhar

Joined: Oct 26, 2004
Posts: 2

how many lines of output will be produced?
Answer is 11

Can anybody explain the flow in detail...

Thanks in advance

( tags added and formatted)
[ October 26, 2004: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Yes 11 lines is correct. 11 = 4 + 2 + 5, does that give you a clue?

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
rengarajan vaikuntam
Ranch Hand

Joined: Oct 04, 2004
Posts: 37
Its like this
Look at the condition of the first for loop
x.length is the length of the x array which is clearly declared in the statement
int [][][] x=new int[3][][];
so x.length is 3, ok, so the first for loop will run for 3 times for values of i=0, i=1, i=2 ok.
Now the next for loop
here the condition is j shoulbe lessthan x[i].length,so
when i is zero, the condition is j<x[0].length,ok
Look at the line 5 from ur code it says
x[0]=new int [4][];
so x[0].length is 4, so the condition is j<4;
so the inner for loop will run for 4 times for j values of j=0, j=1, j=2 & j=3.now the value of i is zero.Now the sout will print 4 times ok.
Now when i=1,
the condition become j<x[1].length ie j<2 now the inner loop will run for 2 times for j values of j=0 and j=1.Now the sout will print 2 times.
Now i=2,
condition j<x[2].length ie j<5 so now the inner j loop will run for 5 times.now the sout will print 5 times.
So totally
when i=0; it prints 4 times
when i=1; it prints 2 times
when i=2; it prints 5 times
So the answer is (5+2+4) 11 times.
Hope this helps you.
Sri Sridhar

Joined: Oct 26, 2004
Posts: 2
Thanks Rangarajan that was a good explanation i got cleared now. Ofcourse i could find the clue earlier but this detailed flow cleared my doubts..thanks barry.
I agree. Here's the link: http://aspose.com/file-tools
subject: Language Fundamentals
It's not a secret anymore!