Intermediate C test (Intermediate)
30 questions · 35 minutes · Free
Memory management, structs, files and the preprocessor.
What it covers
- malloc, free and memory bugs
- Structs and unions
- Pointer arithmetic
- File I/O
- Preprocessor and macros
Interview format
- Questions: 30 questions — 18 multiple choice, 9 multi-answer, 3 predict-the-output
- Time: 35 minutes, timed from the first question
- Difficulty: Intermediate
- Skill area: Backend
- Price: Free
Earn a certificate
The Intermediate C test is one of the 6 tests of the Automotive Embedded Engineer learning path. Pass it and the other 5 tests at 70% or more and readyforhires issues you a verifiable certificate in your name, with its own code and verify link to share or add to LinkedIn.
Sample questions
5 sample questions from the Intermediate C test, with the answers. Samples are taken out of the live test, so you will not meet these when you sit it.
Consider the following C code snippet: 'FILE *file = fopen("output.txt", "w"); fprintf(file, "Hello, World!\n"); fclose(file);'. What will be the output of this code?
- The program will output 'Hello, World!' followed by a newline.
- The program will output 'Hello, World!' without a newline.
- The program will not write anything to the file.
- The program will output 'Hello, World!' and then crash.
Answer: A. The program will output 'Hello, World!' followed by a newline.
The provided code opens a file named 'output.txt' in write mode using 'fprintf' to write 'Hello, World!' followed by a newline. Since 'fprintf' automatically appends a newline character when the format string includes '\n', the output will include a newline. The file is then closed properly, ensuring that the data is written to the file.
Consider a C program that uses malloc to allocate memory for a struct containing a double and an int. What is the likely outcome if the memory is not properly aligned for the struct's members?
- The program will produce a warning but not a crash.
- The program will crash due to incorrect memory alignment when accessing the struct members.
- The program will allocate memory correctly, but the struct members will be initialized to zero.
- The program will compile and run without issues.
Answer: B. The program will crash due to incorrect memory alignment when accessing the struct members.
When using malloc to allocate memory for a struct, it is essential to ensure that the memory is properly aligned for the struct's members. If the struct contains members that require specific alignment (like certain data types), and the allocated memory is not aligned correctly, accessing these members can cause a crash. This is because the CPU may not be able to read or write to misaligned memory addresses, leading to undefined behavior or a segmentation fault.
Consider a struct in C with two members: an integer (4 bytes) and a char (1 byte). What is the correct statement about the size of this struct?
- The struct size depends on the compiler and is not predictable.
- The struct will have a size of 12 bytes as no padding is added in C.
- The struct will have a size of 16 bytes due to padding added to align the members properly.
- The struct will have a size of 8 bytes as padding is not required for performance.
Answer: C. The struct will have a size of 16 bytes due to padding added to align the members properly.
In C, the compiler may add padding between struct members to ensure proper alignment for performance reasons. For example, if a struct contains a 4-byte integer followed by a 1-byte char, padding may be added after the char to align the next member. This results in the struct having a larger size than the sum of the sizes of its members.
What is the likely outcome of dereferencing a null pointer in C?
- The program will always produce a segmentation fault.
- The program will print an error message and terminate gracefully.
- The program will silently ignore the null pointer and continue execution.
- The program may crash or behave unpredictably due to undefined behavior.
Answer: D. The program may crash or behave unpredictably due to undefined behavior.
Dereferencing a null pointer in C leads to undefined behavior. This means the program may crash, produce incorrect results, or behave unpredictably. It is not guaranteed to cause a segmentation fault in all cases, but it is generally considered a critical bug.
Which of the following statements accurately compares fscanf and fgets when reading from files?
- fscanf is suitable for reading from files when the data format is known and predictable, while fgets is preferred for reading lines of text or when the format is not fixed.
- fscanf is better for reading binary data, while fgets is better for reading text data.
- fgets is suitable for reading from files when the data format is known and predictable, while fscanf is preferred for reading lines of text or when the format is not fixed.
- fscanf is more efficient than fgets for reading large files.
Answer: A. fscanf is suitable for reading from files when the data format is known and predictable, while fgets is preferred for reading lines of text or when the format is not fixed.
fscanf is useful for reading data in a specific format (like integers, floats, or strings) from a file, but it can be error-prone if the data does not match the expected format. On the other hand, fgets reads a line of text from a file, making it more reliable for unstructured or variable-length data. Therefore, fscanf is better suited for structured data, while fgets is better for unstructured or line-based data.
Frequently asked questions
- How many questions are in the Intermediate C test?
- 30 questions (18 multiple choice, 9 multi-answer, 3 predict-the-output), with 35 minutes to answer them.
- What does the Intermediate C test cover?
- malloc, free and memory bugs, Structs and unions, Pointer arithmetic, File I/O, Preprocessor and macros.
- Is the Intermediate C test free?
- Yes. It is free to take with a readyforhires account.
- What level is the Intermediate C test?
- Intermediate, in Backend.