-
What does the escape sequence \n do inside a printf string?
- Prints the letter n
- Moves the output to a new line
- Ends the program
- 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.
-
Which header file declares printf and scanf?
- string.h
- stdlib.h
- stdio.h
- 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.
-
An array is declared as int a[8]; Which index refers to its last element?
- 7
- 8
- 9
- 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.
-
The variable price has type double. Which call correctly reads a value into it?
- scanf("%f", &price);
- scanf("%lf", price);
- scanf("%d", &price);
- 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.
-
What does this print? double r = 7 / 2; printf("%.1f", r);
- 3.5
- 3.0
- 4.0
- 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.
-
What is printed? int d = 2; switch (d) { case 1: printf("A"); case 2: printf("B"); case 3: printf("C"); break; default: printf("D"); }
- BC
- B
- BCD
- 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.
-
A function contains: static int n = 0; n++; printf("%d ", n); and it is called three times. What is printed?
- 0 0 0
- 1 1 1
- 1 2 3
- 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.
-
Which fopen mode opens a file so that new text is added at the end without erasing what is already there?
- "r"
- "w"
- "rb"
- "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.
-
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);
- 4 3
- 40 3
- 40 40
- 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.
-
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?
- The real number of elements in the array that was passed
- 1
- 2
- 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