logo

CuriousTab

CuriousTab

Discussion


Home C Programming Constants Comments

  • Question
  • What will be the output of the program?
    #include<stdio.h>
    
    int main()
    {
        const int i=0;
        printf("%d\n", i++);
        return 0;
    }
    


  • Options
  • A. 10
  • B. 11
  • C. No output
  • D. Error: ++needs a value

  • Correct Answer
  • Error: ++needs a value 

    Explanation
    This program will show an error "Cannot modify a const object".

    Step 1: const int i=0; The constant variable 'i' is declared as an integer and initialized with value of '0'(zero).

    Step 2: printf("%d\n", i++); Here the variable 'i' is increemented by 1(one). This will create an error "Cannot modify a const object".

    Because, we cannot modify a const variable.


    Constants problems


    Search Results


    • 1. 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
    • 2. What will be the output of the program?
      #include<stdio.h>
      int get();
      
      int main()
      {
          const int x = get();
          printf("%d", x);
          return 0;
      }
      int get()
      {
          return 20;
      }
      

    • Options
    • A. Garbage value
    • B. Error
    • C. 20
    • D. 0
    • Discuss
    • 3. What will be the output of the program (in Turbo C)?
      #include<stdio.h>
      
      int fun(int *f)
      {
          *f = 10;
          return 0;
      }
      int main()
      {
          const int arr[5] = {1, 2, 3, 4, 5};
          printf("Before modification arr[3] = %d", arr[3]);
          fun(&arr[3]);
          printf("\nAfter modification arr[3] = %d", arr[3]);
          return 0;
      }
      

    • Options
    • A. Before modification arr[3] = 4
      After modification arr[3] = 10
    • B. Error: cannot convert parameter 1 from const int * to int *
    • C. Error: Invalid parameter
    • D. Before modification arr[3] = 4
      After modification arr[3] = 4
    • Discuss
    • 4. What will be the output of the program?
      #include<stdio.h>
      int fun(int **ptr);
      
      int main()
      {
          int i=10;
          const int *ptr = &i;
          fun(&ptr);
          return 0;
      }
      int fun(int **ptr)
      {
          int j = 223;
          int *temp = &j;
          printf("Before changing ptr = %5x\n", *ptr);
          const *ptr = temp;
          printf("After changing ptr = %5x\n", *ptr);
          return 0;
      }
      

    • Options
    • A. Address of i
      Address of j
    • B. 10
      223
    • C. Error: cannot convert parameter 1 from 'const int **' to 'int **'
    • D. Garbage value
    • 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. What will be the output of the program in TurboC?
      #include<stdio.h>
      int fun(int **ptr);
      
      int main()
      {
          int i=10, j=20;
          const int *ptr = &i;
          printf(" i = %5X", ptr);
          printf(" ptr = %d", *ptr);
          ptr = &j;
          printf(" j = %5X", ptr);
          printf(" ptr = %d", *ptr);
          return 0;
      }
      

    • Options
    • A. i= FFE2 ptr=12 j=FFE4 ptr=24
    • B. i= FFE4 ptr=10 j=FFE2 ptr=20
    • C. i= FFE0 ptr=20 j=FFE1 ptr=30
    • D. Garbage value
    • Discuss
    • 7. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          const char *s = "";
          char str[] = "Hello";
          s = str;
          while(*s)
              printf("%c", *s++);
      
          return 0;
      }
      

    • Options
    • A. Error
    • B. H
    • C. Hello
    • D. Hel
    • Discuss
    • 8. What will be the output of the program?
      #include<stdio.h>
      #include<stdlib.h>
      
      union employee
      {
          char name[15];
          int age;
          float salary;
      };
      const union employee e1;
      
      int main()
      {
          strcpy(e1.name, "K");
          printf("%s %d %f", e1.name, e1.age, e1.salary);
          return 0;
      }
      

    • Options
    • A. Error: RValue required
    • B. Error: cannot convert from 'const int *' to 'int *const'
    • C. Error: LValue required in strcpy
    • D. No error
    • Discuss
    • 9. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          const int x=5;
          const int *ptrx;
          ptrx = &x;
          *ptrx = 10;
          printf("%d\n", x);
          return 0;
      }
      

    • Options
    • A. 5
    • B. 10
    • C. Error
    • D. Garbage value
    • Discuss
    • 10. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          const c = -11;
          const int d = 34;
          printf("%d, %d\n", c, d);
          return 0;
      }
      

    • Options
    • A. Error
    • B. -11, 34
    • C. 11, 34
    • D. None of these
    • Discuss


    Comments

    There are no comments.

Enter a new Comment