Constants Questions
Practice Constants MCQs with answers and explanations. Page 1 of 1.
Category
C Programming
Topic
Constants
Page
1 / 1
Mode
Practice
Questions
Open any question to view the answer and explanation.
Pointers and qualifiers in C: What happens when calling fun(&ptr) if fun expects int ** but ptr has type const int * in the given program?
Open
View answer
Const-correctness and arrays: Given a const int array in Turbo C and a function fun(int *), what is the outcome of calling fun(&arr[3]) after printing arr[3]?
Open
View answer
Initialization of const from a function: What will be printed when const int x = get(); with get() returning 20 is executed and x is printed?
Open
View answer
Binding a const to a non-const value: After int y = 128; const int x = y; what is printed by printf("%d
", x)?
Open
View answer
Applying ++ to a const object: For const int i = 0; what happens when evaluating printf("%d
", i++);?
Open
View answer
Printing through a pointer-to-const: In Turbo C, given const int *ptr pointing to i, then reassigned to j, what is the likely output of printing the pointer values (as hex) and the pointed integers?
Open
View answer
Reading and printing a C string via a pointer-to-const: With const char *s = "" then s = str where str is "Hello", what does the loop while(*s) printf("%c", *s++); print?
Open
View answer
Attempting to write into a const union member: Given const union employee e1; and strcpy(e1.name, "K");, what is the compilation result?
Open
View answer
Dereferencing a pointer-to-const bound to a const object: With const int x = 5; const int *ptrx = &x; what happens when executing *ptrx = 10; then printing x?
Open
View answer
C programming: what will be the output of this program on a standards-compliant compiler?
#include<stdio.h>
int main()
{
const c = -11;
const int d = 34;
printf("%d, %d
", c, d);
return 0;
}
Open
View answer
C program diagnosis: modifying a const union and printing its members
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
union employee {
char name[15];
int age;
float salary;
};
const union employee e1;
int main()
{
strcpy(e1.name, "K");
printf("%s", e1.name);
e1.age = 85;
printf("%d", e1.age);
printf("%f", e1.salary);
return 0;
}
Open
View answer
Const-correctness check in C: assigning a const char* to a non-const char* variable
#include<stdio.h>
const char *fun();
int main()
{
char *ptr = fun();
return 0;
}
const char *fun()
{
return "Hello";
}
Open
View answer
Turbo C (non-C99) diagnostic: using a const int variable as an array bound vs a #define constant
#include<stdio.h>
#define MAX 128
int main()
{
const int max = 128; /* not an ICE in old Turbo C /
char array[max]; / variable length array in old compiler → error /
char string[MAX]; / OK: macro constant */
array[0] = string[0] = 'A';
printf("%c %c
", array[0], string[0]);
return 0;
}
Open
View answer
C const-correctness: attempting to modify a string literal through a const char* return value
#include<stdio.h>
const char *fun();
int main()
{
*fun() = 'A';
return 0;
}
const char *fun()
{
return "Hello";
}
Open
View answer
Const qualifier on the pointed-to data: diagnose the error when writing through a pointer to const
#include<stdio.h>
int main()
{
char mybuf[] = "India";
char yourbuf[] = "CURIOUSTAB";
char const ptr = mybuf; / pointer to const char */
ptr = 'a'; / attempt to modify through const-qualified pointer /
ptr = yourbuf; / re-pointing is allowed */
return 0;
}
Open
View answer
Const object initialization rule in C: diagnosing assignment after declaration
#include<stdio.h>
int main()
{
const int x; /* uninitialized /
x = 128; / attempted assignment after declaration */
printf("%d
", x);
return 0;
}
Open
View answer
Type qualification mismatch: binding a pointer to non-const int to the address of a const int
#include<stdio.h>
int main()
{
const int k = 7;
int const q = &k; / constant pointer to non-const int */
printf("%d", *q);
return 0;
}
Open
View answer
Const pointer vs pointer to const: identify the actual error in reassignment
#include<stdio.h>
int main()
{
char mybuf[] = "India";
char yourbuf[] = "CURIOUSTAB";
char const ptr = mybuf; / const pointer to char (pointer is fixed) */
ptr = 'a'; / writing through ptr is allowed (mybuf is writable) /
ptr = yourbuf; / ERROR: cannot reassign a const pointer */
return 0;
}
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.