Wk2-6 Content Flashcards
What is the primary focus of procedural programming?
Focusing on a specific aim or end result with limited reusability.
How do procedures (or functions) in procedural programming relate to the main() function?
They are declared/defined independent of main() and called within the program.
What is the main() function in C++ compared to Java?
In C++, main() is a standalone function that returns an int, whereas in Java, it is a static public member of a Runnable class.
What does the return value of 0 typically indicate in a C++ program?
It typically means a normal termination of the program.
Describe the basic structure of a C++ ‘Hello World!’ program.
The program includes function arguments, a return_type, and outputs ‘Hello World!’.
What is the role of #include in C++?
It brings in a header, like iostream, effectively a collection of code written elsewhere.
How do input/output streams work in C++?
Using cin for input and cout for output, with operators like «_space;for insertion.
Explain the difference between literals and variables in C++ output.
Literals have fixed explicit values, whereas variables do not.
What is the significance of using namespace std in C++?
It allows for shorter syntax but can lead to name collisions.
How is scope defined in C++ and what is its importance?
Scope is the context where a variable is visible and is defined by braces {}.
What does the term ‘global scope’ mean in C++?
A variable or entity is visible anywhere in the file after its declaration.
Can main() in C++ have arguments? If yes, what are they?
Yes, int main(int argc, char *argv[]) where argc is the argument count and argv is an array of arguments.
What is the purpose of the stoi function in C++?
It is used to convert strings to integers.
What is a forward declaration in C++?
Declaring a function before defining it, typically before reaching main().
Why is it advantageous to spread code across multiple files in C++?
It helps in organizing code, reusing functions, and easing debugging.
What is the purpose of header (.h) files in C++?
To declare data structures and function prototypes for use in other files.
How does the C++ library cmath enhance programming capabilities?
It provides access to mathematical functions, like square root.
Is it necessary to include a return statement in C++ functions?
While it can be omitted, it is best practice to include it for clarity.
What are the advantages of using functions in procedural programming?
Code reusability, ease of maintenance, and improved program structure.
What is the difference between signed and unsigned types in C++?
Signed types can represent both positive and negative values, while unsigned types represent only non-negative values.
How is the input/output functionality implemented in C++?
Through iostream library, using cin for input and cout for output.
What is the role of endl in C++?
It is a manipulator used to insert a newline character and flush the output buffer.
What does the term ‘unstructured code’ refer to in C++?
Code written without using functions or procedures, often in a sequential manner.
How does the concept of namespaces improve code organization in C++?
Namespaces prevent name collisions and organize code into logical groups.
How do you declare a pointer to an integer in C++?
int* ptr;
This declares a pointer ptr that can point to an integer.
What are control structures in C++ used for?
Control structures are used to dictate the flow of control in a program.
What is the syntax of a simple if statement in C++?
if (condition) { statements }
How does an if-else structure work in C++?
It executes the if block if the condition is true, else executes the else block.
In a C++ if-else-if ladder, how is the condition evaluated?
The conditions are evaluated in sequence and the block for the first true condition is executed.
How do logical operators work in control structures in C++?
Logical operators like AND, OR, and NOT are used to combine or invert conditions.
What is the NOT logical operator in C++ and how is it used?
The NOT operator (!) inverts the truth value of a condition.
How does the AND operator (&&) function in C++ conditions?
It returns true if both conditions are true.
What is the role of the OR operator (||) in C++?
It returns true if at least one of the conditions is true.
Describe the switch-case control structure in C++.
It allows the execution of one out of multiple blocks of code based on a variable’s value.
When is a switch-case structure more suitable than if-else?
When there are multiple outcomes based on the value of a single variable.
What are the types of loops available in C++?
Pre-test (for, while) and post-test (do-while) loops.
What is the significance of the for loop in C++?
The for loop provides a concise way of writing the loop structure.
How do you write a for loop in C++?
for (initialisation; condition; update) { // statements }
What is a variation of the for loop in C++?
Using multiple initialization expressions or omitting parts of the loop syntax for different behaviors.
What is an infinite loop in C++ and how is it written?
An infinite loop continues forever and can be written as for (;;) { // statements }.
How does the range-based for loop work in C++?
It iterates over elements in a range, like in an array or a container.
What is the basic structure of a while loop in C++?
while (condition) { // statements }
How does the do-while loop differ from the while loop in C++?
The do-while loop executes its statements at least once before checking the condition.
What is the importance of loop control in C++?
It helps in executing a set of statements repeatedly based on a condition.
How can you terminate a loop prematurely in C++?
Using the break statement.
What is the purpose of the continue statement in loops in C++?
It skips the current iteration and moves to the next iteration of the loop.
How do you ensure a loop does not become infinite in C++?
By ensuring the loop condition will eventually become false.
What is a nested loop in C++?
A loop inside another loop, allowing for more complex iterative processes.
How can control structures impact the readability of a C++ program?
Proper use of control structures can make a program easier to understand and maintain.
What are the advantages of using control structures effectively in programming?
They allow for better organization, flexibility, and efficient flow control in a program.
What is a pointer in C++?
A pointer is a variable that stores the memory address of another variable.
How do you declare a pointer in C++?
A pointer is declared using the asterisk () symbol, e.g., int ptr;
What is the use of the & operator in C++?
The & operator is used to obtain the memory address of a variable.
How do you assign a value to a pointer in C++?
By using the & operator on a variable, e.g., ptr = &var;
What is the role of the * operator when used with pointers?
The * operator is used to access the value at the memory address pointed to by the pointer.
How do you declare a reference in C++?
A reference is declared using the & symbol next to the type, e.g., int& ref = var;
What is the difference between a pointer and a reference?
A pointer can be reassigned to point to different addresses, whereas a reference always refers to the same object it was initially assigned.
What are arrays in C++?
Arrays are collections of elements of the same type, stored in contiguous memory locations.
How do you declare an array in C++?
An array is declared by specifying the type, followed by square brackets containing the size, e.g., int arr[10];
What is dynamic memory allocation in C++?
Dynamic memory allocation allows allocating memory during runtime using operators like new and delete.