DefinitionModerate2 marks
What is an infinite loop? Give an example.
Topic: Infinite loops
Previous-year question practice database
Answer
Exam answer
An infinite loop is a loop whose terminating condition never becomes false, so it goes on executing endlessly until the program is stopped by force.
Example: for(int i = 1; i > 0; i++) System.out.println(i); Here i only ever increases, so the condition i > 0 stays true forever.
Explanation
Define it, then show one. Any correct example is accepted — while(true), a for loop with no update, or a condition that can never fail. The instructive kind is the one that looks correct: for(int i = 5; i != 0; i -= 2) steps 5, 3, 1, -1, -3 ... and skips straight past 0, so the test i != 0 never holds. That is why != is risky whenever the counter changes by more than 1 — worth adding as your example if you want to show real understanding.
More from Revision Tour II
Read the if program segment given below:
if(a > b)
z = 25;
else
z = 35;
Which one of the following is the correct conversion of the if program segment to ternary?2026The earth spins on its axis completing one rotation in a day. The earth revolves around the sun in 365 days to complete one revolution. What is the Java concept depicted in the given picture?2026Rewrite the following program segment using a for loop.
int a = 5, b = 10;
while(b > 0)
{
b -= 2;
}
System.out.println(a * b);2026Users must be above 10 years to open a self-operated bank account. Write this logic using a ternary operator and store the result (the eligibility message) in a String variable named idStatus and print it.2026Give the output of the following program segment and mention how many times the loop is executed.
int K = 1;
do
{
K += 2;
System.out.println(K);
} while(K <= 6);2026Write a program to accept a number and check if it is a Mark number or not. A number is said to be Mark when the sum of the squares of each digit is an even number as well as the last digit of the sum and the last digit of the number given is the same.
Example: n = 246
sum = 2 x 2 + 4 x 4 + 6 x 6 = 56
56 is an even number as well as last digit is 6 for both sum as well as the number.2026