Wk2-6 Content Flashcards

1
Q

What is the primary focus of procedural programming?

A

Focusing on a specific aim or end result with limited reusability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do procedures (or functions) in procedural programming relate to the main() function?

A

They are declared/defined independent of main() and called within the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the main() function in C++ compared to Java?

A

In C++, main() is a standalone function that returns an int, whereas in Java, it is a static public member of a Runnable class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the return value of 0 typically indicate in a C++ program?

A

It typically means a normal termination of the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe the basic structure of a C++ ‘Hello World!’ program.

A

The program includes function arguments, a return_type, and outputs ‘Hello World!’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the role of #include in C++?

A

It brings in a header, like iostream, effectively a collection of code written elsewhere.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do input/output streams work in C++?

A

Using cin for input and cout for output, with operators like &laquo_space;for insertion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain the difference between literals and variables in C++ output.

A

Literals have fixed explicit values, whereas variables do not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the significance of using namespace std in C++?

A

It allows for shorter syntax but can lead to name collisions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How is scope defined in C++ and what is its importance?

A

Scope is the context where a variable is visible and is defined by braces {}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the term ‘global scope’ mean in C++?

A

A variable or entity is visible anywhere in the file after its declaration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can main() in C++ have arguments? If yes, what are they?

A

Yes, int main(int argc, char *argv[]) where argc is the argument count and argv is an array of arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of the stoi function in C++?

A

It is used to convert strings to integers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a forward declaration in C++?

A

Declaring a function before defining it, typically before reaching main().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why is it advantageous to spread code across multiple files in C++?

A

It helps in organizing code, reusing functions, and easing debugging.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of header (.h) files in C++?

A

To declare data structures and function prototypes for use in other files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How does the C++ library cmath enhance programming capabilities?

A

It provides access to mathematical functions, like square root.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Is it necessary to include a return statement in C++ functions?

A

While it can be omitted, it is best practice to include it for clarity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are the advantages of using functions in procedural programming?

A

Code reusability, ease of maintenance, and improved program structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the difference between signed and unsigned types in C++?

A

Signed types can represent both positive and negative values, while unsigned types represent only non-negative values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How is the input/output functionality implemented in C++?

A

Through iostream library, using cin for input and cout for output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the role of endl in C++?

A

It is a manipulator used to insert a newline character and flush the output buffer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What does the term ‘unstructured code’ refer to in C++?

A

Code written without using functions or procedures, often in a sequential manner.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How does the concept of namespaces improve code organization in C++?

A

Namespaces prevent name collisions and organize code into logical groups.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How do you declare a pointer to an integer in C++?

A

int* ptr;
This declares a pointer ptr that can point to an integer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What are control structures in C++ used for?

A

Control structures are used to dictate the flow of control in a program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is the syntax of a simple if statement in C++?

A

if (condition) { statements }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

How does an if-else structure work in C++?

A

It executes the if block if the condition is true, else executes the else block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

In a C++ if-else-if ladder, how is the condition evaluated?

A

The conditions are evaluated in sequence and the block for the first true condition is executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

How do logical operators work in control structures in C++?

A

Logical operators like AND, OR, and NOT are used to combine or invert conditions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the NOT logical operator in C++ and how is it used?

A

The NOT operator (!) inverts the truth value of a condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

How does the AND operator (&&) function in C++ conditions?

A

It returns true if both conditions are true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What is the role of the OR operator (||) in C++?

A

It returns true if at least one of the conditions is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Describe the switch-case control structure in C++.

A

It allows the execution of one out of multiple blocks of code based on a variable’s value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

When is a switch-case structure more suitable than if-else?

A

When there are multiple outcomes based on the value of a single variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What are the types of loops available in C++?

A

Pre-test (for, while) and post-test (do-while) loops.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What is the significance of the for loop in C++?

A

The for loop provides a concise way of writing the loop structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

How do you write a for loop in C++?

A

for (initialisation; condition; update) { // statements }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

What is a variation of the for loop in C++?

A

Using multiple initialization expressions or omitting parts of the loop syntax for different behaviors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What is an infinite loop in C++ and how is it written?

A

An infinite loop continues forever and can be written as for (;;) { // statements }.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

How does the range-based for loop work in C++?

A

It iterates over elements in a range, like in an array or a container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

What is the basic structure of a while loop in C++?

A

while (condition) { // statements }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

How does the do-while loop differ from the while loop in C++?

A

The do-while loop executes its statements at least once before checking the condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

What is the importance of loop control in C++?

A

It helps in executing a set of statements repeatedly based on a condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

How can you terminate a loop prematurely in C++?

A

Using the break statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

What is the purpose of the continue statement in loops in C++?

A

It skips the current iteration and moves to the next iteration of the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

How do you ensure a loop does not become infinite in C++?

A

By ensuring the loop condition will eventually become false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

What is a nested loop in C++?

A

A loop inside another loop, allowing for more complex iterative processes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

How can control structures impact the readability of a C++ program?

A

Proper use of control structures can make a program easier to understand and maintain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

What are the advantages of using control structures effectively in programming?

A

They allow for better organization, flexibility, and efficient flow control in a program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

What is a pointer in C++?

A

A pointer is a variable that stores the memory address of another variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

How do you declare a pointer in C++?

A

A pointer is declared using the asterisk () symbol, e.g., int ptr;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

What is the use of the & operator in C++?

A

The & operator is used to obtain the memory address of a variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

How do you assign a value to a pointer in C++?

A

By using the & operator on a variable, e.g., ptr = &var;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

What is the role of the * operator when used with pointers?

A

The * operator is used to access the value at the memory address pointed to by the pointer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

How do you declare a reference in C++?

A

A reference is declared using the & symbol next to the type, e.g., int& ref = var;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

What is the difference between a pointer and a reference?

A

A pointer can be reassigned to point to different addresses, whereas a reference always refers to the same object it was initially assigned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

What are arrays in C++?

A

Arrays are collections of elements of the same type, stored in contiguous memory locations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

How do you declare an array in C++?

A

An array is declared by specifying the type, followed by square brackets containing the size, e.g., int arr[10];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

What is dynamic memory allocation in C++?

A

Dynamic memory allocation allows allocating memory during runtime using operators like new and delete.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

How does the new operator work in C++?

A

The new operator allocates memory on the heap for a variable and returns a pointer to it.

62
Q

What is the delete operator in C++?

A

The delete operator frees up the memory allocated by new when the memory is no longer needed.

63
Q

How do you create a dynamic array in C++?

A

A dynamic array is created using the new keyword, followed by the type and size, e.g., int* arr = new int[size];

64
Q

What is a memory leak in C++?

A

A memory leak occurs when dynamically allocated memory is not released back to the system using delete.

65
Q

How do you initialize an array in C++?

A

An array is initialized by enclosing the initial values in curly braces, e.g., int arr[] = {1, 2, 3};

66
Q

What is the significance of const in C++?

A

const is used to declare variables whose value cannot be changed after initialization.

67
Q

How do you pass an array to a function in C++?

A

Arrays are passed to functions by passing the name of the array, which is a pointer to its first element.

68
Q

What is the difference between passing by value and passing by reference?

A

Passing by value creates a copy of the variable, while passing by reference allows the function to modify the original variable.

69
Q

How do you allocate memory for a multi-dimensional array in C++?

A

By using the new keyword with the required dimensions, e.g., int** arr = new int*[rowSize]; for each row, arr[i] = new int[colSize];

70
Q

What is the null pointer in C++ and how is it used?

A

A null pointer points to nothing and is used to indicate that a pointer does not point to any valid memory location.

71
Q

What is pointer arithmetic in C++?

A

Pointer arithmetic involves adding or subtracting integers to or from pointers, which moves the pointer to different memory addresses.

72
Q

How do you deallocate a dynamic array in C++?

A

By using delete[] followed by the array name, e.g., delete[] arr;

73
Q

What is the size of a pointer in C++?

A

The size of a pointer in C++ is dependent on the architecture, typically 4 bytes on a 32-bit system and 8 bytes on a 64-bit system.

74
Q

How do you use a void pointer in C++?

A

A void pointer can point to any data type, but it cannot be dereferenced directly without typecasting.

75
Q

What are the advantages of using dynamic memory allocation?

A

Dynamic memory allocation allows for flexible management of memory and can allocate memory as needed during runtime.

76
Q

What is a struct in C++ and how is it used?

A

A struct is a user-defined data type that groups related variables of different data types.

77
Q

How do you define a struct in C++?

A

A struct is defined using the struct keyword followed by structure name and definition.

78
Q

What are the primary differences between a struct and a class in C++?

A

The main difference is that struct members are public by default, whereas class members are private.

79
Q

How can you access elements of a struct in C++?

A

Elements of a struct are accessed using the dot (.) operator.

80
Q

What is a union in C++?

A

A union is a special data type that allows storing different data types in the same memory location.

81
Q

When is it appropriate to use a union in C++?

A

Use a union when you need to store one of many objects of different types but never simultaneously.

82
Q

What is the difference between struct and union in terms of memory usage?

A

Struct allocates separate memory for each of its members, whereas union shares the same memory for all its members.

83
Q

How do you initialize an empty vector in C++?

A

std::vector<int> v; or std::vector<int> v{};</int></int>

84
Q

What is randomness in programming and how is it achieved in C++?

A

Randomness in programming refers to generating unpredictable results, achieved in C++ using the library.

85
Q

How do you generate random numbers in C++?

A

Use the default_random_engine and distribution classes from the library to generate random numbers.

86
Q

What is a probability distribution in C++?

A

A probability distribution is a function that describes the likelihood of obtaining the possible values of a random variable.

87
Q

What are the common types of probability distributions in C++?

A

Common types include uniform, normal, binomial, and exponential distributions.

88
Q

How do you include file handling capabilities in C++?

A

Include the library to use file streams for file handling in C++.

89
Q

What are the different types of file streams in C++?

A

The primary file streams are ifstream for reading, ofstream for writing, and fstream for both reading and writing.

90
Q

How do you open a file in C++?

A

Use the open() method of the file stream object with the file name as an argument.

91
Q

How do you close a file in C++?

A

Use the close() method of the file stream object.

92
Q

What is the importance of checking the status of a file stream in C++?

A

Checking the status helps to ensure that file operations like opening, reading, and writing are successful.

93
Q

How do you read data from a file in C++?

A

Use extraction operators (») or getline() function with ifstream objects to read data.

94
Q

How do you write data to a file in C++?

A

Use insertion operators («) with ofstream objects to write data to a file.

95
Q

What are text files and binary files in C++?

A

Text files store data in readable text format, whereas binary files store data in binary format.

96
Q

What are the advantages of using binary files over text files?

A

Binary files are more efficient in terms of storage and performance as they involve direct data writing and reading.

97
Q

How do you handle errors during file operations in C++?

A

Use file stream’s fail(), bad(), and eof() methods to check for different types of errors.

98
Q

What is buffering in file I/O and why is it used?

A

Buffering is the temporary storage of data during file I/O to enhance performance by reducing the number of direct disk accesses.

99
Q

What is the role of the std namespace in C++?

A

The std namespace includes features of the C++ Standard Library to avoid naming conflicts.

100
Q

How do you read and write complex data types to a file in C++?

A

Serialize complex data types into a format suitable for file I/O, such as converting objects into byte streams for binary files.

101
Q

What is an exception in C++?

A

An exception is an unusual occurrence during execution, often representing errors.

102
Q

How do you throw an exception in C++?

A

Use the ‘throw’ keyword followed by an exception object or value.

103
Q

What is the purpose of a try block in C++?

A

A try block encloses code that might throw an exception.

104
Q

How do you catch an exception in C++?

A

Use a catch block following a try block to handle exceptions.

105
Q

What types of exceptions can be thrown in C++?

A

Any type, including basic data types or objects.

106
Q

How does exception handling improve C++ programs?

A

It separates error handling from regular code, improving readability and reliability.

107
Q

What happens if an exception is not caught in C++?

A

The program terminates and the default handler is invoked.

108
Q

Can multiple exceptions be caught in C++?

A

Yes, by using multiple catch blocks for different exception types.

109
Q

What is a namespace in C++?

A

A namespace is a declarative region that provides a scope to identifiers.

110
Q

How do you declare a namespace in C++?

A

Using the ‘namespace’ keyword followed by the namespace name and a block of code.

111
Q

What is the purpose of the std namespace in C++?

A

It contains standard library functions and avoids name conflicts.

112
Q

How can you use elements from a namespace in C++?

A

By using the scope resolution operator (::) or the ‘using’ directive.

113
Q

What is the syntax to use a specific element from a namespace?

A

namespace_name::element_name

114
Q

Can namespaces be nested in C++?

A

Yes, namespaces can be defined within other namespaces.

115
Q

What are unnamed or anonymous namespaces?

A

Namespaces without a name, used for defining items with internal linkage.

116
Q

What is the advantage of using namespaces?

A

They help avoid name conflicts and organize code logically.

117
Q

How can you create an alias for a namespace?

A

Using the syntax ‘namespace alias = existing_namespace;’

118
Q

Why is exception handling preferred over traditional error handling in C++?

A

It provides a cleaner and more flexible way to handle errors and exceptions.

119
Q

Can a catch block rethrow an exception in C++?

A

Yes, using the ‘throw;’ statement within a catch block.

120
Q

What is exception propagation in C++?

A

The process by which an exception is passed up the function call stack until caught.

121
Q

How do you initialize a vector with specific values (e.g., 1, 2, 3, 4, 5)?

A

std::vector<int> v = {1, 2, 3, 4, 5};</int>

122
Q

What are standard exception classes in C++?

A

Classes like std::runtime_error, std::logic_error, and their derived classes.

123
Q

How do you handle exceptions from standard library functions?

A

By enclosing library calls in a try block and catching exceptions.

124
Q

What is the significance of exception specifications in C++?

A

They declare which exceptions a function might throw, but are not recommended in modern C++.

125
Q

How do namespaces and exceptions collectively enhance C++ programming?

A

Namespaces organize code and avoid conflicts, while exceptions provide robust error handling.

126
Q

What is defensive programming in C++?

A

A methodology aimed at improving software and source code quality.

127
Q

What are some key practices in defensive programming?

A

Consistent indentation, meaningful error messages, and handling all possible input cases.

128
Q

Why is consistent indentation important in C++?

A

It makes the source code easier to read and debug.

129
Q

What is the role of the default case in a switch statement in C++?

A

It acts as a catch-all for any cases not explicitly handled.

130
Q

What is the significance of time handling in C++ programming?

A

Time handling is important for operations like timeouts, delays, and measuring durations.

131
Q

What are the main time types defined in C++?

A

Clocks, time points, and durations.

132
Q

How do you make a C++ program wait for a specified time?

A

Use std::this_thread::sleep_for with a duration, like std::chrono::seconds or milliseconds.

133
Q

What are some common time durations used in C++?

A

Hours, minutes, seconds, milliseconds, microseconds, nanoseconds.

134
Q

What does preprocessing in C++ involve?

A

It includes operations like file inclusion, macro definition, and conditional compilation.

135
Q

What is the syntax for defining a macro in C++?

A

define MACRO replacement-text.

136
Q

What is the use of #define in C++?

A

It is used for defining macros, often for constants or compiler directives.

137
Q

How do you use extern keyword in C++?

A

extern is used to indicate a variable is defined elsewhere.

138
Q

What are predefined macros in C++?

A

Macros like __TIME__, __DATE__, __LINE__, __FILE__, and __func__ for debugging purposes.

139
Q

How do you compile C++ code with debugging enabled?

A

Use the -DDEBUG flag with the g++ compiler.

140
Q

What is the use of a Makefile in C++?

A

A Makefile automates the build process, specifying how to compile and link the program.

141
Q

What is the GNU Project Debugger (gdb) in C++?

A

A debugger that helps in examining what is happening inside a program while it runs.

142
Q

How do you run a program in gdb?

A

Use the command ‘gdb program_name’ followed by ‘run’.

143
Q

What is the purpose of breakpoints in debugging?

A

Breakpoints allow pausing program execution at specific points to examine the current state.

144
Q

How can you print a variable’s value in gdb?

A

Use the ‘print variable_name’ command in gdb.

145
Q

What is the importance of clearing the cin buffer in C++?

A

Clearing the cin buffer prevents unwanted input from affecting subsequent reads.

146
Q

What is the purpose of the cin.ignore() function in C++?

A

It discards characters in the input buffer, often used before cin.get().

147
Q

What is the significance of the -o option in g++?

A

It specifies the output filename for the compiled program.

148
Q

Why is defensive programming important?

A

It helps in creating more reliable, readable, and maintainable code.

149
Q

How does preprocessing aid in C++ development?

A

Preprocessing provides flexibility in code compilation and can optimize performance.

150
Q

What are the benefits of using a debugger like gdb?

A

It allows for detailed inspection and troubleshooting of runtime behavior in C++ programs.