Skip to content
VJ Classes

One place to learn science, commerce, arts, computers and life skills

10 questions. Think of your answer first, then open “Show answer”.

  1. What does the escape sequence \n do inside a printf string?

    1. Prints the letter n
    2. Moves the output to a new line
    3. Ends the program
    4. Prints a backslash
    Show answer

    Answer: B · Moves the output to a new line

    \n is the newline escape sequence. To print an actual backslash you would write \\ instead.

  2. Which header file declares printf and scanf?

    1. string.h
    2. stdlib.h
    3. stdio.h
    4. math.h
    Show answer

    Answer: C · stdio.h

    stdio.h is the standard input-output header. string.h holds the string functions and stdlib.h holds malloc and free.

  3. An array is declared as int a[8]; Which index refers to its last element?

    1. 7
    2. 8
    3. 9
    4. 6
    Show answer

    Answer: A · 7

    Indexes start at 0, so an array of 8 elements uses the indexes 0 to 7. Index 8 would lie outside the array.

  4. The variable price has type double. Which call correctly reads a value into it?

    1. scanf("%f", &price);
    2. scanf("%lf", price);
    3. scanf("%d", &price);
    4. scanf("%lf", &price);
    Show answer

    Answer: D · scanf("%lf", &price);

    scanf needs %lf for a double and the address of the variable. %f is for float, and leaving out the & gives scanf no place to store the value.

  5. What does this print? double r = 7 / 2; printf("%.1f", r);

    1. 3.5
    2. 3.0
    3. 4.0
    4. 3
    Show answer

    Answer: B · 3.0

    Both 7 and 2 are ints, so 7 / 2 is integer division and gives 3. The 3 is then converted to 3.0 and stored in r.

  6. What is printed? int d = 2; switch (d) { case 1: printf("A"); case 2: printf("B"); case 3: printf("C"); break; default: printf("D"); }

    1. BC
    2. B
    3. BCD
    4. ABC
    Show answer

    Answer: A · BC

    Execution starts at case 2 and, because there is no break after it, falls through into case 3. The break after case 3 stops it, so default never runs.

  7. A function contains: static int n = 0; n++; printf("%d ", n); and it is called three times. What is printed?

    1. 0 0 0
    2. 1 1 1
    3. 1 2 3
    4. 3 3 3
    Show answer

    Answer: C · 1 2 3

    A static local variable is created once and keeps its value between calls, so n counts 1, 2, 3.

  8. Which fopen mode opens a file so that new text is added at the end without erasing what is already there?

    1. "r"
    2. "w"
    3. "rb"
    4. "a"
    Show answer

    Answer: D · "a"

    Mode "a" appends to the end of the file. Mode "w" creates the file or erases an existing one, and "r" only reads.

  9. What does this print? int b[5] = {1, 2, 3, 4, 5}; int *q = b; q += 2; q[1] = 40; printf("%d %d", b[3], *q);

    1. 4 3
    2. 40 3
    3. 40 40
    4. 3 40
    Show answer

    Answer: B · 40 3

    After q += 2, q points at b[2], which holds 3. The expression q[1] is the next element, b[3], so it becomes 40.

  10. Inside a function declared as void f(int arr[]), on a 64-bit machine with 4-byte ints and 8-byte pointers, what does sizeof(arr) / sizeof(arr[0]) give?

    1. The real number of elements in the array that was passed
    2. 1
    3. 2
    4. 8
    Show answer

    Answer: C · 2

    The parameter arr is really a pointer, so sizeof(arr) is 8, not the size of the whole array. 8 divided by 4 is 2 whatever array was passed, so the length must be passed as a separate parameter.

Want to test yourself? Take the same questions as a timed practice test and see your score. Start the practice test