Advanced Programming in C++ Flashcards

1
Q

Why do we use C/C++?

A
  • Complete, non-simplified
  • Efficient
  • Can handle high-performance tasks like simulations, real-time processing, and visualization
  • Language of choice for embedded systems
  • Can be made safe with proper education
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the main steps in the C++ compilation process?

A
  • Preprocessing
  • Compilation (converting source code to object code)
  • Linking (combining object code with libraries to create an executable)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of the preprocessing stage in C++ compilation?

A

Preprocessing handles directives like #include and #define, preparing the code for compilation

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

What does the term “data type” refer to in C++?

A

A data type specifies the kind of data a variable can hold, such as int, char, or float.

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

What are the main categories of primitive data types in C++?

A
  • Integer Numbers (int)
  • Character (char)
  • Booleans (bool)
  • Floating Point (float)
  • Void (void)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you declare and initialize a variable in C++?

A

Use the syntax scope type identifier = value;
(e.g., global int x = 5;)

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

What is a scope in C++?

A

A scope defines the region of code where a variable is accessible. Variables in a local scope (like inside a function) are not accessible outside of it.

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

What are the 3 different types of scope?

A
  • Global: Variable is declared outside all functions and is accessible throughout the program, but not from other files
  • Local: Variable is only accessible within the function or block where it is declared, only exists during an invocation and ceases to exist at the end of it
  • Local Statics: Variable is declared during first invocation and doesn’t cease to exist at the end of it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between stack and heap memory?

A

Stack memory is used for local variables and function calls, while heap memory is for dynamic allocation, managed manually

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

What are the roles of new and delete in C++?

A
  • new allocates memory on the heap
  • delete deallocates memory to avoid memory leaks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is malloc and how is it used in C++?

A
  • Function in C++ to allocate a block of memory on the heap
  • Returns a pointer to the beginning of the block
  • Unlike new, malloc does not call constructors and requires explicit casting of the returned pointer to the desired data type
  • Memory allocated with malloc should be deallocated using free()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the 3 elements of a flowchart?

A
  • Activity (rectangular block, e.g. instructions, subprograms, …)
  • Decision (diamond, condition (e.g. Boolean expression) is specified)
  • Terminal activity (rounded rectangle)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the structured program theorem?

A

Flow graphs can compute any computable function by composing the following 3 control structures:
- Sequential execution (execute one subprogram and after that another one)
- Conditional branch, selection (execute one of two subprograms according to the value of a Boolean expression)
- Loop (repeat execution of a subprogram as long as a Boolean expression evaluates to true each time before executing the subprogram)

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

What is a block scope in C++?

A
  • Block scope refers to variables declared within a block of code, enclosed by {}
  • These variables are only accessible within that block
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does a switch statement work in C++?

A
  • A switch statement evaluates an expression and executes the corresponding case block
  • If no cases match, the default block (if provided) is executed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of the break statement in a switch case?

A

break exits the switch statement to prevent “fall-through” to the next case (execution continues until break is encountered)

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

What is a function in C++?

A
  • A function is a block of code designed to perform a specific task
  • It can accept inputs (parameters) and return a result
18
Q

How can the parameters given to a function be changed?

A

If given as a reference (e.g. int& a), the variable can be changed in the function and persists after, not just as local variable

19
Q

What is a const reference?

A
  • The variable data is not copied and given to the function and it is not editable
  • Safe and easy
  • E.g. void func(const a& data)
  • Faster than function call by value
20
Q

What is a function prototype in C++ and why is it used?

A
  • A function prototype declares a function’s return type, name, and parameters before its definition
  • It is used to inform the compiler about the function’s existence
21
Q

How do you define a function in C++?

A

A function is defined with a return type, name, parameter list, and body:
return_type function_name(parameters) { body }

22
Q

What is recursion in C++?

A

Recursion occurs when a function calls itself to solve a smaller instance of the same problem, often used in algorithms like factorial calculation

23
Q

What is a for loop and when would you use it?

A
  • A for loop is used for iterating a fixed number of times
  • It consists of initialization, condition, and increment/decrement expressions
24
Q

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

A
  • while loop checks the condition before executing the loop body
  • do-while loop checks the condition after, ensuring the body executes at least once
25
Q

What is a pointer in C++?

A
  • A pointer is a variable that stores the memory address of another variable
  • It is declared using the * operator
26
Q

How do you access the value pointed to by a pointer?

A

Use the dereference operator * to access the value at the memory address stored by the pointer

27
Q

What is a reference in C++?

A
  • A reference is an alias for another variable, declared using the & operator
  • It must be initialized when declared
28
Q

What is the difference between ++x and x++ in C++?

A
  • ++x is pre-increment (increments before the value is used)
  • x++ is post-increment (increments after the value is used)
29
Q

What are the logical operators in C++?

A

Logical operators include:
- && (AND)
- || (OR)
- ! (NOT)
used to combine or negate boolean expressions

30
Q

What is the sizeof operator used for in C++?

A

The sizeof operator returns the size, in bytes, of a data type or object

31
Q

What is a conditional (ternary) operator in C++?

A
  • The conditional operator (?:) is a shorthand for an if-else statement
  • It evaluates an expression and returns one of two values depending on the result
32
Q

How do you handle errors in C++ using exceptions?

A

Use try to wrap the code that might throw an exception, catch to handle exceptions, and throw to generate an exception

33
Q

What is an array in C++?

A

An array is a collection of elements of the same type, stored in contiguous memory locations, and accessed by index

34
Q

How do you declare an array in C++?

A
  • Use the syntax type array_name[size];
  • E.g., int numbers[10];
35
Q

What is function overloading in C++?

A
  • If two functions have the same name, but different parameter types
  • Call is only clear sometimes, but if different types are used, type conversion might be needed
36
Q

What is the main function in C++?

A
  • Start of the execution of the program
  • Returns 0 (success), error if return otherwise
37
Q

What is a structure in C++?

A
  • Binds together multiple variables in a record
  • struct type_name {variables;} object_name1, object_name2;
38
Q

What is the primary idea behind object orientation?

A
  • Organize software as an abstraction of real-world entities
  • Objects encapsulate data and methods, and classes provide a blueprint to create multiple objects with similar structure and behavior
39
Q

What is the difference between a class and an object?

A
  • A class is a blueprint or a description of a type of object, defining its properties and behaviors
  • An object is an instance of a class, possessing unique identity and the attributes and behaviors defined by its class, several objects can have the same class
40
Q

How are classes represented in UML?

A

In UML, a class is represented as a rectangle with three compartments:
- The top contains the class name
- The middle lists attributes
- The bottom lists methods