Hi everyone

I have an exam coming up soon, and now is the time for review. I have some questions regarding this review, I will only ask the ones I am so unclear about.

How many times will the following do-while loop be executed?
int x = 11;
do
{
x += 20;
} while (x > 100);
a. 0
b. 1
c. 4
d. 5

The answer is 1, b. Am i right in saying, that this loop has to be executed at least once, and when the compiler reads the "while ...." it stops? Therefore it will be executed at least once?

What will be the value of x after the following code is executed?
int x = 10;
while (x < 100)
{
x += 10;
}
a. 90
b. 100
c. 110
d. This is an infinite loop

The answer is 100, which I do not understand why...

What is meant by the term void method?

The scope of a private instance field is
a. the instance methods of the same class
b. inside the class, but not inside any method
c. inside the parentheses of a method header
d. the method in which they are defined

The answer is a, but again, I am not sure why it is so.

When a method's return type is a class, what is actually returned to the calling
program?
a. An object of that class
b. A reference to an object of that class
c. Only the values in the object that the method accessed
d. Nothing, the return type is strictly for documentation in this situation

Last question. B is the answer. Again, need explanation.

Appreciate any help. Thanks in advance

Recommended Answers

All 6 Replies

you fail. May be the correct answers (I've not checked) but if you don't know why you don't know enough to pass the exam (if it's a proper exam) and in fact deserve to fail at it.

Wow, someone is angry.

Relax, I am only asking for help before exam.

Any one out there want to help?

Yes that was a bit too mean.

Okay for the first question, I think the question would be, a.0. As the rule is while x >100. x is not more than 100 so it won't be stored.

The next one x has to be less than 100, which it is, so 10 will be added to x until it reaches 100. Therefore this will only be 10 times (10x10=100) and then it will stop as the rule is: x numst be less than 100.

A void method is when it does not return a value, where q5 is the opposite, the reference of that return value object.

1st Q : The OP was right. a do while is always executed at least once (unless there is an Exception thrown).

2nd Q: x will be assigned values 10, 20, ... 90, 100. When it reaches 100 the condition x<100 is false and the loop exits, leaving x as 100

The last three answers can be found in the Java Language Specification, or numerous other sites via Google.

Oh right, sorry about that. It would only not go through the first time when the while is at the start as with Q2. Right?

Yes, that's right.
While has its condition tested at the start of the loop, so if it's false the loop body is never executed.
do...while has its condition tested at the end of the loop, so even if it's false the body is executed once before the condition is tested.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.