= 0) pr"> = 0) pr">
logo

CuriousTab

CuriousTab

Discussion


Home C Programming Control Instructions Comments

  • Question
  • What will be the output of the program?
    #include<stdio.h>
    int main()
    {
        int i = 5;
        while(i-- >= 0)
            printf("%d,", i);
        i = 5;
        printf("\n");
        while(i-- >= 0)
            printf("%i,", i);
        while(i-- >= 0)
            printf("%d,", i);
        return 0;
    }
    


  • Options
  • A. 4, 3, 2, 1, 0, -1
    4, 3, 2, 1, 0, -1
  • B. 5, 4, 3, 2, 1, 0
    5, 4, 3, 2, 1, 0
  • C. Error
  • D. 5, 4, 3, 2, 1, 0
    5, 4, 3, 2, 1, 0
    5, 4, 3, 2, 1, 0

  • Correct Answer
  • 4, 3, 2, 1, 0, -1
    4, 3, 2, 1, 0, -1 

    Explanation
    Step 1: Initially the value of variable i is '5'.
    Loop 1: while(i-- >= 0) here i = 5, this statement becomes while(5-- >= 0) Hence the while condition is satisfied and it prints '4'. (variable 'i' is decremented by '1'(one) in previous while condition)
    Loop 2: while(i-- >= 0) here i = 4, this statement becomes while(4-- >= 0) Hence the while condition is satisfied and it prints '3'. (variable 'i' is decremented by '1'(one) in previous while condition)
    Loop 3: while(i-- >= 0) here i = 3, this statement becomes while(3-- >= 0) Hence the while condition is satisfied and it prints '2'. (variable 'i' is decremented by '1'(one) in previous while condition)
    Loop 4: while(i-- >= 0) here i = 2, this statement becomes while(2-- >= 0) Hence the while condition is satisfied and it prints '1'. (variable 'i' is decremented by '1'(one) in previous while condition)
    Loop 5: while(i-- >= 0) here i = 1, this statement becomes while(1-- >= 0) Hence the while condition is satisfied and it prints '0'. (variable 'i' is decremented by '1'(one) in previous while condition)
    Loop 6: while(i-- >= 0) here i = 0, this statement becomes while(0-- >= 0) Hence the while condition is satisfied and it prints '-1'. (variable 'i' is decremented by '1'(one) in previous while condition)
    Loop 7: while(i-- >= 0) here i = -1, this statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits.
    The output of first while loop is 4,3,2,1,0,-1

    Step 2: Then the value of variable i is initialized to '5' Then it prints a new line character(\n).
    See the above Loop 1 to Loop 7 .
    The output of second while loop is 4,3,2,1,0,-1

    Step 3: The third while loop, while(i-- >= 0) here i = -1(because the variable 'i' is decremented to '-1' by previous while loop and it never initialized.). This statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits.

    Hence the output of the program is
    4,3,2,1,0,-1
    4,3,2,1,0,-1


    Control Instructions problems


    Search Results


    • 1. What will be the output of the program?
      #include<stdio.h>
      int main()
      {
          int i=0;
          for(; i<=5; i++);
              printf("%d", i);
          return 0;
      }
      

    • Options
    • A. 0, 1, 2, 3, 4, 5
    • B. 5
    • C. 1, 2, 3, 4
    • D. 6
    • Discuss
    • 2. What will be the output of the program?
      #include<stdio.h>
      int main()
      {
          float a = 0.7;
          if(0.7 > a)
              printf("Hi\n");
          else
              printf("Hello\n");
          return 0;
      }
      

    • Options
    • A. Hi
    • B. Hello
    • C. Hi Hello
    • D. None of above
    • Discuss
    • 3. What will be the output of the program?
      #include<stdio.h>
      int main()
      {
          unsigned int i = 65535; /* Assume 2 byte integer*/
          while(i++ != 0)
              printf("%d",++i);
          printf("\n");
          return 0;
      }
      

    • Options
    • A. Infinite loop
    • B. 0 1 2 ... 65535
    • C. 0 1 2 ... 32767 - 32766 -32765 -1 0
    • D. No output
    • Discuss
    • 4. What will be the output of the program?
      #include<stdio.h>
      int main()
      {
          int x = 10, y = 20;
          if(!(!x) && x)
              printf("x = %d\n", x);
          else
              printf("y = %d\n", y);
          return 0;
      }
      

    • Options
    • A. y =20
    • B. x = 0
    • C. x = 10
    • D. x = 1
    • Discuss
    • 5. What will be the output of the program?
      #include<stdio.h>
      int main()
      {
          unsigned int i = 65536; /* Assume 2 byte integer*/
          while(i != 0)
              printf("%d",++i);
          printf("\n");
          return 0;
      }
      

    • Options
    • A. Infinite loop
    • B. 0 1 2 ... 65535
    • C. 0 1 2 ... 32767 - 32766 -32765 -1 0
    • D. No output
    • Discuss
    • 6. What will be the output of the program?
      #include<stdio.h>
      int main()
      {
          int i = 1;
          switch(i)
          {
              printf("Hello\n");
              case 1:
                  printf("Hi\n");
                  break;
              case 2:
                  printf("\nBye\n");
                  break;
          }
          return 0;
      }
      

    • Options
    • A. Hello
      Hi
    • B. Hello
      Bye
    • C. Hi
    • D. Bye
    • Discuss
    • 7. What will be the output of the program?
      #include<stdio.h>
      int main()
      {
          int k, num = 30;
          k = (num < 10)? 100 : 200;
          printf("%d\n", num);
          return 0;
      }
      

    • Options
    • A. 200
    • B. 30
    • C. 100
    • D. 500
    • Discuss
    • 8. What will be the output of the program?
      #include<stdio.h>
      int main()
      {
          int a=0, b=1, c=3;
          *((a)? &b : &a) = a? b : c;
          printf("%d, %d, %d\n", a, b, c);
          return 0;
      }
      

    • Options
    • A. 0, 1, 3
    • B. 1, 2, 3
    • C. 3, 1, 3
    • D. 1, 3, 1
    • Discuss
    • 9. What will be the output of the program?
      #include<stdio.h>
      int main()
      {
          int x = 3;
          float y = 3.0;
          if(x == y)
              printf("x and y are equal");
          else
              printf("x and y are not equal");
          return 0;
      }
      

    • Options
    • A. x and y are equal
    • B. x and y are not equal
    • C. Unpredictable
    • D. No output
    • Discuss
    • 10. What will be the output of the program?
      #include<stdio.h>
      int main()
      {
          int i=4;
          switch(i)
          {
              default:
                 printf("This is default\n");
              case 1:
                 printf("This is case 1\n");
                 break;
              case 2:
                 printf("This is case 2\n");
                 break;
              case 3:
                 printf("This is case 3\n");
          }
          return 0;
      }
      

    • Options
    • A. This is default
      This is case 1
    • B. This is case 3
      This is default
    • C. This is case 1
      This is case 3
    • D. This is default
    • Discuss


    Comments

    There are no comments.

Enter a new Comment