Quiz
Q1
public class ForLoopDemo {
public static void main(String[] args) {
int i = 0;
for (; i<10; i++){
System.out.println(i);
}
}
}
The for loop shown above is not valid as it does not have the initialization block.
Q2
public class ForLoopDemo {
public static void main(String[] args) {
for (int i=0; i<10;){
System.out.println(i);
i++;
}
}
}
The for loop shown above is not valid as it has no update block.
Q3
public class ForLoopDemo {
public static void main(String[] args) {
for (int x=0; x<10;){
System.out.println(x);
x++;
}
}
}
The for loop shown above is not valid as it has 'int x= 0' instead of 'int i = 0'
Q4
If the conditional is false, the do-while loop still executes the body of the loop once.
Q5
public class InfiniteWhileLoopDemo {
public static void main(String[] args) {
while (true) {
// loop body statements go here
}
}
}
The above while loop should have a conditional break statement in the body of the loop, otherwise it will become a run away infinite loop.
Q6
public class VariablesExample {
public static void main(String[] args) {
int a = 0;
while (a < 10) {
System.out.println(a+2);
}
System.out.println(a);
}
}
When you compile and run the above program, the last System.out.println statement will print the value of 'a' as..
Q7
The expression `a += 2` is same as
Q8
class NumbersAdder {
public static void main(String[] args) {
for (int i = 0; i <= 10; i++) {
int sum = 0;
sum = sum + i;
}
System.out.println(sum);
}
}
What is the result of the above code compilation and execution?
Q9
public class VariablesExample2 {
public static void main(String[] args) {
int a = 0;
while (a < 10) {
a+2;
}
System.out.println(a);
}
}
What is the output from this program?