Curioustab
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Constants Questions
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?
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]?
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?
Binding a const to a non-const value: After int y = 128; const int x = y; what is printed by printf("%d ", x)?
Applying ++ to a const object: For const int i = 0; what happens when evaluating printf("%d ", i++);?
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?
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?
Attempting to write into a const union member: Given const union employee e1; and strcpy(e1.name, "K");, what is the compilation result?
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?
C programming: what will be the output of this program on a standards-compliant compiler? #include
int main() { const c = -11; const int d = 34; printf("%d, %d ", c, d); return 0; }
C program diagnosis: modifying a const union and printing its members #include
#include
#include
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; }
Const-correctness check in C: assigning a const char* to a non-const char* variable #include
const char *fun(); int main() { char *ptr = fun(); return 0; } const char *fun() { return "Hello"; }
Turbo C (non-C99) diagnostic: using a const int variable as an array bound vs a #define constant #include
#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; }
C const-correctness: attempting to modify a string literal through a const char* return value #include
const char *fun(); int main() { *fun() = 'A'; return 0; } const char *fun() { return "Hello"; }
Const qualifier on the pointed-to data: diagnose the error when writing through a pointer to const #include
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; }
Const object initialization rule in C: diagnosing assignment after declaration #include
int main() { const int x; /* uninitialized / x = 128; / attempted assignment after declaration */ printf("%d ", x); return 0; }
Type qualification mismatch: binding a pointer to non-const int to the address of a const int #include
int main() { const int k = 7; int const q = &k; / constant pointer to non-const int */ printf("%d", *q); return 0; }
Const pointer vs pointer to const: identify the actual error in reassignment #include
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; }