logo

CuriousTab

CuriousTab

Discussion


Home C Programming Strings See What Others Are Saying!
  • Question
  • What will be the output of the program?
    #include<stdio.h>
    void swap(char *, char *);
    
    int main()
    {
        char *pstr[2] = {"Hello", "CuriousTab"};
        swap(pstr[0], pstr[1]);
        printf("%s\n%s", pstr[0], pstr[1]);
        return 0;
    }
    void swap(char *t1, char *t2)
    {
        char *t;
        t=t1;
        t1=t2;
        t2=t;
    }
    


  • Options
  • A. CuriousTab
    Hello
  • B. Address of "Hello" and "CuriousTab"
  • C. Hello
    CuriousTab
  • D. Iello
    HndiaCURIOUSTAB

  • Correct Answer
  • Hello
    CuriousTab 

    Explanation
    Step 1: void swap(char *, char *); This prototype tells the compiler that the function swap accept two strings as arguments and it does not return anything.

    Step 2: char *pstr[2] = {"Hello", "CuriousTab"}; The variable pstr is declared as an pointer to the array of strings. It is initialized to

    pstr[0] = "Hello", pstr[1] = "CuriousTab"

    Step 3: swap(pstr[0], pstr[1]); The swap function is called by "call by value". Hence it does not affect the output of the program.

    If the swap function is "called by reference" it will affect the variable pstr.

    Step 4: printf("%s\n%s", pstr[0], pstr[1]); It prints the value of pstr[0] and pstr[1].

    Hence the output of the program is

    Hello
    CuriousTab


    More questions

    • 1. It is necessary to call the macro va_end if va_start is called in the function.

    • Options
    • A. Yes
    • B. No
    • Discuss
    • 2. Left shifting a number by 1 is always equivalent to multiplying it by 2.

    • Options
    • A. True
    • B. False
    • Discuss
    • 3. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          printf("%%%%\n");
          return 0;
      }
      

    • Options
    • A. %%%%%
    • B. %%
    • C. No output
    • D. Error
    • Discuss
    • 4. A function that receives variable number of arguments should use va_arg() to extract arguments from the variable argument list.

    • Options
    • A. True
    • B. False
    • Discuss
    • 5. 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
    • 6. What will be the output of the program (sample.c) given below if it is executed from the command line?
      cmd> sample friday tuesday sunday
      /* sample.c */
      #include<stdio.h>
      
      int main(int sizeofargv, char *argv[])
      {
          while(sizeofargv)
              printf("%s", argv[--sizeofargv]);
          return 0;
      }
      

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

    • Options
    • A. s
    • B. f
    • C. u
    • D. r
    • Discuss
    • 8. Point out the error in the program.
      #include<stdio.h>
      
      int main()
      {
          const int k=7;
          int *const q=&k;
          printf("%d", *q);
          return 0;
      }
      

    • Options
    • A. Error: RValue required
    • B. Error: Lvalue required
    • C. Error: cannot convert from 'const int *' to 'int *const'
    • D. No error
    • Discuss
    • 9. Bit fields CANNOT be used in union.

    • Options
    • A. True
    • B. False
    • Discuss
    • 10. What will be the output of the program (myprog.c) given below if it is executed from the command line?
      cmd> myprog 10 20 30
      /* myprog.c */
      #include<stdio.h>
      
      int main(int argc, char **argv)
      {
          int i;
          for(i=0; i<argc; i++)
              printf("%s\n", argv[i]);
          return 0;
      }
      

    • Options
    • A. 10 20 30
    • B. myprog 10 20
    • C. myprog 10 20 30
    • D. 10 20
    • Discuss


    Comments

    There are no comments.

Enter a new Comment