Chapter 4
Q1) What is wrong with this method?
void doSomething(){ return 4; }
A) It returns a value but has a void
return type.
Q2) What will x
be equal to at the end of this code snippet?
int x=19; do{ x=11; x++; }while(x<20)
A) Okay, this was a slightly tricky question. Regardless of the value of x
, the do
block always executes at least once. Then x
is set to 11
, and after that, it is incremented to 12
. So when the while
expression is evaluated, it is true and the do
block executes again. Once more, x
is set to 11
and then incremented to 12
. The program is stuck in a never-ending (infinite) loop. This code is most likely a bug.
Get Learning Java by Building Android Games now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.