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.

true false It is not required for the 'for' loop to have an initilization 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.

true false It is not required for the 'for' loop to have an update block as long as the conditional section ends with a semicolon. However to ensure that you do not have an infinite loop which never ends, you have to ensure that the conditional becomes false after some interations through an update inside the loop body or you should add a break statement inside the loop body which ends the loop after some interations.

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'

true false You can name the variable used for representing number of interations what ever you want. While it is recommended that most other variables should have meaningful names, this is one of the few times where you are allowed to use typical single alphabets like i, n, m, x, y, z etc.. to represent the number of interations.

Q4

If the conditional is false, the do-while loop still executes the body of the loop once.

true false Yes, in a do-while loop, the conditional is evaulated after the loop body is executed once. This is not true in a while loop however. In a while loop the loop body is never executed if the conditional is false.

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.

true false Yes, although you can use while(true) construct, you should ensure that the loop ends after it hits a conditional which has a break statement which stops the loop from executing further.

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..

0 10 The program never reaches this statement 8 This results in an infinite loop because variable 'a' always remains at '0' as it is never updated and hence cannot get out of the loop as the while boolean condition always remain true.

Q7

The expression `a += 2` is same as

a = a+2 a + 2 a The expression 'a += 2' will update the value of a and hence 'a = a+2' is the correct answer

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?

Compilation Error 10 55 'sum' variable declared is inside the 'for' loop and so cannot be seen by System.out.println statement which is outside of the for loop code block. That is why you get compilation error.

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?

Prints out 0 Prints out 10 Compile Error Prints out 8 Since the expression 'a+2' is not assigned to any variable, it results in 'not a statement' compile error

results matching ""

    No results matching ""