Chapter 5 · Constants and Strings
Practice · Chapter 5

Constants and Strings

Short drills to make the concepts stick — predict what code prints, fix a broken snippet, or write a small function. Commit to an answer before you run anything.

5.3 — Numeral systems (decimal, binary, hexadecimal, and octal)

predict

Sticky hex manipulator

Both a and b are ordinary ints. What does this program print?

C++
#include <iostream>

int main()
{
    int a { 255 };
    int b { 16 };

    std::cout << std::hex << a << '\n';
    std::cout << b << '\n';

    return 0;
}
Choose the output
fix

Octal literal masquerading as decimal

The function is supposed to classify codes below 100 as "small" and codes 100 or above as "large", but the threshold is wrong. Find the bug.

student.cppeditable

Edit student.cpp, then Run to check it against the tests.

5.6 — Constexpr variables

predict

constexpr with a non-constexpr function

What happens when you compile this program?

C++
#include <iostream>

int getMax() { return 100; }

int main()
{
    constexpr int limit { getMax() };
    std::cout << limit << '\n';
    return 0;
}
Choose the output

5.7 — Introduction to std::string

fix

Fix the name-builder

The function is supposed to return "First Last" but produces only the last name. Find and fix the single-character mistake.

student.cppeditable

Edit student.cpp, then Run to check it against the tests.

5.8 — Introduction to std::string_view

write

Build a badge label with constexpr and string_view

Implement badgeLabel and badgeLabelLength using the provided constexpr prefix constant and std::string_view parameters.

student.cppeditable

Edit student.cpp, then Run to check it against the tests.