About Question enthuware.ocajp.i.v7.2.1016 :
Moderator: admin
-
- Posts: 14
- Joined: Sat Mar 01, 2014 9:12 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1016 :
I just want to know if the actual exam asks this kind of example? Because i think it may be time consuming to plot all values for count and sum (although they are easy)
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1016 :
Yes, you will see some questions like this where you have to work out the values.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Mon Jan 04, 2016 12:28 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1016 :
This is labeled "easy"?? An easier one like this is labeled "tough".
-
- Posts: 7
- Joined: Tue Oct 04, 2016 6:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1016 :
Any time saving tips on how to approach these questions that require a lot of loop iterations?
Unfortunately i am not good at seeing the pattern, so i am stuck with writing down each iteration.
Unfortunately i am not good at seeing the pattern, so i am stuck with writing down each iteration.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1016 :
You will need to work out the value of each variable for each iteration on paper. For beginners, this is the only way. Most people are able to run the iteration in their heads after a bit of experience.
But you should not worry. The questions in the exam do not have a lot of iterations. They are reasonable.
HTH,
Paul.
But you should not worry. The questions in the exam do not have a lot of iterations. They are reasonable.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1016 :
I would like to know why the program continued to execute after the iteration count=10 and sum=37? After this loop iteration, count++ < 11 is the same as 11 < 11 which returns false and would exit the program because 11 is not strictly less than 11. Please help me understand. Thanks in advance!
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1016 :
Observe that the while condition uses the post increment operator on count. You should read the count++<11 condition like this:
compare current value of count with 11, remember the result, increment count, and based on the result of the comparison proceed with the next iteration or exit the loop.
Therefore, when count is 10, the condition will return true, count will be incremented to 11 and then since the comparison is true, the loop will be continue once more. Now, count is 11, the condition will return false, count will be incremented to 12, and since the condition is false, the loop will terminate.
HTH,
Paul.
compare current value of count with 11, remember the result, increment count, and based on the result of the comparison proceed with the next iteration or exit the loop.
Therefore, when count is 10, the condition will return true, count will be incremented to 11 and then since the comparison is true, the loop will be continue once more. Now, count is 11, the condition will return false, count will be incremented to 12, and since the condition is false, the loop will terminate.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Thu Mar 30, 2017 7:15 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1016 :
What will the following code snippet print?
//Please can u post iteration wise results in detail? m not able to analyse how to control flows properly.
Thanks
Code: Select all
int count = 0, sum = 0;
do{
if(count % 3 == 0) continue;
sum+=count;
}
while(count++ < 11);
System.out.println(sum);
Thanks
Last edited by admin on Sun Apr 23, 2017 4:56 am, edited 1 time in total.
Reason: Please enter code inside [code] [/code] tags
Reason: Please enter code inside [code] [/code] tags
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1016 :
Well, you should add some printlns to print the value of variables in each iteration like this and see what it prints.
Code: Select all
int count = 0, sum = 0;
do{
System.out.println("Outer iteration, count = "+count+" sum = "+sum);
if(count % 3 == 0) continue;
sum+=count;
}
while(count++ < 11);
System.out.println(sum);
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 115 guests