Skip to the content

onlineexamguide

  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • English Language
    • Staff Nurse Exams
    • General Knowledge
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • English Language
    • Staff Nurse Exams
    • General Knowledge
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Loops in Java WHILE FOR DO WHILE Interview MCQ

Loops in Java

Table of Contents

    • Loops in Java Do While with Break & Continue Tutorial
  • Loops in Java Do While with Break and Continue
    • Loops in Java Do While with Break Statement
    • Java Loops Do While with Continue Statement
    • Nesting of Do While Loops in Java
    • Java Loops FOR with Break and Continue Tutorial
  • Loops in Java FOR Explained
    • 1. Initialization Part – FOR Loop
    • 2. Condition Part – FOR Loop
    • 3. Iteration Part – FOR Loop
  • FOR Loop with Break Statement
  • FOR loop with Continue Statement
  • Nested FOR Loops in Java
    • Java Loops While with Break and Continue Tutorial
  • Java Loops While with Break and Continue Statements
    • Example 1: A simple while loop
    • While Loop with a break statement
    • While loop with a Continue statement
    • Nested While Loops in Java
    • Java Break with Label Statement Tutorial
  • Label in Java
    • Rules to define a Label
  • 1. Java Break without Label Statement
  • 2. Java Break with Label Statement or GOTO Statement
    • Break with a Label Block
    • Break with a Label using Loops
    • Java Continue with Label Statement Tutorial
  • Java Continue Statement without LABEL
  • Java Continue with LABEL Statement and Rules to Use
    • Example: Continue Outer Loop with Label
  • Loops in Java Interview MCQ
    • 1) What is a Loop in Java programming language?
    • 3) Every loop in Java has a condition that should be ___ in order to proceed for execution. (TRUE / FALSE)
    • 9) A BREAK statement inside a Loop like WHILE, FOR, DO WHILE and Enhanced-FOR causes the program execution ___ Loop.
    • 30) A Loop in Java language may contain ___.
    • 32) The Enhanced FOR loop in Java was introduced by ___.
    • 40) Choose rules for naming a Label in Java below.

Study and learn Interview MCQ Questions and Answers on Loops in Java WHILE FOR DO WHILE and Break & Continue Label Statements. Attend job interviews easily with these Multiple Choice Questions of Java Loops WHILE FOR DO WHILE

Loops in Java Do While with Break & Continue Tutorial

Java language provides a Do While loop with slightly modified features of the WHILE loop.

Normal WHILE loop does not execute loop statements if the condition is not satisfied or true. But a DO WHILE loop executes the loop statements for the first time without even checking the loop condition. Programmers usually prefer a WHILE loop to DO WHILE loop.

A Do While loop condition should evaluate to either true or false values.

Loops in Java Do While with Break and Continue

Syntax of a Do While loop is as follows.

do{
  //statements
  //loop counter
}while( condition ); //Semicolon is present

Example:

int a=1;
do {
  System.out.print(a +",");	
  a++;	
}while(a<=5);
//OUTPUT
//1,2,3,4,5

Loops in Java Do While with Break Statement

“Break” statement inside a Do While loop causes the program control to exit the loop. The statements below the Do While loop if any will be executed. We use an IF statement to break out of the loop based on a condition.

Example:

int a=1;
do {
    if(a>5)
      break;
    System.out.print(a +",");	
    a++;	
}while(true);

//OUTPUT //1,2,3,4,5,

Java Loops Do While with Continue Statement

“Continue” statement inside a Do While loop causes the program control to skip the statements below it and go to the beginning of the loop. Loop condition is checked again before executing the statements from the beginning. We have used an IF statement to use CONTINUE statement.

Example:

int a=0;
do{
    a++;	
    if(a%2==0)
	  continue;
    System.out.print(a +",");
}while(a <= 9);
//
//OUTPUT
//1,3,5,7,9,

Nesting of Do While Loops in Java

A Do While loop can be nested inside another Do While loop, For Loop and While loop without any problem.

Example:

int a=5, b=5;
do
{
  b=a;
  do
  {	
    System.out.print(b+",");
    b--;
  }while(b>0);
  a--;
  System.out.println("");
}while(a > 0);
//
//OUTPUT
//5,4,3,2,1,
//4,3,2,1,
//3,2,1,
//2,1,
//1,

This is how a Java DO WHILE loop works.

Java Loops FOR with Break and Continue Tutorial

Java language provides another good looping control statement called FOR loop. A FOR loop is superior to WHILE loop and DO WHILE loop in terms of flexibility in maintaining loop variables.

Loops in Java FOR Explained

There are two types of FOR loops.

  1. FOR loop
  2. FOR EACH or Advanced FOR or Enhanced FOR loop

In this chapter, we shall see usage examples of the Traditional FOR loop.

Syntax:

for(initialization; condition ; iteration)
{
  //STATEMENTS //BODY
}

Example:

for(int i=1; i<=5; i++)
  System.out.print(i + ",");
//
//OUTPUT
//1,2,3,4,5,

There are three parts in a FOR loop.

  1. Initialization
  2. Condition
  3. Iteration (Increment or Decrement)

1. Initialization Part – FOR Loop

You can declare and initialize any type of variables and any number of variables under INITIALIZATION part of a FOR loop. If you declare a variable, you must initialize it with some value. There is no default value assigned.

Rules for declaring and Initializing Variables

  1. Multiple variables are separated with Commas(,).
  2. All variables should be of the same data type.
  3. Any type of variable can be declared and initialized.
  4. After this initialization part, a Semicolon (;) is mandatory which separates CONDITION part of FOR loop.

Example:

for(int i=1, j=0, k=4; (i<=5)&&(j<=5); i++)
  System.out.print(i + ",");
//
//OUTPUT
//1,2,3,4,5,
//j and k are not used.

2. Condition Part – FOR Loop

The CONDITION can be an expression that evaluates to a boolean constant. Usually, this consists of Relational or Conditional Operators along with Logical Operators to create a complex CONDITION.

3. Iteration Part – FOR Loop

Iteration Part consists of Increment and Decrement Operations on the Loop and other Variables.

FOR Loop with Break Statement

You can use a BREAK statement inside a FOR loop to break out of the loop. Program control exits the loop after encountering a BREAK statement. Usually, an IF Statement or Block surrounds the Break statement. Without the IF condition, the loop becomes Infinite or never-ending.

Example:

for(int i=1; ; i++)
{
  if(i>5)
    break;
  System.out.print(i + ",");	
}
//OUTPUT
//1,2,3,4,5

FOR loop with Continue Statement

You can use a CONTINUE statement inside a FOR loop to force the program execution control go to the beginning of the loop. Loop condition is checked once again before executing the loop body from the beginning to the end. A CONTINUE statement usually follows an IF statement.

Example:

for(int i=1; i<=10; i++)
{
  if(i%2!=0)
    continue;
  System.out.print(i + ",");	
}
//OUTPUT
//2,4,6,8,10,

Nested FOR Loops in Java

You can nest a FOR loop inside another FOR loop, WHILE loop and DO WHILE loops. Nested loops are useful to access Multidimensional Arrays.

Example:

for(int i=1; i<=5; i++)
{
  for(int j=i; j>0; j--)
  {
    System.out.print(j+" ");
  }
  System.out.println("");
}
//OUTPUT
//1 
//2 1 
//3 2 1 
//4 3 2 1 
//5 4 3 2 1 

This is how a Java FOR loop works.

Java Loops While with Break and Continue Tutorial

As part of Loop control statements, Java provides three loops namely WHILE loop, FOR loop and DO WHILE loop. Loops repeatedly execute a set of statements as long as the Loop condition is satisfied. A while loop executes the statements under it only if the condition is satisfied.

Java Loops While with Break and Continue Statements

Java continued the same syntax of while loop from the C Language. A while loop accepts a condition as an input. The condition should evaluate to either true or false. A while loop in Java does not work with integers. In Java, a number is not converted to a boolean constant.

Syntax:

while( condition )
{
  //statements
  //loop counters
}

Example 1: A simple while loop

Note that this while loop will become an Infinite Loop if we do not increment the loop counter variable.

int num=1;
while(num <= 5)
{
  System.out.print(num + ", ");
  num++; //Loop counter
  //Comment this to make it infinite loop
}
//1, 2, 3, 4, 5,

While Loop with a break statement

Java provides break statement to make the program control abruptly leave the block or loop inside which a break statement is encountered. Here, we will use the break statement without a label.

int num=1;
while(true)
{
  System.out.print(num + ", ");
  num++; //Loop counter
  if(num > 5)
    break; //break the loop
}
//1, 2, 3, 4, 5,

While loop with a Continue statement

Java provides a continue statement to make the program control abruptly go to the beginning of the loop. So, the loop is not exited in this case. Only that current iteration is skipped. In the below program we try to print only EVEN numbers in a given range. Break or Continue statements are generally used along with an IF condition.

int num=1;
while( num <=10 )
{
  num++; //Loop counter
  if(num%2 !=0 )
    continue;
  System.out.print(num + ", ");
}
//2, 4, 6, 8, 10

Nested While Loops in Java

You can nest one while loop inside another while loop. It is fun to use break and continue with nested while loop. In the below program, you will how to break inner and outer loops with break and continue statements. You can also nest different loops like for loop and do-while inside a while loop.

class NestedWhile
{
  public static void main(String args[])
  {
    int i=1, j=1;
    while(i <= 5)
    {
      j=1;
      while(true)
      {
        System.out.print(j + " ");
        if(i==j)
          break;
        j++;	
      }
      System.out.println("");
      i++;
    }
  }
}
//OUTPUT
//1
//1 2
//1 2 3
//1 2 3 4
//1 2 3 4 5

This is how a Java While Loop works

Java Break with Label Statement Tutorial

A BREAK statement in Java causes the program control to exit the block or loop immediately.

Label in Java

A Java Label is any valid Identifier or name with a COLON after it.

Rules to define a Label

  1. A Label name must start with either Alphabet, Underscore(_) or a Dollar ($) symbol. It may contain numbers.
  2. A Label must be followed by a Loop or a Block.
  3. Defining a Label like a Goto statement produces a compiler error

There are two types of Break statements.

  1. Break without label
  2. Break with label

1. Java Break without Label Statement

Using a Break statement without specifying any Label causes the loop to close and exit immediately. It works only with Loops.

You can use the Break statement without a Label with any loop like FOR, Enhanced FOR, WHILE and DO WHILE loop. You can not break more than one loop using just a BREAK without Label.

2. Java Break with Label Statement or GOTO Statement

C language supported goto statements for the convenience of developers or programmers. Java supports GOTO statement in the form of BREAK with Label or CONTINUE with LABEL statements. That means you can not use the keyword “goto” as of now. It is just a reserved word.

Break with a Label works with both Code BLOCKS and Loops.

Break with a Label Block

You can define a Block in Java program using an Opening Brace ( { )and a Closed Brace ( } ). Just add a name to that block with a COLON symbol. SWITCH block is the best example of using a BREAK with a BLOCK statement. After the BREAK statement is encountered, program control goes out of that named block or labelled block.

Example: 

class LabelBlock
{
  public static void main(String[] args)
  {
    int a=10;
    MYLABEL: {
      System.out.println("Entered Label");
      if(a>5)
        break MYLABEL;
      System.out.println("NOT REACHABLE");
    }
    System.out.println("AFTER LABEL BLOCK");
  }
}
//OUTPUT
//Entered Label
//AFTER LABEL BLOCK

Break with a Label using Loops

You can define a LABEL with a Loop BLOCK like WHILE loop block, FOR loop block, Do WHILE loop block or an Enhanced FOR loop Block. You can escape any number of loops with a Break inside a Labelled Block. Nesting is not just 2 levels in the real programming world.

Rules to use Break with a Labelled Loop

  1. Break statement should be part of the labelled loop.
  2. You can not break out of a labelled loop which does not enclose the break statement.

Example: Break a Nested Loop or Inner Loop

class BreakNesetedLoop
{
  public static void main(String[] args)
  {
    outer:
    for(int i=0; i<5; i++)
    {
      for(int j=0; j<5; j++)
      {
        System.out.print(j + ",");
        if(j==3)
          break outer;
      }
    }
    System.out.println("\nOUTSIDE of LABEL");
  }
}
//OUTPUT
//0,1,2,3,
//OUTSIDE of LABEL

This is how a BREAK with LABEL works in Java.

Java Continue with Label Statement Tutorial

A CONTINUE statement inside a loop or iteration in Java causes the program control to go to the beginning of the loop. Next iteration of the loop continues as usual. A CONTINUE statement can be used only with Loops. If you use, you get the error “continue cannot be used outside of a loop“.

There are two types of CONTINUE statements.

  1. CONTINUE without LABEL
  2. CONTINUE with LABEL

Java Continue Statement without LABEL

A CONTINUE statement without a LABEL statement can Skip remaining statements of the loop for the current iteration. It can affect only the Inner Loop or a Single Loop or the Loop where the CONTINUE statement is present.

You can use CONTINUE without label statement with the loops, FOR, WHILE, DO WHILE and FOR-EACH. You can nest any number of loops and use CONTINUE statement with a Label at any Level. The number of loops may be more than two for example.

Java Continue with LABEL Statement and Rules to Use

Usually, a CONTINUE statement is used inside an IF statement which is surrounded by a Loop with a Label.

Rules:

1. A Label name or an identifier can start with an Alphabet, Underscore ( _ ) or a Dollar ($) sign. It may contain numbers.

2. A Label should be declared before the loop which contains the CONTINUE statement.

3. You can not use CONTINUE on a Label that is outside the Loop Block. It means that LABEL loop block is different.

4. You can exit an Inner loop with CONTINUE on Label for Outer loop. Without using a Break statement, you are exiting a loop.

5. You can restart an Inner loop without affecting Loop Counter value.

Example: Continue Outer Loop with Label

class ContinueOuterLabel
{
  public static void main(String args[]){
  outer: 
  for(int i=1; i<=5; i++)
  {
    inner:
    for(int j=i; j>0 ;j--)
    {
      if(i>3)
        continue outer;
      System.out.print(j +",");
    }
    System.out.println("");
  }
  System.out.println("OUTSIDE LABEL");
  }
}
//OUTPUT
//1,
//2,1,
//3,2,1,
//OUTSIDE LABEL

This is how a Java Loops CONTINUE statement works with or without a LABEL.

[WpProQuiz 154]

Loops in Java Interview MCQ

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

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

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

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

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

Answer [=] B

Explanation:

for(;;) is valid.

29) Any loop can be nested inside any loop in Java. (TRUE/FALSE).

A) FALSE

B) TRUE

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

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

Answer [=] B

42) The keyword “goto” can be used in Java programs with labels. (TRUE/FALSE)

A) FALSE

B) TRUE

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

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

Write a comment Cancel reply

You must be logged in to post a comment.

*
  • किस राज्य/केंद्र शासित प्रदेश ने ‘कोडवा हॉकी महोत्सव’ (Kodava Hockey Festival) की मेजबानी की?

  • उत्तर – कर्नाटक

  • हाल ही में खबरों में रहा बरदा वन्यजीव अभयारण्य (Barda Wildlife Sanctuary) किस राज्य/केंद्र शासित प्रदेश में स्थित है?

  • उत्तर – गुजरात

  • किस देश ने ‘सऊदी-ईरान सम्बन्ध सामान्यीकरण’ शांति समझौते की मध्यस्थता की?

  • उत्तर – चीन

  • ‘संयुक्त राष्ट्र 2023 जल सम्मेलन’ (United Nations 2023 Water Conference) का मेजबान कौन सा देश है?

  • उत्तर – अमेरिका

  • ‘अल-मोहद-अल हिंदी-23’ (Al-Mohed-Al Hindi-23) अभ्यास भारत और किस देश के बीच आयोजित किया जा रहा है?

  • उत्तर – सऊदी अरब

  • सेमी-हाई-स्पीड वंदे भारत एक्सप्रेस ट्रेन चलाने वाली पहली महिला लोको पायलट कौन हैं?

  • उत्तर – सुरेखा यादव

  • भारतीय रिजर्व बैंक ने 2023 तक कितने देशों के बैंकों को रुपये में व्यापार करने की अनुमति दी थी?

  • उत्तर – 18

  • MD15 बसों का प्रायोगिक परीक्षण और M100 (100% मेथनॉल) का प्रोटोटाइप किस शहर में लॉन्च किया गया?

  • उत्तर – बेंगलुरु

  • फरवरी 2023 में भारत में थोक मूल्य सूचकांक (WPI) आधारित मुद्रास्फीति कितनी है?

  • उत्तर – 3.85%

  • किस संस्थान ने एक व्यापक स्व-निगरानी ढांचा ‘ATL सारथी’ लॉन्च किया है?

  • उत्तर – नीति आयोग

  • उस चक्रवात का क्या नाम है जिससे मलावी और मोज़ाम्बिक में तेज़ हवाएँ चलीं और मूसलाधार बारिश हुई?

  • उत्तर – फ्रेडी

  • ऑस्कर 2023 इवेंट के दौरान किस फिल्म ने सात पुरस्कार जीते?

  • उत्तर – Everything Everywhere All at Once

  • MoSPI के हालिया आंकड़ों के अनुसार, फरवरी 2023 में भारत की खुदरा मुद्रास्फीति कितनी दर्ज की गई?

  • उत्तर – 6.44%

  • कौन सा राज्य/केंद्र शासित प्रदेश ‘साझा बौद्ध विरासत पर पहला अंतर्राष्ट्रीय सम्मेलन’ का मेजबान है?

  • उत्तर – नई दिल्ली

  • किस राज्य ने राज्य के कार्यकर्ताओं को राज्य सरकार की नौकरी में 10% क्षैतिज आरक्षण को मंजूरी दी?

  • उत्तर – उत्तराखंड

  • Unique Land Parcel Identification Number (ULPIN) कितने अंकों वाला एक अल्फा-न्यूमेरिक नंबर है?

  • उत्तर – 14

  •  किस संस्था ने ‘Landslide Atlas of India’ जारी किया?

  • उत्तर – इसरो

  • किस केंद्रीय मंत्रालय ने ‘लीन योजना’ (LEAN Scheme) शुरू की?

  • उत्तर – MSME मंत्रालय

  • कौन सा केंद्रीय मंत्रालय ‘World Food India-2023’ कार्यक्रम की मेजबानी करने जा रहा है?

  • उत्तर – खाद्य प्रसंस्करण उद्योग मंत्रालय

  • माधव राष्ट्रीय उद्यान (Madhav National Park), जो हाल ही में खबरों में था, किस राज्य/केंद्र शासित प्रदेश में है?

  • उत्तर – मध्य प्रदेश

  • किस राज्य/केंद्र शासित प्रदेश ने विभिन्न क्षेत्रों में शहर के विकास का मार्गदर्शन करने के लिए ‘2041 के लिए मास्टर प्लान’ जारी किया?

  • उत्तर – नई दिल्ली

  • हाल ही में ख़बरों में रहा ‘Safe Harbour Principle’ किस अधिनियम से संबंधित है?

  • उत्तर – सूचना प्रौद्योगिकी अधिनियम, 2000

  • 2023 में शंघाई सहयोग संगठन (SCO) की अध्यक्षता किस देश के पास है?

  • उत्तर – भारत

  • हाल ही में खबरों में रहा टोरिनो स्केल (Torino Scale) किस क्षेत्र से जुड़ा है?

  • उत्तर – अंतरिक्ष विज्ञान

  • कृत्रिम बुद्धि (artificial intelligence) द्वारा संचालित दुनिया के पहले रेडियो प्लेटफॉर्म का नाम क्या है?

  • उत्तर – RadioGPT

  • नासा के क्यूरियोसिटी रोवर (Curiosity Rover) ने हाल ही में किस ग्रह पर क्रिपस्कुलर किरणों (crepuscular rays) को कैप्चर किया है?

  • उत्तर – मंगल

  • कौन सा केंद्रीय मंत्रालय ‘स्वदेश दर्शन 2.0 कार्यक्रम’ लागू करता है?

  • उत्तर – पर्यटन मंत्रालय

  • हाल ही में Prevention of Money Laundering Act को किन उत्पादों को शामिल करने के लिए विस्तारित किया गया था?

  • उत्तर – वर्चुअल डिजिटल संपत्ति

  • किस देश ने National Platform for Disaster Risk Reduction (NPDRR) के तीसरे सत्र की मेजबानी की?

  • उत्तर – भारत

  • किस राज्य के राज्यपाल ने राज्य मंत्रिमंडल द्वारा पारित ऑनलाइन जुआ निषेध विधेयक (Prohibition of Online Gambling Bill) को लौटा दिया है?

  • उत्तर – तमिलनाडु

  • भारत में पहली बार माइम्युसेमिया सीलोनिका (Mimeusemia ceylonica), दुर्लभ पतंगे की प्रजाति को किस राज्य में देखा गया है?

  • उत्तर – केरल

  • किस देश ने ‘Illegal Migration Bill’ पेश किया?

  • उत्तर – यूके

  • किस देश ने 25 वर्षों में पहली बार महिलाओं के लिए सैन्य सेवा खोली है?

  • उत्तर – कोलंबिया

  • केंद्र ने हाल ही में NAFED, NCCF को किस उत्पाद की खरीद के लिए बाजार में तत्काल हस्तक्षेप करने का निर्देश दिया है?

  • उत्तर – लाल प्याज

  • ‘ट्रोपेक्स 2023’ किस देश द्वारा आयोजित एक प्रमुख परिचालन स्तर का अभ्यास है?

  • उत्तर – भारत

  • किस संस्था ने ‘Global Greenhouse Gas Monitoring Infrastructure’ पेश किया?

  • उत्तर – WMO

  • किस केंद्रीय मंत्रालय ने ‘स्वच्छोत्सव’ महिलाओं के नेतृत्व में स्वच्छता अभियान शुरू किया?

  • उत्तर – आवास और शहरी मामलों के मंत्रालय

  • सल्हौतुओनुओ क्रूस (Salhoutuonuo Kruse) ने किस राज्य की पहली महिला कैबिनेट मंत्री बनकर इतिहास रचा है?

  • उत्तर – नागालैंड

  • कौन सा शहर वित्तीय समावेशन के लिए वैश्विक भागीदारी की दूसरी बैठक का मेजबान था?

  • उत्तर – हैदराबाद

  • डॉ माणिक साहा ने 2023 में किस भारतीय राज्य के मुख्यमंत्री के रूप में शपथ ली?

  • उत्तर – त्रिपुरा

  • ‘डिजिटल इंडिया बिल’ किस केंद्रीय मंत्रालय से जुड़ा है?

  • उत्तर – इलेक्ट्रॉनिक्स और आईटी मंत्रालय

  • ड्राफ्ट केंद्रीय विद्युत प्राधिकरण विनियम (Draft Central Electricity Authority Regulations) हाल ही में किस प्रजाति की रक्षा के लिए जारी किया गया था?

  • उत्तर – ग्रेट इंडियन बस्टर्ड

  • किस देश ने ‘International Big Cat Alliance’ बनाने का प्रस्ताव दिया है?

  • उत्तर – भारत

  • ब्रह्मोस मिसाइल को रूस के NPO मशीनोस्ट्रोयेनिया और भारत के किस संगठन के बीच साझेदारी से विकसित किया गया है?

  • उत्तर – DRDO

  • दुनिया का पहला 200 मीटर लंबा बैंबू क्रैश बैरियर ‘बाहु बल्ली’ किस राज्य में स्थापित किया गया है?

  • उत्तर – महाराष्ट्र

  • किस देश ने लगभग 8.5 मिलियन मीट्रिक टन लिथियम अयस्क की खोज करने का दावा किया है?

  • उत्तर – ईरान

  • किस संस्था ने ‘Women, Business and the Law Index’ जारी किया?

  • उत्तर – विश्व बैंक

  • 2021-22 के लिए आवधिक श्रम बल सर्वेक्षण (PLFS) रिपोर्ट के अनुसार, कृषि क्षेत्र में रोजगार का अंश कितना है?

  • उत्तर – 45.5%

  • किस संस्था ने ‘Advanced Towed Artillery Gun System (ATAGS)’ डिजाइन किया है?

  • उत्तर – DRDO

  • हाल ही में खबरों में रहा HUID नंबर किस तत्व/उत्पाद से जुड़ा है?

  • उत्तर – सोना

  • किन संस्थानों ने ‘More than a billion reasons: The urgent need to build universal social protection’ शीर्षक से रिपोर्ट जारी की?

  • उत्तर – UNICEF- ILO

  • केंद्रीय सिंचाई एवं विद्युत बोर्ड (CBIP) पुरस्कार किस संस्था को प्रदान किया गया?

  • उत्तर – NTPC

  • किस संस्था ने ‘Mind the Gender Gap’ रिपोर्ट जारी की?

  • उत्तर – CFA Institute

  • हाल ही में खबरों में रही ‘समर्थ योजना’ (SAMARTH scheme) किस मंत्रालय से जुड़ी है?

  • उत्तर – कपड़ा मंत्रालय

  • राष्ट्रीय सुरक्षा दिवस (National Safety Day) 2023 की थीम क्या है?

  • उत्तर – Our Aim – Zero Harm

  • कौन सा शहर ‘G20 विदेश मंत्रियों की बैठक (FMM)’ का मेजबान है?

  • उत्तर – नई दिल्ली

  • किस देश ने इंडो-पैसिफिक टेक दूत (Indo-Pacific tech envoy) की घोषणा की?

  • उत्तर – यूके

  • किस बैंक ने सिटीग्रुप के भारतीय उपभोक्ता कारोबार का अधिग्रहण पूरा कर लिया है?

  • उत्तर – Axis Bank

  • किस केंद्रीय मंत्रालय ने ‘Grievance Appellate Committee (GAC)’ लॉन्च की?

  • उत्तर – इलेक्ट्रॉनिक्स और आईटी मंत्रालय

  • ‘Indian States’ Energy Transition’ रिपोर्ट के अनुसार, किन राज्यों ने स्वच्छ बिजली में परिवर्तन में सबसे अधिक प्रगति की है?

  • उत्तर – कर्नाटक और गुजरात

  • धरोई आर्द्रभूमि (Dharoi wetland), जहाँ हाल ही में एक पक्षी सर्वेक्षण किया गया था, किस राज्य में स्थित है?

  • उत्तर – गुजरात

  • IMF के अनुसार, किस देश में 2023 में वैश्विक विकास में 15% योगदान देने की क्षमता है?

  • उत्तर – भारत

  • भारत के किस पड़ोसी देश ने सौर ऊर्जा के उपयोग को बढ़ाने के लिए ISA के साथ समझौता ज्ञापन पर हस्ताक्षर किए?

  • उत्तर – बांग्लादेश

  • अंतर्राष्ट्रीय बौद्धिक संपदा सूचकांक 2023 में भारत का रैंक क्या है?

  • उत्तर – 42

  • कौन सा राज्य ‘वैश्विक उत्तरदायी पर्यटन शिखर सम्मेलन’ (Global Responsible Tourism Summit) का मेजबान है?

  • उत्तर – केरल

  • ‘UPI LITE पेमेंट्स’ लॉन्च करने वाला पहला प्लेटफॉर्म कौन सा है?

  • उत्तर – पेटीएम पेमेंट्स बैंक

  • किस संस्था ने भारत का पहला म्यूनिसिपल बॉन्ड इंडेक्स लॉन्च किया?

  • उत्तर – NSE

  • किस शहर का नाम बदलकर ‘छत्रपति संभाजीनगर’ कर दिया गया है?

  • उत्तर – औरंगाबाद

  • भारत की G-20 अध्यक्षता के तहत W20 इंसेप्शन मीटिंग की मेजबानी कौन सा शहर कर रहा है?

  • उत्तर – औरंगाबाद

  • कौन सा शहर ‘International Bio-resource Conclave & Ethno-pharmacology Congress 2023’ का मेजबान है?

  • उत्तर – इंफाल

  • फेंटानिल और पशु ट्रैंक्विलाइज़र का मिश्रण जिसे ज़ाइलाज़ीन कहा जाता है, जिसे ‘ट्रांक डोप’ के रूप में जाना जाता है, किस देश में चिंता पैदा कर रहा है?

  • उत्तर – अमेरिका

  • किस राज्य को ‘Foundational Literacy and Numeracy Index 2022’ में शीर्ष प्रदर्शन करने वाला स्थान मिला?

  • उत्तर – पश्चिम बंगाल

  • किस देश ने ‘National Green Fiscal Incentives Policy Framework’ लॉन्च किया?

  • उत्तर – केन्या

  • काजीरंगा राष्ट्रीय उद्यान किस राज्य में स्थित है?

  • उत्तर – असम

  • ‘राष्ट्रीय स्वास्थ्य प्राधिकरण की स्कैन एंड शेयर सर्विस’ किस योजना के तहत शुरू की गई थी?

  • उत्तर – आयुष्मान भारत डिजिटल मिशन

  • किस देश ने ‘कमर्शियल आर्म्स ट्रांसफर (CAT) पॉलिसी’ लॉन्च की है?

  • उत्तर – अमेरिका

  • हाल ही में खबरों में रही ‘INS सिंधुकेसरी’ क्या है?

  • उत्तर – पनडुब्बी

  • 2022 में किस देश की प्रजनन दर दुनिया में सबसे कम 0.78 है?

  • उत्तर – दक्षिण कोरिया

  • हाल ही में खबरों में रहा रिड्यू कैनाल स्केटवे (Rideau Canal Skateway) किस देश में है?

  • उत्तर – कनाडा

Recent Posts

  • Starting Methods of Synchronous Motor
  • Torque and Power Relation
  • Phasor Diagram for Synchronous Motor
  • Synchronous Motor: Applications, Starting Methods & Working Principle
  • Prime mover
  • Parallel Operation of Alternators
  • Slip Test on Synchronous Machine
  • Salient Pole and Non Salient Pole Synchronous Generator
  • Power Angle Curve of Synchronous Machine
  • Methods of finding Voltage Regulation in Synchronous Generator
  • Voltage Regulation of Alternator or Synchronous Generator
  • Potier Reactance – Synchronous Generator
  • Short Circuit Ratio of a Synchronous Machine (SCR)
  • Synchronous Reactance and Synchronous Impedance
  • Armature Reaction in Alternator

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming
  • C programming
  • C++ programming
  • Aptitude Tricks

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2023
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2023
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2023 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.