← All practice tests

COMP 503 · Exam 1

Quick drills

One-line questions for the last few minutes before the exam. Answer in your head, then tap a question to check yourself.

Linux Module 1

  1. 1Print the full path of the directory you're in.

    pwd (print working directory)

  2. 2From anywhere, go back to your home directory.

    cd on its own (or cd ~)

  3. 3Read a long file one screen at a time.

    less notes.txt: Space for the next page, q to quit. more works too.

  4. 4On one line, run make and then ls, running ls even if make fails.

    make ; ls. With && instead, ls would be skipped after a failure.

  5. 5What does ls -l show for secret after chmod 700 secret?

    -rwx------: the owner can read, write and run it; the group and others get nothing.

  6. 6Make a gzip-compressed archive hw.tar.gz of the directory hw.

    tar -czvf hw.tar.gz hw: c creates, z compresses, and the archive's name comes right after f, before the folder.

  7. 7In vim's Command Mode, undo the last change. Then, after searching with /main, jump to the next match.

    u undoes. n jumps to the next match (N goes back one).

Data representation Module 2

  1. 80xFF in decimal?

    255 (15 × 16 + 15)

  2. 91010 1010 (binary) in hexadecimal?

    0xAA: each group of 4 bits, 1010, is A.

  3. 1045 as 8-bit binary?

    0010 1101 (32 + 8 + 4 + 1)

  4. 11−1 as an 8-bit two's complement integer?

    1111 1111 (0xFF): flip 0000 0001 to 1111 1110, then add 1.

  5. 12The largest value a 16-bit unsigned integer can hold?

    65,535, which is 2¹⁶ − 1.

  6. 13How many bits does one hex digit stand for? How many hex digits make one byte?

    4 bits per hex digit, so 2 hex digits per byte.

C basics Module 3

  1. 14In C, what are 7 / 2 and 7 % 2?

    3 and 1. Dividing two ints drops the fraction; % gives the remainder.

  2. 15(double) 7 / 2 or (double) (7 / 2): which one is 3.5?

    (double) 7 / 2 is 3.5. The other divides as integers first and gives 3.0.

  3. 16What does printf("[%5.1f]\n", 3.14159); print?

    [  3.1]: one decimal place, padded on the left with two spaces to a width of 5.

  4. 17After int a = 5; a *= 2 + 1; what is a?

    15. a *= 2 + 1 means a = a * (2 + 1).

  5. 18What is '7' - '0'?

    7, as an int. The digit characters are next to each other in ASCII, so this turns a digit character into its number.

  6. 19How many times does the body of do { ... } while (0); run?

    Once. A do-while runs its body before it checks the condition.

Functions and arrays Module 4

  1. 20For int temps[12]; what is sizeof(temps) / sizeof(temps[0])?

    12: the array is 48 bytes, and each int is 4.

  2. 21What return type does a function that returns nothing have?

    void

  3. 22Where does a function's prototype go, and how does it end?

    Above main (or in a header file), ending with ;. It must match the definition's return type and parameters.

  4. 23What are the valid indexes of int scores[8]?

    0 to 7. scores[8] is past the end: C doesn't stop you, but it's undefined behavior.

Structs and pointers Module 5

  1. 24p_s->name is short for what?

    (*p_s).name. The parentheses matter, because . binds tighter than *.

  2. 25With int a[3] = {4, 5, 6}; and int *p = a; what are *(p + 2) and p[1]?

    6 and 5. p[i] means *(p + i).

  3. 26Which printf specifier prints an address?

    %p

  4. 27On Zeus, how big is a pointer to a char? To a large struct?

    8 bytes either way. A pointer holds an address, whatever it points to.

  5. 28Why does sscanf(line, "%d", &n) need the &?

    sscanf has to store the number in n, so it needs n's address. Passing n itself would only give it a copy of the value.

Strings, heap, gdb Module 6

  1. 29strlen("hello"), and sizeof(s) for char s[] = "hello";?

    5 and 6. sizeof counts the '\0' at the end; strlen doesn't.

  2. 30What does strcmp return when the two strings are equal?

    0. That's why the test is strcmp(a, b) == 0, not a == b, which compares addresses.

  3. 31Two differences between malloc(n * sizeof(int)) and calloc(n, sizeof(int))?

    calloc takes the count and the size separately, and it fills the memory with zeros. malloc leaves whatever was there.

  4. 32The command that checks ./prog for memory leaks?

    valgrind --leak-check=full ./prog

  5. 33In gdb: show the chain of calls that led to where you're stopped, then keep running to the next breakpoint.

    backtrace (bt), then continue (c).