Skip to content
QuiznExam

const in C

Test your knowledge of the const keyword in C, including constant variables, pointers to const data, const pointers, function parameters, and read-only data.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is the primary purpose of the const keyword in C?

    • A. To indicate that an object should not be modified through that identifier
    • B. To allocate dynamic memory
    • C. To create a function
    • D. To declare a loop
    Show answer

    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.

  2. 2. Which declaration defines a const integer variable?

    • A. int const x = 10;
    • B. constant int x = 10;
    • C. int x const = 10;
    • D. readonly int x = 10;
    Show answer

    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.

  3. 3. What happens if you try to modify a const-qualified variable directly?

    • A. The compiler normally diagnoses the modification as an error
    • B. The value is always changed successfully
    • C. The variable automatically becomes non-const
    • D. The program always terminates immediately
    Show answer

    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.

  4. 4. Which declaration represents a pointer to const int?

    • A. const int *p;
    • B. int *const p;
    • C. int const p*;
    • D. int **const p;
    Show answer

    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.

  5. 5. What does `int *const p = &x;` declare?

    • A. A const pointer to an int
    • B. A pointer to a const int
    • C. A const integer
    • D. A pointer to a pointer
    Show answer

    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.

  6. 6. Which declaration represents a const pointer to const int?

    • A. const int *const p = &x;
    • B. int *p;
    • C. int *const p;
    • D. const int p;
    Show answer

    A. const int *const p = &x;

    The first const makes the pointed-to int const, while the second const makes the pointer itself const.

  7. 7. Which statement about a pointer declared as `const int *p` is correct?

    • A. p cannot be changed to point to another object
    • B. The pointed-to int cannot be modified through p
    • C. p itself must be initialized with a constant address
    • D. Both p and the pointed-to int are const
    Show answer

    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.

  8. 8. Why might a function parameter be declared as `const int *p`?

    • A. To prevent the function from modifying the pointed-to int through p
    • B. To prevent p from changing its address
    • C. To make p a function
    • D. To allocate memory automatically
    Show answer

    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.

  9. 9. What is required when defining a const object with automatic storage duration if it is not otherwise initialized?

    • A. It must be initialized before use
    • B. It is automatically initialized to zero in all cases
    • C. It must always be dynamically allocated
    • D. It cannot be declared inside a function
    Show answer

    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.

  10. 10. What does `const` qualify in the declaration `const int *p`?

    • A. The pointer p
    • B. The int object pointed to by p
    • C. The address stored in memory only
    • D. The function containing p
    Show answer

    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.

  11. 11. Which statement about `int *const p` is correct?

    • A. The pointer p cannot be reassigned after initialization
    • B. The pointed-to integer is const
    • C. Neither p nor the pointed-to integer can be modified
    • D. p can never point to an integer
    Show answer

    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.

  12. 12. Can a const-qualified object be accessed through a non-const pointer after an explicit cast?

    • A. Yes, and modifying the original const object through that pointer is safe
    • B. No cast can ever be written
    • C. A cast can remove the qualifier, but modifying an originally const object through the resulting lvalue causes undefined behavior
    • D. Casting automatically creates a copy
    Show answer

    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.

  13. 13. Which keyword is used in C to specify that an object should not be modified through a particular access path?

    • A. const
    • B. static
    • C. extern
    • D. volatile
    Show answer

    A. const

    The const qualifier is used to indicate that an object is not to be modified through a particular lvalue or access path.

  14. 14. Which declaration is valid for a pointer that can change its target but cannot modify the target through that pointer?

    • A. const char *p;
    • B. char *const p;
    • C. const char *const p;
    • D. char const p*;
    Show answer

    A. const char *p;

    const char *p allows p to point to different char objects, but the characters cannot be modified through p.

  15. 15. Which statement best summarizes the difference between `const int *p` and `int *const p`?

    • A. The first makes the pointed-to int const; the second makes the pointer const
    • B. Both make the pointer const
    • C. Both make the pointed-to int const
    • D. Neither declaration uses const meaningfully
    Show answer

    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.