0% found this document useful (0 votes)
30 views

Revision Lecture7

The document discusses loops and repetition statements in Java. It contains true/false and multiple choice questions about while loops, for loops, and do-while loops. Some key points covered are: - Java has three types of repetition statements - while, for, and do-while loops - Loops repeat a block of code an arbitrary number of times as long as a condition is met - The for loop contains three sections for initialization, condition, and increment/decrement

Uploaded by

amrahmed0159
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Revision Lecture7

The document discusses loops and repetition statements in Java. It contains true/false and multiple choice questions about while loops, for loops, and do-while loops. Some key points covered are: - Java has three types of repetition statements - while, for, and do-while loops - Loops repeat a block of code an arbitrary number of times as long as a condition is met - The for loop contains three sections for initialization, condition, and increment/decrement

Uploaded by

amrahmed0159
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lecture 7

T/F
1 Repetition statements cannot allow us to execute a statement or a False
block of statements multiple times
2 Often, repetition statements are referred to as loops True
3 Like conditional statements, repetition statements are controlled True
by boolean expressions
4 Java has two kinds of repetition statements; while and do-while False
repetition statements.
5 The programmer should choose the right kind of loop statement True
for the situation
6 In a while-loop, if the condition is true, then the statement(s) will True
be executed.
7 In a for-loop, the initialization section cannot be used to declare a False
variable.
8 Like a while loop, the condition of a for loop is tested prior to True
executing the loop body. Therefore, the body of a for loop will
execute zero or more times.
9 Every loop in Java has a condition that should be true in order to True
proceed for execution.
10 A while loop in Java executes the statements at least once even False
the condition is not satisfied.
11 An Increment operator "++" and/or a Decrement operator "--" are True
used along with a Loop-Counter variable in Java.
12 In a for1 loop, the Initialization-part, Condition-part and True
Increment/Decrement part can be empty.
13 A Loop in Java language can contain only one statement. False
MCQ
1. How many choices are possible when using a single if-else statement?
a. 1
b. 2
c. 3
d. 4
2. Say that value has a 19 stored in it, and that extra has a 25 stored in it.
Evaluate (to true or false) each of the following expressions:
value <= extra extra == value
a. true true
b. false true
c. true false
d. false false

Page 1 of 5
3. The Java if statement:
a. Evaluates whether an expression is equal or not
b. Evaluates whether an expression is true or false
c. Evaluates whether an expression is less than or more than a number
d. None of the above
4. 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
5. Choose a right Java Statement.
a. Loops or Repetition block executes a group of statements
repeatedly.
b. Loop is usually executed as long as a condition is met.
c. Loops usually take advantage of Loop Counter
d. All the above.
6. Loops in Java Language are implemented using:
a. While loop
b. For loop
c. Do-While loop
d. All the above
7. Which loop is faster in Java Language, for, while or Do While.?
a. for
b. while
c. do-while
d. All work at same speed
8. Choose the correct syntax of the while loop in Java below.
a.
while(condition)
{
//statements
}
b.
while{
//statements
}(condition)

Page 2 of 5
c.
while(condition));
{
//statements
}
d.
while()
{
if(condition)
{
//statements
}
}
9. 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. C)
for(increment-or-decrement; condition; initialization)
{
//statements
}
d. None
10.Choose a correct java for loop syntax.
a.
for(initialization; condition; increment-or-decrement)
{
//statements
}
b.

Page 3 of 5
for(declaration; condition; increment-or-decrement)
{
//statements
}
c.
for(declaration; increment-or-decrement; condition)
{
//statements
}
d.
for(initialization; condition; increment-or-decrement;);
{
//statements
}
11.What will be the output of the following code fragment?
int n = 5;
while( n > 0 )
{
System.out.print(n);
n--;
}
a. 12345
b. 54321
c. 4321
d. 543210
12.Choose the Java-Code below with a never-ending loop.
a. while(true);
b. for(;true;);
c. a) and b)
d. Nor a) or b)
13.What is the output of the below Java program?
int time=50;
while(time < 53)
{
System.out.print(time + ",");
time++;
}
a. 50,50,50,

Page 4 of 5
b. 50,51,52,
c. 51,52,53,
d. Compiler error
14.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
15.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
16.What will be the output of the following code fragment?
int i = 1;
int x = 1;
while(i < 4)
{
x += i;
i++;
}
System.out.println(x);
a. 6
b. 7
c. 8
d. 9

Page 5 of 5

You might also like