C Arrays Quiz – 20 Questions with Answers
20 questions · 15 min · Medium
Test your knowledge of the const keyword in C, including constant variables, pointers to const data, const pointers, function parameters, and read-only data.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
A. To indicate that an object should not be modified through that identifier
The const qualifier specifies that an object is not intended to be modified through the const-qualified lvalue.
A. int const x = 10;
int const x = 10; declares x as a const-qualified integer. The equivalent form const int x = 10; is also valid.
A. The compiler normally diagnoses the modification as an error
An attempt to modify an object through a const-qualified lvalue is not permitted and is normally diagnosed by the compiler.
A. const int *p;
const int *p means p is a pointer to const int. The pointed-to integer cannot be modified through p, but p itself can be changed.
A. A const pointer to an int
The const applies to the pointer itself, so p cannot be made to point somewhere else after initialization. The pointed-to int can be modified through p.
A. const int *const p = &x;
The first const makes the pointed-to int const, while the second const makes the pointer itself const.
B. The pointed-to int cannot be modified through p
In const int *p, the data pointed to is const-qualified through p, but the pointer itself can be changed to point elsewhere.
A. To prevent the function from modifying the pointed-to int through p
A pointer-to-const parameter allows a function to access the pointed-to data without modifying it through that pointer.
A. It must be initialized before use
A const-qualified object with automatic storage duration must be initialized because it cannot later be assigned a value through a const-qualified lvalue.
B. The int object pointed to by p
The const qualifier applies to int in this declaration, making the pointed-to integer const-qualified through p.
A. The pointer p cannot be reassigned after initialization
The const qualifier applies to the pointer itself. The pointer must be initialized and cannot later be assigned a different address.
C. A cast can remove the qualifier, but modifying an originally const object through the resulting lvalue causes undefined behavior
A cast can remove qualifiers from a pointer type, but if the underlying object was actually defined as const, attempting to modify it results in undefined behavior.
A. const
The const qualifier is used to indicate that an object is not to be modified through a particular lvalue or access path.
A. const char *p;
const char *p allows p to point to different char objects, but the characters cannot be modified through p.
A. The first makes the pointed-to int const; the second makes the pointer const
In const int *p, const qualifies the pointed-to int. In int *const p, const qualifies the pointer itself.