MCQs
Step 1: const char *s = ""; The constant variable s is declared as an pointer to an array of
characters type and initialized with an empty string.
Step 2: char str[] = "Hello"; The variable str is declared as an array of charactrers type and
initialized with a string "Hello".
Step 3: s = str; The value of the variable str is assigned to the variable s. Therefore strcontains
the text "Hello".
Step 4: while(*s){ printf("%c", *s++); } Here the while loop got executed untill the value of the
variable s is available and it prints the each character of the variable s. and it prints the each
character of the variable s.
Hence the output of the program is "Hello".
Step 1: const int x=5; The constant variable x is declared as an integer data type and
initialized with value '5'.
Step 2: const int *ptrx; The constant variable ptrx is declared as an integer pointer.
Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer
variable ptrx.
Step 4: *ptrx = 10; Here we are indirectly trying to change the value of the constant vaiable
x. This will result in an error.
To change the value of const variable x we have to use *(int *)&x = 10;
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;
}
A. Address of i
Address of j
B. 10
223
C. Error: cannot CONVERT parameter 1 from 'const int **' to 'int **'
D. Garbage value
Answer: Option C
The output will be (in 16-bit platform DOS):
K 75 0.000000
Step 1: int y=128; The variable 'y' is declared as an integer type and initialized to value "128".
Step 2: const int x=y; The constant variable 'x' is declared as an integer and it is initialized with the variable 'y' value.
Step 3: printf("%dn", x); It prints the value of variable 'x'.
Hence the output of the program is "128"