int main() { int i, fss; char ch, source["> int main() { int i, fss; char ch, source[">
logo

CuriousTab

CuriousTab

Discussion


Home C Programming Input / Output See What Others Are Saying!
  • Question
  • On executing the below program what will be the contents of 'target.txt' file if the source file contains a line "To err is human"?
    #include<stdio.h>
    
    int main()
    {
        int i, fss;
        char ch, source[20] = "source.txt", target[20]="target.txt", t;
        FILE *fs, *ft;
        fs = fopen(source, "r");
        ft = fopen(target, "w");
        while(1)
        {
            ch=getc(fs);
            if(ch==EOF)
                break;
            else
            {
                fseek(fs, 4L, SEEK_CUR);
                fputc(ch, ft);
            }
        }
        return 0;
    }
    


  • Options
  • A. r n
  • B. Trh
  • C. err
  • D. None of above

  • Correct Answer
  • Trh 

    Explanation
    The file source.txt is opened in read mode and target.txt is opened in write mode. The file source.txt contains "To err is human".

    Inside the while loop,

    ch=getc(fs); The first character('T') of the source.txt is stored in variable ch and it's checked for EOF.

    if(ch==EOF) If EOF(End of file) is true, the loop breaks and program execution stops.

    If not EOF encountered, fseek(fs, 4L, SEEK_CUR); the file pointer advances 4 character from the current position. Hence the file pointer is in 5th character of file source.txt.

    fputc(ch, ft); It writes the character 'T' stored in variable ch to target.txt.

    The while loop runs three times and it write the character 1st and 5th and 11th characters ("Trh") in the target.txt file.


    More questions

    • 1. Point out the error in the program?
      typedef struct data mystruct;
      struct data
      {
          int x;
          mystruct *b;
      };
      

    • Options
    • A. Error: in structure declaration
    • B. Linker Error
    • C. No Error
    • D. None of above
    • Discuss
    • 2. Left shifting an unsigned int or char by 1 is always equivalent to multiplying it by 2.

    • Options
    • A. True
    • B. False
    • Discuss
    • 3. Can you combine the following two statements into one?
      char *p;
      p = (char*) malloc(100);
      

    • Options
    • A. char p = *malloc(100);
    • B. char *p = (char) malloc(100);
    • C. char *p = (char*)malloc(100);
    • D. char *p = (char *)(malloc*)(100);
    • Discuss
    • 4. How many bytes are occupied by near, far and huge pointers (DOS)?

    • Options
    • A. near=2 far=4 huge=4
    • B. near=4 far=8 huge=8
    • C. near=2 far=4 huge=8
    • D. near=4 far=4 huge=8
    • Discuss
    • 5. Can a structure can point to itself?

    • Options
    • A. Yes
    • B. No
    • Discuss
    • 6. What will be the output of the program if the array begins 1200 in memory?
      #include<stdio.h>
      
      int main()
      {
          int arr[]={2, 3, 4, 1, 6};
          printf("%u, %u, %u\n", arr, &arr[0], &arr);
          return 0;
      }
      

    • Options
    • A. 1200, 1202, 1204
    • B. 1200, 1200, 1200
    • C. 1200, 1204, 1208
    • D. 1200, 1202, 1200
    • Discuss
    • 7. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          union a
          {
              int i;
              char ch[2];
          };
          union a u;
          u.ch[0]=3;
          u.ch[1]=2;
          printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
          return 0;
      }
      

    • Options
    • A. 3, 2, 515
    • B. 515, 2, 3
    • C. 3, 2, 5
    • D. 515, 515, 4
    • Discuss
    • 8. What will be the output of the program given below in 16-bit platform?
      #include<stdio.h>
      
      int main()
      {
          enum value{VAL1=0, VAL2, VAL3, VAL4, VAL5} var;
          printf("%d\n", sizeof(var));
          return 0;
      }
      

    • Options
    • A. 1
    • B. 2
    • C. 4
    • D. 10
    • Discuss
    • 9. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          struct byte
          {
              int one:1;
          };
          struct byte var = {1};
          printf("%d\n", var.one);
          return 0;
      }
      

    • Options
    • A. 1
    • B. -1
    • C. 0
    • D. Error
    • Discuss
    • 10. Point out the error in the program?
      #include<stdio.h>
      
      int main()
      {
          struct bits
          {
              float f:2;
          }bit;
      
          printf("%d\n", sizeof(bit));
          return 0;
      }
      

    • Options
    • A. 4
    • B. 2
    • C. Error: cannot set bit field for float
    • D. Error: Invalid member access in structure
    • Discuss


    Comments

    There are no comments.

Enter a new Comment