Current Affairs updated daily
KalviGavaniKalviGavani
Class 11 · Tamil Nadu State Board

Introduction to C++ Exercise Solutions | KalviGavani

TN Class 11 Computer Science Introduction to C++ exercise solutions — tokens, structure, compilation, history with worked examples.

Updated 15 June 202613 min read

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

YearMilestone
1979Bjarne Stroustrup begins work on "C with Classes" at Bell Labs
1983Language renamed to C++
1985First commercial release of C++
1998ISO C++ standard (C++98) published
2011C++11 modern standard released
2017C++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 TypeDescriptionExamples
KeywordsReserved words with predefined meaningint, return, class, while, if
IdentifiersNames given by programmer to variables, functions, etc.myAge, total_marks, calculateArea
LiteralsConstant values used in the program42, 3.14, 'A', "Hello"
OperatorsSymbols that perform operations on operands+, -, *, /, ==, &&
PunctuatorsSymbols that separate or terminate program units;, {, }, (, ), ,

Compilation Process

StageInputOutputAction
Preprocessing.cppExpanded sourceIncludes headers, expands macros
CompilationExpanded sourceObject code (.obj/.o)Checks syntax, translates to machine code
LinkingObject code + librariesExecutable (.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 (Age and age are different)

Data Types in C++

Data TypeSize (bytes)Range
char1-128 to 127
int4-2,147,483,648 to 2,147,483,647
float4~7 decimal digits precision
double8~15 decimal digits precision
bool1true (1) or false (0)
void0No value (used for functions that don't return)

3. I. Choose the Best Answer (MCQs)

1. Who is the developer of C++?
Answer

(a) Dennis Ritchie (b) James Gosling (c) Bjarne Stroustrup (d) Guido van Rossum

Answer(c) Bjarne Stroustrup Bjarne Stroustrup developed C++ at Bell Labs in the early 1980s. Dennis Ritchie developed C, James Gosling developed Java, and Guido van Rossum developed Python.
2. Which of the following is a valid C++ identifier?
Answer

(a) 2name (b) my-var (c) _score (d) class

Answer(c) _score Identifiers can start with a letter or underscore but not a digit, so 2name is invalid. Hyphens are not allowed, so my-var is invalid. class is a keyword, so it cannot be used as an identifier. _score follows all rules.
3. The extension of a C++ source file is:
Answer

(a) .c (b) .cpp (c) .obj (d) .exe

Answer(b) .cpp C++ source files use the .cpp extension. .c is for C source files, .obj is for object files after compilation, and .exe is for the final executable.
4. Which of the following is NOT a C++ keyword?
Answer

(a) int (b) float (c) main (d) return

Answer(c) main main is a function name, not a keyword. While every C++ program needs a main() function, the word main itself is not reserved — it is an identifier. int, float, and return are all keywords.
5. Which symbol is used to end a C++ statement?
Answer

(a) . (b) : (c) ; (d) ,

Answer(c) ; Every C++ statement must end with a semicolon (;). This is a common source of errors for beginners. A period is used for member access, a colon for labels/initializers, and a comma separates items in a list.
6. The process of converting source code to object code is called:
Answer

(a) Linking (b) Loading (c) Compilation (d) Execution

Answer(c) Compilation Compilation translates source code into object code. Linking combines object code with libraries. Loading places the program in memory. Execution runs the program.

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 AColumn B
1. #includea. Links object code with libraries
2. Semicolon (;)b. Preprocessor directive
3. coutc. Output stream object
4. Linkerd. Terminates a statement
5. inte. Keyword for integer data type
6. //f. Single-line comment marker
7. cing. Input stream object
Answer1-b, 2-d, 3-c, 4-a, 5-e, 6-f, 7-g

7. V. Answer Briefly (2-mark questions)

Q1. What are tokens? Name the types of tokens in C++.
Answer

Tokens are the smallest indivisible units in a C++ program that the compiler can recognise. The five types of tokens are: (1) Keywords, (2) Identifiers, (3) Literals, (4) Operators, and (5) Punctuators. For example, in int x = 5;, the tokens are int, x, =, 5, and ;.

Q2. Differentiate between keywords and identifiers.
Answer
FeatureKeywordIdentifier
MeaningReserved word with fixed meaningName given by the programmer
CustomisationCannot be changed or reusedProgrammer-defined
Exampleint, return, classmyAge, total, Student
CountLimited (about 95 in C++)Unlimited
Q3. Write the steps involved in the compilation of a C++ program.
Answer

1. Preprocessing — The preprocessor expands macros, includes header files, and handles conditional compilation directives. 2. Compilation — The compiler checks syntax and translates the preprocessed source code into object code (machine code with unresolved references). 3. Linking — The linker combines the object code with necessary library functions to create the final executable file.

Q4. List the rules for naming identifiers in C++.
Answer
  • Must begin with a letter (A-Z, a-z) or underscore (_), never a digit.
  • Can contain letters, digits, and underscores after the first character.
  • Cannot be a keyword (e.g., class, int, return).
  • No spaces or special characters (other than _) are allowed.
  • C++ is case-sensitive, so Name and name are different identifiers.
Q5. What is the difference between cout and cin?
Answer
Featurecoutcin
PurposeOutput to screenInput from keyboard
OperatorInsertion <<Extraction >>
Stream classostreamistream
Examplecout << x;cin >> x;

8. VI. Answer in Detail (5-mark questions)

Q1. Explain the structure of a C++ program with a suitable example.
Answer

A C++ program has four main parts:

1. Header Section — Uses #include to include header files that provide declarations for library functions and objects.

2. Namespace Section — using namespace std; allows direct use of cout, cin, etc., without the std:: prefix.

3. Main Function — int main() is the entry point. Every executable C++ program must have exactly one main() function. It returns an int — 0 signals successful execution.

4. Statements — The body of main() contains declarations, input/output operations, computations, and a return statement.

#include <iostream>      // 1. Header section
using namespace std;      // 2. Namespace section

int main() {              // 3. Main function
    int num1, num2, sum;  // 4. Declaration
    cout << "Enter two numbers: ";
    cin >> num1 >> num2;  // Input
    sum = num1 + num2;    // Computation
    cout << "Sum = " << sum; // Output
    return 0;             // Return value
}
Q2. Explain the various types of tokens in C++ with examples.
Answer

C++ has five types of tokens:

1. Keywords — Reserved words that have a special meaning to the compiler. They cannot be used as identifiers. Examples: int, float, if, else, while, return, class, void.

2. Identifiers — Names given by the programmer to variables, functions, classes, etc. They must follow naming rules. Examples: studentName, _count, totalMarks, calculateAverage.

3. Literals — Constant values that appear directly in the program. Types include:

  • Integer literals: 10, -5, 0
  • Floating-point literals: 3.14, -0.5
  • Character literals: 'A', '9', '\n'
  • String literals: "Hello", "Tamil Nadu"
4. Operators — Symbols that perform operations on operands. Examples: Arithmetic (+, -, *, /, %), Relational (<, >, <=, >=, ==, !=), Logical (&&, `, !), Assignment (=, +=, -=`).

5. Punctuators — Symbols that organise and separate program elements. Examples: Semicolon ; (statement terminator), Braces { } (block delimiters), Parentheses ( ) (function parameters), Comma , (separator).

Q3. Describe the compilation process of a C++ program in detail.
Answer

The compilation of a C++ program involves three stages:

Stage 1 — Preprocessing: The preprocessor processes directives that begin with #. It includes the content of header files (e.g., #include <iostream> copies the iostream header into the source), replaces macros (e.g., #define PI 3.14 replaces every PI with 3.14), and handles conditional compilation (#ifdef, #ifndef). The output is an expanded source file with all directives resolved.

Stage 2 — Compilation: The compiler takes the expanded source file and performs two tasks: (a) syntax checking — it verifies that the code follows C++ grammar rules, and reports errors if violations are found; (b) code generation — it translates the valid source code into machine-level object code (.obj on Windows, .o on Linux). At this stage, external function calls (like library functions) are left unresolved.

Stage 3 — Linking: The linker resolves all external references by combining the object code with the necessary library code. For example, if your program uses cout, the linker connects your object code with the C++ standard library that implements cout. The final output is a complete executable file (.exe on Windows, a.out on Linux).

Source Code (.cpp) | v [Preprocessor] --> Expanded Source | v [Compiler] --> Object Code (.obj/.o) | v [Linker] + Libraries --> Executable (.exe)

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 cout and cin, 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

Frequently Asked Questions

A C++ program has header inclusion (#include), namespace declaration, main() function as the entry point, statements ending with semicolons, and return 0 for successful execution.
← Browse all Class 11 Exercise Solutions

Other question types — same chapter