logo

CuriousTab

CuriousTab

Discussion


Home C Programming Functions See What Others Are Saying!
  • Question
  • What will be the output of the program?
    #include<stdio.h>
    int reverse(int);
    
    int main()
    {
        int no=5;
        reverse(no);
        return 0;
    }
    int reverse(int no)
    {
        if(no == 0)
            return 0;
        else
            printf("%d,", no);
        reverse (no--);
    }
    


  • Options
  • A. Print 5, 4, 3, 2, 1
  • B. Print 1, 2, 3, 4, 5
  • C. Print 5, 4, 3, 2, 1, 0
  • D. Infinite loop

  • Correct Answer
  • Infinite loop 

    Explanation
    Step 1: int no=5; The variable no is declared as integer type and initialized to 5.

    Step 2: reverse(no); becomes reverse(5); It calls the function reverse() with '5' as parameter.

    The function reverse accept an integer number 5 and it returns '0'(zero) if(5 == 0) if the given number is '0'(zero) or else printf("%d,", no); it prints that number 5 and calls the function reverse(5);.

    The function runs infinetely because the there is a post-decrement operator is used. It will not decrease the value of 'n' before calling the reverse() function. So, it calls reverse(5) infinitely.

    Note: If we use pre-decrement operator like reverse(--n), then the output will be 5, 4, 3, 2, 1. Because before calling the function, it decrements the value of 'n'.


    More questions

    • 1. Preprocessor directive #undef can be used only on a macro that has been #define earlier

    • Options
    • A. True
    • B. False
    • Discuss
    • 2. We want to round off x, a float, to an int value, The correct way to do is

    • Options
    • A. y = (int)(x + 0.5)
    • B. y = int(x + 0.5)
    • C. y = (int)x + 0.5
    • D. y = (int)((int)x + 0.5)
    • Discuss
    • 3. In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). What will str contain?

    • Options
    • A. "I am a boy\r\n\0"
    • B. "I am a boy\r\0"
    • C. "I am a boy\n\0"
    • D. "I am a boy"
    • Discuss
    • 4. What will be the output of the program in 16 bit platform (Turbo C under DOS)?
      #include<stdio.h>
      int main()
      {
          extern int i;
          i = 20;
          printf("%d\n", sizeof(i));
          return 0;
      }
      

    • Options
    • A. 2
    • B. 4
    • C. vary from compiler
    • D. Linker Error : Undefined symbol 'i'
    • Discuss
    • 5. Which of the following operations can be performed on the file "NOTES.TXT" using the below code?
      FILE *fp;
      fp = fopen("NOTES.TXT", "r+");
      

    • Options
    • A. Reading
    • B. Writing
    • C. Appending
    • D. Read and Write
    • Discuss
    • 6. In the following program how long will the for loop get executed?
      #include<stdio.h>
      int main()
      {
          int i=5;
          for(;scanf("%s", &i); printf("%d\n", i));
          return 0;
      }
      

    • Options
    • A. The for loop would not get executed at all
    • B. The for loop would get executed only once
    • C. The for loop would get executed 5 times
    • D. The for loop would get executed infinite times
    • Discuss
    • 7. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          int y=128;
          const int x=y;
          printf("%d\n", x);
          return 0;
      }
      

    • Options
    • A. 128
    • B. Garbage value
    • C. Error
    • D. 0
    • Discuss
    • 8. When we mention the prototype of a function?

    • Options
    • A. Defining
    • B. Declaring
    • C. Prototyping
    • D. Calling
    • Discuss
    • 9. What will be the output of the program?
      #include<stdio.h>
      #define PRINT(i) printf("%d,",i)
      
      int main()
      {
          int x=2, y=3, z=4;
          PRINT(x);
          PRINT(y);
          PRINT(z);
          return 0;
      }
      

    • Options
    • A. 2, 3, 4,
    • B. 2, 2, 2,
    • C. 3, 3, 3,
    • D. 4, 4, 4,
    • Discuss
    • 10. What will be the output of the program
      #include<stdio.h>
      void fun(int);
      
      int main(int argc)
      {
          printf("%d ", argc);
          fun(argc);
          return 0;
      }
      void fun(int i)
      {
          if(i!=4)
              main(++i);
      }
      

    • Options
    • A. 1 2 3
    • B. 1 2 3 4
    • C. 2 3 4
    • D. 1
    • Discuss


    Comments

    There are no comments.

Enter a new Comment