Java Loops WHILE FOR DO WHILE Interview MCQ Questions and Answers

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.



1) What is a Loop in Java programming language?
A) A Loop is a block of code that is executed repeatedly as long as a condition is satisfied.
B) A Loop is a block of code that is executed only once if the condition is satisfied.
C) A Loop is a block of code that is executed more than 2 times if the condition is satisfied.
D) None
Answer [=]
Answer[=]
2) Choose a valid loop name in Java below.
A) for
B) while
C) do while
D) All
Answer [=]
Answer[=]
3) Every loop in Java has a condition that should be ___ in order to proceed for execution. (TRUE / FALSE)
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
Answer[=]
4) Choose the correct syntax of the WHILE loop in Java below.
A)
while(condition)
{
  //statements
}
B)
while(condition);
{
  //statements
}
C)
while
{
  //statements
}(condition)
D) None
Answer [=]
Answer[=]
5) Choose the correct Syntax of FOR loop in Java below.
A)
for(initialization; condition; increment-or-decrement)
{
  //statements
}
B)
for(condition; increment-or-decrement; initialization)
{
  //statements
}
C)
for(increment-or-decrement; condition; initialization)
{
  //statements
}
D) None
Answer [=]
Answer[=]
6) Choose the correct syntax of the DO WHILE loop in Java below.
A)
do
{
  //statements
}while(condition);
B)
do
{
  //statements
}while(condition)
C)
do while(condition)
{
  //statements
}
D) None
Answer [=]
Answer[=]
7) Choose the correct syntax of an Enhanced FOR loop in Java below.
A)
for(Type variable: Collection)
{
  //statements
}
B)
for(Type variable; Collection)
{
  //statements
}
C)
for(Collection: Type variable)
{
  //statements
}
D) None
Answer [=]
Answer[=]


 

8) State TRUE or FALSE. A WHILE loop in Java executes the statements at least once even the condition is not satisfied.
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
Answer[=]
9) A BREAK statement inside a Loop like WHILE, FOR, DO WHILE and Enhanced-FOR causes the program execution ___ Loop.
A) Exit
B) Continuation with next iteration
C) Never exit
D) None
Answer [=]
Answer[=]
10) A CONTINUE statement inside a Loop like WHILE, FOR, DO-WHILE and Enhanced-FOR causes the program execution ___ the loop.
A) Skip
B) Skip present iteration and continue with next iteration of the loop
C) Exit
D) None
Answer [=]
Answer[=]
11) Choose the Java-Code below with a never-ending loop.
A)
while(true);
B)
for(;true;);
C)
do
{
  ;
}while(true);
D) All
Answer [=]
Answer[=]
12) A loop in Java generally contains a Loop-Counter variable. State TRUE or FALSE.
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
Answer[=]
13) An Increment operator "++" and/or a Decrement operator "--" are used along with a Loop-Counter variable in Java. (TRUE / FALSE).
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
Answer[=]
14) What is the output of the below Java program?
int a=1;
while(a<4)
{
  System.out.print(a + " ");
  a++;
}
A) 1 2 3 4
B) 1 2 3
C) 6
D) Compiler error
Answer [=]
Answer[=]


 

15) 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--;
}
A) 4 3 2 1
B) 3 2 1
C) Compiler error
D) None
Answer [=]
Answer[=]
16) What is the output of the below Java program?
String str="FOX";
int i=0;
while(i<str.length())
{
  System.out.print(str.charAt(i));
  i++;
}
A) FFF
B) FOX
C) Compiler error
D) None
Answer [=]
Answer[=]
17) 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++;
}
A) 0,1,2,3,4,
B) 1,2,3,4,
C) 1,2,3,4
D) Compiler error
Answer [=]
Answer[=]
18) What is the main difference between a WHILE and a DO-WHILE loop in Java?
A) WHILE loop executes the statements inside of it at least once even if the condition is false.
B) DO-WHILE loop executes the statements inside of it at least once even if the condition is false.
C) WHILE loop is fast.
D) DO-WHILE loop is fast.
Answer [=]
Answer[=]
19) 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);
A) 20
B) 21
C) Compiler error
D) None
Answer [=]
Answer[=]
20) 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++;
}
A) 1,2,3,4,1,2,3,4,
B) 1,2,3,4,
C) 1,2,3,1,2,3,
D) 1,2,3,
Answer [=]
Answer[=]
21) What is the output of the below Java program?
int time=50;
do
{
System.out.print(time + ",");
time++;
}while(time < 53)
A) 50,50,50,
B) 50,51,52,
C) 51,52,53,
D) Compiler error
Answer [=]
Answer[=]


 

22) What is the output of the below Java program?
char ch[] = {'A', 'B', 'C'};
int i=0;
do
{
  System.out.print(ch[i] + ",");
  i++;
}while(i < ch.length);
A) A,B,C,
B) A,B,C
C) A,A,A
D) Compiler error
Answer [=]
Answer[=]
23) What is the output of the below Java program?
String str[] = {"A","B","C"};
int i=0;
do
{
  if(i>= str.length)
    break;
  System.out.print(str[i] + ",");
  i++;
}while(true);
A) A,B,C,
B) A,B,C
C) Runtime Exception with Index Of Bounds Exception
D) Compiler error
Answer [=]
Answer[=]
24) What is the output of the below Java code with a FOR loop?
for(int i=1; i<5; i++)
{
  System.out.print(i +",");
}
A) 1,2,3,4,
B) 1,2,3,4
C) 1,2,3,4,5,
D) 1,2,3,4,5
Answer [=]
Answer[=]
25) What is the output of the below Java code?
boolean[] ary = {true, false, true, true};
for(int i=0; i<ary.length; i++)
{
    System.out.print(ary[i] +",");
}
A) true,true,true,true,
B) true,false,false,true
C) true,false,true,true
D) Compiler error
Answer [=]
Answer[=]
26) What is the output of the below Java code?
int score=1;
for(; true; score++)
{
  System.out.print(score +",");
  if(score > 3)
    break;
}
A) 1,2,3,
B) 1,2,3
C) 1,2,3,4,
D) 1,2,3,4
Answer [=]
Answer[=]
27) What is the output of the below Java program with FOR loop?
for(int j=0; j<5;j++;)
  System.out.print(j + ",");
A) 1,2,3,4,
B) 0,1,2,3,4
C) Compiler error
D) None
Answer [=]
Answer[=]
28) State TRUE or FALSE. In a FOR loop, the Initialization-part, Condition-part and Increment/Decrement part can be empty.
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
Answer[=]
29) Any loop can be nested inside any loop in Java. (TRUE/FALSE).
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
Answer[=]
30) A Loop in Java language may contain ___.
A) Any loop
B) IF-ELSE statements
C) SWITCH statements
D) All
Answer [=]
Answer[=]
31) In Java language, BREAK or CONTINUE statements can be implemented inside a Loop only with the help of ___ statements to avoid never-ending loops.
A) IF ELSE
B) SWITCH
C) ENUM
D) None
Answer [=]
Answer[=]
32) The Enhanced FOR loop in Java was introduced by ___.
A) JDK 4
B) JDK 5
C) JDK 6
D) JDK 7
Answer [=]
Answer[=]
33) An enhanced FOR loop work with only Collection type data. Examples of Collection are ___.
A) Array Class type or any regular array variable
B) ArrayList
C) HashMap, HashSet
D) All
Answer [=]
Answer[=]
34) What is the output of Java Enhanced FOR loop below?
String names[] = {"MOGLI", "SHAREKHAN", "BALU"};
for(String str: names)
{
  System.out.print(str + ",");
}
A) MOGLI,
B) MOGLI,SHAREKHAN,
C) MOGLI,SHAREKHAN,BALU,
D) Compiler error
Answer [=]
Answer[=]
35) An Enhanced FOR loop in Java misses ___ and __ compared to the old-style FOR loop.
A) Speed and Easiness
B) Initialization, Increment/Decrement
C) Semicolons, Variables
D) None
Answer [=]
Answer[=]
36) What is the output of the Java program with Enhanced FOR loop below?
String countries[] = {"BRAZIL", "CHILE", "SYDNEY"};
int i=0;
for(String str: countries)
{
  if(i<2)
    ;
  else
    break;
  System.out.print(str + ",");
  i++;
}
A) BRAZIL,CHILE,SYDNEY,
B) BRAZIL,CHILE,
C) BRAZIL,
D) Compiler error
Answer [=]
Answer[=]
37) What is the output of the Java code snippet?
int i=0;
for(i=1; i<=6;i++)
{
  if(i%3==0)
    continue;
  System.out.print(i+",");
}
A) 1,2,
B) 1,2,4,5,
C) 3,6,
D) Compiler error
Answer [=]
Answer[=]
38) A BREAK or CONTINUE statement applies only to the ___ loop.
A) Inner loop or the loop containing break or continue
B) always Outer loop
C) Sometimes inner loop, sometimes outer loop
D) None
Answer [=]
Answer[=]
39) A BREAK-WITH-LABEL or CONTINUE-WITH-LABEL are used in particular in Java to select __ loop either to Break or Continue.
A) Inner loop
B) Outer loop
C) -
D) -
Answer [=]
Answer[=]
40) Choose rules for naming a Label in Java below.
A) The name of a label or identifier may start only with Alphabet, Underscore ( _ ) or Dollar ($) symbol
B) A label is kept before the loop in general
C) Duplicate label names are not allowed
D) All
Answer [=]
Answer[=]
41) State TRUE or FALSE. You can exit an inner loop without using a BREAK statement but with a CONTINUE and Label on the outer loop.
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
Answer[=]
42) The keyword "goto" can be used in Java programs with labels. (TRUE/FALSE)
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
Answer[=]
43) Is it possible to break all loops with a single BREAK with a Label statement? (YES/NO)
A) YES
B) NO
C) -
D) -
Answer [=]
Answer[=]
44) What is the output of the Java code snippet below?
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");
}
A) A
B) AAAA
C) No Output
D) Compiler error
Answer [=]
Answer[=]
45) What is the output of the below Java program?
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 +",");	
  }
}
A) 1,1,1
B) 1,2,2,
C) 1,1,2,
D) Compiler error
Answer [=]
Answer[=]


Like or Share

Show some care. Like or Subscribe. [FB]...[Youtube]

C MCQ App by ExamTray 

Android APP

Java MCQ App by ExamTray 

Android APP
ALL MCQ App by ExamTray Android APP

Ad

 

Try Some Java Books

Book Price
1. Java - The Complete Reference  Check Price
2. Core Java: An Integrated Approach, Black Book Check Price
3. Java All-in-One for Dummies Check Price
4. OCP Java SE 8: Programmer II Check Price
5. Programming with Java Check Price
6. Head First Java Check Price

We may get some affiliate commission for the above purchases.