Unit 3 QB
Unit 3 QB
Question bank
1. List out the details you could infer from the following statement int sum(int, int);
The statement is function declaration statement. This declaration ensures that the compiler knows
the function's name, return type, and parameter list before its actual usage in the code.
The function returns a value of type int (integer). The function is named sum. The function takes
two parameters, both of which are of type int.
Incorrect Header File Syntax:The header file < stdio.h> has a space after <
Incorrect Function Name in Declaration and Definition: The function sum two is invalid, because
C does not allow spaces in function names
Mismatch in Function Name During Call: In the printf statement, the function is called as sum
tow instead of sum two
3. “Could we able to create two different functions with same name in c” Justify
In C, it is not possible to create two different functions with the same name. This is because C does
not support function overloading, which allows multiple functions with the same name but different
parameters.
Function Definition: Provides the actual implementation of the function. This is where the logic is
written.
return_type function_name(parameter_list) { // Function body (implementation) }
Function Call: Executes the function by passing the required arguments and using the returned value (if
any).
variable = function_name(arguments);
1. return_type:
o Specifies the type of value the function returns.
o Use void if the function does not return a value.
2. function_name:
o The name of the function, which is used to call it.
3. parameter_list:
o A list of input parameters the function accepts, including their data types.
o If the function takes no parameters, use void.
4. Function Body:
o Enclosed in { }, contains the code to execute the function's task.
o Use a return statement to return a value if the return type is not void.
7. Write a function to add one integer and one float number and returns the floor value in float.
#include <stdio.h>
#include <math.h>
float addAndFloor(int x, float y) {
float result = x + y;
return floor(result);
}
int main() {
int a = 5;
float b = 3.7;
printf("The floor of the sum is: %.2f\n", addAndFloor(a, b));
return 0;
}
8. List out different types of function calling parameters
Call by Value Function receives a copy of the argument's value. Used when don't need
Modifications do not affect the original value. to modify the
argument.
Call by Function receives the address of the argument, allowing it to Used When need to
Reference modify the original value. modify the argument.
10. Differentiate pass by value and pass by reference with respect to function calling
Feature Pass by Value Pass by Reference
Argument Passed A copy of the argument’s value. The address (reference) of the
argument.
Effect on Original No change to the original Modifies the original argument.
argument.
More memory usage due to Less memory usage, as only the
Memory Usage
copying the value. address is passed.
Can be slower with large data Faster for large data types (only
Performance
types (due to copying). address is passed).
When the function should not When the function should
Typical Use
modify the argument. modify the argument.
11. Compare and contrast looping and recursion function? Justify why recursion function required a base
condition to return.
Feature Looping Recursion
Control Mechanism Uses iterative constructs (e.g., Uses function calls and the
increment/decrement counters). program stack.
Ease of Debugging Easier to debug as the flow is Harder to debug due to nested
linear and iterative. function calls.
Use Cases Suitable for problems with a Suitable for problems naturally
fixed or easily predictable defined in terms of smaller sub-
number of iterations. problems (e.g., factorial,
Fibonacci, tree traversals).
Stack Usage Does not use the program stack Explicitly uses the program
explicitly; uses looping stack for each function call.
variables.
12. What is the use of return statement explain with a small example of function definition.
13. List out what you could infer from the following provided the address of x is say 1000
int x = 5;
int* p = &x;
Note: this is on 32 bit compiler
Dereferencing
Dereferencing is the process of accessing the value stored at the memory address that a pointer holds,
using the * (dereference) operator.
int x = 10;
int* p = &x; // Referencing
printf("Value of x: %d\n", *p); // Dereferencing: accessing the value at the address stored in 'p'
The *p operator accesses the value stored at that address, which is 10.
16. Write a function definition to swap two variables with pass by reference method.
void swap(int* a, int* b) {
int temp = *a; // Store the value pointed to by 'a' in a temporary variable
*a = *b; // Assign the value pointed to by 'b' to the location pointed to by 'a'
*b = temp; // Assign the temporary value to the location pointed to by 'b'
}
17. Justify why and how wild pointer is not recommended and dangerous?
A wild pointer is a pointer in C that has not been initialized to a valid memory address. It contains a
garbage value (an arbitrary memory address) and does not point to any specific object or valid memory.
Security Risks:
Accessing random memory can lead to leakage of sensitive data or compromise program
integrity.
Undefined Behavior:
Dereferencing a wild pointer may result in undefined behavior, such as segmentation faults or
corruption of data in unintended memory regions.
18. If ptr is an integer pointer that points the address, say 1000, then find the following
i) ptr++
ii) ptr + 5
iii) ptr - 3
iv) if p is another pointer, points say 2000. Then p – ptr
ii) ptr + 5 : ptr + 5 moves the pointer forward by 5 integer addresses 1000+5*4 = 1020
iii) ptr - 3 ptr - 3 moves the pointer backward by 3 integer addresses. 1000−(3×4)=1000−12=988
iv) if p is another pointer, points say 2000. Then p – ptr , (2000-1000)/4 = 250 elements
Definition A pointer whose address cannot A pointer that cannot modify the
change after initialization. value it points to.
Address Cannot change the pointer to point to Can change the pointer to point to
Modifiability another address. another address.
Value Modifiability The value at the pointed address can The value at the pointed address
be modified. cannot be modified.
Declaration Syntax int* const p = &x; const int* p = &x; or int const* p =
&x;
#include<stdio.h>
void main()
{
int arr[] = {11,21,13,41,15,61,17,81,19,20};
int* p;
p = arr;
printf(“%d\n%d\n%d\n%d\n”, *p, *p+3, *p+5, *p+8);
}
Output:
11
14
16
19
24. Justify how and why? “if pointer p points the base address of array arr, then arr[index] and *(p +
index) are same”*
p is a pointer to the base of the array, and p + index computes the address of the index-th element, which
is then dereferenced to retrieve its value.
Big questions
3. Discuss how pointer used in accessing array elements and array of pointers
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *p = arr; // Pointer to the first element of the array
// Access elements using pointer arithmetic
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, *(p + i)); // Equivalent to arr[i]
}
return 0;
}
Output:
arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50
*(p + i) accesses the i-th element of the array, as p points to the base address of the array.
Array of Pointers
An array of pointers is a collection of pointers, where each pointer can point to a different memory
location. This is useful in scenarios like handling strings or dynamic memory allocation.
Example:
#include <stdio.h>
int main() {
const char *words[] = {"Hello", "World", "Pointers"}; // Array of string pointers
Return Value Usage The return value of the recursive The return value of the recursive
call is directly returned. call is used in additional
computations before returning.
Memory Usage Requires less memory because Requires more memory because
only one stack frame is used at a each recursive call adds a new
time. stack frame until the base case is
reached.
Example Use Cases Suitable for problems like Suitable for problems like tree
factorial, Fibonacci sequence, traversal and divide-and-
and simple traversal. conquer algorithms where
intermediate results need to be
combined.
10. Justify the following statement with an example. The statement is “Sometimes Recursion takes
more time and wastes recursive call for repeating the same recursive call ”
Consider the Fibonacci sequence, where each term is the sum of the two preceding terms:
F(0) = 0,
F(1) = 1
F(n)=F(n−1)+F(n−2)
base cases are F(0)=0 and F(1)=1
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) // Base cases
return n;
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive calls
}
int main() {
int n = 5; // Find the 5th Fibonacci number
printf("Fibonacci(%d) = %d\n", n, fibonacci(n));
return 0;
}
Trace of Recursive Calls for fibonacci(5)
Redundant Calls:
12. Discuss how to return an array and how to pass array as argument in a function with example.
write a program to that accepts 2 matrixes and checks whether 2 matrix can be multiplied or not,
if yes multiply them using functions.
When passing an array as an argument to a function, the base address of the array is passed, allowing the
function to operate on the original array.
#include<stdio.h>
for(int i=0;i<s;i++)
int main()
int a[]={2,3,4,5,6};
int s=(sizeof(a)/sizeof(a[0]));
ArrayPrint(a,s);
In ArrayPrint(a,s), whole array is not passed. Only base address of the array is passed. So array name
which contains base address is always treated as pointer
#include <stdio.h>
int main() {
int rowsA, colsA, rowsB, colsB;
int matA[10][10], matB[10][10], result[10][10];
return 0;
}