NEW
Font size
WorksheetsMastering C Programming Concepts
Total questions: 13
Worksheet time: 7mins
Explain the difference between 'while' and 'do-while' loops.
'While' loops execute at least once, while 'do-while' loops may not execute at all.
Both 'while' and 'do-while' loops check conditions before execution.
The main difference is that 'while' checks the condition before execution, while 'do-while' checks after execution.
'While' loops can only run a fixed number of times, while 'do-while' loops can run indefinitely.
What is a function in C and how is it defined?
A function in C is declared with: function_name(parameter_type parameter_name) { // body }
In C, a function is defined as: function_name(parameter_name) { // return_type }
A C function is structured as: return_type { function_name(parameter_type) } // body goes here
A function in C is defined using the syntax: return_type function_name(parameter_type parameter_name) { // function body }
What is the scope of a variable declared inside a function?
Block scope
Local scope
Static scope
Global scope
How do you read from a file in C?
Use fopen to open the file, then read with fread/fscanf/fgets, and close with fclose.
Utilize fopen to create the file, then access with getc/gets, and terminate with close.
Use read to open the file, then write with fwrite/fprintf/fputs, and close with fclose.
Open the file with open, then read using readfile/readline, and finish with closefile.
How do you create a structure in C?
struct Animal { char type[30]; float weight; };
struct Vehicle { int wheels; char model[20]; };
struct Book { char title[100]; float price; };
struct Person { char name[50]; int age; };
What is the difference between 'struct' and 'union' in C?
A 'struct' shares memory among its members, while a 'union' allocates separate memory for each member.
A 'struct' allocates separate memory for each member, while a 'union' shares memory among its members.
A 'struct' can only hold one data type, while a 'union' can hold multiple types simultaneously.
A 'struct' is used for functions, while a 'union' is used for arrays in C.
How can you pass an array to a function in C?
Arrays can be passed using 'functionName(int size, dataType arrayName)' syntax.
You must use pointers to pass an array to a function in C.
You can pass an array to a function by using the syntax 'functionName(dataType arrayName[], int size)'.
You can pass an array by declaring it as 'arrayName[]' in the function.
How do you open a file for writing in C?
fopen('file.txt', 'rb');
fopen('file.txt', 'a');
fopen("filename.txt", "w");
fopen('file.txt', 'r');
How do you access members of a structure in C?
Utilize the hash symbol (#) for structure variables and the percent sign (%) for pointers.
Access members using the asterisk (*) for structure variables and the ampersand (&) for pointers.
Use the dot operator (.) for structure variables and the arrow operator (->) for pointers.
Use the semicolon (;) for structure variables and the colon (:) for pointers.
What is the purpose of the 'break' statement in loops?
To exit a loop prematurely.
To skip the current iteration of a loop.
To pause the execution of a loop.
To continue a loop indefinitely.
How do you handle errors while opening a file in C?
Check if the file pointer is NULL after calling fopen, and handle the error if it is.
Close the file without checking if it was opened.
Use printf to display the file contents immediately.
Always assume the file opened successfully without checking.
What is a pointer and how is it used in C?
A pointer is a special data structure that stores multiple variables in C.
A pointer is a constant that defines a variable's type in C.
A pointer is a type of function that returns a variable's value in C.
A pointer is a variable that holds the memory address of another variable, used for direct memory access and manipulation in C.
Explain the concept of recursion in functions.
Recursion is a technique where a function runs in parallel to increase efficiency.
Recursion involves using loops to repeat a function until a condition is met.
Recursion in functions is a method where a function calls itself to solve smaller instances of a problem, using a base case to terminate the calls.
Recursion is a method of defining a function without any termination condition.
