Section A — 1 Mark Questions
1. What is the main difference between C and C++ regarding programming paradigms? [1 Mark]
Answer: C is a procedure-oriented language, while C++ is primarily an object-oriented programming language.
2. Which header file is typically used for input/output operations in C++? [1 Mark]
Answer: iostream
3. What is a token in C++? [1 Mark]
Answer: A token is the smallest individual unit of a program, such as keywords, identifiers, constants, strings, or operators.
4. Identify the C++ keyword used to output data to the console. [1 Mark]
Answer: cout
5. What are identifiers in C++? [1 Mark]
Answer: Identifiers are the names given to various program elements such as variables, functions, arrays, and classes.
6. Define a constant in C++. [1 Mark]
Answer: A constant is a data item whose value cannot change during the program's execution.
7. What is the purpose of a reference variable in C++? [1 Mark]
Answer: A reference variable acts as an alias for an existing variable.
8. What is the operator used to define a reference variable? [1 Mark]
Answer: The ampersand symbol (&).
9. How are comments represented in C++? [1 Mark]
Answer: Single-line comments use // and multi-line comments use /* ... */.
10. What is the default return type of the main function in C++? [1 Mark]
Answer: int
11. State one characteristic of a keyword. [1 Mark]
Answer: Keywords are reserved words in the language that have special, predefined meanings and cannot be used as identifiers.
12. What is the purpose of the linker in a C++ program? [1 Mark]
Answer: The linker combines separate object files into a single executable file.
13. Which data type is used to store characters in C++? [1 Mark]
Answer: char
14. Is C++ case-sensitive? [1 Mark]
Answer: Yes, C++ is case-sensitive, meaning 'Variable' and 'variable' are treated as different identifiers.
15. What does the #include directive do? [1 Mark]
Answer: It tells the preprocessor to include the contents of a specific header file in the source code.
16. What is the purpose of the semicolon (;) in C++? [1 Mark]
Answer: It acts as a statement terminator.
17. What is a floating-point data type used for? [1 Mark]
Answer: It is used to store numbers with fractional parts, such as float or double.
18. What does the 'using namespace std;' statement imply? [1 Mark]
Answer: It allows the programmer to use elements of the standard namespace without explicitly using the prefix 'std::'.
19. Can an identifier start with a digit? [1 Mark]
Answer: No, an identifier must start with an underscore or a letter.
20. What is the role of the compiler? [1 Mark]
Answer: The compiler translates human-readable source code into machine-readable object code.
Section B — 2/3 Mark Questions
1. Differentiate between keywords and identifiers. [2 Marks]
Answer: Keywords are reserved words with predefined meanings in the compiler, whereas identifiers are user-defined names for variables, functions, or classes. Keywords cannot be renamed or repurposed by the user, while identifiers are created based on naming rules.
2. Explain the concept of a reference variable with a small example. [3 Marks]
Answer: A reference variable provides an alternative name for an already existing variable. Changes made to the reference variable are reflected in the original variable. Example: int x = 10; int &ref = x; Here, 'ref' is an alias for 'x', so updating 'ref' updates 'x'.
3. Why is C++ considered an extension of C? [2 Marks]
Answer: C++ is largely compatible with C syntax but adds object-oriented features such as classes, inheritance, polymorphism, and encapsulation, which are not present in procedural C.
4. What is the significance of compiling and linking? [3 Marks]
Answer: Compiling converts source code into an object file (machine code). Linking takes these object files and library files to resolve external references, ultimately creating a single executable program that can be run on the operating system.
5. List any three fundamental data types available in C++. [3 Marks]
Answer: 1. int (for integers), 2. float (for floating-point numbers), and 3. char (for single characters).
6. Describe the structure of a simple C++ program. [2 Marks]
Answer: A basic C++ program typically includes necessary header files (e.g., #include <iostream>), a namespace declaration, the main function (int main()), and the executable code within the main function block, ending with a return statement.
7. What are constants and how are they defined in C++? [2 Marks]
Answer: Constants are fixed values that do not change during program execution. They are usually defined using the 'const' keyword, e.g., 'const int MAX = 100;' which ensures that any attempt to modify 'MAX' will result in a compiler error.
8. Compare 'char' and 'int' data types. [3 Marks]
Answer: 'int' is primarily used to store numerical integer values and occupies more memory (usually 4 bytes). 'char' is used to store a single character or its ASCII numerical equivalent and occupies 1 byte. They differ in purpose, storage size, and range of values.
9. What is the 'cout' object? [2 Marks]
Answer: 'cout' is the standard output stream object in C++ associated with the 'iostream' library. It is used to send data to the standard output device, which is typically the monitor screen, using the insertion operator (<<).
10. Explain the role of the 'main' function. [3 Marks]
Answer: The main function acts as the entry point of every C++ program. The operating system calls the main function to start program execution. Every C++ program must contain exactly one main function to be valid and executable.
Section C — 5 Mark Questions
1. Explain the compilation and linking process in detail. [5 Marks]
Introduction
The process of converting a high-level C++ source code file into an executable file is a multi-step procedure involving the preprocessor, compiler, and linker.
The Process
- Preprocessing: The preprocessor handles directives starting with #, such as #include, inserting the content of header files into the source code.
- Compilation: The compiler translates the source code into assembly language or machine-level object code. It checks for syntax errors during this phase.
- Linking: The linker takes one or more object files and library files and combines them. It resolves external function calls to their definitions, creating a single final executable file.
Outcome
If any stage, particularly the compilation or linking, fails due to syntax or missing library definitions, the process stops, and the programmer must rectify the errors before an executable file is generated.
2. Discuss the data types in C++ with their usage. [5 Marks]
Introduction
Data types in C++ define the type and size of data that a variable can hold, ensuring the compiler allocates the correct amount of memory.
Categories of Data Types
- Integral Types: These include 'int', 'short', and 'long', used for representing whole numbers. 'char' is also considered an integral type as it stores character values as integers based on ASCII.
- Floating-point Types: These include 'float' and 'double'. 'float' is used for single-precision decimals, while 'double' is used for higher precision requirements.
- Void Type: Represents the absence of a type, typically used as the return type for functions that do not return a value.
- Boolean Type: The 'bool' type represents truth values, either 'true' or 'false'.
Importance
Choosing the correct data type is critical for memory optimization and preventing overflow or precision loss during arithmetic operations.
3. Describe the features of C++ that distinguish it from C. [5 Marks]
Introduction
C++ is often called a 'superset' of C because it retains the features of C while introducing powerful new paradigms.
Key Distinctions
- Paradigm: C is strictly procedural; C++ adds Object-Oriented Programming (OOP) concepts such as classes, objects, inheritance, and encapsulation.
- Standard Library: C++ offers a comprehensive Standard Template Library (STL) that includes pre-built algorithms and data structures.
- Reference Variables: C++ supports reference variables as aliases, whereas C primarily relies on pointers for indirect access.
- Input/Output: C++ uses streams (cin/cout) for I/O, which are typesafe and extensible, compared to C's function-based approach (scanf/printf).
- Function Overloading: C++ allows multiple functions to have the same name provided their parameter signatures differ, a feature not supported in C.
4. Explain tokens in C++ with examples for each type. [5 Marks]
Introduction
Tokens are the smallest building blocks of a C++ program. The compiler breaks down the source code into these tokens to parse and understand the logic.
Types of Tokens
- Keywords: Reserved words like 'int', 'return', 'if', and 'while'. These cannot be redefined.
- Identifiers: User-defined names like 'myVariable' or 'calculateSum'. They must follow naming conventions.
- Constants: Fixed literals like '10', '3.14', or 'A'. Their value cannot be altered.
- Operators: Symbols that perform operations like '+', '-', '*', '/', and '='.
- Special Characters: Symbols like '{', '}', '(', ')', and ';', which define the structure of the program.
5. Write a detailed note on the structure and execution flow of a C++ program. [5 Marks]
Structure
A C++ program follows a standard structure that enables readability and organization. It starts with preprocessor directives for library access, followed by namespace definitions, and then the function definitions.
Execution Flow
- Step 1: The program begins execution at the 'main()' function.
- Step 2: Statements are executed sequentially from top to bottom within the main block.
- Step 3: Variable declarations allocate memory as needed.
- Step 4: Input/Output operations facilitate interaction with the user through 'cin' and 'cout'.
- Step 5: Control structures (if/else/loops) may alter the flow, allowing for conditional execution or repetition.
- Step 6: The program terminates when the 'main' function returns a value to the operating system, typically '0' indicating successful execution.