Exercise Solutions Available
Step-by-step answers for all textbook questions in this chapter
1. How to Use This Page
1. Solve every exercise on your own first. Attempt each question before reading the solution. 2. Compare your method, not just the final answer. The process matters in the board exam. 3. Pay attention to the explanations — they clarify why an answer is correct, which helps with similar questions. 4. Revisit the "Common Mistakes" section before every test to avoid repeat errors. 5. Use the Exam Checklist on the night before the exam for a quick final review.
2. Key Concepts (Quick Recap)
C++ Language Timeline
| Year | Milestone |
|---|---|
| 1979 | Bjarne Stroustrup begins work on "C with Classes" at Bell Labs |
| 1983 | Language renamed to C++ |
| 1985 | First commercial release of C++ |
| 1998 | ISO C++ standard (C++98) published |
| 2011 | C++11 modern standard released |
| 2017 | C++17 standard released |
Structure of a C++ Program
// 1. Header inclusion
#include <iostream>
// 2. Namespace
using namespace std;
// 3. Main function — program entry point
int main() {
// 4. Statements
cout << "Hello, Tamil Nadu!";
// 5. Return value
return 0;
}
C++ Tokens
A token is the smallest indivisible unit in a C++ program.
| Token Type | Description | Examples |
|---|---|---|
| Keywords | Reserved words with predefined meaning | int, return, class, while, if |
| Identifiers | Names given by programmer to variables, functions, etc. | myAge, total_marks, calculateArea |
| Literals | Constant values used in the program | 42, 3.14, 'A', "Hello" |
| Operators | Symbols that perform operations on operands | +, -, *, /, ==, && |
| Punctuators | Symbols that separate or terminate program units | ;, {, }, (, ), , |
Compilation Process
| Stage | Input | Output | Action |
|---|---|---|---|
| Preprocessing | .cpp | Expanded source | Includes headers, expands macros |
| Compilation | Expanded source | Object code (.obj/.o) | Checks syntax, translates to machine code |
| Linking | Object code + libraries | Executable (.exe) | Resolves external references, combines modules |
Identifier Rules
- Must begin with a letter (A-Z, a-z) or underscore (
_) - Cannot start with a digit
- Cannot be a keyword
- No special characters except underscore
- Case-sensitive (
Ageandageare different)
Data Types in C++
| Data Type | Size (bytes) | Range |
|---|---|---|
char | 1 | -128 to 127 |
int | 4 | -2,147,483,648 to 2,147,483,647 |
float | 4 | ~7 decimal digits precision |
double | 8 | ~15 decimal digits precision |
bool | 1 | true (1) or false (0) |
void | 0 | No value (used for functions that don't return) |
3. I. Choose the Best Answer (MCQs)
4. II. Fill in the Blanks
1. C++ was developed by Bjarne Stroustrup at Bell Labs. 2. The original name of C++ was "C with Classes". 3. The smallest indivisible unit in a C++ program is called a token. 4. The header file used for input and output operations is <iostream>. 5. cout is an object of the ostream class. 6. cin is an object of the istream class. 7. The main() function is the entry point of every C++ program. 8. The insertion operator used with cout is <<. 9. The extraction operator used with cin is >>. 10. Keywords are also called reserved words because they cannot be used as identifiers. 11. A literal is a constant value that appears directly in the program. 12. The compilation stage that expands macros and includes header files is called preprocessing. 13. An identifier cannot begin with a digit. 14. The linker combines object code with library code to produce an executable.
5. III. True or False (with corrections)
1. C++ is a purely object-oriented programming language.
False. C++ is a multi-paradigm language that supports both procedural and object-oriented programming. You can write C-style procedural code in C++.
2. Every C++ statement must end with a semicolon.
True. All executable statements, declarations, and class definitions end with a semicolon. The only exceptions are compound statements (blocks enclosed in {}).
3. The main() function can be omitted in a C++ program.
False. Every executable C++ program must have exactly one main() function. It is the designated entry point where execution begins.
4. Keywords can be used as variable names in C++.
False. Keywords are reserved by the language and cannot be used as identifiers. For example, you cannot write int class = 5; because class is a keyword.
5. C++ is case-sensitive.
True. C++ distinguishes between uppercase and lowercase letters. The identifiers Score, score, and SCORE are all different.
6. The preprocessor produces object code.
False. The preprocessor only handles macro expansion, conditional compilation, and header inclusion. It produces an expanded source file, not object code. The compiler produces object code.
6. IV. Match the Following
| Column A | Column B |
|---|---|
1. #include | a. Links object code with libraries |
2. Semicolon (;) | b. Preprocessor directive |
3. cout | c. Output stream object |
| 4. Linker | d. Terminates a statement |
5. int | e. Keyword for integer data type |
6. // | f. Single-line comment marker |
7. cin | g. Input stream object |
7. V. Answer Briefly (2-mark questions)
cout and cin?8. VI. Answer in Detail (5-mark questions)
9. Common Mistakes to Avoid
1. Forgetting the semicolon at the end of a statement — the #1 beginner error. Every statement, declaration, and class definition must end with ;.
2. Using = instead of == — = is assignment, == is comparison. if (x = 5) assigns 5 to x and is always true; if (x == 5) compares.
3. Missing using namespace std; — Without this, you must write std::cout, std::cin, etc. Beginners often forget the namespace and get "undeclared identifier" errors.
4. Confusing >> and << — cin >> x (extraction, input) pulls data from the stream into the variable; cout << x (insertion, output) pushes data from the variable into the stream.
5. Starting identifiers with a digit — 2name is invalid; name2 is valid.
6. Using keywords as identifiers — Words like int, class, return cannot be used as variable names.
7. Mismatched braces { } — Every opening brace must have a corresponding closing brace. Use consistent indentation to avoid this error.
8. Confusing object code with executable — Object code (.obj) is not yet linked; the executable (.exe) is the final runnable file after linking.
10. Exam Checklist
- [ ] I can list the five types of C++ tokens with examples
- [ ] I can write the structure of a C++ program (header, namespace, main, statements)
- [ ] I know the rules for naming identifiers
- [ ] I can distinguish between keywords, identifiers, literals, operators, and punctuators
- [ ] I can explain the three stages of compilation (preprocessing, compilation, linking)
- [ ] I can identify the developer of C++ (Bjarne Stroustrup) and its origin (C with Classes)
- [ ] I know the difference between
coutandcin, and their operators (<<vs>>) - [ ] I can explain what
#include <iostream>does - [ ] I can list at least 10 C++ keywords
- [ ] I understand that C++ is case-sensitive and why it matters
