CLA (C language) study materials
CLA (C language) study resources

CLA (C language) practice questions

One question at a time. Build your CLA (C language) understanding.

First 50 of 200 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.

Continue in the practice tool above to load more questions, 50 at a time.

Question 1C Essentials 1 (C101) aligned content: basics of C programming

Which header must be included to use printf and scanf in a C program?

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

Explanation

The declarations of printf and scanf live in the standard input/output header stdio.h, so a program calling them must include that header before use. Without it the compiler has no prototype and behaviour is undefined.

Question 2C Essentials 1 (C101) aligned content: basics of C programming

What is the value of the expression 7 / 2 when both operands are int in C?

  1. 3.0
  2. 3.5
  3. 3 Correct answer.
  4. 4

Explanation

When both operands of the division operator are integers, C performs integer division and truncates the fractional part toward zero. The result is therefore 3, and its type is int, not a floating type.

Question 3C Essentials 1 (C101) aligned content: basics of C programming

Which declaration creates a variable that stores a single character?

  1. char *letter;
  2. char letter[1];
  3. char letter; Correct answer.
  4. int letter;

Explanation

A plain char object holds exactly one character value, so char letter; declares a single character variable. The other forms declare an array, an integer, or a pointer, which are different kinds of objects.

Question 4C Essentials 1 (C101) aligned content: basics of C programming

Which escape sequence inserts a newline into a string literal?

  1. \0
  2. \t
  3. \\
  4. \n Correct answer.

Explanation

The escape sequence \n represents the newline character and moves output to the start of the next line. The other sequences represent a tab, a backslash, and the null character respectively.

Question 5C Essentials 1 (C101) aligned content: basics of C programming

What does the statement return 0; at the end of main indicate?

  1. That main returns nothing
  2. A compile error
  3. An abnormal termination
  4. Successful program termination Correct answer.

Explanation

The value returned from main is the program exit status passed to the host environment, and zero conventionally signals successful termination. Nonzero values report failure to the caller.

Question 6C Essentials 1 (C101) aligned content: basics of C programming

Which operator is used to access a member of a structure through a pointer to that structure?

  1. *
  2. &
  3. -> Correct answer.
  4. .

Explanation

The arrow operator -> combines dereferencing and member selection, so ptr->member is equivalent to (*ptr).member. The dot operator works only on structure objects, not on pointers to them.

Question 7C Essentials 1 (C101) aligned content: basics of C programming

How many times does the loop body execute in for (i = 0; i < 5; i++)?

  1. 6
  2. 4
  3. 5 Correct answer.
  4. 0

Explanation

The loop variable takes the values 0, 1, 2, 3 and 4, and the condition i < 5 fails when i reaches 5. That gives exactly five executions of the body.

Question 8C Essentials 1 (C101) aligned content: basics of C programming

What is the index of the first element of an array declared as int a[10];?

  1. 0 Correct answer.
  2. 10
  3. -1
  4. 1

Explanation

C arrays are zero-based, so the valid indices for int a[10]; run from 0 through 9. Index 10 lies one past the last element and must not be accessed.

Question 9C Essentials 1 (C101) aligned content: basics of C programming

What is printed by the following code? int x = 5; printf("%d", x++); printf("%d", x);

  1. 65
  2. 66
  3. 55
  4. 56 Correct answer.

Explanation

The postfix increment x++ yields the current value 5 for the first printf and then increments x to 6. The second printf therefore prints 6, so the output is 56 with no separator.

Question 10C Essentials 1 (C101) aligned content: basics of C programming

Given int a[5] = {1, 2, 3}; what is the value of a[4]?

  1. Garbage
  2. 3
  3. 0 Correct answer.
  4. 5

Explanation

When an array initializer supplies fewer values than the array size, the remaining elements are initialized to zero. Elements a[3] and a[4] are therefore both 0.

Question 11C Essentials 1 (C101) aligned content: basics of C programming

Which statement about the scope of a variable declared inside a function body without a storage class specifier is correct?

  1. It is visible only inside main.
  2. It is visible throughout the entire program.
  3. It is visible in all functions defined after it.
  4. It is visible only within the block in which it is declared. Correct answer.

Explanation

An automatic variable declared inside a block has block scope, meaning its name is visible from its declaration to the end of that block. Other functions cannot refer to it by name.

Question 12C Essentials 1 (C101) aligned content: basics of C programming

What does the expression *p yield when p is a pointer to int that points to a valid int object?

  1. The address stored in p
  2. The int value stored at the pointed-to address Correct answer.
  3. A copy of the pointer
  4. The size of the pointed-to object

Explanation

The unary * operator dereferences a pointer and designates the object it points to, so *p is an lvalue for that int. Using its value reads the stored integer.

Question 13C Essentials 1 (C101) aligned content: basics of C programming

Which function compares two C strings for equality of their contents?

  1. strcpy
  2. strlen
  3. strcmp Correct answer.
  4. strcat

Explanation

strcmp compares two null-terminated strings character by character and returns zero when their contents are equal. Using == on two char pointers compares addresses instead of contents.

Question 14C Essentials 1 (C101) aligned content: basics of C programming

What is the effect of the break statement inside a loop?

  1. It skips the rest of the current iteration and continues with the next one.
  2. It restarts the loop from the beginning.
  3. It terminates the innermost enclosing loop or switch immediately. Correct answer.
  4. It terminates the whole program.

Explanation

break transfers control out of the nearest enclosing loop or switch statement, and execution resumes after that construct. In nested loops only the innermost one is left.

Question 15C Essentials 1 (C101) aligned content: basics of C programming

What does the following code print? int i = 0; while (i < 3) { printf("%d", i); i += 2; }

  1. 024
  2. 01
  3. 012
  4. 02 Correct answer.

Explanation

The variable starts at 0, is printed, then increased to 2; 2 is printed and increased to 4, at which point the condition fails. The output is therefore 02.

Question 16C Essentials 1 (C101) aligned content: basics of C programming

Which statement correctly passes an array named data of 10 ints to a function that expects a pointer to int?

  1. func(&data);
  2. func(data[10]);
  3. func(data); Correct answer.
  4. func(*data);

Explanation

In most expressions an array name decays to a pointer to its first element, so passing data supplies an int pointer. Writing data[10] would read out of bounds.

Question 17C Essentials 1 (C101) aligned content: basics of C programming

What is the difference between the declarations int *const p; and const int *p;?

  1. Both make the pointer and the pointed-to int constant.
  2. The first makes the pointed-to int constant, the second makes the pointer constant.
  3. Both are identical and differ only in style.
  4. The first makes the pointer itself constant, the second makes the pointed-to int read-only through p. Correct answer.

Explanation

In int *const p the const qualifies the pointer, so p cannot be reassigned but the int it points to can be modified. In const int *p the const qualifies the pointed-to int, so *p cannot be modified through p while p itself can be reassigned.

Question 18C Essentials 1 (C101) aligned content: basics of C programming

What does the sizeof operator return for an array declared as double v[8];?

  1. The number of elements, always 8
  2. The total size in bytes of all eight elements Correct answer.
  3. The address of the first element
  4. The size of one element in bytes

Explanation

Applied to an array name, sizeof yields the size of the whole array in bytes, which equals eight times the size of one double. Dividing this by sizeof v[0] gives the element count.

Question 19C Essentials 1 (C101) aligned content: basics of C programming

What is the value of the expression 10 % 3 + 2 * 3 in C?

  1. 16
  2. 7 Correct answer.
  3. 9
  4. 4

Explanation

The multiplicative operators bind tighter than addition, so 10 % 3 gives 1 and 2 * 3 gives 6. Adding them yields 7.

Question 20C Essentials 1 (C101) aligned content: basics of C programming

A function is defined as void swap(int a, int b). Why does calling swap(x, y) fail to exchange the caller's variables?

  1. The function name swap is reserved.
  2. C forbids passing variables of type int by value.
  3. The parameters are copies, so changes affect only the local copies. Correct answer.
  4. The void return type prevents modifications.

Explanation

C passes arguments by value, so swap receives copies of x and y and any assignment inside the function affects only those copies. Exchanging the caller's objects requires passing their addresses and dereferencing them.

Question 21C Essentials 1 (C101) aligned content: basics of C programming

What is printed by the following code? int x = 3; switch (x) { case 1: printf("A"); case 3: printf("B"); case 4: printf("C"); break; default: printf("D"); }

  1. BC Correct answer.
  2. BCD
  3. B
  4. C

Explanation

Control jumps to case 3, which prints B, and because no break follows, execution falls through to case 4 and prints C. The break then leaves the switch, so the output is BC.

Question 22C Essentials 1 (C101) aligned content: basics of C programming

Given char s[] = "hello"; what is the value of sizeof s?

  1. 6 Correct answer.
  2. 5
  3. 7
  4. 4

Explanation

A string literal used to initialize a char array includes its terminating null character, so the array holds five letters plus one null byte. sizeof s therefore evaluates to 6.

Question 23C Essentials 1 (C101) aligned content: basics of C programming

What is the result of the expression (char)200 on a typical platform where char is signed and 8 bits wide?

  1. 0
  2. 200
  3. -56 Correct answer.
  4. 56

Explanation

Converting 200 to a signed 8-bit char reinterprets the low eight bits 11001000, whose two's complement value is -56. The conversion is implementation-defined when the value does not fit, and this is the usual behaviour.

Question 24C Essentials 1 (C101) aligned content: basics of C programming

Consider int a[3][4]; what does the expression a[1] have as its type after the usual array-to-pointer conversion?

  1. int **, a pointer to pointer to int
  2. int (*)[4], a pointer to an array of 4 int Correct answer.
  3. int, the value of one element
  4. int *, a pointer to int

Explanation

a is an array of three arrays of four int, so a[1] is an array of four int. In an expression it converts to a pointer to its first element, giving type pointer to array of 4 int, written int (*)[4].

Question 25C Essentials 1 (C101) aligned content: basics of C programming

A recursive function has no base case. What is the most likely consequence at run time?

  1. The function returns zero after a fixed number of calls.
  2. The compiler rejects the program before it runs.
  3. The recursion stops automatically when the counter reaches zero.
  4. Unbounded recursion exhausts the call stack and the program terminates abnormally. Correct answer.

Explanation

Without a base case each call invokes another call and stack frames accumulate until the available stack space is exhausted, typically causing a crash. The language does not impose an automatic depth limit that returns a value.

Question 26C Essentials 1 (C101) aligned content: basics of C programming

In C, what does the declaration `int *p;` establish about p?

  1. p is an int object that stores an integer value
  2. p is a function returning an int pointer
  3. p is an object that can hold the address of an int object Correct answer.
  4. p is an array of int values with unspecified length

Explanation

The asterisk in a declaration marks the declared identifier as a pointer, so p names an object whose stored value is an address of an int. The pointed-to int is a separate object that may or may not exist yet. Until p is initialized it holds an indeterminate value and must not be dereferenced.

Question 27C Essentials 1 (C101) aligned content: basics of C programming

Which preprocessor directive makes the contents of a header file available to a translation unit before compilation?

  1. #include Correct answer.
  2. #define
  3. #ifdef
  4. #pragma

Explanation

#include causes the preprocessor to textually insert the named file at that point, which is how declarations from headers become visible to the compiler. #define creates a macro, #pragma gives implementation-defined instructions, and #ifdef begins conditional compilation. Only #include performs file inclusion.

Question 28C Essentials 1 (C101) aligned content: basics of C programming

A variable declared as `unsigned char c;` on a platform where char is 8 bits wide. Which range of values can c represent?

  1. 0 to 255 Correct answer.
  2. 0 to 65535
  3. -32768 to 32767
  4. -128 to 127

Explanation

An unsigned 8-bit integer uses all eight bits for magnitude, giving 2^8 = 256 distinct values from 0 through 255. The negative ranges apply only to signed types. The 65535 upper bound would require 16 bits.

Question 29C Essentials 1 (C101) aligned content: basics of C programming

Which escape sequence in a C string literal represents a single newline character?

  1. \\
  2. \0
  3. \n Correct answer.
  4. \t

Explanation

In a character or string literal the backslash introduces an escape sequence, and \n denotes the newline character. \t is a horizontal tab, \\ is a literal backslash, and \0 is the null character. Each escape sequence still occupies exactly one character position in the string.

Question 30C Essentials 1 (C101) aligned content: basics of C programming

Given `int n = 7;`, what is the value of the expression `n / 2` in C?

  1. 3.5
  2. 3 Correct answer.
  3. 4
  4. 3.0

Explanation

When both operands of / have integer type, C performs integer division and discards the fractional part, so 7/2 yields 3. No rounding to the nearest value occurs. To obtain 3.5 at least one operand would need a floating type.

Question 31C Essentials 1 (C101) aligned content: basics of C programming

Which header must be included to use the printf function in a C program?

  1. <string.h>
  2. <stdio.h> Correct answer.
  3. <math.h>
  4. <stdlib.h>

Explanation

printf is declared in the standard header <stdio.h>, which provides input and output facilities. <stdlib.h> declares general utilities, <string.h> declares string handling functions, and <math.h> declares mathematical functions. Calling printf without its declaration is a constraint violation in modern C.

Question 32C Essentials 1 (C101) aligned content: basics of C programming

What is the value of the expression `!0` in C?

  1. An indeterminate value
  2. -1
  3. 0
  4. 1 Correct answer.

Explanation

The logical negation operator yields 1 when its operand compares equal to zero and 0 otherwise. Since 0 is false, !0 produces the int value 1. The result of any logical operator in C is always exactly 0 or 1.

Question 33C Essentials 1 (C101) aligned content: basics of C programming

Which statement about the `continue` statement inside a for loop is correct?

  1. It skips the rest of the current iteration and proceeds to the loop's increment expression Correct answer.
  2. It restarts the program from the beginning of main
  3. It skips the increment expression and jumps directly to the loop condition test
  4. It terminates the entire loop immediately and transfers control past the loop

Explanation

continue ends the current iteration of the innermost enclosing loop. In a for loop control then passes to the increment expression, after which the condition is tested again. This differs from break, which exits the loop entirely.

Question 34C Essentials 1 (C101) aligned content: basics of C programming

What is the value of `sizeof(char)` in C, in bytes?

  1. It depends on the platform's word size
  2. 2
  3. 1 Correct answer.
  4. 4

Explanation

The C standard defines sizeof(char) to be exactly 1, because the size of a char is the unit in which object sizes are measured. Other types vary between platforms, but char does not. The value is of type size_t and is a compile-time constant here.

Question 35C Essentials 1 (C101) aligned content: basics of C programming

Consider `int x = 10; int y = 3; double d = x / y;`. What value does d hold?

  1. 3.5
  2. 4.0
  3. 3.3333333333333335
  4. 3.0 Correct answer.

Explanation

Both operands of / are int, so integer division is performed first and yields 3; only then is the int result converted to double for assignment. The fractional part is lost before the conversion. Writing x / (double)y or (double)x / y would produce the fractional result.

Question 36C Essentials 1 (C101) aligned content: basics of C programming

Which declaration creates a pointer that can be reassigned but cannot be used to modify the int it points to?

  1. int *const p;
  2. int const *const p;
  3. const int *p; Correct answer.
  4. const int *const p;

Explanation

In const int *p the const qualifies the pointed-to int, so *p may not be assigned through p, while p itself can be pointed elsewhere. Placing const after the asterisk, as in int *const p, makes the pointer itself constant instead. The two const forms make both the pointer and the pointee read-only.

Question 37C Essentials 1 (C101) aligned content: basics of C programming

What does the expression `arr + 2` yield when arr is declared as `int arr[10];`?

  1. A pointer to the second element of arr
  2. The address of the array itself, unchanged
  3. A pointer to the third element of arr Correct answer.
  4. The value stored in arr[2]

Explanation

In an expression an array name converts to a pointer to its first element. Pointer arithmetic scales by the size of the pointed-to type, so adding 2 advances by two int objects and points at arr[2], the third element. The element's value is obtained only by dereferencing.

Question 38C Essentials 1 (C101) aligned content: basics of C programming

A function is declared as `int f(int);` and later called with a double argument, as in `f(2.7);`. What happens to the argument?

  1. The call is rejected because the types do not match exactly
  2. It is converted to int, truncating toward zero to 2 Correct answer.
  3. It is converted to int by rounding to the nearest value, 3
  4. The double bits are reinterpreted as an int without conversion

Explanation

When the prototype is visible, the argument is converted to the parameter type as if by assignment. Conversion from double to int discards the fractional part, so 2.7 becomes 2. The conversion truncates toward zero rather than rounding.

Question 39C Essentials 1 (C101) aligned content: basics of C programming

Which initializer correctly creates a null-terminated string containing exactly the characters 'O', 'K' in a char array?

  1. char s[3] = {'O', 'K', '!'};
  2. char s[2] = "OK";
  3. char s[] = {'O', 'K'};
  4. char s[] = "OK"; Correct answer.

Explanation

A string literal always includes a terminating null character, so "OK" supplies three characters and the unsized array is given room for all of them. The result is a valid string that functions such as strlen can process. Omitting the space for the terminator leaves an array that is not a string.

Question 40C Essentials 1 (C101) aligned content: basics of C programming

What does the expression `&a[3]` denote when a is an array of int?

  1. The address of the array a as a whole
  2. The address of the fourth element of a Correct answer.
  3. The address of the first element of a
  4. The value of the fourth element of a

Explanation

The subscript operator a[3] designates the element at index 3, which is the fourth element because indexing starts at zero. Applying the address-of operator yields a pointer to that element. Its type is int *, and dereferencing it gives the element's value.

Question 41C Essentials 1 (C101) aligned content: basics of C programming

Given `int m[2][3] = {{1,2,3},{4,5,6}};`, what is the value of `m[1][2]`?

  1. 3
  2. An out-of-bounds access
  3. 5
  4. 6 Correct answer.

Explanation

The first index selects the row and the second selects the column, both counting from zero. m[1] is the second row {4,5,6}, and its element at index 2 is 6. Both indices are within the declared bounds of 2 rows and 3 columns.

Question 42C Essentials 1 (C101) aligned content: basics of C programming

Which statement correctly declares an array of 5 pointers to char?

  1. char *arr[5]; Correct answer.
  2. char arr[5];
  3. char *arr;
  4. char (*arr)[5];

Explanation

The postfix brackets bind more tightly than the asterisk, so char *arr[5] declares an array of five elements, each of type char *. This is the usual form for an array of strings. Parentheses would be needed to declare a pointer to an array instead.

Question 43C Essentials 1 (C101) aligned content: basics of C programming

A function is defined with the prototype `void g(int *p);` and an array `int v[4];` exists in the caller. Which call passes the address of v's first element?

  1. g(&v);
  2. g(v); Correct answer.
  3. g(v[0]);
  4. g(*v);

Explanation

An array name in an argument expression converts to a pointer to its first element, which matches the parameter type int *. Passing v[0] or *v would pass an int value where a pointer is required. Passing &v supplies a pointer to the whole array, whose type int (*)[4] does not match int *.

Question 44C Essentials 1 (C101) aligned content: basics of C programming

Which statement about a function declared with the storage-class specifier `static` at file scope is correct?

  1. It has internal linkage and is visible only within its own translation unit Correct answer.
  2. It is automatically inlined at every call site by the compiler
  3. It can be called only once during program execution
  4. It is stored in read-only memory and cannot be modified at run time

Explanation

At file scope, static gives a function internal linkage, so its name is not visible to other translation units. This is a linkage property, not a statement about memory protection, call counting, or inlining. Its definition must still appear in the same file for calls to be valid.

Question 45C Essentials 1 (C101) aligned content: basics of C programming

Inside a function, a variable declared as `static int counter = 0;` is incremented on each call. What is its lifetime?

  1. It is allocated on the heap and must be freed by the caller
  2. It is recreated and reinitialized to 0 on every call
  3. It exists only while the function's stack frame is active
  4. It persists for the entire program run and keeps its value between calls Correct answer.

Explanation

The static storage class gives the object static storage duration, so it is created before program startup and destroyed only at program termination. Its initializer runs once, and the value carries over between calls. Block scope still limits where the name can be used.

Question 46C Essentials 1 (C101) aligned content: basics of C programming

What does the expression `x = 5, y = 7;` do when x and y are int variables?

  1. It assigns 5 to x and leaves y unchanged
  2. It assigns 5 to x and then assigns 7 to y, with the overall expression value 7 Correct answer.
  3. It assigns 7 to both x and y
  4. It is a syntax error because commas cannot separate assignments

Explanation

The comma operator evaluates its left operand, discards that value, then evaluates the right operand, and the result of the whole expression is the right operand's value. Both assignments are therefore performed in sequence. Its low precedence means the whole expression here is (x = 5), (y = 7).

Question 47C Essentials 1 (C101) aligned content: basics of C programming

Given `int a = 5, b = 0;`, what is the value of the expression `b && (a = 10)`?

  1. 0, and a becomes 10
  2. 1, and a becomes 10
  3. 0, and a remains 5 Correct answer.
  4. 1, and a remains 5

Explanation

The && operator short-circuits: if the left operand compares equal to zero, the right operand is not evaluated at all. Because b is 0, the assignment to a never executes and the result is 0. This guarantees that a keeps its original value of 5.

Question 48C Essentials 1 (C101) aligned content: basics of C programming

A struct is declared as `struct P { int x; int y; };` and a variable `struct P s;` is defined. Which expression reads the y member of s?

  1. *s.y
  2. s.y Correct answer.
  3. &s.y
  4. s->y

Explanation

The dot operator accesses a member of a structure object directly, so s.y designates the member. The arrow operator applies only when the left operand is a pointer to a structure. The address-of operator would yield a pointer to the member rather than its value.

Question 49C Essentials 1 (C101) aligned content: basics of C programming

Consider `int i = 0; while (i < 3) { int k = i; i++; }`. Which statement about k is correct?

  1. k has block scope limited to the loop body and is created anew on each iteration Correct answer.
  2. k can be read by other functions through an implicit external linkage
  3. k is visible after the loop because it was declared inside a compound statement
  4. k retains its value from the previous iteration because it is implicitly static

Explanation

A variable declared inside a compound statement has block scope, which ends at the closing brace of that block. With no storage-class specifier it also has automatic storage duration, so each iteration creates a fresh object. Its name is not visible outside the loop body.

Question 50C Essentials 1 (C101) aligned content: basics of C programming

A pointer is declared as `char *p = "abc";` and the program later executes `p[0] = 'A';`. What is the most accurate description of this operation?

  1. It attempts to modify a string literal, which is undefined behavior Correct answer.
  2. It is a compile-time error because p is not declared const
  3. It has no effect because string literals are automatically copied into p
  4. It reliably changes the literal to "Abc" in place

Explanation

A string literal has static storage and its elements are not modifiable; the standard makes modifying them undefined behavior. The declaration char *p does not copy the characters, it points p at the literal's storage. Compilers often place literals in read-only memory, so the attempt may crash.