Programming and data structure miscellaneous
- Consider the following three C functions
[P1] int*g(void)
{
intx = 10;
return (&x);
}
[P2] int*g(void)
{
int* px;
*px = 10;
return px;
}
[P3] int*g(void)
{
int*px
px = (int*) malloc (size of (int));
*px = 10;
return px;
}
Which of the above 3 functions are likely to cause problems with pointers ?
-
View Hint View Answer Discuss in Forum
P1 : Here the function is returning address of the variable x (& x) but the return type is pointer to integer not address. So incorrect.
P2 : *px = 0 directly assigned a value but still px doesn't point to any memory location, so memory initialization or allocation should be done before. So incorrect.
P3 : Correction made in P2, memory pre allocated, So correct.
Hence (c) is correct option.Correct Option: C
P1 : Here the function is returning address of the variable x (& x) but the return type is pointer to integer not address. So incorrect.
P2 : *px = 0 directly assigned a value but still px doesn't point to any memory location, so memory initialization or allocation should be done before. So incorrect.
P3 : Correction made in P2, memory pre allocated, So correct.
Hence (c) is correct option.
- What is printed by the print statements in the program P1 assuming call by reference parameter passing ?
Program P1()
{
x = 10;
y = 3;
func1(y, x, x);
print x;
print y;
}
func 1 (x, y, z)
{
y = y + 4;
z = x + y + z;
}
-
View Hint View Answer Discuss in Forum
Note the order in which parameters are passed. Inside func1(), x will actually refer to y of main (); and y and z will refer to x of main (). The statement y = y + 4; will result in 14 and statement z = x + y + z will make z = 3 + 14 + 14 = 31 (because y and z point to same variable x of main). Since z refers to x of main(), main will print 31.
Correct Option: B
Note the order in which parameters are passed. Inside func1(), x will actually refer to y of main (); and y and z will refer to x of main (). The statement y = y + 4; will result in 14 and statement z = x + y + z will make z = 3 + 14 + 14 = 31 (because y and z point to same variable x of main). Since z refers to x of main(), main will print 31.
- In the C language
-
View Hint View Answer Discuss in Forum
(a) There is no such restriction in C language
(b) True as soon as a function is called its activation record is created in the function stock.
(c) False. In C, variables are statically scoped, not dynamically.
(d) False. The activation records are stored on the same stack.Correct Option: B
(a) There is no such restriction in C language
(b) True as soon as a function is called its activation record is created in the function stock.
(c) False. In C, variables are statically scoped, not dynamically.
(d) False. The activation records are stored on the same stack.
- The results returned by function under value – result and reference parameter passing conventions
-
View Hint View Answer Discuss in Forum
In call by reference, the called function is working with the memory location of the passed variables. So, any update to the variables are immediately effective.
In call by value-result, the called function is working with a copy of the passed variable. On return, the updated values are copied back to the original variables. So during a function execution if an exception happens, in call by value-result, the passed variables won’t be getting update values.Correct Option: D
In call by reference, the called function is working with the memory location of the passed variables. So, any update to the variables are immediately effective.
In call by value-result, the called function is working with a copy of the passed variable. On return, the updated values are copied back to the original variables. So during a function execution if an exception happens, in call by value-result, the passed variables won’t be getting update values.
- Consider the C program shown below :
#include < stdio. h >
#define print(x) printf (“ %d”, x)
int x;
void Q (int z) {
z + = x; print(z);
}
void P (int *y) {
int x = *y + 2;
Q (x); *y = x – 1;
print (x);
}
main (void) {
x = 5;
P (& x)
Print (x);
}
The output of this program is
-
View Hint View Answer Discuss in Forum
Here X is the global variable so still 5.
Here this is global X whose xy has been changed to 6 so 6 is printed
12 66
First x = 5
Then by function P(&x)
X = 5 +2 = 7
Then by function Q(x)
z = z + x
= 7 + 5 = 12
Here x is global variable so still it is 5.
Return to function P(&x)
Y = 7–1= 6 print x =7
return to main
Print x = 6
Here this is global x whose *y has been changed to 6 so 6 is printed.Correct Option: A
Here X is the global variable so still 5.
Here this is global X whose xy has been changed to 6 so 6 is printed
12 66
First x = 5
Then by function P(&x)
X = 5 +2 = 7
Then by function Q(x)
z = z + x
= 7 + 5 = 12
Here x is global variable so still it is 5.
Return to function P(&x)
Y = 7–1= 6 print x =7
return to main
Print x = 6
Here this is global x whose *y has been changed to 6 so 6 is printed.