click below
click below
Normal Size Small Size show me how
More Cis 1300
| Question | Answer |
|---|---|
| What is a shell? | The shell (or terminal, or command line) is a text-based interface to the computer's operating system. Instead of clicking on icons, you type commands |
| The program you're typing into is often called | Bash |
| what does the cd command do | moves you into a new directory |
| cp source destination | Copy a file. |
| mv source dest: | Move or rename a file. |
| rm file: | Remove a file (careful!). |
| touch file: | Create an empty file. |
| cat file: | Display file content. |
| less file: | View file content page by page. |
| zip arch.zip files: | Create a zip archive. |
| refers to the current directory | . |
| refers to previous path or directory | .. |
| absolute paths | start from root of filesystem |
| relative paths paths | starts from your current location |
| What if you forget -o in your gcc? | If you just type gcc hello.c, the compiler will work, but the executable will be named a.out by default. This is a historical artifact from early Unix. (basically you just cant nae the output) |
| Which line of code contains a preprocessor directive? A) int main(void) B) printf("Hello!\n"); C) #include <stdio.h> D) return 0; | Lines starting with # are preprocessor directives, which are processed before the main compilation stage. |
| What is the primary purpose of the `gcc -o hello hello.c` command? | `gcc` is the compiler. `hello.c` is the source input. `-o hello` specifies the output executable's name |
| what is the format specifier for a doulble | %lf |
| Which line of code correctly reads a single floating-point number into a variable named price? A) scanf("%d", &price); B) scanf("%f", price); C) scanf("%f", &price); D) scanf("%s", &price); | scanf("%f", &price); |
| In C, this standard input stream is called | stdin |
| fgets(); what goes in the () | buffer, size_of_buffer, stdin |
| What is a key advantage of fgets() over scanf("%s", ...)? | it prevents buffer overflows by using a size limit |
| What if you have data inside a string and need to extract it? For this, we use | sscanf(), used to "parse string" |
| Given char str[] = "ID: 99"; int id;, how do you correctly extract the number into the id variable? | sscanf(str, "ID: %d", &id); |
| What is a Variable? | A variable is a human-readable name that refers to some data in memory |
| Which of the following is a valid variable name in C? A) 2_players B) __score C) player_one_score D) final-score | player_one_score |
| Variable rules! 1. Cannot start with a ______ 2. Cannot start with 2 ______ 3. Cannot start with an underscore followed by a _______ _______ | digit, underscores, capital letter |
| What is declaration vs initialization? | Declaration is giving a variable a type and a name and initialization is actually setting it equal to something |
| WWhat does -32 evaluate to in boolean logic | true, Any non-zero integer (e.g., 1, -37, 42) means true. |
| what is the exponentiation operator (exponent) | trick question there is none, you can use pow() for exponents but this is a function that comes with <math.h> |
| int a = 5; int b = a++ * 2; what are the final values | a = 6, b = 10 |
| What is the output of this code snippet? int x = 5; if (x > 5) { printf("A"); } else if (x == 5) { printf("B"); } else { printf("C"); } printf("D\n"); | BD |
| What is the output of this code? char grade = 'B'; switch (grade) { case 'A': printf("Excellent"); break; case 'B': case 'C': printf("Good"); case 'D': printf(" Pass"); break; default: printf("Fail"); } | Good pass |
| Whats the output int i = 1; // Initialization while (i <= 5) { // Terminating Condition printf("%d\n", i); i++; // Post Increment } printf("All done!\n"); | 1 2 3 4 5 |
| Whats the sum final total? int sum = 0; for (int i = 1; i <= 3; i++) { sum += i; } | 6 |
| A function definition has a | header and a body. |
| The local variables declared in the function's definition. They are placeholders for the data the function will receive. | Parameters |
| The actual values or expressions passed to the function when it is called. | Arguments |
| when you pass an argument to a function, a copy of that argument's value is made and stored locally within the function. | Pass-by-Value |
| Output? #include <stdio.h> void modify_value(int x) { x = x * 2; } int main(void) { int y = 5; modify_value(y); printf("y = %d\n", y); return 0; } | y = 5 |
| What is the primary purpose of a function prototype? | To tell the compiler about a function signature before it is defined, allowing calls to be made from anywhere |
| What are the 5 things stored in the header file? | 1. Function prototypes. 2. Struct definitions. 3. Macro definitions (#define). 4. Professional documentation for the functions. |
| The #ifndef/#define/#endif lines are called "_______ ______". They prevent the header from being included multiple times, which would cause an error. | include guards |
| Why don't you have to include the h files in the gcc compiler | they are brought in by the #include directive |
| You have three files: main.c, math_utils.c, and math_utils.h. Which command is the correct way to compile them into an executable named calc? | gcc -Wall -std=c99 main.c math_utilities.c -o calc |
| int age = 30; int *p_age; how do you get the address of the variable age and assign it to the pointer p_age? | p_age = &age; |
| Whats the final values of x and y int x = 5; int y = 10; int *p = &x; *p = 20; p = &y; *p = 30; | x = 20, y = 30 |
| You want to write a function void get_data(int *a, int *b) that sets two integer variables in main. How would you call this function from main? int main(void) { int val1, val2; // How to call get_data here? } | get_data(&val1, &val2); |
| What is the index of the last element in the array int scores[10];? | 9 |
| int a[10] = {[5] = 55, 66, 77}; what does the resulting array contain | {0, 0, 0, 0, 0, 55, 66, 77, 0, 0} |
| int arr[5] = {[2] = 10, 20}; | {0, 0, 10, 20, 0} |
| int a[2][5] = { {0, 1, 2, 3, 4}, {5, 6, 7, 8, 9} }; int value = a[1][3]; what is this value | 8 |
| What is the value of x after this code runs? int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int x = matrix[1][2]; | 6 |
| Output? void modify(int arr[]) { arr[0] = 99; } int main(void) { int data[] = {10, 20}; modify(data); printf("%d\n", data[0]); } | 99 |
| Which of these code snippets will most likely cause a crash (Segmentation Fault)? A) char s[] = "Hi"; s[0] = 'B'; B) char *s = "Hi"; s[0] = 'B'; | B) char *s = "Hi"; s[0] = 'B'; because char *s creates a pointer to a read-only string literal. Trying to modify it (s[0] = 'B') causes a segmentation fault. |
| strlen() | Get the length of a string |
| strcpy() / strncpy() | Copy a string |
| strcat() / strncat() | Concatenate (join) strings. |
| strcmp() / strncmp() | Compare two strings |
| strchr() / strrchr() | Find a character in a string. |
| strstr() | Find a substring in a string. |
| what is the formatting of strncpy() | char *strncpy(char *dest, const char *src, size_t n); 1. destination, 2. what you want to copy to it, 3. size |
| what isa critical pitfall of strncpy()? | strncpy() is safer, but tricky. If the source string src is longer than n, it will NOT copy the \0' terminator! |
| What is the formatting for strncat()? | char *strncat(char *dest, const char *src, size_t n); destination, what your joining, size |
| What is the formatting for strncmp() | int strncmp(const char *s1, const char *s2, size_t n); if this is = to 0 the strings are identical |
| What does strchr() do? | it looks for a single character within a string |
| What is the difference between strchr() and strrchr() | the normal one looks for the FIRST occurrence strrchr() looks for the last occurrence or "most right" |
| What does strchr() return | if the char is found it returns a pointer to that char if not it returns NULL |
| What does strstr() do? what does it return()? | Finds the first occurrence of the entire needle string inside the haystack string. Returns a pointer to the start of the match within haystack. Returns NULL if the needle is not found. |
| char s[] = "Hello"; is a _________ array. (Good!) char *s = "Hello"; is an _________ string literal (don't try to change it or do any functions to it) | mutable, immutable |
| how do you open a file? | FILE *fp = fopen("filename.txt", "mode"); |
| what are the 3 basic file modes | "r" read, "w" write, "a" append |
| what must you always do after you open the file? What must you always do at the end once your done with the file? | check if its NULL, close the file fclose(fp) |
| what does this do fputc(int c, FILE *fp) | Writes a single character into the file |
| what does this do fputs(const char *s, FILE *fp) | Writes a string (but doesn't add \n). |
| At the end of a file you can find a special integer called _____ | EOF - end of file |
| Why must the variable c in c = fgetc(fp); be an int and not a char? | To hold the special EOF value, which is outside the char range. |
| What is the most safe way to read a file that contains name age pairs ("Alice 25") per line? A) Use fgetc() in a loop and parse it manually B) Use fscanf(fp, "%s %d", ) in a loop C) Use fgets() to read a line, then sscanf() to parse the line | C) Use fgets() to read a line, then sscanf() to parse the line |
| What is the output of this code? char line[] = "C|is|fun"; char *t1 = strtok(line, "|"); char *t2 = strtok(NULL, "|"); printf("%s %s", t1, t2); A) C|is|fun C|is|fun B) C is C) C C D) (Crash) | B) C is |
| What is the stack vs the Heap? | Stack: Managed automatically by the computer. Data is "local" to functions. Fixed size, relatively small. Heap: Managed by you, the programmer. Data is "global" and persistent. Flexible size, much larger. |
| How can we manage the heap? | We manage the heap with malloc(), calloc(), realloc(), and free() |
| Which line correctly allocates memory for an array of 50 float s? A) float *p = malloc(50); B) float *p = malloc(sizeof(float) * 50); C) float *p = malloc(sizeof(float) + 50); D) float *p = malloc(sizeof(50)); | B) float *p = malloc(sizeof(float) * 50); |
| What is the primary functional difference between malloc(100) and calloc(100, 1)? B) malloc() is faster than calloc() C) malloc() leaves memory uninitialized (garbage), calloc() clears it to zero. D) malloc() returns void*, calloc() returns char*. | C) malloc() leaves memory uninitialized (garbage), calloc() clears it to zero. |
| What is the primary danger of the line p = realloc(p, new_size);? B) It doesn't work; you must use malloc and free C) If realloc fails and returns NULL, you lose your original pointer p and cause a memory leak | C) If realloc fails and returns NULL, you lose your original pointer p and cause a memory leak |