Theory Based Question Answers of C
Theory Based Question Answers of C
---
2. **Procedural Programming**:
- Retains the procedural programming style of C, allowing for
structured programming.
4. **Function Overloading**:
- Allows multiple functions with the same name but different
parameters.
5. **Operator Overloading**:
- Enables defining custom behavior for operators (e.g., `+`, `-`, `*`)
for user-defined types.
6. **Exception Handling**:
- Provides mechanisms for handling runtime errors using `try`,
`catch`, and `throw`.
7. **Namespaces**:
- Helps avoid naming conflicts by grouping entities under a unique
scope.
9. **Templates**:
- Supports generic programming by allowing functions and classes to
operate with any data type.
11. **Multi-threading**:
- Supports concurrent programming through libraries like
`<thread>`.
12. **Portability**:
- C++ code can be compiled and run on various platforms with
minimal changes.
---
**Tokens** are the smallest individual units in a C++ program. They are
the building blocks of a program and are recognized by the compiler.
Tokens include:
---
#### **Keywords**:
- Keywords are reserved words that have special meaning in C++.
- They cannot be used as identifiers (e.g., variable names).
- Examples: `int`, `if`, `else`, `while`, `return`, `class`, `public`, etc.
#### **Identifiers**:
- Identifiers are names given to variables, functions, classes, etc.
- Rules for identifiers:
- Must start with a letter or underscore (`_`).
- Can contain letters, digits, or underscores.
- Cannot be a keyword.
- Case-sensitive.
- Examples: `sum`, `myVariable`, `calculateArea`, `_count`.
#### **Constants/Literals**:
- Constants are fixed values that do not change during program
execution.
- Literals are the actual values assigned to variables or constants.
- Examples:
- Integer literal: `10`
- Floating-point literal: `3.14`
- Character literal: `'A'`
- String literal: `"Hello"`
---
### 3. Explain the different data types in C++.
2. **Floating-Point Types**:
- `float`: Single-precision floating-point numbers (e.g., `3.14f`).
- `double`: Double-precision floating-point numbers (e.g., `3.14`).
- `long double`: Extended precision floating-point numbers.
3. **Character Types**:
- `char`: Stores a single character (e.g., `'A'`).
- `wchar_t`: Wide character for larger character sets.
4. **Boolean Type**:
- `bool`: Stores `true` or `false`.
2. **Pointers**:
- Stores memory addresses (e.g., `int *ptr;`).
3. **References**:
- Alias for a variable (e.g., `int &ref = a;`).
2. **Classes (`class`)**:
- Blueprint for objects, supporting OOP features.
3. **Unions (`union`)**:
- Similar to structures but shares memory for its members.
4. **Enumerations (`enum`)**:
- Defines a set of named integer constants.
---
---
1. **Conditional Statements**:
- `if`, `else if`, `else`: Execute code based on a condition.
- `switch`: Execute code based on the value of a variable.
2. **Looping Statements**:
- **`for` loop**: Repeats a block of code a specific number of times.
```cpp
for (int i = 0; i < 5; i++) {
cout << i << endl;
}
```
- **`while` loop**: Repeats a block of code as long as a condition is
true.
```cpp
int i = 0;
while (i < 5) {
cout << i << endl;
i++;
}
```
- **`do-while` loop**: Executes the block of code at least once, then
repeats as long as the condition is true.
```cpp
int i = 0;
do {
cout << i << endl;
i++;
} while (i < 5);
```
3. **Jump Statements**:
- `break`: Exits a loop or switch statement.
- `continue`: Skips the current iteration of a loop.
- `return`: Exits a function and optionally returns a value.
- `goto`: Jumps to a labeled statement (rarely used).
---
**Example**:
```cpp
int i = 5;
while (i < 5) { // This loop will not execute
cout << i << endl;
i++;
}
---
2. **User-Defined Functions**:
- Functions defined by the programmer.
- Example:
```cpp
int add(int a, int b) {
return a + b;
}
```
3. **Parameterized Functions**:
- Functions that accept parameters (arguments).
4. **Non-Parameterized Functions**:
- Functions that do not accept any parameters.
---
**Example**:
```cpp
#include <iostream>
using namespace std;
int main() {
cout << add(2, 3) << endl; // Calls first function
cout << add(2, 3, 4) << endl; // Calls second function
cout << add(2.5, 3.5) << endl; // Calls third function
return 0;
}
```
**Output**:
```
5
9
6.0
```
---
**Example**:
```cpp
#include <iostream>
using namespace std;
// Call by value
void incrementByValue(int a) {
a++;
}
// Call by reference
void incrementByReference(int &a) {
a++;
}
int main() {
int x = 5;
incrementByValue(x); // x remains 5
cout << x << endl;
incrementByReference(x); // x becomes 6
cout << x << endl;
return 0;
}
```
**Output**:
```
5
6
```
---
**Inline Functions**:
- Inline functions are functions where the compiler replaces the function
call with the actual code of the function to reduce the overhead of
function calls.
- Defined using the `inline` keyword.
**Example**:
```cpp
#include <iostream>
using namespace std;
int main() {
cout << square(5) << endl; // Compiler replaces square(5) with 5 * 5
return 0;
}
```
**Advantages**:
1. **Reduced Overhead**:
- Eliminates the overhead of function calls (saving and restoring
registers, stack operations, etc.).
2. **Faster Execution**:
- Suitable for small, frequently called functions.
3. **Improved Performance**:
- Useful in performance-critical applications.
**Disadvantages**:
1. **Increased Binary Size**:
- The code is duplicated at each call site, increasing the size of the
executable.
2. **Not Suitable for Large Functions**:
- Inlining large functions can lead to code bloat.