logo

CuriousTab

CuriousTab

Discussion


Home C Programming Constants See What Others Are Saying!
  • Question
  • Point out the error in the program.
    #include<stdio.h>
    #define MAX 128
    
    int main()
    {
        char mybuf[] = "India";
        char yourbuf[] = "CURIOUSTAB";
        char const *ptr = mybuf;
        *ptr = 'a';
        ptr = yourbuf;
        return 0;
    }
    


  • Options
  • A. Error: cannot convert ptr const value
  • B. Error: unknown pointer conversion
  • C. No error
  • D. None of above

  • Correct Answer
  • Error: cannot convert ptr const value 

    Explanation
    Step 1: char mybuf[] = "India"; The variable mybuff is declared as an array of characters and initialized with string "India".

    Step 2: char yourbuf[] = "CURIOUSTAB"; The variable yourbuf is declared as an array of characters and initialized with string "CURIOUSTAB".

    Step 3: char const *ptr = mybuf; Here, ptr is a constant pointer, which points at a char.

    The value at which ptr it points is a constant; it will be an error to modify the pointed character; There will not be any error to modify the pointer itself.

    Step 4: *ptr = 'a'; Here, we are changing the value of ptr, this will result in the error "cannot modify a const object".


    More questions

    • 1. What will be the output of the program in Turbo C?
      #include<stdio.h>
      
      int main()
      {
          char str[10] = "India";
          str[6] = "CURIOUSTAB";
          printf("%s\n", str);
          return 0;
      }
      

    • Options
    • A. India CURIOUSTAB
    • B. CURIOUSTAB
    • C. India
    • D. Error
    • Discuss
    • 2. Bitwise | can be used to multiply a number by powers of 2.

    • Options
    • A. Yes
    • B. No
    • Discuss
    • 3. By default a real number is treated as a

    • Options
    • A. float
    • B. double
    • C. long double
    • D. far double
    • Discuss
    • 4. In the following program where is the variable a getting defined and where it is getting declared?
      #include<stdio.h>
      int main()
      {
          extern int a;
          printf("%d\n", a);
          return 0;
      }
      int a=20;
      

    • Options
    • A. extern int a is declaration, int a = 20 is the definition
    • B. int a = 20 is declaration, extern int a is the definition
    • C. int a = 20 is definition, a is not defined
    • D. a is declared, a is not defined
    • Discuss
    • 5. The library function used to find the last occurrence of a character in a string is

    • Options
    • A. strnstr()
    • B. laststr()
    • C. strrchr()
    • D. strstr()
    • Discuss
    • 6. Point out the error in the following program.
      #include<stdio.h>
      #include<stdarg.h>
      fun(...);
      
      int main()
      {
          fun(3, 7, -11.2, 0.66);
          return 0;
      }
      fun(...)
      {
          va_list ptr;
          int num;
          va_start(ptr, n);
          num = va_arg(ptr, int);
          printf("%d", num);
      }
      

    • Options
    • A. Error: fun() needs return type
    • B. Error: ptr Lvalue required
    • C. Error: Invalid declaration of fun(...)
    • D. No error
    • Discuss
    • 7. Which of the following function sets first n characters of a string to a given character?

    • Options
    • A. strinit()
    • B. strnset()
    • C. strset()
    • D. strcset()
    • Discuss
    • 8. If the two strings are identical, then strcmp() function returns

    • Options
    • A. -1
    • B. 1
    • C. 0
    • D. Yes
    • Discuss
    • 9. What will be the output of the program?
      #include<stdio.h>
      #include<string.h>
      
      int main()
      {
          char dest[] = {97, 97, 0};
          char src[] = "aaa";
          int i;
          if((i = memcmp(dest, src, 2))==0)
              printf("Got it");
          else
              printf("Missed");
          return 0;
      }
      

    • Options
    • A. Missed
    • B. Got it
    • C. Error in memcmp statement
    • D. None of above
    • Discuss
    • 10. Consider the following program and what will be content of t?
      #include<stdio.h>
      
      int main()
      {
          FILE *fp;
          int t;
          fp = fopen("DUMMY.C", "w");
          t = fileno(fp);
          printf("%d\n", t);
          return 0;
      }
      

    • Options
    • A. size of "DUMMY.C" file
    • B. The handle associated with "DUMMY.C" file
    • C. Garbage value
    • D. Error in fileno()
    • Discuss


    Comments

    There are no comments.

Enter a new Comment