Font size
WorksheetsFSD Looping
Total questions: 107
Worksheet time: 4hrs 34mins
What is the output?
5 7 6
5 6 7
6 6 6
5 7 7
What is the output?
Compile Time Error
Run Time Error
Hai
No Hai
Output?
Compilation Error
Run time error
strings are equal
not equal
abd
cd
Compile time error
ab
abd
Compile time error
ab
cd
1
2
1 2
Run time error
1
Hi 2
Run time error
2
7
8
Compile time error
run time error
Select different parts of "for" looping statement-
Initialization part
Condition Part
Update part
All of these
What will be the output of following code- for(int i = 1; i < 5; i++ )
{
System.out.print(i);
}
13
135
1234
12345
_________ in programming come into use when we need to repeatedly execute a block of statements.
nested for loop
none
for loop
loop
The __________ statement uses single expression for multiple choices
loop
switch
multiple value
if else
FOR loops are
loops which run an unknown number of times
loops which run for a specific number of times
the same as if statements
not part of programming
In a Python program, what would be the ending value for y in the WHILE LOOP code?
5
51
10
3
This statement is used to terminate the execution of the loop
break
jump
goto
continue
Predict the output.
320 11 330
320 10 330
330 11 320
Predict the output
5
Empty Loop
No Answer
1234
Read the program carefully and give your output
Welcome Child!!!
Error
Welcome Child!!! Welcome Child!!!Welcome Child!!!
Identify the iteration statments from the following:
for
switch
if
while
do while
int i=1;
while(i<5)
{ cout<<i;i++; }
How many times the above loop will be executed?
5
1
Will not be executed at all
4
RABBIT
RABBIT is printed unlimited number of times.
No output
Compiler error
GREEN
RABBIT GREEN
RABBIT is printed unlimited number of times.
Compiler error.
No Output
32 33 34
32 33 34 35
Compiler error
compiler error
10 10 10 10 10
11 12 13 14 15
None of the above
Write the output : -
int a;
for(a=1;i<=5;a++);
System.out.println(a); System.out.println(a);
6
6
1
2
3
4
5
6
6
Compile time error
Runtime error
How many times 'Hello' is printed?
public class Test1 {
public static void main(){
for(int i = 0; i<5; i++) {
System.out.println("Hello");
break;
}
}
}
1
4
5
6
What will be the output of the following program?
class Test2 {
public static void main(String s[]) {
for(int i = 0; i <= 5; i++ ) {
System.out.println("i = " + i );
}
System.out.println("i after the loop = " + i );
}
}
i=0
i=1
i=2
i=3
i=4
i after the loop=5
i=0
i=1
i=2
i=3
i=4
i=5
i after the loop=6
i=0
i=1
i=2
i=3
i=4
i=5
i after the loop=5
0=0
1=1
2=2
3=3
4=4
5=5
6 after the loop=6
What will be the output of the following program?
class ForSample
{
public static void main(String s[])
{
int i = 0;
for(;i <= 5; i++ )
{
System.out.println("i = " + i );
}
System.out.println("i after the loop = " + i );
}
}
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i after the loop = 6
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
Compilation Errors
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
What will be the output of the following program?
class ForSample
{
public static void main(String s[])
{
for(int i = 10; i <= 5; i-- )
{
System.out.println("i = " + i );
}
}
}
i = 10
i = 9
i = 8
i = 7
i = 6
i = 10
i = 9
i = 8
i = 7
i = 6
i = 5
i = 4
i = 3
i = 2
i = 1
No output
What will be the output of the following program?
class Factorial
{
public static void main(String s[])
{
int number = 5;
int factorial = 1;
for(int i = 2; i <= number; i++ )
{
factorial *= factorial;
}
System.out.println("Factorial of 5 is " + factorial);
}
}
Factorial of 5 is 1
Factorial of 5 is 120
Compilation Errors
Factorial of 5 is 625
What will be the output of the following program?class MultiVariableFor{
public static void main(String s[]) {
int a, b;
for(a = 1, b = 4; a < b; a++, b-)
{
System.out.println("a = " + a);
System.out.println("b = " + b);
} }}
Compilation error since both a++ and b--
should not be present in the iteration section (i.e after the second semicolon)
a = 1
b = 4
a = 2
b = 3
a = 3
b = 2
a = 1
b = 4
a = 2
b = 3
Compilation error since both a = 1 and b = 4
should not be present in the initialization section (i.e before the first semicolon)
A while loop carries on as long as...
The condition in the brackets is true
The condition in the brackets is false
i < 10
the for loop has not ended
How many stars are printed?
7
3
4
5
Infinite Loop
In Given syntax, condition refers to ________ expression
int
char
boolean
None
Determine the output of this Java program.
(a)
In programming, what is iteration?
The repetition of steps within a program
The order in which instructions are carried out
A decision point in a program
Testing a program to make sure it works
Which of the following is considered a pre-test loop?
do while
if-while
while
if-else
What is one main reason that we use loops in java?
To make us suffer
To help us make decisions
To help us complete repetitive tasks
To make java make the entire program for us.
What operation should be in the blue space to print out the word Hello from the String defined as word?
(a)
How many times will the following code print the I'm Hungry? Please insert in numerical format
(a)
How many times will a do-while loop execute?
One time
Zero or more times
One or more times
Zero
When should you use a for loop?
if I want to check a condition every second
if the number iteration is fixed
if i want to run it once then check condition
if i am unsure of how many times a piece of code will run
What is the output of the code above?
Error
1 2 3 4 5 6
1 2 4 9 12 15
1 2 4 8 16 32
What will be the output of the code?
ERROR
5 4 3 2 1 Blast off
5 Blast off 4 Blast off 3 Blast off
2 Blast off
1 Blast off
Blast off
How many times will the following loop print Hello?
12
6
15
Infinite
What relational operator should go in the highlighted space in order to print Hello World! five times?
(a)
Which of the following can a break statement be used in? SELECT ALL THAT APPLY
Do-while loop
for loop
if-else block
Switch Statement
A loop that runs forever and can only be stopped by terminating the program is referred to as a...
Run-on loop
Nested Loop
Terminal loop
Infinite loop
Simulation of code execution in which you step through instructions and track the values of the variables is referred as...
Tracing
Test Cases
Unit Testing
Pseudocode
Which of the following correctly lists the elements (from Left to right) that need to be in the parenthesis of a For loop?
Declare, Condition, Increment/Decrement
Condition, Initialization, Looping times
Initialize, Condition, Increment/Decrement
Increment/Decrement, condition, intialize
A pre-test loop that will run until condition is false is known as the (a) Loop (amount of iterations is unknown)
How many times will the inner for loop iterate?
(a)
What is output by the code? Pretend it says System.out.println
2
3
4
5
6
What will the following program segment display?
for(int a = 2; a <= 10; a += 3)
System.out.print(a+””);
2 5 8
2 5
2 5 8 11
2
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
int x = 0;
while (x < 4) {
x = x + 1;
}
System.out.println("x is " + x);
Repeating a task multiple times in programming is known as ?
Iterators
Scope
Looping
Which loop loops a set number of times?
for
while
infinite
loopy
Which of these is the correct syntax for a loop?
for(int i = 0; i<10; 1++){}
fore(integer i = 0; i>10; i++){}
for(int i = 0; i<10){}
for(int i){}
Which of the following is the correct way to create a String?
String s = "I am cool";
string string = i am cool;
String s = I am cool
String s = "I" am cool;
Which of the following is a type of loop in Java?
loop
while
iteration
i
What does the following loop print?
for(int i = 1; i<3; i++)
{ System.out.print(i); }
12
012
0123
123
What will the following loop print?
int i = 0;
while (i>0)
{ System.out.println(i); }
Infinite loop
012345678910
0
Nothing
What will the following loop print?
int i = 1;
while (i>0)
{ System.out.println(i); }
012345678910
nothing
infinite loop
0
What does the following loop print?
for(i = i +1; i<5; int i = 0;)
{ System.out.println(i) }
01234
12345
error
0
Which of the following includes the correct syntax for a for loop?
for(int i = 0, i<10, i++)
for(int i =0, run = 10)
for(int i = 0; run = 10)
for(int i = 0; i < 10; i++)
for(int i; i<10; i++) {} Why won’t this code segment run?
There is a missing semicolon at the end.
For should be capitalized
The person did not set the beginning value of i.
The person did not set the ending value of i.
What does this loop do?
There is an error; it won’t run.
It prints all the odd numbers from 0 - 20 inclusive on both ends.
It prints all the even numbers from 0 - 20 inclusive on both ends.
It prints all the even numbers from 0 - 20 inclusive with 0 and exclusive with 20.
What loop should you use if you know how many times you are going to loop?
For loop
Do while loop
While loop
No loop
for( int i = 0; i < 10; i++)
out.println("hello");
int start, qti = 0;
for(int i = start; i < 5; i++)
qti++;
for(int i = 0; i <= 3; i++)
out. print("sup");
If a loop condition is never satisfied then the loop does not execute.
for(int i = 2; i < 11; i++)
out. print("foo");
for(int i = 0; i < 3; i++)
out. print("foo");
for(int i = 0; i < 10; i = i + 3)
out. print("foo");
for(int i = 2; i < 9; i = i + 3)
out. print("foo");
for(int i = 0; i < 9; i = i + 3)
out. print("foo");
for(int i = 0; i > 9; i = i + 3)
out. print("foo");
A ____ structure in a Programming language allows for repetitive execution of code.
procedural
loop
conditional
none of the mentioned
What type of loops is/are available in Java?
for loop
while loop
both for and while loops
neither for nor while loop
State true or false. The correct syntax for a for loop in Java is:
for (initialization expression; test expression; update expression)
{
// body of the loop
// statements we want to execute
}
True
False
How many times will “Hello World” be printed when the following code is executed?
for (int i = 1; i <= 5; i++)
System.out.println("Hello World");
4 times
5 times
0 times
Error
What will be the output of the following code?
int sum = 0;
for (int x = 1; x <= 5; x++) {
sum = sum + x;
}
System.out.println("Sum: " + sum);
15
5
10
Error
Which is the right syntax for the while loop?
WHILE bacon <= 5 { cout << "text"; }
while bacon <= 5 cout << "text";
while (bacon <= 5) { cout << "text"; }
Fill in the blanks.
(a)
เติมส่วนของโปรแกรมในช่องว่าง เพื่อเพิ่มค่าให้ตัวแปร bacon ครั้งละ 2 แล้วแสดงผลออกมาเรื่อยๆ
(a)
เติมส่วนของคำสั่ง เพื่อรับค่าจำนวนเต็ม 5 จำนวน
และหาผลรวมของจำนวนเหล่านั้น
โดยเก็บผลรวมไว้ในตัวแปร total
(a)
loop do...while ข้อใดถูกต้อง
do { } while (test);
doo while(test)
DO while test;
เติมส่วนของโปรแกรม เพื่อแสดงค่าของตัวแปร x 10 ครั้ง
(a)
How is a "do... while" loop different from a "while" loop?
while loop runs the code before testing the condition
do...while loop runs your code at least one time
do...while loop tests the condition before running the code
เติมส่วนของโปรแกรม เพื่อพิมพ์ข้อความ
"this is a loop" จำนวน 15 ครั้ง
(a)
Print i as long as i is less than 6.
(a)
Use the do/while loop to print i as long as i is less than 6.
(a)
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
int x = 0;
while (x < 4) {
x = x + 1;
}
System.out.println("x is " + x);
Repeating a task multiple times in programming is known as ?
Iterators
Scope
Looping
Which of the following is a type of loop in Java?
loop
while
iteration
i
What will the following loop print?
int i = 0;
while (i>0)
{ System.out.println(i); }
Infinite loop
012345678910
0
Nothing
The body of a while loop will run as long as the test condition is "false".
True
False
How many times will this loop body run?
0
infinitely many times
5
it depends on what the user inputs
int count = 0;
do {
System.out.println("Welcome to Java");
count++;
} while (count < 10);
Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?
do-while
while
for
none of the mentioned
What will this code display?
int i = 0;
while (i < 5) {
System.out.print (i + ", ");
i ++;
} ;
0, 0, 0, 0, 0, 0, 0, 0, (repeat forever)
0, 1, 2, 3, 4,
1, 2, 3, 4, 5,
Compile error
10,
