About Question enthuware.ocajp.i.v7.2.1388 :
Moderator: admin
-
- Posts: 2
- Joined: Fri Apr 24, 2015 9:04 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1388 :
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.
Obviously that is also not true.. but without a main it also cannot print something.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1388 :
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.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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Thu Feb 25, 2016 9:51 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1388 :
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?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1388 :
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: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?
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Sun Aug 27, 2017 12:35 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1388 :
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
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1388 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1388 :
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?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1388 :
innerCounter is declared outside the while loop but inside the for loop.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1388 :
Ohww! I din look at the braces! Thank you.
Who is online
Users browsing this forum: Bing [Bot] and 42 guests