Hey everyone. This may seem a bit nutty but I have some questions off of my practice test (29 to be exact) that I want to post here (answers included) in hopes that someone can kind of explain some of the question to me and why the each answer is correct. I"m not asking you to help with everyone. Just pick and choose if you wish and of course, feel free to skip this all together.
*
OK Here goes: 1) What is i after the following for loop?int y = 0;for (int i = 0; iy += i;}A) 10 9 C) 11 D) undefinedANSWER IS D___________________________________________________________2) What is the number of iterations in the following loop:for (int i = 1; i // iteration}A) n n + 1 C) n - 1 D) 2*nANSWER IS A________________________________________________________3) After the continue outer statement is executed in the following loop, which statement is executed?outer:for (int i = 1; i inner:for (int j = 1; j if (i * j > 50)continue outer;System.out.println(i * j);}}next:A) The statement labeled next. The control is in the inner loop, and the next iteration of the inner loop is executed.C) The control is in the outer loop, and the next iteration of the outer loop is executed.D) The program terminates.ANSWER IS C_______________________________________________________________4) How many times will the following code print "Welcome to Java"?int count = 0;while (count System.out.println("Welcome to Java");count++;}A) 0 9 C) 10 D) 11 E) 8ANSWER IS D5) What is the output for y?int y = 0;for (int i = 0; iy += i;}System.out.println(y);A) 11 10 C) 12 D) 45 E) 13ANSWER IS D___________________________________________________________6) You can always convert a for loop to a while loop. A) true falseANSWER IS A________________________________________________________7) What is 1.0 + 1.0 + 1.0 == 3.0? A) true falseC) There is no guarantee that 1.0 + 1.0 + 1.0 == 3.0 is true.ANSWER IS C__________________________________________________________8) Analyze the following code.int x = 1;while (0 System.out.println(x++);A) The loop runs for ever. The code does not compile because (0 parentheses.C) The numbers 2 to 100 are displayed.D) The code does not compile because the loop body is not in the braces.E) The numbers 1 to 99 are displayed.ANSWER IS B______________________________________________________________9) You can always convert a while loop to a for loop. A) true false29) The while loop and the do loop are equivalent in their expressive power; in other words, you canrewrite a while loop using a do loop, and vice versa.ANSWER IS A________________________________________________________________10)A) true falseANSWER IS A___________________________________________11) What balance after the following code is executed?int balance = 10;while (balance >= 1) if (balance balance = balance - 9;A) The loop does not end -1C) 2D) 1E) 0ANSWER IS A_____________________________________________________12) What the output of the following code:for ( ; ; )System.out.println("Welcome to Java");A) prints out Welcome to Java one time. prints out Welcome to Java forever.C) prints out Welcome to Java two times. D) does not print anything.ANSWER IS B__________________________________________________________13) Assume x is 0. What is the output of the following statement?if (x > 0)printf("x is greater than 0");else if (x printf("x is less than 0");elseprintf("x equals 0");A) x equals 0 x is greater than 0C) x is less than 0 D) NoneANSWER IS B____________________________________________________________14) What is y after the following for loop statement is executed?int y = 0;for (int i = 0; i y += 1;}A) 12 10 C) 11 D) 9___________________________________________________15) The elements inside the for loop control are separated using semicolons instead of commas. 34)A) true falseANSWER IS A_______________________________________________________16) What the output of the following code:for ( ; false ; )System.out.println("Welcome to Java");A) prints out Welcome to Java one time. prints out Welcome to Java forever.C) does not print anything. D) prints out Welcome to Java two times.ANSWER IS C_______________________________________________________17) A variable declared in the for loop control can be used after the loop exits. 36)A) true falseANSWER IS B________________________________________18) A continue statement can be used only in a loop. A) true falseANSWER IS A_______________________________________________________19) What is the output of the following fragment?int i = 1;int j = 1;while (i i++;j = j * 2;}System.out.println(j);A) 16 64 C) 32 D) 4 E) 88ANSWER IS A20) You can always write a program without using break or continue in a loop. A) true falseANSWER IS A___________________________________________________________21) What is the value of balance after the following code is executed?int balance = 10;while (balance >= 1) if (balance balance = balance - 9;A) 1 2 C) -1 D) 0ANSWER IS A__________________________________22) Analyze the following code.double sum = 0;for (double d = 0; d d += 0.1;}A) The program has a syntax error because the control variable in the for loop cannot be of thedouble type. The program compiles and runs fine.C) The program compiles but does not stop because d would always be less than 10.D) The program has a syntax error because the adjustment statement is incorrect in the for loop.ANSWER IS B______________________________________23) Which of the following expression yields an integer between 0 and 100, inclusive? 42)A) (int)(Math.random() * 100) (int)(Math.random() * 101)C) (int)(Math.random() * 100 + 1) D) (int)(Math.random() * 100) + 1ANSWER IS B_____________________________________________________24) In a for statement, if the continuation condition is blank, the condition is assumed to be ________. A) true false__________________________________________________________25) What is the output of the following fragment?for (int i = 0; i if (i % 4 == 1)System.out.print(i + " ");}A) 1 3 5 7 9 11 13 1 3 5 7 9 11 13 15C) 1 5 9 13 16D) 1 5 9 13E) 1 4 8 12ANSWER IS D____________________________________________________26) A break statement can be used only in a loop.


You are watching: Which of the loop statements always have their body executed at least once?


See more: 28 Euros To Dollars (Usd) - 28 Eur To Usd Or Convert 28 Euro In Us Dollar

4A) true falseANSWER IS B________________________________________________--27) Suppose cond1 is a Boolean expression. When will this while condition be true?while (cond1) ...A) in case cond1 is true always falseC) in case cond1 is false D) always trueANSWER IS C___________________________________________________________28) Which of the following loops produces the following output? (choose all that apply)1 2 3 4 1 2 3 1 2 1(I)for (int i = 5; i > 0; i--) for (int j = 1; j System.out.print(j + " ");System.out.println();(II)for (int i = 1; i for (int j = 1; j System.out.print(j + " ");System.out.println();}(III)int i = 0;while (i for (int j = 1; j System.out.print(j + " ");System.out.println();i++;}(IV)int i = 5;while (i > 0) for (int j = 1; j System.out.print(j + " ");System.out.println();i--;A) (II) (IV) C) (I) D) (III)ANSWER IS B & C29) Which of the loop statements always have their body executed at least once? A) The while loons The for loop C) The do-while loopANSWER IS C