Programming and data structure miscellaneous
- Consider the following C function :
float f (float x, int y) {
float p, s; int i;
for (s = 1), p = 1, i = 1; i < y; i ++) {
p* = x/i;
s + = p;
}
return s;
}
For large value of y, the return value of the function f best approximates
-
View Hint View Answer Discuss in Forum
The function is rewritten as Here initial value of s increments every time with a factor of (p)x / i
Thus it can be checked for every value. Here the assumption is that the value of y is very large so y approaches infinity So the series 1+ x + x2 / 2! + x3 / 3!...till infinity will have infinite terms & from our previous knowledge we know that this series is expansion of ex (exponential series) so. 1 + x + x2 / 2! + x3 / 3!...........till infinity = ex
Hence (b) is correct option.Correct Option: B
The function is rewritten as Here initial value of s increments every time with a factor of (p)x / i
Thus it can be checked for every value. Here the assumption is that the value of y is very large so y approaches infinity So the series 1+ x + x2 / 2! + x3 / 3!...till infinity will have infinite terms & from our previous knowledge we know that this series is expansion of ex (exponential series) so. 1 + x + x2 / 2! + x3 / 3!...........till infinity = ex
Hence (b) is correct option.
- The best data structure to check whether an arithmetic expression has balanced parenthesis is a
-
View Hint View Answer Discuss in Forum
Balanced parenthesis in an equation are such that the no. of opening and closing parenthesis and in correct order should be there. We can check balancing using stack. When we get any opening parenthesis, then we push that in the stack & if we get a closing one, then we pop the stack. After the complete scanning of input string if stack is found empty then the arithmetic expression is balanced.
Correct Option: B
Balanced parenthesis in an equation are such that the no. of opening and closing parenthesis and in correct order should be there. We can check balancing using stack. When we get any opening parenthesis, then we push that in the stack & if we get a closing one, then we pop the stack. After the complete scanning of input string if stack is found empty then the arithmetic expression is balanced.
- Consider the following C function :
void swap (int a, int b)
{ int temp; temp = a;
a = b;
b = temp;
}
In order to exchange the values of two variables x and y.
-
View Hint View Answer Discuss in Forum
Why a, b and c are incorrect? a) call swap (x, y) will not cause any effect on x and y as parameters are passed by value. b) call swap (&x, &y) will no work as function swap() expects values not addresses (or pointers). c) swap (x, y) cannot be used but reason given is not correct. Here the function takes the arguments by value.
Option (a) sends parameter by value but only the local variable a & b will be exchanged but not the actual variables x & y so incorrect.
Option (b) is incorrect sending address of x & y.
Option (c) swap (x, y) is usable there is no need to return.
Option (d) is the opposite statement of option (a), it says that the values are passed by value so won't swap so the option is correct.Correct Option: D
Why a, b and c are incorrect? a) call swap (x, y) will not cause any effect on x and y as parameters are passed by value. b) call swap (&x, &y) will no work as function swap() expects values not addresses (or pointers). c) swap (x, y) cannot be used but reason given is not correct. Here the function takes the arguments by value.
Option (a) sends parameter by value but only the local variable a & b will be exchanged but not the actual variables x & y so incorrect.
Option (b) is incorrect sending address of x & y.
Option (c) swap (x, y) is usable there is no need to return.
Option (d) is the opposite statement of option (a), it says that the values are passed by value so won't swap so the option is correct.
- The goal of structured programming is to
-
View Hint View Answer Discuss in Forum
The main goal of structured Programming is to get an understanding about the flow of control in the given program text. In structure programming various control structures such as switch case. If then – else, while, etc.
Allows a programmer to decode the flow of the program easily.Correct Option: C
The main goal of structured Programming is to get an understanding about the flow of control in the given program text. In structure programming various control structures such as switch case. If then – else, while, etc.
Allows a programmer to decode the flow of the program easily.
- Consider the following C-program :
void foo (int n, int sum) {
int k = 0, j = 0
if (n = = 0) return;
k = n% 10, j = n/10;
sum = sum + k;
foo (j, sum);
printf (“%d”, k);
}
int main () {
int a = 2048, sum = 0;
foo (a, sum);
printf (“%d\n”, sum);
What does the above program print?
-
View Hint View Answer Discuss in Forum
Sum has no use in foo(), it is there just to confuse. Function foo() just prints all digits of a number. In main, there is one more printf statement after foo(), so one more 0 is printed after all digits of n.
From the given code it is found that foo is a recursive function and when the function foo (a, sum) is called where a = 2048 and sum = 0 as per given conditions.
Then, the execution of the function takes in the following manner.
i) k= n% 10 => modulus operator will return the remainder. for example, if n = 2048, then 2048 %10 will store the value 8 in k.
ii) j is declared as integer datatype in foo function. Therefore after division if the result contains decimal part, it will be truncated and only the integer part will be stored in j. For example if j = 2048 / 10, (actual result = 204.8) then 204 will be stored in j.
iii) The sum variable declared in the main function will not be used by the foo function.Correct Option: D
Sum has no use in foo(), it is there just to confuse. Function foo() just prints all digits of a number. In main, there is one more printf statement after foo(), so one more 0 is printed after all digits of n.
From the given code it is found that foo is a recursive function and when the function foo (a, sum) is called where a = 2048 and sum = 0 as per given conditions.
Then, the execution of the function takes in the following manner.
i) k= n% 10 => modulus operator will return the remainder. for example, if n = 2048, then 2048 %10 will store the value 8 in k.
ii) j is declared as integer datatype in foo function. Therefore after division if the result contains decimal part, it will be truncated and only the integer part will be stored in j. For example if j = 2048 / 10, (actual result = 204.8) then 204 will be stored in j.
iii) The sum variable declared in the main function will not be used by the foo function.