Bird
Raised Fist0
Cprogramming~20 mins

Constants and literals - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Constants and Literals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of integer and floating-point constants
What is the output of this C program?
#include <stdio.h>
int main() {
    const int a = 5;
    const float b = 3.0f;
    printf("%d %.1f\n", a, b);
    return 0;
}
C
#include <stdio.h>
int main() {
    const int a = 5;
    const float b = 3.0f;
    printf("%d %.1f\n", a, b);
    return 0;
}
A5 3.0
B5 3
C5.0 3.0
D3 5.0
Attempts:
2 left
💡 Hint
Look at the format specifiers used in printf.
❓ Predict Output
intermediate
2:00remaining
Output of character and string literals
What will this C program print?
#include <stdio.h>
int main() {
    const char c = 'A';
    const char *s = "Hello";
    printf("%c %s\n", c, s);
    return 0;
}
C
#include <stdio.h>
int main() {
    const char c = 'A';
    const char *s = "Hello";
    printf("%c %s\n", c, s);
    return 0;
}
AHello A
B65 Hello
CA 'Hello'
DA Hello
Attempts:
2 left
💡 Hint
Remember that '%c' prints a single character and '%s' prints a string.
❓ Predict Output
advanced
2:00remaining
Output of octal and hexadecimal integer constants
What is the output of this C program?
#include <stdio.h>
int main() {
    int x = 010;  // octal 10
    int y = 0x10; // hex 10
    printf("%d %d\n", x, y);
    return 0;
}
C
#include <stdio.h>
int main() {
    int x = 010;  // octal 10
    int y = 0x10; // hex 10
    printf("%d %d\n", x, y);
    return 0;
}
A8 16
B10 10
C16 8
D010 0x10
Attempts:
2 left
💡 Hint
Octal 010 is 8 in decimal, hex 0x10 is 16 in decimal.
❓ Predict Output
advanced
2:00remaining
Output of escape sequences in character constants
What does this C program print?
#include <stdio.h>
int main() {
    char c1 = '\n';
    char c2 = '\t';
    printf("%d %d\n", c1, c2);
    return 0;
}
C
#include <stdio.h>
int main() {
    char c1 = '\n';
    char c2 = '\t';
    printf("%d %d\n", c1, c2);
    return 0;
}
A92 116
B10 9
Cn t
D0 0
Attempts:
2 left
💡 Hint
Escape sequences represent special ASCII codes.
❓ Predict Output
expert
2:00remaining
Value of a floating-point constant with suffix
What is the output of this C program?
#include <stdio.h>
int main() {
    double d = 1.5;
    float f = 1.5f;
    printf("%.1f %.1f\n", d, f);
    return 0;
}
C
#include <stdio.h>
int main() {
    double d = 1.5;
    float f = 1.5f;
    printf("%.1f %.1f\n", d, f);
    return 0;
}
A1.0 1.5
B1.5 0.0
C1.5 1.5
D1.5 1.0
Attempts:
2 left
💡 Hint
The suffix 'f' makes the constant a float, but printing with %.1f shows the value.

Practice

(1/5)
1. What is a constant in C programming?
easy
A. A function that returns a value
B. A fixed value that cannot be changed during program execution
C. A variable that can change its value anytime
D. A type of loop in C

Solution

  1. Step 1: Understand the meaning of constant

    A constant is a value that remains the same throughout the program and cannot be modified.
  2. Step 2: Compare options with definition

    Only A fixed value that cannot be changed during program execution describes a fixed value that does not change, which matches the definition of a constant.
  3. Final Answer:

    A fixed value that cannot be changed during program execution -> Option B
  4. Quick Check:

    Constant = fixed value [OK]
Hint: Constants never change value once set [OK]
Common Mistakes:
  • Confusing constants with variables
  • Thinking constants are functions
  • Mixing constants with loops
2. Which of the following is the correct way to define a constant integer named MAX with value 100 in C?
easy
A. All of the above
B. #define MAX 100
C. const int MAX = 100;
D. int const MAX = 100;

Solution

  1. Step 1: Review ways to define constants in C

    Constants can be defined using #define or using const keyword with variable declaration.
  2. Step 2: Check each option

    #define MAX 100 uses #define, which is valid. int const MAX = 100; uses int const, which is valid. const int MAX = 100; uses const int, also valid. So all are correct.
  3. Final Answer:

    All of the above -> Option A
  4. Quick Check:

    All methods define constants [OK]
Hint: Use #define or const keyword for constants [OK]
Common Mistakes:
  • Using variable without const
  • Missing semicolon in const declaration
  • Confusing #define with variable
3. What will be the output of the following C code?
#include <stdio.h>
#define PI 3.14
int main() {
    const int radius = 5;
    float area = PI * radius * radius;
    printf("%.2f", area);
    return 0;
}
medium
A. Syntax error
B. 31.40
C. 78.50
D. 25.00

Solution

  1. Step 1: Calculate area using given formula

    Area = PI * radius * radius = 3.14 * 5 * 5 = 3.14 * 25 = 78.5
  2. Step 2: Check printf formatting

    Using %.2f prints the float with 2 decimal places, so output is 78.50
  3. Final Answer:

    78.50 -> Option C
  4. Quick Check:

    Area = 3.14 * 25 = 78.50 [OK]
Hint: Calculate formula carefully and check decimal format [OK]
Common Mistakes:
  • Using wrong formula
  • Ignoring decimal places
  • Confusing radius with diameter
4. Identify the error in the following C code snippet:
const int x;
x = 10;
printf("%d", x);
medium
A. Cannot assign value to const variable after declaration
B. Missing semicolon after declaration
C. printf format specifier is wrong
D. No error, code runs fine

Solution

  1. Step 1: Understand const variable rules

    A const variable must be initialized when declared and cannot be assigned a value later.
  2. Step 2: Analyze the code

    Here, const int x; declares x without initialization, then tries to assign 10 later, which is not allowed.
  3. Final Answer:

    Cannot assign value to const variable after declaration -> Option A
  4. Quick Check:

    const variables must be initialized once [OK]
Hint: Initialize const variables when declared [OK]
Common Mistakes:
  • Assigning value after declaration
  • Not initializing const variables
  • Ignoring const rules
5. You want to create a constant array of 3 integers in C that cannot be modified after initialization. Which of the following declarations achieves this correctly?
hard
A. int arr[3] = {1, 2, 3};
B. int const *arr = {1, 2, 3};
C. #define arr {1, 2, 3}
D. const int arr[3] = {1, 2, 3};

Solution

  1. Step 1: Understand const array declaration

    Declaring an array with const int arr[3] means the elements cannot be changed after initialization.
  2. Step 2: Check each option

    int arr[3] = {1, 2, 3}; declares a normal array, elements can be changed. const int arr[3] = {1, 2, 3}; declares a constant array correctly. #define arr {1, 2, 3} uses #define incorrectly for array. int const *arr = {1, 2, 3}; declares a pointer to const int, but initialization is invalid.
  3. Final Answer:

    const int arr[3] = {1, 2, 3}; -> Option D
  4. Quick Check:

    const array elements cannot change [OK]
Hint: Use const before type for constant arrays [OK]
Common Mistakes:
  • Using #define for arrays
  • Declaring non-const arrays
  • Incorrect pointer initialization
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard