Q: Choose the Java-Code below with a never-ending loop.
Solution: no solution
Q: A loop in Java generally contains a Loop-Counter variable. State TRUE or FALSE.
Solution: no solution
Q: An Increment operator "++" and/or a Decrement operator "--" are used along with a Loop-Counter variable in Java. (TRUE / FALSE).
Solution: no solution
Q: What is the output of the below Java program? int a=1; while(a<4) { System.out.print(a + " "); a++; }
Solution: a++; yields to a=a+1;
Q: What is the output of the below Java program with a decrement operator and WHILE-loop? int a=4; while(a>0) { System.out.print(a + " "); a--; }
Solution: no solution
Q: What is the output of the below Java program? String str="FOX"; int i=0; while(i
Solution: no solution
Q: What is the output of the below Java program with WHILE, BREAK and CONTINUE? int cnt=0; while(true) { if(cnt > 4) break; if(cnt==0) { cnt++; continue; } System.out.print(cnt + ","); cnt++; }
Solution: CONTINUE takes the program execution to the beginning of the loop by skipping the present iteration.
Q: What is the main difference between a WHILE and a DO-WHILE loop in Java?
Solution: Both the WHILE loop and DO-WHILE loop work at the same speed. A DO-WHILE loop executes the statements inside of it even the condition is false. It is the reason why a DO-WHILE loop is used in MENU driven console java programs.
Q: What is the value of "age" in the below Java program with a DO-WHILE loop? int age=20; do { age++; }while(age<20); System.out.println(age);
Solution: WHILE condition fails. By that time, the increment statement was executed for one time. So its new value is 21.
Q: What is the output of the below java program that implements nesting of loops? int i=1, j=1; while(i<3) { do { System.out.print(j + ","); j++; }while(j<4); i++; }
Solution: Do-WHILE works for the first time printing (1,2,3). In the next iteration of i=2, the value of j is already 4. So it is printed and checked for condition (j<4). The inner loop exits. In the third iteration with i=3, nothing is printed.
You Have Score    | /10 |