WorksheetsC Programming Challenge for Students
Total questions: 20
Worksheet time: 7mins
What are the basic data types available in C?
int, float, double, char, void
string
array
boolean
How do you declare a variable in C?
declare int x;
int x = 5;
var x int;
To declare a variable in C, use the syntax: type variable_name; e.g., int x;.
What is the purpose of the 'if' statement in C?
To execute code conditionally based on a boolean expression.
To declare variables in C.
To define a function in C.
To create loops in C.
Explain the use of 'switch' statements in C.
The 'switch' statement is used to declare variables in C.
The 'switch' statement is used for looping through arrays in C.
The 'switch' statement is used for selecting one of many code blocks to execute based on the value of an expression.
The 'switch' statement can only handle boolean expressions.
What is a function in C and how is it defined?
A function in C is a block of code that performs a specific task, defined with a return type, name, parameters, and a body.
A function in C is a keyword that defines a data structure.
A function in C is a variable that stores data.
A function in C is a type of loop used for iteration.
What is recursion and how does it work in C?
Recursion in C is a method where a function calls itself to solve smaller instances of a problem, using base cases to terminate the calls.
Recursion in C is a way to optimize memory usage by avoiding function calls.
Recursion in C is a technique that uses loops to repeat function calls.
Recursion in C is a method where a function runs in parallel to improve performance.
What is a pointer in C? How do you declare one?
You declare a pointer using the syntax: pointerName *type;
Pointers can only point to integer variables.
A pointer in C is a variable that holds the address of another variable. It is declared using the syntax: type *pointerName;
A pointer in C is a type of function.
Explain the concept of dynamic memory allocation in C.
Dynamic memory allocation in C is the process of allocating memory using only the stack.
Memory allocation in C is only possible using global variables.
Dynamic memory allocation in C is the process of allocating memory during program execution using functions like malloc and free.
Dynamic memory allocation in C is done at compile time using static arrays.
How do you declare and initialize an array in C?
int arr = {1, 2, 3, 4, 5};
array int arr[5] = new int[5];
int arr[5] = {1, 2, 3, 4, 5};
int arr[5] = (1, 2, 3, 4, 5);
What is the difference between an array and a string in C?
An array is a collection of elements of the same type, while a string is a character array terminated by a null character.
An array can only store integers, while a string can store any data type.
An array is a single character, while a string is a collection of numbers.
A string is a fixed-size data structure, while an array can change size dynamically.
How do you read from a file in C?
Use fopen to open the file, then read with fread, fscanf, or fgets, and close with fclose.
Open the file with open and read using readlines.
Use readfile to open the file and read directly into memory.
Use file_get_contents to read the entire file at once.
What is the purpose of the 'fopen' function in C?
To read data from a file.
To write data to a console.
To delete a file from the system.
To open a file and associate it with a stream.
How do you write data to a file in C?
Use fopen() to open a file, fprintf() or fwrite() to write data, and fclose() to close the file.
Use open() to create a file and print() to write data.
Use fwrite() to read data from a file and fclose() to open it.
Use read() to open a file and write data directly to memory.
What is the significance of the 'return' statement in a function?
The 'return' statement is used to define a function's name.
The 'return' statement is optional and has no effect on function execution.
The 'return' statement can only be used in global scope.
The 'return' statement is significant because it allows a function to output a value and terminate its execution.
How do you pass an array to a function in C?
You pass an array to a function in C by using a pointer to the first element of the array.
You pass an array by copying it directly into the function.
You can only pass arrays by value in C.
You must declare the array size in the function signature.
What is the difference between call by value and call by reference?
Call by value modifies the original variable, while call by reference does not.
Call by value and call by reference are the same and have no differences.
Call by value passes a copy of the variable, while call by reference passes a reference to the variable.
Call by value passes a reference to the variable, while call by reference passes a copy of the variable.
How do you use the 'malloc' function for memory allocation?
Assign 'ptr = new(size);' to allocate memory.
Call 'malloc(size, ptr);' for memory allocation.
Use 'ptr = allocate(size);' to allocate memory.
Use 'ptr = malloc(size);' where 'ptr' is a pointer and 'size' is the number of bytes to allocate.
What are the common string manipulation functions in C?
strcatn
strdup
strrev
strlen, strcpy, strcat, strcmp, strchr
How do you close a file in C?
Use fclose(file_pointer);
end(file_pointer);
Terminate(file_pointer);
Use close(file_pointer);
What is the role of the 'sizeof' operator in C?
The 'sizeof' operator converts data types to their string representations.
The 'sizeof' operator returns the size in bytes of a data type or variable.
The 'sizeof' operator calculates the total memory usage of a program.
The 'sizeof' operator is used to declare variables in C.
