The C Family Flashcards

1
Q

What does the term ‘imperative’ describe in programming?

A

Describes computation in terms of statements that change a program state

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

What is the opposite of imperative programming?

A

Declarative programming

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

What are the two main programming paradigms associated with C?

A

Procedural and functional

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

Is C a compiled or interpreted language?

A

Compiled

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

What type of typing does C use?

A

Statically, weakly typed

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

What does it mean for C to be statically typed?

A

Types are checked before runtime

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

What does weakly typed mean in the context of C?

A

Supports implicit type conversions

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

On what platforms is C available?

A

Pretty much every platform

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

What is a key characteristic of C regarding portability?

A

Very fast

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

What feature does C provide for memory management?

A

Explicit memory management

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

What standards does C adhere to?

A

ANSI/ISO standards

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

True or False: C has runtime error checking.

A

False

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

What type of error handling is lacking in C?

A

Sophisticated exception handling

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

What does GNU stand for?

A

GNU stands for ‘GNU’s Not Unix’

GNU is a free, UNIX-compatible operating system started by Richard Stallman in 1983.

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

What types of software packages does the GNU system include?

A

The GNU system includes various software packages, including:
* Compilers
* Libraries
* Tools

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

What is GCC?

A

GCC stands for GNU Compiler Collection, a powerful compiler that allows compiling different programming languages including:
* C
* C++
* Objective-C
* Fortran
* Java
* Ada
* Go

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

What command is used to compile a C program with GCC?

A

gcc myProgram.c

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

What is the default name of the executable file created by GCC?

A

a.out

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

How can you run the compiled program in Linux?

A

./a.out

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

How can you specify a custom output file name when compiling with GCC?

A

Use the -o option, e.g., gcc myProgram.c -o myProgram

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

What is the purpose of compiling to an object file?

A

Object files are portable and don’t need the full source code. They allow linking multiple object files to create a final executable.

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

What command is used to compile a C program into an object file?

A

gcc -c myProgram.c -o myProgram.o

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

What command is used to link an object file and create an executable?

A

gcc myProgram.o -o myProgram

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

What is the command to compile multiple source files at once?

A

gcc circle.c radius.c -o circle -lm

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What does the -lm option do when compiling?
-lm links the math library, needed for functions like sqrt()
26
What is the first step in compiling multiple files separately before linking?
Compile each file separately using gcc -c
27
What is the command to link object files after compiling them separately?
gcc circle.o radius.o -o circle -lm
28
What is the solution to the error 'sqrt not defined'?
Always link the math library using -lm.
29
What is the solution to the error 'radius.o not found'?
Make sure all .o files are correctly compiled before linking.
30
What does K&R C refer to?
K&R C is the original version of C created by Kernighan & Ritchie.
31
What is ANSI C?
ANSI C (C89/C90) is the first standardized version of C, widely used.
32
What version of C was introduced in 1999?
C99
33
What is the latest update of the C language as of 2011?
C11
34
What is Embedded C?
Embedded C is a version designed for small systems (microcontrollers).
35
What integer value does a C program return to indicate success?
0
36
What does a C program return to indicate failure or error?
Any other number
37
Fill in the blank: When a C program finishes running, it returns an integer value to the _______.
operating system
38
Give an example of a return statement indicating success in a C program.
return 0;
39
What does 'weakly typed' mean in C?
C allows implicit type conversions, which can lead to unexpected results. ## Footnote Example of implicit conversions can cause issues like data loss or overflow.
40
What is a potential issue when converting larger types to smaller types in C?
Data loss when converting larger types to smaller types. ## Footnote This can occur when a larger data type is assigned to a smaller data type, resulting in truncation.
41
What is an example of signed int overflow in C?
When unsigned short int x = 65535; is assigned to short int y, y becomes -1. ## Footnote This occurs because the maximum value of signed short int is 32767.
42
What is the output of printf for variable c when c = x; with x = 65535?
Undefined behaviour. ## Footnote This happens because c is a char, which cannot hold the value 65535.
43
What are the two types of arrays in C?
1D arrays and 2D arrays. ## Footnote Example: int numbers[5] = {1, 2, 3, 4, 5}; for 1D and int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; for 2D.
44
What does C not do with respect to arrays?
C does not perform bounds checking. ## Footnote This can lead to accessing out-of-bounds elements, which can cause memory issues.
45
How are strings represented in C?
Strings are arrays of characters ending with a null terminator (0). ## Footnote Example: char greeting[] = 'Hello!'; automatically includes 0.
46
What does the function strlen(s) do in C?
Returns the length of the string (excluding the null terminator). ## Footnote strlen counts the characters until it hits the null terminator.
47
What is the purpose of srand(seed) in C?
Sets a new seed to change the sequence of random numbers. ## Footnote Using srand with time(0) ensures different sequences on each run.
48
What does the function rand() generate?
A pseudo-random integer between 0 and RAND_MAX. ## Footnote RAND_MAX is a constant defined in stdlib.h.
49
What is the shape of the Normal (Gaussian) Distribution?
Follows a bell-curve shape. ## Footnote This distribution is commonly used in statistics and natural phenomena.
50
What does the GNU Scientific Library (GSL) provide?
Better random number generators than rand(). ## Footnote GSL offers more sophisticated methods for generating random numbers.
51
What is the purpose of the function gsl_rng_set in GSL?
Sets the seed for the random number generator. ## Footnote This is important for ensuring reproducibility of random sequences.
52
Fill in the blank: Strings in C are just arrays of characters ending with a ______.
null terminator. ## Footnote The null terminator is represented as 0 in C.
53
True or False: C automatically checks for buffer overflows in arrays.
False. ## Footnote C does not provide any built-in mechanism for bounds checking.
54
What is the output of printf when using sprintf(buffer, "%s %d", str, num)?
Formats and stores output into buffer. ## Footnote This allows for formatted strings to be stored for later use.
55
What is a one-dimensional array in C?
An array that holds a fixed number of elements of the same type, initialized with specific values ## Footnote Example: Char array[5] = {'A', 'B', 'C', 'D', 'E'};
56
How can the size of an array be defined in C?
The size can be implicit and defined by its initialization ## Footnote Example: Int a[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
57
What is an uninitialized array in C?
An array declared without an initial value, containing random data ## Footnote Example: Int anArray[10];
58
How is a two-dimensional array initialized in C?
An array of arrays initialized with specific values ## Footnote Example: Int a2[2][3] = { {1,2,3} , {4,5,6} };
59
How do you access and set individual values in an array?
Using the index notation, e.g., anArray[0] = 42 or array[3] = 'Z';
60
What is a string in C?
A string is an array of characters terminated by a null character (0) ## Footnote Example: Char string[] = 'Hello World!';
61
What is the significance of the null terminator in strings?
It signals the end of the string to prevent the program from getting stuck.
62
True or False: Strings in C are enclosed in single quotes.
False ## Footnote Strings use double quotes, while characters use single quotes.
63
What are functions in C?
Uniquely named groups of program statements that may accept parameters and return values.
64
What is the syntax for defining a function in C?
functionName() { /* statements */ return ; }
65
What is the main function requirement in a C program?
Every program must include a main() function.
66
What is a procedure in C?
A function that does not return a value, defined with void.
67
What is the purpose of function prototypes?
To inform the compiler of the function’s signature before it is called.
68
What is the difference between global and local scope?
Global scope variables are declared outside functions, while local scope variables exist only within the function.
69
What are static variables in C?
Variables that are initialized only once and retain their value between function calls.
70
Fill in the blank: Functions must have a _______ name.
unique
71
What should be avoided unless necessary in function design?
Using global variables.
72
What are the key points regarding functions in C?
* Define using a clear structure and unique names * Avoid overloading; separate by purpose and return type * Use prototypes to declare functions before use * Avoid global variables unless justified * Utilize static variables for data persistence
73
What fundamentals were covered in this lecture?
Defining and using functions, role of prototypes, scope, and static variables.
74
What is stored in memory?
Every variable ## Footnote Memory is a linear array of bytes.
75
What does each memory location have?
An address (a numerical label) ## Footnote This address is used to identify the location of the variable in memory.
76
How many bytes are typically allocated for an integer variable?
4 bytes ## Footnote This is common on many systems.
77
What is a pointer?
A variable that stores a memory address instead of a value.
78
How do you declare a pointer to a character?
char *p; ## Footnote This declares p as a pointer to a character.
79
What does the & operator do?
Gives the address of a variable.
80
What does the * operator do?
Dereferences a pointer to access the value at the stored memory address.
81
What is pointer arithmetic?
Performing arithmetic operations on pointers.
82
Fill in the blank: A void pointer (void *) is a _______.
Generic pointer that can store addresses of any data type.
83
What function is used for dynamic memory allocation?
malloc()
84
What should you always check after using malloc()?
If malloc() succeeds.
85
What function is used to release allocated memory?
free()
86
What are common pitfalls related to pointers?
* Memory Leaks * Dangling Pointers * Wild Pointers
87
What is a segmentation fault?
Occurs when trying to access memory illegally.
88
What tool can be used for memory debugging?
valgrind
89
True or False: Pointer arithmetic follows the size of the data type.
True
90
What happens if you forget to free memory?
Memory Leaks occur.
91
What is a dangling pointer?
Using a pointer after the memory it points to has been freed.
92
What is a wild pointer?
Using uninitialized pointers.
93
What does p++ do if p is a pointer to an int?
Moves p to the next int (increases by sizeof(int)).
94
What happens when you write beyond allocated space?
It can corrupt adjacent memory.
95
What is the output of printf("p = %p, *p = %c\n", p, *p) if p points to 'A'?
p= 0x1e224eff, *p= A
96
Fill in the blank: The function _______ is used to allocate memory for an array of integers.
malloc()
97
What is Pass by Value?
A copy of the variable is passed to the function, keeping the original variable unchanged. ## Footnote Default in C; can be inefficient for large data structures.
98
What is a downside of Pass by Value?
Can be inefficient for large data structures. ## Footnote It involves copying the entire data, which takes extra memory and processing time.
99
How does Pass by Reference work?
A pointer to the variable is passed instead of a copy, allowing the original variable to be modified. ## Footnote Great for modifying large data structures efficiently.
100
What is a pointer in C?
A pointer stores the memory address of another variable. ## Footnote Syntax example: int *p = &x; where p stores the address of x.
101
What is the syntax to dereference a pointer?
Using the asterisk (*) before the pointer variable. ## Footnote Example: printf("%d", *p); prints the value at the address stored in p.
102
What is a function pointer?
A function pointer stores the address of a function. ## Footnote Syntax example: int (*funcPtr)(int, int); for a function taking two ints and returning an int.
103
Why use function pointers?
Makes code modular and reusable; enables dynamic function execution. ## Footnote Useful in scenarios like callbacks and sorting.
104
What is a key advantage of linked lists over arrays?
They do not need a predefined size and allow efficient insertions and deletions. ## Footnote This makes linked lists more flexible for dynamic data storage.
105
What is the basic structure of a linked list node?
struct Node { int data; struct Node *next; }; ## Footnote Each node contains data and a pointer to the next node.
106
How do you insert a node at the beginning of a linked list?
Create a new node, set its next to the current head, and update the head to the new node. ## Footnote Example: void insert(struct Node** head, int newData) {...}
107
What is the purpose of the printList function?
To traverse and print all the nodes in the linked list. ## Footnote Example: void printList(struct Node *node) {...}
108
True or False: Pass by reference allows modifying original values.
True. ## Footnote This is done by passing pointers to the function.
109
Fill in the blank: _______ enables dynamic execution of functions.
Function pointers. ## Footnote They allow the program to choose which function to execute at runtime.
110
Final takeaway: What do linked lists provide compared to arrays?
More flexibility for dynamic data storage. ## Footnote Linked lists can grow and shrink in size easily compared to static arrays.
111
What is the purpose of scanf() in C?
Takes user input ## Footnote It is like the reverse of printf()
112
What does scanf() return?
Number of values successfully assigned
113
What happens if input fails in scanf()?
Exits the loop
114
Define a stream in the context of programming.
A way of handling data flow in a program
115
What are the key features of a stream?
* Abstracts file handling * Buffered * Two Types: Text Streams and Binary Streams
116
What is the function to open a file in C?
fopen()
117
What is the function to close a file in C?
fclose()
118
What does a file pointer (FILE *) do?
Accesses files in C
119
What does fopen() return if it fails?
NULL
120
What mode is used to open a text file for reading?
r
121
Fill in the blank: To create a binary file for writing, use mode _______.
wb
122
What is the action of the function fputs()?
Write a string
123
What does fprintf() do?
Writes to a file like printf()
124
What is the purpose of the function fseek()?
Jump to a specific position in a file
125
What is the role of errno in error handling?
Global variable to check errors
126
What are the three standard streams in C?
* stdin * stdout * stderr
127
What does stdout represent?
Standard output (console)
128
What is the result of the command './myprogram > output.txt 2> errors.txt'?
Saves stdout in output.txt and stderr in errors.txt
129
What happens when you reach the end of a file (EOF)?
Stops reading
130
What is the summary takeaway regarding file operations?
Always check if fopen() worked before reading/writing
131
True or False: The function remove() is used to delete a file.
True
132
What is the C Preprocessor?
Before your code is compiled, it passes through a preprocessor that expands macros and header files, removes comments, allows conditional compilation, and modifies source code before sending it to the compiler.
133
What does the directive #include do?
Includes a system header file.
134
What does the directive #include "file" do?
Includes a user-defined header file.
135
What is the purpose of #define MACRO?
Defines a macro (a shortcut for values or code).
136
What happens when #ifdef MACRO is used?
Compiles code only if a macro is defined.
137
What does #ifndef MACRO do?
Compiles code only if a macro is NOT defined.
138
What is the function of #undef MACRO?
Undefines a macro.
139
What does the #pragma directive provide?
Compiler-specific instructions (rarely used).
140
What are system headers and how are they included?
System headers are included using angle brackets < >, e.g., #include .
141
What are user-defined headers and how are they included?
User-defined headers are included using quotes " ", e.g., #include "myfile.h".
142
What does #define PI 3.14159 accomplish?
Replaces every PI in the code with 3.14159.
143
What is a common pitfall when using macros?
Not using parentheses can lead to unexpected results.
144
How can the MAX macro be defined correctly?
#define MAX(a, b) ((a) > (b) ? (a) : (b))
145
What does stringifying macros do?
Converts macro arguments into strings.
146
What is the benefit of structuring code?
Makes code easier to read, allows reusability, and improves debugging and maintainability.
147
What are the components of a structured project?
* myProgram.c (Main Program) * myProgram.h (Header File) * stack.c (Stack Implementation) * stack.h (Stack Function Declarations)
148
What do header files contain?
Function declarations and constants.
149
What do source files contain?
Function definitions.
150
What is the purpose of #ifndef STACK_H?
Prevents multiple inclusions (aka include guards).
151
What are the three stages of compilation?
* Preprocessing * Compilation * Linking
152
What command compiles source files separately?
gcc -c stack.c -o stack.o
153
What is the command to link object files?
gcc stack.o myProgram.o -o myProgram
154
What is the purpose of a Makefile?
Saves time when recompiling large projects, automatically tracks dependencies, and reduces manual errors.
155
What command is used to build a project using a Makefile?
make
156
What is a library in C?
A collection of precompiled functions that can be used in multiple projects.
157
What is the difference between a static library and a shared library?
* Static Library (.a): Linked at compile time (larger executable) * Shared Library (.so): Linked at runtime (smaller executable)
158
What command links the math library?
gcc myMaths.c -lm -o myMaths
159
What does the -lm flag do?
Tells GCC to link the math library.
160
What does the preprocessor do?
Expands macros, includes headers, and modifies code.
161
What should you do to prevent errors when using #define?
Use #define wisely.
162
What is important to remember about structuring code?
Structure code into multiple files (.c and .h).
163
What is the benefit of using Makefiles?
Automate compilation.
164
Fill in the blank: A library is a collection of _______.
[precompiled functions]
165
What is the role of *.h (Header files) in C programming?
Declarations & interfaces ## Footnote Header files contain declarations of functions and structures but do not include their definitions.
166
What do *.c (Source files) contain?
Code definitions, logic ## Footnote Source files contain the actual implementation of functions and logic.
167
What elements are included in headers?
* #include * #define macros (e.g. M_PI) * struct definitions * typedefs * Function prototypes ## Footnote Headers serve as an interface for other files.
168
What elements are excluded from headers?
* Function bodies (definitions) * Executable code ## Footnote Headers should not contain any executable code.
169
What does the file stack.h contain?
Structure & function prototypes ## Footnote It defines the data structures and declares the functions used in the stack implementation.
170
What is the purpose of include guards in C headers?
Prevent duplicate definitions in headers ## Footnote They ensure that a header file is included only once in a single compilation.
171
What is the syntax for include guards?
#ifndef __STACK_H #define __STACK_H // ... header content ... #endif ## Footnote This prevents the header file from being included multiple times.
172
What is an alternative to include guards?
#pragma once ## Footnote This is simpler but not universally supported across all compilers.
173
What is the command to compile a C source file into an object file?
gcc -c stack.c ## Footnote This generates stack.o from stack.c.
174
What is the command to link object files into an executable?
gcc myProgram.o stack.o -o myProgram ## Footnote This combines the object files into the final executable named myProgram.
175
What are the two types of libraries in C?
* Static (.a) * Dynamic (.so) ## Footnote Static libraries are linked at compile time, while dynamic libraries are linked at runtime.
176
What is the linker flag to use the math library in C?
-lm ## Footnote This flag is necessary when linking against the math library.
177
What is the purpose of Makefiles?
Automate compilation ## Footnote They help manage complex builds by defining how to compile and link the program.
178
What command is used to run the Makefile?
make myProgram ## Footnote This command triggers the build process defined in the Makefile.
179
What happens when a dependency like stack.h is updated?
Recompile if any dependency is updated ## Footnote The make utility checks timestamps to determine what needs to be recompiled.
180
Fill in the blank: Header files contain ______, not definitions.
declarations ## Footnote This highlights the primary role of header files in C programming.
181
What does the command 'gcc -c file.c' do?
Creates object files ## Footnote It compiles the source file into an object file without linking.
182
What is a key point about libraries in C?
Use -l for others ## Footnote This is how additional libraries can be linked during the compilation process.
183
What is one advantage of using Makefiles?
Keep project organized ## Footnote They help maintain an organized structure for large projects.
184
What is a Process?
A running instance of a program with a unique PID and one parent process, except for PID 0.
185
What does everything in UNIX represent?
Everything in UNIX is either a file or a process.
186
What is the purpose of POSIX?
Makes code portable across UNIX-like systems.
187
What did The Open Group create?
The Single UNIX Specification.
188
Which Linux distributions are mostly POSIX compliant?
Ubuntu, RedHat, etc.
189
What command shows running processes?
ps
190
What is the purpose of the top command?
Provides a live view of CPU/memory/processes.
191
What symbol is used to run a process in the background?
&
192
What does the jobs command do?
Shows your background jobs.
193
What command is used to terminate a process?
kill
194
What is the function of nohup?
Keeps a job running even after logout.
195
What do nice and renice commands do?
Set process priority.
196
What happens in the foreground mode?
Shell waits for the process to finish.
197
What happens in the background mode?
Shell continues running.
198
What command is used to pause a foreground job?
CTRL+Z
199
How do you kill a process by job number?
kill %1
200
How do you kill a process by PID?
kill 12345
201
What command starts a low-priority background process?
nice
202
What command changes the priority of a running process?
renice
203
How do you keep a process running even after logout?
nohup myProgram &
204
What does fork() do?
Creates a child process.
205
What does a return value of < 0 from fork() indicate?
Fork failed.
206
What does a return value of 0 from fork() indicate?
Child process.
207
What does a return value > 0 from fork() indicate?
Parent process (value = child’s PID).
208
What do both parent and child processes share?
* stdout * file descriptors * global/static variables (as copies)
209
What does waitpid() do?
Wait for a child process to finish.
210
What can waitpid() wait for?
* A specific child (pid) * Any child (pid = 0) * Return immediately if no child has exited (WNOHANG)
211
What does exec() do?
Replaces the current process with a new one.
212
What happens if exec succeeds?
Nothing after it runs.
213
What happens if exec fails?
Handle the error.
214
What does pipe() do?
Facilitates communication between parent and child processes.
215
What are the characteristics of pipes?
* Unbuffered * Unidirectional * Must be created before fork()
216
What does SIGINT represent?
Ctrl+C
217
Is SIGTERM catchable?
Yes
218
Is SIGKILL catchable?
No
219
What are SIGUSR1 and SIGUSR2?
Custom user signals.
220
What does SIGSEGV represent?
Segfault
221
What command is used to send a signal?
kill / signal()
222
What tool is used to create a process?
fork()
223
What tool is used to replace a process?
exec()
224
What tool is used to wait for a process?
waitpid()
225
What tool is used for communication?
pipe()
226
What is the virtual address space in a program?
Each program thinks it has full access to memory, but the OS maps it.
227
What are the three main segments of runtime memory?
* Text/code segment * Stack * Heap
228
What is stored in the code/text segment?
Stores the program’s machine instructions
229
What is stored in the stack?
Local variables, function calls
230
What is stored in the heap?
Memory from malloc(), calloc() etc.
231
How is stack memory used during function calls?
Functions use stack frames to store parameters + local variables
232
What happens when a function is called?
* Save old frame pointer (fp) * Move stack pointer (sp) * Allocate space for locals * Execute function * Restore previous frame + return address on return
233
How does the stack grow in memory?
Stack grows downward from high → low memory addresses.
234
What registers are used for the frame pointer and stack pointer?
* %rbp: Base pointer (start of stack frame) * %rsp: Stack pointer (top of stack)
235
What is recursion in relation to stack memory?
Each recursive call pushes a new frame on the stack.
236
What occurs if there are too many recursive calls?
Stack overflow!
237
What is heap memory used for?
Dynamic allocation of memory.
238
What functions are commonly used for dynamic memory allocation?
* malloc() * calloc()
239
What must be done with memory allocated on the heap?
Must free() manually.
240
What is a consequence of not freeing heap memory?
Memory leak!
241
What is heap fragmentation?
Occurs when allocated memory blocks are freed, leaving gaps that may not fit new allocations.
242
What are common allocation strategies for heap memory?
* First fit * Best fit * Next fit
243
How can you inspect the stack frame layout?
By using printf to display the addresses of parameters and local variables.
244
What is the role of the frame pointer?
It is the base of the current function call.
245
What is the role of the stack pointer?
It indicates the top of the stack.
246
What tools can be used to inspect compiled binaries?
* objdump (CLI disassembler) * Boomerang (GUI decompiler)
247
Fill in the blank: The stack is used for _______ and function frames.
Local vars
248
Fill in the blank: The heap is used for _______ memory.
Dynamic
249
True or False: Each process has its own space managed by the OS using page tables.
True
250
What does a jump do in programming?
Moves the program counter (PC) to a new instruction address.
251
List three uses of jumps in programming.
* Control flow (loops, conditions) * Error handling * Special tricks like coroutines
252
What is an unconditional jump in assembly?
jmp
253
What are conditional jumps used in assembly?
* jg * je * etc.
254
What is the purpose of the 'goto' statement in C?
Transfers control to a labeled statement.
255
What is one pro and one con of using 'goto' in C?
Pro: Useful for error handling or cleanup Con: Can lead to spaghetti code
256
True or False: Dijkstra’s paper states that 'goto' is beneficial for programming.
False
257
What does the 'goto' statement allow for in cleanup operations?
Acts like try/catch for low-level cleanup.
258
What is the example of using 'goto' for cleanup in C?
if (!(p = malloc())) goto errexit;
259
What is 'setjmp()' used for?
Saves CPU state (registers, stack, etc.) into a jmp_buf.
260
What does 'longjmp()' do?
Restores saved state and jumps back to the setjmp call.
261
Fill in the blank: 'setjmp' creates a _______.
[savepoint]
262
How does error handling with setjmp/longjmp work?
setjmp captures state, longjmp restores it on error.
263
What is the main difference between subroutines and coroutines?
Subroutines have one-way calls; coroutines yield back and forth.
264
What happens to the stack when using coroutines?
Stack stays intact.
265
What is a risk when using coroutines?
Stack may overflow or behave unpredictably if deeper recursion occurs.
266
When should you use 'goto'?
Quick jump for cleanup only.
267
What is the low-level control transfer mechanism in C?
setjmp/longjmp
268
What do coroutines allow two routines to do?
Take turns executing.
269
What is a crucial role in coroutines?
The stack.
270
What is the first OOP language?
Simula 1967
271
Which programming language did C++ evolve from?
C
272
Who created C++?
Bjarne Stroustrup
273
What is a key feature introduced in C++ compared to C?
Classes
274
What does a precompiler do?
Translates annotated/extended code into standard C
275
Give an example of a precompiler.
Oracle Pro*C
276
What is a notable difference between C and C++?
C++ adds classes, exceptions, function/operator overloading, default parameters, pass-by-reference
277
What command is used to compile a C++ file?
g++ -std=c++11 -c MyClass.cpp -o MyClass.o
278
What is the convention for header files in C++?
.hpp
279
What year was C++98 released?
1998
280
What major feature was introduced in C++11?
Auto, lambdas, smart pointers
281
What does the command 'cout' output to?
stdout
282
True or False: 'cerr' flushes the buffer.
False
283
What does the function 'setfill' do in C++?
Sets the fill character for output formatting
284
What is function overloading?
Defining multiple versions of the same function with different parameters
285
What is the default access level for class members in C++?
Private
286
What are constructors in C++?
Called when an object is created
287
What are destructors in C++?
Called when an object goes out of scope
288
What is the purpose of a destructor?
Automatically handles cleanup logic
289
What is the main use of classes in C++?
Organize code and enable OOP
290
Fill in the blank: C++ is a __________ of C.
superset
291
What command is used to link a C++ object file?
g++ MyClass.o -o MyProgram
292
What is the significance of the C++ Standard version 2020?
Introduced concepts and ranges
293
What feature does 'setw' provide in C++?
Sets the width of the next output field
294
What does the 'using namespace std;' statement do?
Allows access to the standard namespace
295
What is the principle of modularity in C++?
Code organized into classes/files
296
Define abstraction in the context of C++.
Hide unnecessary details
297
What does encapsulation mean in C++?
Private data, accessed only via class
298
What is extensibility in C++?
Reuse and build on top of existing code
299
Define polymorphism in C++.
Same interface, different behavior
300
What is inheritance in C++?
Child class inherits from parent class
301
What is the syntax to define a class in C++?
class ClassName { /* members */ };
302
What does a constructor do in C++?
Called automatically when an object is created
303
What is the role of a destructor in C++?
Called automatically when an object goes out of scope
304
How do you declare a parameterized constructor?
ClassName(int i, int j)
305
Fill in the blank: A parameterized constructor creates objects with _______.
initial values
306
What is the rule for default parameters in C++?
Once a default is used, all following parameters must have one too!
307
What does the scope resolution operator (::) do?
Defines class methods outside the class
308
True or False: You should use malloc() and free() in C++.
False
309
What is the correct way to allocate memory for an integer in C++?
int* p = new int(42);
310
What is the difference between a reference and a pointer in C++?
Reference: Implicit dereference; Pointer: * required
311
What is the syntax for passing by reference?
void function(int& x)
312
Fill in the blank: The 'this' pointer is used to refer to the _______.
current object inside its method
313
What is a copy constructor in C++?
Creates a new object from another existing one
314
Why is a custom copy constructor necessary?
Default copy = shallow copy (bad for pointers!)
315
What is a static member in a C++ class?
Value is shared across all instances
316
How do you access a static member in C++?
ClassName::memberName
317
What are the two main types of constructors in C++?
* Default constructor * Parameterized constructor
318
What happens when you use 'new' in C++?
Calls constructor
319
What happens when you use 'delete' in C++?
Calls destructor
320
How do you define a member function outside the class?
ReturnType ClassName::functionName() { /* body */ }
321
Fill in the blank: In C++, the 'this' pointer allows method _______.
chaining
322
What is Inheritance?
Inheritance lets a class acquire properties (variables & methods) from another class.
323
What is the analogy used to explain Inheritance?
Product → base class (general), PackagedProduct → derived class (adds packaging), LabelledProduct → derived class (adds barcode, label)
324
What is the syntax for defining a derived class in C++?
class Derived : access Base { // new or extended features };
325
Provide an example of a derived class in C++.
class MySpecialString : public Stringy { char type; };
326
How are base class members inherited with respect to access specifiers?
* public: accessible * protected: accessible * private: not accessible
327
What should you use for most inheritance scenarios?
Use public inheritance for most use cases unless you’re intentionally hiding things.
328
What are the access levels for members in inheritance?
* public: accessible * protected: accessible * private: not accessible
329
What is the order of execution for constructors and destructors?
Constructors run top-down (base → derived), Destructors run bottom-up (derived → base).
330
What is the output order of constructors and destructors in the provided example?
Base, Derived, Bye Derived, Bye Base
331
How do you pass parameters to a base class constructor?
Use the initializer list in the derived class constructor.
332
What is chain inheritance?
class base { ... }; class derived1 : public base { ... }; class derived2 : public derived1 { ... };
333
What is the order of construction and destruction in chain inheritance?
Construct: base → derived1 → derived2, Destruct: derived2 → derived1 → base.
334
What is multiple inheritance?
class derived : public base1, public base2 { ... };
335
What issue may arise from multiple inheritance?
Ambiguity if both base classes have similar members.
336
What is the Diamond Problem in inheritance?
Derived class gets two copies of the base class member.
337
How can you resolve the Diamond Problem?
* Use scope resolution * Use virtual base classes
338
What are virtual base classes?
Ensure only one copy of the base is inherited.
339
What is the purpose of virtual member functions?
Enables polymorphism.
340
What is the result of dynamic dispatch using virtual functions?
Function is bound at runtime, not compile time.
341
What is an abstract class?
A class that has at least one pure virtual method.
342
What happens if you try to instantiate an abstract class?
It results in a compilation error.
343
Summarize the key ideas of inheritance.
* Inheritance: Let one class use another's members * Access Levels: public / protected / private * Constructor Order: Base → Derived * Multiple Inheritance: Can cause conflicts – use virtual * Virtual Function: Enables dynamic dispatch * Abstract Class: Has at least one pure virtual method * Diamond Problem: Fixed using virtual base classes