1/95
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What are the 3 levels of abstraction of programming languages from high level to low level?
High level languages, assembly, machine language
How are they converted from high level to low level? (What are the names of these two "converters"
Compiler: HHL → ASM, Assembler: ASM → ML
Name and explain briefly the three programming language paradigms.
Object oriented - State and behavior are tied together in objects, hierarchical (ex. Java)
Imperative - Programs are sequences of statements that change program state step by step (ex. C)
Functional - Programs are composed of pure functions that map inputs to outputs without side effects. There is no state (ex. Haskell)
gcc -c program.c
Compile
./program
Run
gcc -o program program.o
Link
gcc -E program.c
See program after preprocessor
What is the basic unit of information in a computer?
Voltage, represented as 0 and 1 in digital systems.
What is machine language?
A low-level programming language that consists of binary code understood by a computer's CPU.
What does the instruction 'Addi $1, $2, 350' signify?
It adds the constant 350 to the value in register $2 and stores the result in register $1.
What is assembly language?
A low-level programming language that uses symbolic instructions to represent machine language.
What is the role of a compiler in programming?
It translates high-level language code into machine language for the computer to understand.
What is C in the context of programming languages?
C is a high-level programming language developed by Bell Labs in the 1970s, foundational for later languages like C++ and Java.
What are the characteristics of object-oriented programming?
Programs are collections of classes with encapsulated data types, bundling information and operations together.
What is imperative programming?
A programming paradigm where statements are commands that change the state of the program, often structured as a sequence of statements.
What defines functional programming?
Computation is treated as a series of function calls, producing reliable outputs from inputs without maintaining state.
What command is used to create a folder in a Unix-like system?
mkdir
What does the command 'gcc -c hello.c' do?
It compiles the source file hello.c into an object file without linking.
What is the purpose of the '#define' directive in C?
It defines a constant or macro that replaces occurrences in the code with a specified value.
What are the primary data types in C?
bool, char, int, float, double, and void.
What is a pointer in C?
A variable that stores the memory address of another variable.
What is the significance of the 'return 0;' statement in a C program?
It indicates that the program has executed successfully.
What does the command 'cp' do in a Unix-like system?
It copies files or directories.
What does the 'printf' function do in C?
It outputs formatted text to the standard output (usually the console).
What is the purpose of the 'int main()' function in a C program?
It serves as the entry point for program execution.
What does the 'void' data type indicate in C?
It specifies that a function does not return a value.
What is an array in C?
A collection of elements of the same data type, stored in contiguous memory locations.
What is the purpose of a for loop in C?
A for loop is used for iterating over a range of values, typically with a counter.
How does a while loop function in C?
A while loop repeatedly executes a block of code as long as a specified condition is true.
What does the if statement do in C?
An if statement executes a block of code if a specified condition evaluates to true.
What is the function of a switch statement in C?
A switch statement allows multi-way branching based on the value of a variable.
What is a nested for loop?
A nested for loop is a for loop inside another for loop, allowing for multi-dimensional iteration.
What are type specifiers in C?
Type specifiers define the data type of a variable, affecting its storage size and characteristics.
What is the difference between signed and unsigned integers?
Signed integers can represent both positive and negative values, while unsigned integers can only represent non-negative values.
What is the purpose of the static keyword in C?
The static keyword limits the visibility of a variable to the file it is declared in (global) or retains its value between function calls (local).
What are literal constants in C?
Literal constants are fixed values that do not change, such as integers, floating-point numbers, and characters.
What is implicit type conversion?
Implicit type conversion automatically converts one data type to another during assignment or operation.
What is explicit type casting?
Explicit type casting is the manual conversion of one data type to another using a cast operator.
What is the memory layout of a process in C?
The memory layout includes the stack, heap, BSS, data, and text segments.
What is the stack in memory layout?
The stack is used for static memory allocation and function call management, automatically managed by the system.
What is the heap in memory layout?
The heap is used for dynamic memory allocation, where memory management is the programmer's responsibility.
What is virtual memory?
Virtual memory abstracts physical storage, providing an illusion of a large, contiguous memory space.
What is the difference between segmentation and paging?
Segmentation is more space-efficient but less flexible than paging, which uses fixed-size pages for memory management.
What are the goals of an operating system kernel?
The goals include protection, abstraction, and resource management of processes.
What does the term 'CPU sharing' refer to?
CPU sharing refers to how multiple processes utilize the CPU, with good sharing ensuring efficient computation and resource use.
What is the significance of precedence and associativity in C?
Precedence determines the order of operations, while associativity defines the order of evaluation for operators of the same precedence.
What is the purpose of the `main` function in a C program?
It serves as the entry point for program execution.
What does the `#define` directive do in C?
It defines a macro that can be used to replace a specific value or expression throughout the code.
What is a null pointer?
A pointer that does not point to any valid memory location.
What is the difference between pass by value and pass by reference?
Pass by value copies the actual value to the function's parameters, while pass by reference passes the address of the actual parameters.
What is a memory leak?
It occurs when a program allocates memory but loses the reference to it, preventing deallocation.
What is a dangling pointer?
A pointer that points to a memory location that has been deallocated.
What does the `strcmp` function do?
It compares two strings and returns an integer indicating their lexicographical order.
What is the purpose of the `printf` function?
It outputs formatted text to the standard output.
How do you dynamically allocate memory in C?
Using functions like `malloc`, `calloc`, or `realloc`.
What is the significance of the `void*` type in C?
It is a generic pointer type that can point to any data type.
What is the result of dereferencing a null pointer?
It leads to undefined behavior, often causing a program crash.
How do you copy a string in C?
Using the `strcpy` function from the `string.h` library.
What is the purpose of the `return` statement in a function?
It exits the function and optionally returns a value to the caller.
What does the `&` operator do in C?
It retrieves the address of a variable.
What is the output of `printf("%d", &&*ptr);`?
It prints the value pointed to by `ptr`, effectively dereferencing it multiple times.
What is the role of the `char` data type?
It is used to store single characters.
What does the `sizeof` operator do?
It returns the size, in bytes, of a data type or variable.
How can you concatenate two strings in C?
Using the `strcat` function from the `string.h` library.
Name Data Types
Char, Double, int, void, float, void
. Write a full program that takes a command line argument which is a name, and prints "Hi, [name]!".
Example executing and expected output:
./program Miles
Hi, Miles!
#include
int main(int argc, char* argv[]) {
printf("Hi, %s!\n", argv[1]);
}
What is the output of the following program?
#include
#define PI 3.14
void makePIMoreAccurate() {
PI = 3.14159265358979;
}
int main() {
printf("PI: %f\n", PI);
makePIMoreAccurate();
printf("PI: %f\n", PI);
}
Errors! You cannot reassign a macro, because it's not a variable. Macros are like "find and replace"
What would this program look like after running: gcc -E program.c? and what would be the output
#include
#define A 5+4
#define B A*A
int main() {
printf("B = %d\n", B)
#include
int main() {
printf("B = %d\n", 5+4*5+4);
}
Output is 29
What is the output of the following program?
#include
int main() {
int a = -1;
int b = 0;
int c = -3;
printf("%d\n", ((a = 6) < b) + (b || c) * (!a + !!c) + ++a + b++);
}
0 + 1 * (0 + 1) + 7 + 0
= 8
Rewrite the following function to use a switch statement and explain why it's faster that way.
void foo(char grade) {
if (grade == 'A') {
printf("Excellent\n");
} else if (grade == 'B') {
printf("Good\n");
} else if (grade == 'C') {
printf("Average\n");
} else if (grade == 'D') {
printf("Poor\n");
} else if (grade == 'F') {
printf("Fail\n");
} else {
printf("Invalid grade\n");
}
}
void foo(char grade) {
switch (grade) {
case 'A':
printf("Excellent\n");
break;
case 'B':
printf("Good\n");
break;
case 'C':
printf("Average\n");
break;
case 'D':
printf("Poor\n");
break;
case 'F':
printf("Fail\n");
break;
default:
printf("Invalid grade\n");
}
}
It's faster because instead of checking each if statement, it jumps right to the corresponding case
You can exit a loop early using what?
break
You can skip to the next iteration of a loop using what?
continue
What is the output of the following code?
#include
int x = 5;
void foo(int x) {
printf("x = %d\n", x);
x = 0;
}
void bar(int y) {
printf("x + y = %d\n", x + y);
}
int main(void) {
printf("x = %d\n", x);
int x = 12;
foo(x);
bar(x);
}
x = 5
x = 12
x + y = 17
Why should you avoid using labels & gotos? When might it be a good time to use them?
Makes code less readable. Could maybe be used if you want to break out of multiple loops at once, but generally avoid using them.
What category do these specifiers go in?
unsigned
Sign
Makes a variable only use positive values, doubling its maximum value
What category do these specifiers go in?
Short and Long
Size
Short: Makes the variable shorter, saving memory, but not allowing as large values
Long: Makes the variable longer, taking more memory, and allowing larger values
What category do these specifiers go in?
Const
modifiability
Makes a variable immutable
What category do these specifiers go in?
Static(local)
Static(Global)
Access/Scope
Local: Makes a variable in a function maintain its value between function calls
Global: Makes a global variable private to that file
What's the output of this program? How would you fix it?
#include
int main(void) {
long int x = 1 << 40;
printf("%ld\n", x);
}
0 because the literal "1" isn't a long int, so shifting it left 40 makes it 0
Change the "1" to a "1l"
What's the output of this program?
#include
int main(void) {
int a = 5;
double b = 2.5;
char c = 'A';
double x = a / 2;
double y = a / 2.0;
int z = b + c;
int w = (int)b + (int)c;
double k = (double)(a / 2);
int m = b;
printf("%f %f %d %d %f %d\n", x, y, z, w, k, m);
}
2.000000 2.500000 67 67 2.000000 2
What is the output of the following program?
#include
int main(void) {
int x = 123;
int* ptr;
ptr = &x;
x = 321;
printf("%d\n", *ptr);
*ptr = 456;
printf("%d\n", x);
ptr = 0;
printf("%d\n", x);
}
321
456
456
17. After declaring an array like so:
int arr[] = { 1, 2, 3 };
What will the value of the variable arr be?
What will the value of *arr be
What is arr[i] shorthand for
The memory address of the 0th element
1
*(arr+1)
If You have a memory layout process of
Stack
Heap
BSS
Data
Text
Assign these local variables to one of the slots
Local variables | non-zero initialized global variables | instructions | dynamically allocated memory | uninitialized or zero'd global variables
In order is
Local variables, function calls;
programmer doesn't have to manage
Dynamically allocated memory;
programmer must manage themself
Uninitialized or zero'd global variables
Non-zero, initialized global variables
Instructions
Why do the stack and heap grow toward each other instead of having a defined division?
Because fixed divisions waste space. Growing toward each other maximizes usable memory.
________is when you add a constant to the virtual memory address to get the physical memory address, and then you check the bounds to ensure it's not accessing memory from another process.
Segmentation
The goal of virtual memory is to give each process the illusion of having ____, ______ and ______ memory
Large, private and contiguous
What is the kernel, and what are the three goals of the kernel?
The kernel is the core of an operating system.
Protection - processes can't use each other's memory
Abstraction - hide details of underlying hardware
Resource management - how processes share hardware
What is the difference between pass by value and pass by reference? Are pointers passed by value or reference?
Pass by value copies the value into the function parameter
Pass by reference passes the memory address of a variable into the function
Pointers themselves are still passed by value! (You're copying the memory address into the function). So everything is passed by value in C.
Write a function, swap, that allows you to pass in your integer variables by reference, and it swaps them for you.
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
Write a function swap_ptrs, that does the same thing as swap, but swaps two int pointers for you, instead of two ints.
void swap_ptrs(int* x, int* y) {
int temp = x;
x = y;
*y = temp;
}
What is the output of the following code?
#include
void setTo321(int* arr) {
int newArray[] = { 3, 2, 1 };
arr = newArray;
}
int main() {
int arr[] = { 1, 2, 3 };
printf("%d %d %d\n", arr[0], arr[1], arr[2]);
}
1 2 3
Side node: never create new arrays on the stack and return them like that!
Fix the function so that it sets to { 3, 2, 1 } correctly.
#include
void setTo321(int* arr) {
int newArray[] = { 3, 2, 1 };
arr = newArray;
}
int main() {
int arr[] = { 1, 2, 3 };
printf("%d %d %d\n", arr[0], arr[1], arr[2]);
}
void setTo321(int* arr) {
arr[0] = 3;
arr[1] = 2;
arr[2] = 1;
}
int main() {
char* c[] = { "Questions", "like", "this", "are", "very", "mean" };
char** c2 = c;
char** cp[] = { c+2, c+5, c+1, c+4, c, c+3 };
char*** cpp = cp;
printf( "%s\n", ((++cpp)) );
printf( "%s\n", (--((++cpp))) + 3 );
printf( "%c\n", ***(cpp - 1) );
printf( "%s\n", ((cpp + 2)) + 1 );
printf( "%c\n", ((*(cpp - 2)) + 2) );
printf( "%c\n", ((int)((c2++)))[1] );
printf( "%s\n", c2[1] );
mean
stions
m
uestions
i
t
this
Write a function, str_cpy(char src, char dst), that copies the src string into the dst string. Assume the dst string has already been allocated with sufficient space for src. Ex:
char src[] = "Hello World!";
char dst[100];
str_cpy(src, dst);
void str_cpy(char src, char dst) {
int i = 0;
while (src[i] != '\0') {
dst[i] = src[i];
i ++;
}
dst[i] = '\0';
}
What is the output of the following program?
#include
char* foo(int index, char c) {
static char str[] = "cat";
str[index] = c;
return str;
}
int main() {
foo(0, 'f');
char* result = foo(1, 'r');
foo(2, 'o');
foo(3, 'g');
printf("%s\n", result);
}
"frog" + whatever garbage was in memory after that point until it hits a 0, since we overwrote the '\0'
Ex: frog≈`°_JêDφ
How to manually copy a string in C
#include
#include
int main() {
char source[] = "Hello, world!";
char destination[100];
strcpy(destination, source);
printf("Source: %s\n", source);
printf("Copied: %s\n", destination);
return 0;
}
OR
#include
int main() {
char source[] = "Hello, world!";
char destination[100];
int i = 0;
while (source[i] != '\0') {
destination[i] = source[i];
i++;
}
destination[i] = '\0';
printf("Source: %s\n", source);
printf("Copied: %s\n", destination);
return 0;
}