Chapter 1 · C++ Basics
Practice · Chapter 1

C++ Basics

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.

1.4 — Variable Assignment and Initialization

predict

What does this print?

Read the initialization line closely before choosing an answer.

C++
#include <iostream>

int main()
{
    int a, b = 5;
    std::cout << a + b << '\n';
    return 0;
}
Choose the output
predict

What does this print?

Think about what happens when a double value is stored in an int variable using copy-initialization.

C++
#include <iostream>

int main()
{
    int x = 4.5;
    std::cout << x << '\n';
    return 0;
}
Choose the output
fix

Fix the uninitialized variable

The function should return a + 5, but the tests all fail. Find and fix the bug.

student.cppeditable

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

1.9 — Introduction to Literals and Operators

fix

Fix the integer arithmetic order

The function should return a percentage like percent(3, 4) == 75, but most cases return 0. Find the operator-ordering bug.

student.cppeditable

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

1.10 — Introduction to Expressions

write

Compute a perimeter inline

Implement the function body using a single return statement with an inline arithmetic expression — no extra variables needed.

student.cppeditable

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