LearnFrenzy
Sign in Take a free test

C Const · Exercise 1 of 4

C Const const MCQ

  • 12 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Twelve multiple-choice questions on const variables, const with pointers, const parameters, const vs #define, casting away const, volatile const and C23 constexpr. Pick an option to check your answer instantly.

Question 1 Easy

What happens with const int x = 5; x = 6;?

A x becomes 6
B A compile error: x is read-only
C x stays 5 silently
D A run-time crash
Question page & discussion
Question 2 Medium

Which declaration means the same as const int *p?

A int *const p
B int const *p
C const int *const p
D int *p const
Question page & discussion
Question 3 Medium

Is const double pi = acos(-1.0); valid inside a function?

A No, const needs a literal
B Yes – a const local may be initialised with any expression and is read-only afterwards
C Only with static
D Only in C23
Question page & discussion
Question 4 Hard

At file scope, is const int N = 10; int buf[N]; valid C?

A Yes, exactly as in C++
B No – in C a const variable is not a constant expression, and VLAs are not allowed at file scope
C Yes, but only if N is static
D Only with -O2
Question page & discussion
Question 5 Easy

Which statement about #define PI 3.14159 versus const double PI = 3.14159; is true?

A Both are typed variables
B The macro is untyped text replaced by the preprocessor and ignores scope; the const is a typed, scoped object
C The macro uses memory at run time
D const is replaced by the preprocessor
Question page & discussion
Question 6 Medium

What is the result of *(int *) &limit = 20; when limit was defined as static const int limit = 10;?

A limit becomes 20
B Undefined behaviour – the object may be in read-only memory or its value folded into the code
C A compile error
D limit becomes 0
Question page & discussion
Question 7 Medium

A function is declared void edit(char *s);. What happens when you call edit(msg) with const char *msg?

A Nothing special
B The const qualifier is discarded; the compiler warns, because edit() could write to read-only data
C msg is copied
D It is converted safely
Question page & discussion
Question 8 Hard

What type does the string literal "hi" have in C?

A const char[3]
B char[3] – not const, but modifying it is still undefined behaviour
C char *
D const char *
Question page & discussion
Question 9 Easy

What is the effect of const in int square(const int n)?

A The caller's variable becomes const
B Only the function's own copy n cannot be changed inside square()
C n is passed by reference
D The function can only be called with literals
Question page & discussion
Question 10 Easy

For const int *const p = &x;, which is allowed?

A *p = 5;
B p = &y;
C Reading *p
D Both *p = 5 and p = &y
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links