Study and learn Interview MCQ Questions and Answers on Java Loops namely FOR, WHILE, DO WHILE and Break & Continue Label Statements. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Notes on FOR, WHILE, DO WHILE, Break and Continue before reading these objective questions.
A Java loop is similar to a C style loop.
while(condition) { //statements }
while(condition); { //statements }
while { //statements }(condition)
Answer B has a WHILE loop immediately terminated by a Semicolon. So it is a wrong representation.
for(initialization; condition; increment-or-decrement) { //statements }
for(condition; increment-or-decrement; initialization) { //statements }
for(increment-or-decrement; condition; initialization) { //statements }
Notice that there is no semicolon at the end of the INCREMENT/DECREMENT part.
do { //statements }while(condition);
do { //statements }while(condition)
do while(condition) { //statements }
There must be a Semicolon at the end of the while(condition) part when writing a DO-WHILE loop.
for(Type variable: Collection) { //statements }
for(Type variable; Collection) { //statements }
for(Collection: Type variable) { //statements }
There should be a COLON before the collection variable name in the Enhanced FOR loop.
Only the DO WHILE loop executes the statements at least once.
while(true);
for(;true;);
do { ; }while(true);
int a=1; while(a<4) { System.out.print(a + " "); a++; }
a++; yields to a=a+1;
int a=4; while(a>0) { System.out.print(a + " "); a--; }
String str="FOX"; int i=0; while(i<str.length()) { System.out.print(str.charAt(i)); i++; }
int cnt=0; while(true) { if(cnt > 4) break; if(cnt==0) { cnt++; continue; } System.out.print(cnt + ","); cnt++; }
CONTINUE takes the program execution to the beginning of the loop by skipping the present iteration.
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.
int age=20; do { age++; }while(age<20); System.out.println(age);
WHILE condition fails. By that time, the increment statement was executed for one time. So its new value is 21.
int i=1, j=1; while(i<3) { do { System.out.print(j + ","); j++; }while(j<4); i++; }
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.
int time=50; do { System.out.print(time + ","); time++; }while(time < 53)
The semicolon after the WHILE (Condition) is missing.
char ch[] = {'A', 'B', 'C'}; int i=0; do { System.out.print(ch[i] + ","); i++; }while(i < ch.length);
String str[] = {"A","B","C"}; int i=0; do { if(i>= str.length) break; System.out.print(str[i] + ","); i++; }while(true);
for(int i=1; i<5; i++) { System.out.print(i +","); }
boolean[] ary = {true, false, true, true}; for(int i=0; i<ary.length; i++) { System.out.print(ary[i] +","); }
int score=1; for(; true; score++) { System.out.print(score +","); if(score > 3) break; }
BREAK condition is checked after printing the variable "score". So, it prints 4 also.
for(int j=0; j<5;j++;) System.out.print(j + ",");
The semicolon after the INCREMENT/DECREMENT part is not allowed.
for(;;) is valid.
String names[] = {"MOGLI", "SHAREKHAN", "BALU"}; for(String str: names) { System.out.print(str + ","); }
The condition part is not required as the Enhanced FOR loop iterates through all elements of the Collection from staring to end.
String countries[] = {"BRAZIL", "CHILE", "SYDNEY"}; int i=0; for(String str: countries) { if(i<2) ; else break; System.out.print(str + ","); i++; }
int i=0; for(i=1; i<=6;i++) { if(i%3==0) continue; System.out.print(i+","); }
CONTINUE statement skips the execution of the remaining statements below it.
Use the Labels only to choose outer loops. Otherwise, simply use BREAK or CONTINUE without any label.
The keyword "goto" is still a reserved keyword. It can not be used.
outer: for(int i=1; i<=4;i++) { inner: for(int j=1; j<=4;j++) { if(j==1) break outer; } System.out.print("A"); }
Even before reaching the PRINT statement, the outer loop will stop because of BREAK outer.
outer: for(int i=1; i<=2;i++) { inner: for(int j=1; j<=2;j++) { if(j>i) break inner; System.out.print(j +","); } }