MCQs
No error. This will produce 7 as output.
A const variable has to be initialized when it is declared. later assigning the value to the const
variable will result in an error "Cannot modify the const object".
Hence Option B is correct
Step 1: A macro named MAX is defined with value 128
Step 2: const int max=128; The constant variable max is declared as an integer data type and it is
initialized with value 128.
Step 3: char array[max]; This statement reports an error "constant expression required". Because,
we cannot use variable to define the size of array.
To avoid this error, we have to declare the size of an array as static. Eg. char array[10];or use macro
char array[MAX];
Note: The above program will print A A as output in Unix platform.
Step 1: const c = -11; The constant variable 'c' is declared and initialized to value "-11".
Step 2: const int d = 34; The constant variable 'd' is declared as an integer and initialized to value '34'.
Step 3: printf("%d, %dn", c, d); The value of the variable 'c' and 'd' are printed.
Hence the output of the program is -11, 34
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("%dn", 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.
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;
}
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
Answer: Option A
Step 1: const int arr[5] = {1, 2, 3, 4, 5}; The constant variable arr is declared as an integer array and initialized to
arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, arr[4] = 5
Step 2: printf("Before modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. 4).
Step 3: fun(&arr[3]); The memory location of the arr[3] is passed to fun() and arr[3]value is modified to 10.
A const variable can be indirectly modified by a pointer.
Step 4: printf("After modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. 10).
Hence the output of the program is
Before modification arr[3] = 4
After modification arr[3] = 10
Step 1: int get(); This is the function prototype for the funtion get(), it tells the compiler returns
an integer value and accept no parameters.
Step 2: const int x = get(); The constant variable x is declared as an integer data type and initialized
with the value "20".
The function get() returns the value "20".
Step 3: printf("%d", x); It prints the value of the variable x.
Hence the output of the program is "20".