Ready to practice?
Sign up to access interactive coding exercises and track your progress.
C++ Basics summary
Review and test your understanding of all fundamental C++ concepts covered in this chapter.
Prerequisites
1.x — Chapter 1 summary and quiz
Congratulations! You've completed Chapter 1: C++ basics.
Chapter 1 recap
Chapter 1 introduced you to the fundamental concepts of C++ programming. You learned about the basic structure of C++ programs, how to work with data, and how to create your first complete programs. These concepts form the foundation for everything else you'll learn in C++.
Key concepts learned
Program Structure (Lessons 1.0, 1.1)
- Every C++ program must have a
main()
function where execution begins - Programs need
#include
directives to use standard library features - The
return 0;
statement indicates successful program completion - C++ programs are made up of statements that end with semicolons
Code Documentation (Lesson 1.2)
- Comments explain code without affecting program execution
- Single-line comments use
//
- Multi-line comments use
/* */
- Good comments explain "why" rather than "what"
Variables and Data Storage (Lessons 1.3, 1.4, 1.6)
- Variables are named memory locations that store data
- Variables must be declared before use with a data type and name
- Assignment uses the
=
operator to store values in variables - Initialization combines declaration and assignment:
int x = 5;
- Uninitialized variables contain garbage values and cause undefined behavior
Input and Output (Lesson 1.5)
std::cout
sends output to the console using the<<
operatorstd::cin
reads input from the console using the>>
operatorstd::endl
creates a new line and flushes the output buffer- Chain multiple values with
<<
:std::cout << "Age: " << age << std::endl;
Code Naming and Style (Lessons 1.7, 1.8)
- Variable names should be descriptive and follow naming conventions
- Keywords (like
int
,return
,if
) are reserved and cannot be used as names - Proper whitespace and formatting make code readable and maintainable
- Consistent indentation shows program structure
Literals and Operators (Lesson 1.9)
- Literals are fixed values written directly in code (
5
,3.14
,"hello"
) - Operators perform operations on values (
+
,-
,*
,/
,=
) - The assignment operator
=
stores values in variables - Arithmetic operators follow mathematical rules
Expressions (Lesson 1.10)
- Expressions combine literals, variables, and operators to produce values
- Every expression has a value and a type
- Operator precedence determines evaluation order:
()
, then*
/
, then+
-
- Parentheses can override default precedence and improve clarity
Program Development (Lesson 1.11)
- Good programs start with understanding the problem and planning the solution
- Break complex problems into smaller, manageable steps
- Test programs with different inputs to ensure they work correctly
- Iterative improvement makes programs better over time
Chapter 1 quiz
Test your understanding of the concepts covered in Chapter 1. Try to answer these questions before looking up the answers.
Question 1
What is wrong with this program?
#include <iostream>
int main()
{
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "You are " << age << " years old" << std::endl;
return 0;
}
Answer: The variable age
is used without being declared. It should be declared first: int age;
Question 2
What is the output of this program?
#include <iostream>
int main() {
std::cout << 2 + 3 * 4 << std::endl;
std::cout << (2 + 3) * 4 << std::endl;
return 0;
}
Answer:
14
20
The first line outputs 14 because multiplication has higher precedence than addition (3 * 4 = 12, then 2 + 12 = 14). The second line outputs 20 because parentheses are evaluated first (2 + 3 = 5, then 5 * 4 = 20).
Question 3
Which of these are valid variable names in C++?
a) int
b) myAge
c) 2ndPlace
d) user_name
e) cout
Answer: b) myAge
and d) user_name
are valid.
- a)
int
is a keyword - c)
2ndPlace
starts with a digit - e)
cout
would technically work but would be confusing since it conflicts withstd::cout
Question 4
What is the difference between these two lines?
int x = 5; // Line 1
int y; // Line 2
Answer: Line 1 declares and initializes variable x
with the value 5. Line 2 only declares variable y
but doesn't initialize it, so y
contains an undefined (garbage) value.
Question 5
Fix this program to calculate the average of two numbers:
#include <iostream>
int main() {
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
average = num1 + num2 / 2;
std::cout << "Average: " << average << std::endl;
return 0;
}
Answer:
#include <iostream>
int main() {
double num1, num2, average; // Declare variables
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
average = (num1 + num2) / 2; // Fix calculation with parentheses
std::cout << "Average: " << average << std::endl;
return 0;
}
Question 6
What type of comment should you use for each situation? a) A brief note explaining a complex calculation b) Temporarily removing several lines of code for testing c) Adding your name and date at the top of a file
Answer:
- a) Single-line comment (
//
) - b) Multi-line comment (
/* */
) - c) Multi-line comment (
/* */
)
Question 7
What will this program output?
#include <iostream>
int main() {
int x = 10;
int y = x + 5;
x = 20;
std::cout << x << " " << y << std::endl;
return 0;
}
Answer: 20 15
y
is calculated asx + 5
whenx
is 10, soy
becomes 15- Changing
x
to 20 afterward doesn't change the value ofy
Question 8
Complete this program to ask for a user's name and greet them:
#include <iostream>
#include <string>
int main() {
// Your code here
return 0;
}
Answer:
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
Moving forward
You now have a solid foundation in C++ basics! In Chapter 2, you'll learn about functions and how to organize your code into multiple files. This will allow you to write larger, more organized programs.
Key takeaways for writing good C++ code:
- Always declare variables before using them
- Initialize variables when you declare them
- Use meaningful variable names
- Format your code consistently
- Add comments to explain complex logic
- Test your programs thoroughly
What's next:
- Chapter 2: Functions and Files - Learn to break programs into reusable functions
- Chapter 3: Debugging - Master techniques for finding and fixing errors
- Chapter 4: Data Types - Explore different types of data in C++
Keep practicing with small programs, and don't hesitate to experiment with the concepts you've learned!
C++ Basics summary - Quiz
Test your understanding of the lesson.
Lesson Discussion
Share your thoughts and questions