The Rest Of It
1. Which of the following statements are true.
1. We have to declare a label before using it.
Ans: False. Labels are not declared.
2. Scope of a label is limited to the function in which it is placed.
Ans: True.
3. A label can be placed in the middle of an expression.
Ans: False. Label is placed at the beginning of a statement.
4. As a good programming practice we should avoid using goto
Ans: True.
5. Use of goto is recommended only for cases where we do not know how control will flow.
Ans: False. Use of goto is not recommended.
2. Which of the following statements are true.
1. Inline functions are textually substituted in the program.
Ans: False. Inline functions are merely an optimization hint.
2. inline is a hint to compiler to optimize execution time of a function.
Ans: True.
3. A static variable is initialized before program execution begins.
Ans: True
4. A local static variable can be accessed by any function in the file.
Ans: False. It is visible only inside the function. You can however use a pointer to access it.
5. An extern variable declaration does not allocate any memory for the variable.
Ans: True.
3. Which of the following statements are true.
1. In a recursive function, we should check base condition before recursive call.
Ans: True.
2. An infinite recursion will eventually evaluate to zero.
Ans: False. It will keep running forever.
3. A recursive function cannot be called with pointer arguments.
Ans: False. It can be called with any type of arguments.
4. If a function calls itself, it is called direct recursion.
Ans: True.
4. Implement recursive program for computing n factorial.
#include <stdio.h>
unsigned long int factorial(unsigned long n) {
if(1 == n) {
return 1;
}
return n * factorial(n-1);
}
int main (int argc, char *argv[]) {
unsigned int num = 0;
unsigned long result = 0;
printf("Enter a positive number: ");
scanf("%u", &num);
if(num == 0) {
result = 1;
} else {
result = factorial(num);
}
printf("%u! = %lu\n", num, result);
return 0;
}
5. Implement recursive program for computing nth Fibonacci number.
#include <stdio.h>
unsigned long int fibonacci(unsigned long n) {
if(0 == n) {
return 0;
}
if(1 == n) {
return 1;
}
return (fibonacci(n-1) + fibonacci(n-2));
}
int main (int argc, char *argv[]) {
unsigned int num = 0;
unsigned long result = 0;
printf("Which <u>Fibonacci</u> Number would you like to find: ");
scanf("%u", &num);
result = fibonacci(num);
printf("F(%u) = %lu\n", num, result);
return 0;
}