Loop (While, For, Do-While) in Java Question Answer - MyCSTutorial - in - The Path To Success in Exam..
Loop (While, For, Do-While) in Java Question Answer - MyCSTutorial - in - The Path To Success in Exam..
The ability of a computer to perform the same set of actions again and again is called looping
Que 2. What is the body of loop?
Answer: The sequence of statements that is repeated again and again is called the body of the loop.
Que 3. What is the test condition of loop?
Answer: The test conditions is an expression, which determine whether a loop is entered or exited and it
is constructed using relational and logical operators.
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 1/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
TF
IIFT Bhopal
TFII
Interior Designing
Institute
Open
Answer: A loop which check the test condition before the execution of the body is called Entry controlled
loop.
Que 8. What is exit controlled loop?
Answer: A loop which check the test condition after the execution of body is called Exit controlled loop.
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 2/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
Interior Designing
Open
Institute
IIFT Bhopal
Que 10. Differentiate between entry controlled and exit controlled loop.
Answer: Entry controlled loop vs Exit controlled loop
In this loop, condition / test expression evaluates In this loop, condition/test expression evaluates
before the execution of body after the execution of body.
In this body of loop does not execute if test The body of loop executed at least one time, if the
condition is false test condition is false.
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 3/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
Answer: The while statement evaluates the body of the loop till the test condition is true. It checks the
condition before executing the body of a loop. It is an entry-controlled loop.
update_statement
}
Que 15. Write a java program to print the numbers from 1 to 5 using a while loop.
Answer:
++number;
}
}
}
Que 16. Write a java program to print the numbers from 1 to 5 and display sum of the numbers.
Answer:
public class SumNumber1to5 {
public static void main (String[ ] args) {
int sum = 0;
int number = 1;
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 4/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
++number;
}
print(“Sum of numbers from 1 to 5 is “, +sum);
}
}
Que 17. Write a java program to print the sum of all even numbers from 1 to 20.
Answer:
public class SumEvenNumber {
}
Que 18. Write a java program to print the squares of numbers from 1 to 10.
Answer:
public class SquareNumber1to10 {
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 5/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
Que 19. Write a java program to print the squares of odd number and cube of even number from 1
to 10.
Answer:
public class SQCUBNumber1to10 {
public static void main (String[ ] args) {
int number = 1;
while (number <= 10) {
if(number % 2 == 0)
System.out.print (“Cube of ” + number);
}
++number;
}
}
}
Que 20. What is the do – while statement?
Answer: The do-while statement evaluates the body of the loop till the test condition is true. It checks the
condition after executing the body of loop. It is an exit controlled loop.
The structure of the Java do-while statement is as shown:
Initialization;
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 6/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
do {
statements // body of loop
update_statement
System.out.println (” ” + number);
++number;
} while (number <= 5) ;
}
}
Que 22. Write a java program to print the squares of numbers from 1 to 10 using do-while loop.
Answer:
public class PrintNumber1to5 {
public static void main (String[ ] args) {
int number = 1;
do {
System.out.print (“Square of ” + number);
System.out.println(“=” + (number * number));
++number;
} while (number <= 10) ;
}
}
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 7/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
Interior Designing
Open
Institute
IIFT Bhopal
Que 23. Differentiate between while and do-while loop? [CBSE QP/ SQP]
Answer: while vs do-while
while do – while
A while loop is an entry controlled loop – A do-while loop is an exit control loop – it tests for a
it tests for a condition prior to running a condition after running a block of code.
block of code
A while loop runs zero or more times A do-while loop runs once or more times but at least once
Body of loop may never be executed
The variables in the test condition must Body of loop is executed at least once It is not necessary
be initialized prior to entering the loop to initialize the variables in the test condition prior to
structure. entering the loop structure.
}
Que 25. What is the Syntax errors?
Answer: Errors occurs due to not writing statement as per syntax rules, called syntax errors.
For example:
number = 1;
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 8/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
do{
System.out.println(“”+number);
++number;
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 9/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
Interior Designing
Open
Institute
IIFT Bhopal
Que 29. Write a java program to print the squares of numbers from 1 to 10, using for loop.
Answer:
public class PrintSqrNumber1to10 {
public static void main (String[ ] args) {
}
}
Que 30. Write a java program to print the numbers from 10 to 1 using for loop.
Answer:
}
}
}
Que 31. Write a java program to print the table of 5 using for loop.
Answer:
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 10/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
System.out.print (number);
}
}
}
Que 32. Write a java program to print the all the even numbers from 11 to 30 .
Answer:
public class EvenNumbers {
public static void main (String[ ] args) {
}
}
Que 33. Find the output of the following code. How many times will the loop be executed?
for (int count = 1; count <= 10; count = count + 2)
{
System.out.println (count);
}
Answer: It print all odd numbers from 1 to 10. The ouptu is
1
3
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 11/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
5
7
9
System.out.println(count);
}
Answer: The output is
5
6
7
8
9
Que 35. How many times following loop will iterate? Also gives it output.
for ( int count = 5; count <=10; count++)
{
System.out.println(count);
}
7
8
9
10
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 12/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
You can use multiple items in the initialization and the updation part of the loop by separating
them with the comma operator.
All parts – initialization, test condition, and updation statement are optional. Any part can be
ignored.
For example:
int x, y;
{
System.out.println(“”+x);
x–;
}
Que 37. Break statement is used to [CBSE QP]
Answer: d. 3 only
Que 38. Find the output of the following code: [CBSE 2019]
public class WhileDemo
{
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 13/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
System.out.println(‘‘=’’ + number*number);
++number;
}
}
}
Answer: Output of the above code is
Square of 1 = 1
Square of 2 = 4
Square of 3 = 9
Square of 4 = 16
Square of 5 = 25
Que 39. What is use of ++ operator in the above code (Q No 36) ? [CBSE 2019]
Answer: The ++ operator in the above code is use to increase the value of number by 1. It is a pre-
increment operator.
Que 40. Why are loops used in programming? What will be the output of the following loop and
why?
int x = 5;
while (x == 5)
{
System.out.println(“In the loop”);
}
Answer: Loops are used in programming to execute set of statements repeatedly till the condition is true.
Output is:
In the loop
In the loop
..
..
As well as this loop does not contains the update statement, so the value of x does not change in loop
iteration.
Que 41. Rewrite the following loop using for loop?
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 14/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
int x = 5;
while (x <= 50)
{
do {
System.out.println(“In the loop”);
x = x + 3;
} while (x <= 50);
Que 43. What is the output of the following code? Give reason also.
for (int count = 5; count <= 1; count++)
System.out.prinln(“Hello”);
Answer: No output.
Reason: Initial value is greater than the limit value and the loop increment is positive. In this case, body of
the loop will never be executed.
Que 44. What is the output of the following code? Give reason also.
for (int count = 1; count >= 5; count–)
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 15/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
System.out.prinln(“Hello”);
Answer: No output.
Reason: Initial value is lesser than the limit value and the loop increment is negative.
System.out.prinln(“Hello”);
}
Answer: Hello
Reason: The statements within the curly braces will be executed only once (after the for loop terminates)
and not as many times as expected. Because loop is terminated due to placing a semicolon at the end of a
for statement.
Que 46. What do you mean by scope of a variable?
Answer: The scope of variable tells about the visibility of the variable. If any variable is declared inside the
block, then it is not visible outside the block.
For example:
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 16/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
{
System.out.println(count * number);
}
Answer: Output is
10
20
30
40
50
Que 49. Predict the output generated by the following code Categorize the loop into entry control
or exit control.
int x =10, b=30;
while (x<=40) {
x += b;
b-=2;
System.out.println(“ ”+x);
}
Answer:
40
68
jTextField1.settext(“”+x);
Answer:
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 17/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
int n = 15;
int i = 10, x = 1;
do {
x = x * i ;
i++;
}while(i <= n) ;
jTextField1.setText(“”+x);
p = p – 2;
System.out.displayln(p);
} while p >= 2
Answer:
int p;
p = 14;
do {
p = p-2;
System.out.println(p);
Que 53. Predict the output of the following code; Also categorize the loop as Entry / Exit Control
Loop.
s = s + j;
j=j+j%3;
}
System.out.println(“=” +s);
Answer: 1+2+4+5+7+8+=27
Entry Control Loop
Que 54. Find the error and give correct code
int number = 1;
while (number <= 5) {
System.out.print(“Square of ” + number);
System.out.println(” = ” + number*number);
}
Answer: This loop will run forever since number never changes, it remains at 1.
Correct code
int number = 1;
while (number <= 5) {
System.out.print(“Square of ” + number);
System.out.println((” = ” + number*number);
number++;
}
Que 55. Predict the output of the following, if the value of N = 19 is passed as an argument to it.
static void method1(int N){
int x, y, flg;
for(x = 1; x < N; x++){
if(x == 1 || x == 0)
continue;
flg = 1;
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 19/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
break;
}
}
if(flg == 1)
System.out.println(x+””);
}
}
Answer: Output of the given code, if N = 19 then, is
2
3
5
7
11
13
17
Related Posts
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 20/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 21/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 22/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 23/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 24/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 25/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 26/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
Recent Posts
Class 10 Employability Skills Unit 2 Self Management Skills – II Study Material Notes
Class 9 Employability Skills Question Answer based on NCERT Textbook
Class 9 Employability Skills Multiple Choice Questions MCQ based on NCERT Book
Class 9 Employability Skills NCERT Textbook Solution
Class 9 Employability Skills Unit 5 Green Skills NCERT Book Solution
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 27/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
Nissan Magnite
GEZA @7.39L
This June, bring home
the India inspired SUV
Nissan Magnite
GEZA. Click to book
now @7.39L
Nissan India
Contact Us
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 28/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
Nissan Magnite
GEZA @7.39L
This June, bring home
the India inspired SUV
Nissan Magnite
GEZA. Click to book
now @7.39L
Nissan India
Contact Us
Search …
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 29/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
MyCSTutorial is working for free education for all. We are try to provide educational aids free of cost,
study material, sample papers and so many other things also.
Our only moto to help the students without any trouble or cost.
Follow Us
Quick Links
Computer Science
Informatics Practices
Information Technology
Artificial Intelligence
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 30/31
6/26/23, 3:12 PM Loop (while, for, do-while) in Java Question Answer - MyCSTutorial.in | The Path to Success in Exam...
Contact Info
Phone
+91 8221909045
Email
info.mycstutorial@gmail.com
https://mycstutorial.in/loop-while-for-do-while-in-java-question-answer/ 31/31