Programming and data structure miscellaneous
- Consider the program given below, in a block-structured pseudo-language with lexical scoping an nesting of procedures permitted.
Program main;
Var ......
Procedure A1;
Var .....
Call A2;
End A1
Procedure A2;
Var...
Procedure A21;
Var....
Call A1;
End A21
Call A21;
End A2;
Call A1;
End main.
Consider the calling chain :
Main → A1 → A2 → A21 → A1
The correct set of activation records along with their access links is given by
-
View Hint View Answer Discuss in Forum
Link to activation record of closest lexically enclosing block in program text. It depends on the static program text
Correct Option: D
Link to activation record of closest lexically enclosing block in program text. It depends on the static program text
- What is the return value of f(p,p), if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value.
int f (int &x, int c) {
c = c – 1;
if (c==0) return 1;
x = x + 1;
return f(x,c) * x;
}
-
View Hint View Answer Discuss in Forum
Return value f (p, p) if the value of p is intialized to 5 before the call.
Since, reference of p is passed as 'x'
Thus, any change in value of x in f would be reflected globally. The recursion can be broken down asCorrect Option: B
Return value f (p, p) if the value of p is intialized to 5 before the call.
Since, reference of p is passed as 'x'
Thus, any change in value of x in f would be reflected globally. The recursion can be broken down as
- Choose the correct option to fill? 1 and ?2 so that the program below prints an input string in reverse order. Assume that the input string is terminated by a newline character.
void recerse (void){
int c;
if (?1) reverse ();
?2
}
main () {
printf(“Enter Text”); printf(“/n”);
reverse (); printf(“/n”)
-
View Hint View Answer Discuss in Forum
The option chosen is
?1 is ((c = getchar ())! = ‘\n’)
?2 is putchar (c);
Because the operator ‘1=’ has higher priority than ‘=’ operator so according to this C = getchar () should be contained in brackets and when the string is reversed then the function putchar (c) is used so that the characters can be printed.Correct Option: D
The option chosen is
?1 is ((c = getchar ())! = ‘\n’)
?2 is putchar (c);
Because the operator ‘1=’ has higher priority than ‘=’ operator so according to this C = getchar () should be contained in brackets and when the string is reversed then the function putchar (c) is used so that the characters can be printed.
- The minimum number of arithmetic operations required to evaluate the polynomial P(X) = X5 + 4X3 + 6X + 5 for a given value of X, using only one temporary variable is _____.
-
View Hint View Answer Discuss in Forum
P (x) = x5 + 4x3 + 6x + 5
= x3 (x2 + 4) + 6x + 5
Now using only one temporary variable ‘t’
(i) t = x * x (Evaluate x2 and store in memory)
(ii) t = t + 4 (Evaluate (x2 + 4) and store in memory)
(iii) t = x2 (Retrieve x2 from memory)
(iv) t = t * x (Evaluate x3 and store in memory)
(v) t = t * (x2 + 4) (Evaluate x3 (x2 + 4) and store in memory)
(vi) t = 6 * x (Evaluate 6x and store in memory)
(vii) t = t + 5 (Evaluate (6x + 5) and store in memory)
(viii) t = t + x3 (x2 + 4) (Retrieve x2 (x2 + 4) from memory and evaluate {x3 (x2 + 4) + 6x + 5}
For the above steps, total number of arithmetic operation is 7
i.e., 4 multiplications and 3 additions.Correct Option: D
P (x) = x5 + 4x3 + 6x + 5
= x3 (x2 + 4) + 6x + 5
Now using only one temporary variable ‘t’
(i) t = x * x (Evaluate x2 and store in memory)
(ii) t = t + 4 (Evaluate (x2 + 4) and store in memory)
(iii) t = x2 (Retrieve x2 from memory)
(iv) t = t * x (Evaluate x3 and store in memory)
(v) t = t * (x2 + 4) (Evaluate x3 (x2 + 4) and store in memory)
(vi) t = 6 * x (Evaluate 6x and store in memory)
(vii) t = t + 5 (Evaluate (6x + 5) and store in memory)
(viii) t = t + x3 (x2 + 4) (Retrieve x2 (x2 + 4) from memory and evaluate {x3 (x2 + 4) + 6x + 5}
For the above steps, total number of arithmetic operation is 7
i.e., 4 multiplications and 3 additions.
- Consider the following function
double f(double x){
if( abs(x*x – 3) < 0.01) return x;
else return f(x/2 + 1.5/x);
}
Give a value q (to 2 decimals) such that f(q) will return q:_____.
-
View Hint View Answer Discuss in Forum
f(g) + q ⇒ x + 1.5 = x 2 x ⇒ x2 + 3 = x ⇒ x2 + 3 = 2x2 2x
⇒ x2 = 3 ⇒ x = 1.73Correct Option: B
f(g) + q ⇒ x + 1.5 = x 2 x ⇒ x2 + 3 = x ⇒ x2 + 3 = 2x2 2x
⇒ x2 = 3 ⇒ x = 1.73