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

CuriousTab

CuriousTab

Discussion


Home C Programming Control Instructions See What Others Are Saying!
  • 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


    More questions

    • 1. What will be the output of the program?
      #include<stdio.h>
      int X=40;
      int main()
      {
          int X=20;
          printf("%d\n", X);
          return 0;
      }
      

    • Options
    • A. 20
    • B. 40
    • C. Error
    • D. No Output
    • Discuss
    • 2. What will be the output of the program (myprog.c) given below if it is executed from the command line?
      cmd> myprog one two three
      /* myprog.c */
      #include<stdio.h>
      #include<stdlib.h>
      
      int main(int argc, char **argv)
      {
          int i;
          for(i=1; i<=3; i++)
              printf("%u\n", &argv[i]);
          return 0;
      }
      
      If the first value printed by the above program is 65517, what will be the rest of output?


    • Options
    • A. 65525 65531
    • B. 65519 65521
    • C. 65517 65517
    • D. 65521 65525
    • Discuss
    • 3. What will be the output of the program?
      #include<stdio.h>
      #include<stdlib.h>
      
      int main()
      {
          int *p;
          p = (int *)malloc(20); /* Assume p has address of 1314 */
          free(p);
          printf("%u", p);
          return 0;
      }
      

    • Options
    • A. 1314
    • B. Garbage value
    • C. 1316
    • D. Random address
    • Discuss
    • 4. Size of short integer and long integer would vary from one platform to another.

    • Options
    • A. True
    • B. False
    • Discuss
    • 5. Names of functions in two different files linked together must be unique

    • Options
    • A. True
    • B. False
    • Discuss
    • 6. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
          int *p, *q;
          p = &arr[1][1][1];
          q = (int*) arr;
          printf("%d, %d\n", *p, *q);
          return 0;
      }
      

    • Options
    • A. 8, 10
    • B. 10, 2
    • C. 8, 1
    • D. Garbage values
    • Discuss
    • 7. The macro va_arg is used to extract an argument from the variable argument list and advance the pointer to the next argument.

    • Options
    • A. True
    • B. False
    • Discuss
    • 8. What will be the output of the program in Turbo C?
      #include<stdio.h>
      
      int main()
      {
          char near *near *ptr1;
          char near *far *ptr2;
          char near *huge *ptr3;
          printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));
          return 0;
      }
      

    • Options
    • A. 4, 4, 8
    • B. 4, 4, 4
    • C. 2, 4, 8
    • D. 2, 4, 4
    • Discuss
    • 9. What will be the output of the program (sample.c) given below if it is executed from the command line?
      cmd> sample 1 2 3
      cmd> sample 2 2 3
      cmd> sample 3 2 3
      /* sample.c */
      #include<stdio.h>
      
      int main(int argc, char *argv[])
      {
          printf("%s\n", argv[0]);
          return 0;
      }
      

    • Options
    • A. sample 3 2 3
    • B. sample 1 2 3
    • C. sample
    • D. Error
    • Discuss
    • 10. It is necessary to call the macro va_end if va_start is called in the function.

    • Options
    • A. Yes
    • B. No
    • Discuss


    Comments

    There are no comments.

Enter a new Comment