- Home
- All Blogs
-
JAVA interview questions on Loops?
JAVA interview questions on Loops?
Published On:
3/6/2023
Author:
Admin
We tried to provide some Interview questions on Loops in Java Programming. There are 4 type of loop statements in Java programming like while, do-while, for and For-each.
Which type of loop is guaranteed to have the body execute at least once?
In JAVA programming Do-while loop is used when you want to execute loop at least once.
In do-while loop, code is first executed and then Condition is checked. Do while loop looks like given below.
Do
{
// body of loop
System.out.println(j);
J++;
}while (j<5);
Which type of loop is used when number of Iterations is unknown?
Do-while and while loops are used when exact number of Iteration are not know. In this loop conditional expression is used to control the loop.
The loop body is executed until expressional condition in While is satisfied.
When Memory will runs out if you are using Loops in JAVA?
If we set condition expression in such a way that it never becomes False then the loop will run for infinite time. The loop stops only when Memory runs out.
Can For Loop be terminated?
Yes. For loop can be terminated in between execution.
We can terminate for loop by inserting Break Statement in Java Programming language. When Break statement is encountered inside a loop, the loop terminates immediately. Break statement is used to exit loop in Java Programming language.
When For Each loop statement is used?
For each statement is used to iterate through array of elements stored in variable. For each loop goes through each element and terminates when all elements are finished processing in a Array variable.
For example
// create an array
int[] nums = {1, 2, 4, 9};
// iterating through the array
for (int number: nums) {
System.out.println(number);
}
In above example for each prints all elements in variable array one by one.
In Java Programming when loop is called as Infinite Loop?
If conditional expression is set in such a way that it never turns/ evaluates false, then loop runs for infinite times and it is called as infinite loop.
Infinite loop runs until memory runs out.
Following are examples of Infinite loops
Infinite do-while loop example
Do{
//body statement
} while (true);
Infinite while loop example
While (true)
{
//body statement
}
Infinite for loop example
For (int I =1; I < 10; --i)
{
System.out.println("Hello");
}
What is nested loop in Java Programming?
If Loop exists inside body of another loop, then it is called as nested loop.
Example
While (Condition)
{
//body statement first loop
While(condition)
{
//body statement inner loop
}
}
Explain “Continue” statement in Java Programming!
“Continue” statement is used in nested loops in Java Programming.
When we use continue statement inside the inner loop, it skips the current iteration of inner loop only. The outer Loop execution is unaffected and continues.
#Java, #JavaProgramming #JavaInterviewQuestions #JavaLoops
Currently no practice papers available for above exam. We are working on it and tests will be available soon.
|
Your Comment:
(0) Comments: