logo

CuriousTab

CuriousTab

Discussion


Home C Programming Control Instructions Comments

  • Question
  • 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

  • Correct Answer
  • Infinite loop 

    Explanation
    Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535.

    Step 1:unsigned int i = 65535;

    Step 2:
    Loop 1: while(i++ != 0) this statement becomes while(65535 != 0). Hence the while(TRUE) condition is satisfied. Then the printf("%d", ++i); prints '1'(variable 'i' is already incremented by '1' in while statement and now incremented by '1' in printf statement) Loop 2: while(i++ != 0) this statement becomes while(1 != 0). Hence the while(TRUE) condition is satisfied. Then the printf("%d", ++i); prints '3'(variable 'i' is already incremented by '1' in while statement and now incremented by '1' in printf statement)
    ....
    ....

    The while loop will never stops executing, because variable i will never become '0'(zero). Hence it is an 'Infinite loop'.


    Control Instructions problems


    Search Results


    • 1. 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
    • 2. 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
    • 3. What will be the output of the program, if a short int is 2 bytes wide?
      #include<stdio.h>
      int main()
      {
          short int i = 0;
          for(i<=5 && i>=-1; ++i; i>0)
              printf("%u,", i);
          return 0;
      }
      

    • Options
    • A. 1 ... 65535
    • B. Expression syntax error
    • C. No output
    • D. 0, 1, 2, 3, 4, 5
    • Discuss
    • 4. What will be the output of the program?
      #include<stdio.h>
      int main()
      {
          char ch;
          if(ch = printf(""))
              printf("It matters\n");
          else
              printf("It doesn't matters\n");
          return 0;
      }
      

    • Options
    • A. It matters
    • B. It doesn't matters
    • C. matters
    • D. No output
    • Discuss
    • 5. What will be the output of the program?
      #include<stdio.h>
      int main()
      {
          int x, y, z;
          x=y=z=1;
          z = ++x || ++y && ++z;
          printf("x=%d, y=%d, z=%d\n", x, y, z);
          return 0;
      }
      

    • Options
    • A. x=2, y=1, z=1
    • B. x=2, y=2, z=1
    • C. x=2, y=2, z=2
    • D. x=1, y=2, z=1
    • Discuss
    • 6. 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
    • 7. 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
    • 8. 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
    • Discuss
    • 9. 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
    • 10. 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


    Comments

    There are no comments.

Enter a new Comment