Page 1 of 1
Re: About Question enthuware.ocajp.i.v7.2.1388 :
Posted: Fri Apr 24, 2015 9:05 am
by lucvandewall
I answered "It fails to compile" as it doesn't has a main in the code.
Obviously that is also not true.. but without a main it also cannot print something.
Re: About Question enthuware.ocajp.i.v7.2.1388 :
Posted: Fri Apr 24, 2015 9:39 am
by admin
lucvandewall wrote:I answered "It fails to compile" as it doesn't has a main in the code.
Obviously that is also not true.. but without a main it also cannot print something.
No, that is ok. This is as per the pattern of questions in the real exam. Many questions do not have complete compilable code. They just just show you the relevant code snippet. You have to assume that the given code is wrapped in an appropriate but irrelevant context of a class and a main method.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1388 :
Posted: Thu Feb 25, 2016 10:40 am
by rch1984@gmail.com
I am struggling to grasp the quessitons when there is a While loop running inside a foreach loop. Could someone point out some ways to maybe write it out or maybe the way you read it?
Re: About Question enthuware.ocajp.i.v7.2.1388 :
Posted: Thu Feb 25, 2016 10:58 am
by admin
rch1984@gmail.com wrote:I am struggling to grasp the quessitons when there is a While loop running inside a foreach loop. Could someone point out some ways to maybe write it out or maybe the way you read it?
The way to handle such type of questions is to use a pen and a paper (you will be provided with these in the exam) and note down the values of each variable at each iteration of outer and inner loop. Like this:
Outer for Iteration 1:
innerCounter=0, dataElement = x
Inner while Iteration 1: 0<3 so execute while body
print dataElement, innerCounter =>x, 0
increment innerCounter to 1
Inner while Iteration 2:1<3 so execute while body
print dataElement, innerCounter =>x, 1
increment innerCounter to 2
Inner while Iteration 3:2<3 so execute while body
print dataElement, innerCounter =>x, 2
increment innerCounter to 3
Inner while Iteration 4: 3 !<3 so stop the while loop
Outer for Iteration 2:
innerCounter=1, dataElement = y
...
so on...
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1388 :
Posted: Thu Sep 14, 2017 1:41 pm
by ramon.carrascom
Why doesn't the second iteration of the for loop raise an exception when it reaches the int innerCount line? AFAIK, int already is declared in the first iteration. Doesn't it try to redeclare i? Does "innerCount" fall out of scope in each iteration? Thanks
Re: About Question enthuware.ocajp.i.v7.2.1388 :
Posted: Thu Sep 14, 2017 10:33 pm
by admin
Yes, the scope of innerCount is only withing an iteration of the for loop. Each iteration effectively creates a new variable innerCount. The one created in the previous iteration is gone with that iteration.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1388 :
Posted: Sun Feb 25, 2018 8:33 am
by Meghana
How come innerCount is incremented to 0, 1 , 2? Shouldn't the scope of innerCount remain within the while loop (as it does for for loop)? So, each time we come out of the while loop, how come innerCount is incremented?
Re: About Question enthuware.ocajp.i.v7.2.1388 :
Posted: Sun Feb 25, 2018 10:09 am
by admin
innerCounter is declared outside the while loop but inside the for loop.
Re: About Question enthuware.ocajp.i.v7.2.1388 :
Posted: Sun Feb 25, 2018 10:21 am
by Meghana
Ohww! I din look at the braces! Thank you.
