Need help reviewing before exam
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;
x += 20;
} while (x > 100);
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?
The answer is 100, which I do not understand why...
What will be the value of x after the following code is executed?
int x = 10;
while (x < 100)
x += 10;
d. This is an infinite loop
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
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
Re: Need help reviewing before exam
(1) OK, but why does the first one stop when it hits "while(x>100)"?
(2) Think about the different values x gets as the second loop executes over and over again. Write them down. What makes this loop stop?
(3) Personally I think the term "void method" means "I'm too lazy to say what I mean, so I just make jargon up as I go along". This is aimed at your teacher not you. There are four things about a method - besides what it actually does! - that are important to the outside world who calls it: (a) the name of the method (otherwise we get the dreaded "cannot find symbol") (b) the number and type of argument it wants (ditto) and (c) the type of thing it returns (otherwise we would not know what to assign the result to) and (d) any exceptions it might throw that we have to catch (like IOException).
Together these 4 things are called the signature of the method.
But what happens if the method doesn't return anything? (eg System.out.println() doesn't return anything)
(4) I'd go for "none of the above" here. And it's really nice when the examiner gets the question wrong (you lose marks for scrawling on the exam paper, but it's nice!) I mean aren't they in scope inside a constructor? Can't they be used - under some circumstances - to initialise other variables: ie not in the scope of any method or constructor?
(5) What do you pass as arguments to a method? Values, which may be a reference to an object.
What is returned from a method? Values, which may be a reference to an object.
What sort of thing can be assigned to a variable? Values, which may be a reference to an object.
Do you get the pattern?
Everything is a value (which is either primitive or a reference to an object). Java is boring. But winding up your teacher isn't! Again, "none of the above". When is a value which is a reference to an object of the class not a reference to any object of that class? When it's null! So the real answer is (b) except when it happens that it's not (b).