home.softwaretechit.com
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 [=]
A
Explanation:
A Java loop is similar to a C style loop.
2) Choose a valid loop name in Java below.
A) for
B) while
C) do while
D) All
Answer [=]
D
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 [=]
B
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 [=]
A
Explanation:
Answer B has a WHILE loop immediately terminated by a
Semicolon. So it is a wrong representation.
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 [=]
A
Explanation:
Notice that there is no semicolon at the end of the
INCREMENT/DECREMENT part.
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 [=]
A
Explanation:
There must be a Semicolon at the end of the
while(condition) part when writing a DO-WHILE loop.
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 [=]
A
Explanation:
There should be a COLON before the collection variable name
in the Enhanced FOR loop.
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 [=]
A
Explanation:
Only the DO WHILE loop executes the statements at least
once.
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 [=]
A
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 [=]
B
11) Choose the Java-Code below with a never-ending
loop.
A)
while(true);
B)
for(;true;);
C)
do
{
;
}while(true);
D) All
Answer [=]
D
12) A loop in Java generally contains a Loop-Counter
variable. State TRUE or FALSE.
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
B
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 [=]
B
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 [=]
B
Explanation:
a++; yields to a=a+1;
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 [=]
A
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 [=]
B
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 [=]
B
Explanation:
CONTINUE takes the program execution to the beginning of
the loop by skipping the present iteration.
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 [=]
B
Explanation:
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.
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 [=]
B
Explanation:
WHILE condition fails. By that time, the increment
statement was executed for one time. So its new value is 21.
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 [=]
B
Explanation:
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.
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 [=]
D
Explanation:
The semicolon after the WHILE (Condition) is missing.
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 [=]
A
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 [=]
A
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 [=]
A
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 [=]
C
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 [=]
C
Explanation:
BREAK condition is checked after printing the variable
"score". So, it prints 4 also.
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 [=]
C
Explanation:
The semicolon after the INCREMENT/DECREMENT part is not
allowed.
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 [=]
B
Explanation:
for(;;) is valid.
29) Any loop can be nested inside any loop in Java.
(TRUE/FALSE).
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
B
30) A Loop in Java language may contain ___.
A) Any loop
B) IF-ELSE statements
C) SWITCH statements
D) All
Answer [=]
D
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 [=]
A
32) The Enhanced FOR loop in Java was introduced by ___.
A) JDK 4
B) JDK 5
C) JDK 6
D) JDK 7
Answer [=]
B
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 [=]
D
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 [=]
C
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 [=]
B
Explanation:
The condition part is not required as the Enhanced FOR loop
iterates through all elements of the Collection from staring to end.
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 [=]
B
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 [=]
B
Explanation:
CONTINUE statement skips the execution of the
remaining statements below it.
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 [=]
A
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 [=]
B
Explanation:
Use the Labels only to choose outer loops. Otherwise,
simply use BREAK or CONTINUE without any label.
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 [=]
D
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 [=]
B
42) The keyword "goto" can be used in Java
programs with labels. (TRUE/FALSE)
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
A
Explanation:
The keyword "goto" is still a reserved keyword.
It can not be used.
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 [=]
A
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 [=]
C
Explanation:
Even before reaching the PRINT statement, the outer loop
will stop because of BREAK outer.
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 [=]
C
0 Comments