Unlock hundreds more features
Save your Quiz to the Dashboard
View and Export Results
Use AI to Create Quizzes and Analyse Results

Sign inSign in with Facebook
Sign inSign in with Google

Advanced C Programming Quiz - Test Your Skills Now!

Think you can ace this computer programmer test? Dive into our C programming quiz!

Difficulty: Moderate
2-5mins
Learning OutcomesCheat Sheet
paper art illustration programming quiz on sky blue background C coding arrays pointers and error handling challenge

This Advanced C quiz helps you practice arrays, pointers, dynamic memory, and error handling so you can write safer, faster code. Use this free practice C quiz to find gaps before an exam or interview and sharpen your logic with short, focused questions.

What is the value returned by sizeof(char) in C?
2
1
It is implementation-defined
4
The C standard mandates that sizeof(char) is always 1, which becomes the unit of size for all types. This does not reflect bytes in memory but a char's storage size. Even on systems where a byte differs in bits, sizeof(char) remains 1.
Which operator is used to obtain the address of a variable?
%
&
*
#
The ampersand (&) operator returns the memory address of its operand in C. It is the counterpart to the dereference operator (*). This distinction is fundamental for pointer usage.
What is the value of x after executing: int x = 5; x++; ?
6
Undefined
5
7
The post-increment operator (x++) increases x by one after evaluating to its original value. After the statement, x becomes 6. This behavior is defined in the C standard.
Which header file must be included to use malloc()?
stdlib.h
stdio.h
string.h
malloc.h
The malloc() function is declared in stdlib.h according to the C standard. Including malloc.h is outdated and non-standard. stdio.h covers input/output functions, while string.h covers string operations.
How do you declare a pointer to a function returning an int with no parameters?
int fp*();
int (*fp)();
int *fp();
int fp();
Function pointers require parentheses around the pointer name. int (*fp)(); declares fp as a pointer to a function returning int and taking unspecified parameters (old-style).
Which syntax denotes a single-line comment in C99 and later?
# comment
// comment
/* comment */
C99 introduced the // syntax for single-line comments, borrowing from C++. The /* ... */ style denotes block comments and works in earlier standards. # is for preprocessor directives.
In C, array indexing starts at what value?
Depends on implementation
1
0
-1
C arrays are zero-based, meaning the first element is arr[0]. This is specified by the standard and is consistent across implementations.
What does free(ptr) do in C?
Deallocates memory previously allocated by malloc/calloc/realloc
Causes undefined behavior always
Sets ptr to NULL
Allocates memory
The free() function releases the dynamic memory pointed to by ptr back to the system. It does not set the pointer to NULL, so using ptr after free is undefined.
Which keyword is used to exit a loop immediately?
stop
break
exit
continue
The break statement terminates the nearest enclosing loop or switch and transfers control out of it. continue skips to the next iteration without exiting the loop.
Is modifying a string literal like char *s = "Hello"; s[1] = 'a'; defined behavior?
No, it is undefined behavior
Yes, it changes the literal
Depends on compiler
It is implementation-defined
String literals reside in read-only memory; attempting to modify them leads to undefined behavior. Always use an array if you need a mutable string.
What is the result of the bitwise AND operation between 3 and 5 (3 & 5)?
7
0
1
15
3 in binary is 011, 5 is 101; ANDing bit by bit yields 001, which is 1. Bitwise operations act on individual bits of integers.
Which function prototype correctly opens a file for reading in C?
fread("filename.txt", "r");
fileopen("filename.txt", "r");
open("filename.txt", "r");
fopen("filename.txt", "r");
The fopen function from stdio.h takes the filename and mode (e.g., "r" for reading). open is a POSIX system call with different semantics.
What does malloc(0) return according to the C standard?
Either NULL or a unique pointer; it is implementation-defined
Causes a compile-time error
Always a valid non-null pointer
Always NULL
The C standard specifies that malloc(0) may return NULL or a non-null pointer that can't be dereferenced. Behavior is implementation-defined.
Which code correctly allocates a 2D array of size m x n dynamically?
int a[m][n];
int a = malloc(m,n);
int *a = malloc(m*n*sizeof(int));
int **a = malloc(m * sizeof(int*)); for(int i=0;i
Allocating an array of pointers and then allocating each row handles dynamic 2D arrays in C. The single malloc of m*n only gives a flat block, not a true double-pointer.
What happens when you initialize char arr[5] = "Hello";
arr contains "Hell"
arr contains "Hello" without null terminator
Compile-time error due to insufficient size
Undefined behavior at runtime
The string "Hello" requires 6 bytes including the null terminator; arr[5] is too small. The compiler must error on this initializer.
In pointer arithmetic, if double *p points to arr[0], what does p+5 represent?
Next byte after 5 doubles
Address of arr[5]
Address of arr[0] plus 5 bytes
Undefined behavior
Pointer arithmetic advances by the size of the pointed type. p+5 moves the pointer forward by 5 * sizeof(double), landing at &arr[5].
What is a dangling pointer in C?
A pointer to a global variable
A null pointer
A pointer not initialized at declaration
A pointer referencing memory that has been freed
After free(), a pointer may still hold the address of deallocated memory; using it causes undefined behavior. Such a pointer is called dangling.
Which function compares two null-terminated strings in C?
strcmp
strncmp
strcpy
strcat
strcmp compares full strings lexicographically. strncmp also compares but with length limit. strcpy copies, strcat concatenates.
What is the output of: int x=1; if(x==1) printf("One"); else if(x=2) printf("Two"); ?
Two
One
Compile error
Undefined behavior
The first condition (x==1) is true, so "One" is printed. The assignment in the second if never executes. This illustrates difference between == and =.
Which tool can detect memory leaks at runtime in C programs?
assert.h
gdb
malloc.h
Valgrind
Valgrind is a dynamic analysis tool that instruments memory allocations and reports leaks. gdb is a debugger but does not directly detect leaks.
How does realloc(ptr, 0) behave according to the C standard?
Always frees and returns NULL
Always returns a valid pointer
Implementation-defined: may free and return NULL or a unique pointer
Undefined behavior
The standard states realloc(ptr, 0) behavior is implementation-defined. Some libraries behave like free(ptr), others return a distinct pointer.
What does the global variable errno represent in C?
Boolean flag indicating an error
Number of errors encountered
Error code set by library calls on failure
Macro for standard error stream
errno is defined in errno.h and is set by system and library calls when they fail. You should check it only when a function indicates an error.
Which syntax aligns a struct to an 8-byte boundary in GCC?
struct __attribute__((aligned(8))) S { ... };
alignas(8) struct S { ... };
#pragma align(8) struct S { ... };
struct S { ... } align4;
GCC supports __attribute__((aligned(n))) to specify alignment. alignas is C11, #pragma align is non-standard.
Accessing the inactive member of a union yields what behavior in C?
Always zero
Implementation-defined bit reinterpretation
Compile-time error
Undefined behavior
Reading from a union member other than the one most recently written is undefined by the C standard, even though many compilers allow type punning.
What is the purpose of the volatile keyword in C?
Marks a variable as thread-safe
Tells the compiler the variable may change externally and prevents certain optimizations
Declares a constant variable
Allocates variable in volatile memory
volatile indicates that a variable's value may change outside the program flow, so the compiler must reload it each time from memory. This prevents aggressive optimizations.
Which action is most likely to cause a segmentation fault?
Dereferencing a NULL pointer
Calling printf with correct format
Declaring an array
Opening a file successfully
Dereferencing a NULL pointer attempts to access memory at address zero, which is invalid and typically causes a segmentation fault.
What does the restrict qualifier indicate for a pointer in C99?
That the pointer is read-only
That the pointer refers to volatile memory
That the pointer is the only way to access the object it points to during its lifetime
That the pointer cannot be NULL
restrict promises the compiler that for the lifetime of the pointer, no other pointer will access the same object, enabling more aggressive optimizations.
When using setjmp() and longjmp(), which of the following is NOT automatically restored?
The program's call stack
Automatic (non-volatile) local variables
Saved signal mask (with sigsetjmp)
The jmp buffer environment
longjmp restores the stack, program counter, and saved environment, but non-volatile local variables may have indeterminate values unless declared volatile.
0
{"name":"What is the value returned by sizeof(char) in C?", "url":"https://www.quiz-maker.com/QPREVIEW","txt":"What is the value returned by sizeof(char) in C?, Which operator is used to obtain the address of a variable?, What is the value of x after executing: int x = 5; x++; ?","img":"https://www.quiz-maker.com/3012/images/ogquiz.png"}

Study Outcomes

  1. Understand Array Operations -

    Explain how to create, traverse, and modify one- and multi-dimensional arrays in C, leveraging pointer arithmetic for efficient data handling.

  2. Analyze Pointer Relationships -

    Distinguish between different pointer types, interpret memory addresses, and trace pointer-to-pointer scenarios in the advanced programming quiz.

  3. Apply Dynamic Memory Management -

    Use malloc, calloc, realloc, and free to manage heap memory safely and prevent leaks during complex C programming tasks.

  4. Evaluate Error Handling Techniques -

    Implement robust error detection and reporting using return codes, errno, and custom checks in this computer programming test context.

  5. Solve Debugging Challenges -

    Identify and fix common runtime errors such as null dereferences and buffer overruns in the C programming quiz environment.

  6. Demonstrate Advanced C Concepts -

    Integrate array manipulation, pointer basics test scenarios, and error handling into cohesive programs that pass rigorous computer programmer test conditions.

Cheat Sheet

  1. Understanding Array Decay and Indexing -

    On computer programming tests, remember arrays decay to pointers so arr[i] and *(arr+i) both access the same element; for int a[5]={1,2,3,4,5}, a[2] and *(a+2) return 3. The mnemonic "array name points to element zero" helps avoid off-by-one indexing errors.

  2. Pointer Arithmetic and Type Safety -

    Pointer arithmetic scales by the size of the pointed type, so a float* p; p+1 jumps sizeof(float) bytes - memorizing "one step equals one element" nails this concept in any pointer basics test. Type safety prevents adding mismatched pointers without casting, a common check in C programming quiz environments.

  3. Dynamic Memory Allocation Best Practices -

    Always check malloc/calloc returns to avoid crashes: if ((p = malloc(n*sizeof *p)) == NULL) { perror("malloc"); exit(EXIT_FAILURE);} This pattern, taught in MIT OpenCourseWare, ensures safe memory handling and mandates free(p) to prevent leaks.

  4. System Error Handling with errno -

    When a system call fails, errno holds the error code and perror() or strerror(errno) prints a clear message; e.g., fopen on a missing file sets errno to ENOENT and perror("fopen") yields "No such file or directory." Resetting errno = 0 before calls, as per POSIX, guarantees accurate reporting.

  5. Passing Multidimensional Arrays Effectively -

    To pass 2D arrays safely, define functions as void f(int matrix[][COLS], int rows), which ensures contiguous memory per ISO C, unlike int** that can fragment rows. For dynamic arrays, malloc(rows*cols*sizeof *matrix) in one block simplifies layout and is a handy tip for any computer programmer test dealing with matrices.

Powered by: Quiz Maker