WorksheetsMastering C Programming Concepts
Total questions: 12
Worksheet time: 6mins
What are the basic data types available in C?
string
int, float, double, char, void
boolean
array
Explain the difference between 'if' and 'switch' control structures.
The 'if' structure is for conditional checks, while 'switch' is for evaluating multiple cases based on a single expression.
'if' is used for loops, while 'switch' is for single conditions.
'if' is a function, while 'switch' is a variable.
'if' can only evaluate two cases, whereas 'switch' can evaluate one case.
What is a function in C and how is it defined?
A function in C is defined using the syntax: return_type function_name(parameter_list) { // function body }
A function in C is created with the syntax: function_name { return_type(parameter_list) }
A function in C is declared using the syntax: function_name return_type(parameter_list);
A function in C is defined as function_name(parameter_list) { // function body }
Describe recursion and provide an example of a recursive function.
An example of recursion is a function that calculates the sum of an array without calling itself.
A recursive function is one that uses loops to repeat actions.
Recursion is a method of sorting data in a list without any function calls.
An example of a recursive function is the calculation of the factorial of a number: function factorial(n) { return n <= 1 ? 1 : n * factorial(n - 1); }
What is a pointer in C and how is it used?
A pointer in C is a variable that holds the address of another variable, used for direct memory access and manipulation.
A pointer in C is a variable that stores a string value.
A pointer in C is a special data type that can only hold integer values.
A pointer in C is a type of function that executes code.
How do you dynamically allocate memory in C?
Use 'print(size)' to allocate memory
Use 'new(size)' to allocate memory in C
Use 'malloc(size)', 'calloc(num, size)', or 'realloc(ptr, new_size)' to dynamically allocate memory in C.
Use 'allocate(size)' for dynamic memory
What are the standard file I/O functions in C?
read, write, open, close
input, output, save, load
fopen, fclose, fread, fwrite, fprintf, fscanf
create, delete, append, modify
How do you open and close a file in C?
Use open() to create a file and close() to delete it.
Use create() to open a file and delete() to close it.
Use read() to open a file and write() to close it.
Use fopen() to open a file and fclose() to close it.
What is the purpose of the 'return' statement in a function?
To create a loop within the function.
To end the function execution immediately.
The purpose of the 'return' statement is to provide a value from a function to its caller.
To define the function's parameters.
Explain the concept of scope in relation to variables in C.
Scope in C is determined by the data type of the variable.
Scope in C only applies to global variables.
Scope in C defines where a variable can be accessed: local (within functions), global (throughout the program), and file scope (within a file).
Variables in C have no scope restrictions.
What is the purpose of the 'malloc' function in C?
To allocate memory dynamically during program execution.
To free up memory that is no longer needed.
To create a new variable in the stack memory.
To initialize a variable with a default value.
How can you pass an array to a function in C?
By passing the array name as an argument, which decays to a pointer to the first element.
By passing the entire array as a single variable.
By using the 'array' keyword in the function parameters.
By passing the size of the array along with the array itself.
