Loop Control


  1. What is the output of this program?

    public class Result
    {
    public static void main(String args[])
    {
    int p = 10;
    int q = 20;
    first:
    {
    second:
    {
    third:
    {
    if (p == q >> 4)
    break second;
    }
    System.out.println(p);
    }
    System.out.println(q);
    }
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Ans: 10
    20


  1. What is the output of this program?

    public class Result
    {
    public static void main(String args[])
    {
    final int p=10, q=20;

    while(p>q)
    {
    System.out.println("Hello");
    }
    System.out.println("Java");
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Every final variable is compile time constant.



  1. What is the output of this program?

    public class comma_operator
    {
    public static void main(String args[])
    {
    int sum = 0;
    for (int k = 0, L = 0; k < 10 & L < 10; ++k, L = k + 1)
    sum += k;
    System.out.println(sum);
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    Using comma operator , we can include more than one statement in the initialization and iteration portion of the for loop. Therefore both ++k and L = k + 1 is executed
    output: 36


  1. Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    do-while loops will execute the body of loop even when condition controlling the loop is initially false