0% found this document useful (0 votes)
2 views

Programming Language

The document contains a series of programming questions and pseudo-code examples focused on C programming concepts, including functions, recursion, and matrix operations. It presents various scenarios and asks for expected outputs or behaviors of the code snippets provided. Additionally, it includes questions on the implications of certain code changes and the effects of recursion on function calls.

Uploaded by

siyamalavaithi03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming Language

The document contains a series of programming questions and pseudo-code examples focused on C programming concepts, including functions, recursion, and matrix operations. It presents various scenarios and asks for expected outputs or behaviors of the code snippets provided. Additionally, it includes questions on the implications of certain code changes and the effects of recursion on function calls.

Uploaded by

siyamalavaithi03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

98 Programming Languages

(a) The matrix A itself


Practice Questions
(b) Transpose of matrix A
Basics (c) Adding 100 to the upper diagonal elements and
subtracting 100 from lower diagonal elements
of A
01. What will be the output of the following C program
(d) None of the above.
segment?
Char inchar = ‘A’;
03. Consider the following pseudo code, where x and y
Switch ( inchar )
are positive integers.
{
begin
case ‘A’ : printf (“Choice A\ n”) ;
q: = 0
case ‘B’ :
r : = x
case ‘C’ : printf (“Choice B”);
while r ≥ y do
case ‘D’ :
begin
case ‘E’ :
r:=r–y
default : printf ( “ No Choice” );
q:=q+1
}
end
(a) No Choice
end
(b) Choice A
The post condition that needs to be satisfied after
(c) Choice A
the program terminates is
Choice B No Choice
(a) {r = qx + y ^ r < y}
(d) Program gives no output as it is erroneous
(b) {x = qy + r ^ r < y}
(c) {y = qx + r ^ 0 < r < y}
02. Let A be a square matrix of size n × n. Consider
(d) {q + 1 < r – y ^ y > 0}
the following pseudo-code. What is the expected
output?
04. Consider the following C program
C = 100;
# include<stdio.h>
for i = 1 to n do
int main ( )
for j = 1 to n do {
{ int i, j, k = 0;
Temp = A[ i ][ j ] + C ; j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;
A[ i ][ j ] = A[ j ] [ i ]; k – = – –j;
for (i = 0; i < 5; i++)
A[ j ][ i ] = Temp – C ;
{
}
switch (i + k)
for i = 1 to n do
{
for j = 1 to n do case 1:
output (A[i][j] ); case 2: printf (“\ n%d”, i+k);

India’s Best Online Coaching Platform for GATE, ESE, PSUs, SSC-JE, RRB-JE, SSC, Banks, Groups & PSC Exams
Enjoy a smooth online learning experience in various languages at your convenience
99 Programming Languages

case 3: printf (“\n%d”, i+k); For large values of y, the return value of the function
default: printf (“\n%d”, i+k); f best approximates
} (a) xy
}
(b) ex
return 0; (c) ln(1+x)
}
(d) xx
The number of times printf statement is executed
is ______.
07. Consider the following C-program:
double foo(double); /* Line 1 */
Functions
int main ( )
{
05. Consider the following C function definition
double da, db;
int Trial (int a, int b, int c)
{ // input da

if ((a > = b) && (c < b)) db = foo(da);


return b; }
else if (a > = b) double foo(double a)
return Trial (a,c,b); {
else return a;
return Trial (b,a,c);
}
}
The function Trial:
(a) Finds the maximum of a, b and c The above code compiled without any error or
(b) Finds the minimum of a, b and c warning. If Line 1 is deleted, the above code will
(c) Finds the middle number of a, b, c show:
(d) None of the above

06. Consider the following C function. (a) no compiler warning or error

float f(float x, int y) (b) some compiler-warnings not leading to


{ unintended results
float p, s; int i; (c) some compiler-warnings due to type-mismatch
for (s=1,p=1,i=1; i<y; i++) eventually leading to unintended results
{ (d) compiler errors
p *= x/i;
s+=p;
}
return s;
}
Regular Live Doubt clearing Sessions | Free Online Test Series | ASK an expert
Affordable Fee | Available 1M | 3M | 6M |12M|18M and 24 Months Subscription Packages
100 Programming Languages

08. Consider the function func shown below: 10. The following function computes the maximum
value contained in an integer
int func(int num)
array p[ ]of size n (n >= 1).
{
int max(int *p, int n) {
int count = 0;
int a = 0, b = n – 1;
while (num)
while (__________)
{
{
count ++;
if (p[a] <= p[b]) { a = a+1; }
num >>= 1;
else { b = b-1; }
}
}
return (count); return p[a];
} }
The value returned by func(435) is ____. The missing loop condition is
(a) a != n (b) b != 0
09. Consider the following C program. (c) b > (a + 1) (d) b != a
void f(int, short);
11. The following function computes XY for positive
void main( ) integers X and Y.
{ int exp(int X, int Y)
{
int i = 100;
int res = 1, a = X, b = Y;
short s = 12; while (b != 0 )
short *p = &s; {
if ( b%2 == 0)
__________ ; // call to f( )
{
} a = a*a; b = b/2;
}
else
Which one of the following expressions, when
{
placed in the blank above, will NOT result in a type
res = res*a; b = b-1;
checking error?
}
(a) f(s,*s) }
(b) i = f(i,s) return res;
}
(c) f(i,*s)
Which one of the following condition is TRUE
(d) f(i,*p) before every iteration of the loop?
(a) XY = ab (b) (res*a)y = (res*X)b
(c) XY = res*ab (d) XY = (res*a)b
India’s Best Online Coaching Platform for GATE, ESE, PSUs, SSC-JE, RRB-JE, SSC, Banks, Groups & PSC Exams
Enjoy a smooth online learning experience in various languages at your convenience
101 Programming Languages

15. In the C language


Recursion
(a) At most one activation record exists between

12. int Get(int a, int b) the current activation record and the activation

{ record for the main

if (b==0) (b) The number of activation records between the


return 1; current activation record and the activation
else record of the main depends on the actual
if (a < b) function calling sequence.
return 0; (c) The visibility of global variables depends on
else the actual function calling sequence.
return Get (a –1, b) + Get (a –1, b–1); (d) Recursion requires the activation record
} for the recursive function to be saved on a
What is the return value of Get(3, 2)? different stack before the recursive function
can be called.
13. What is the result of the following program?
int x, result;
16. Consider the following C-program:
int f (int x)
{ void foo (int n, int sum )
x = x + 1; {
return x; int k = 0, j = 0;
}
if (n= =0) return;
void main ()
{ k = n % 10; j = n / 10;
x = 5; sum = sum + k;
result = f (x) * f (x); foo (j, sum);
printf(“%d”, result);
printf (“%d”, k);
}
(a) 5 (b) 25 (c) 36 (d) 42 }
int main ( )
14. What value would the following function return for
{
the input x = 95?
Function fun (x: integer): integer; int a = 2048, sum = 0;

begin foo (a, sum);


if x > 100 then printf(“%d\n”, sum);
return (x – 10); }
else
return (fun(fun(x+11))); What does the above program print?
end; (a) 8, 4, 0, 2, 14 (b) 8, 4, 0, 2, 0
(a) 89 (b) 90 (c) 91 (d) 92 (c) 2, 0, 4, 8, 14 (d) 2, 0, 4, 8, 0
Regular Live Doubt clearing Sessions | Free Online Test Series | ASK an expert
Affordable Fee | Available 1M | 3M | 6M |12M|18M and 24 Months Subscription Packages
102 Programming Languages

17. Choose the correct option to fill ?1 and ?2 so that 18. What is the return value of the function foo when it
the program below prints an input string in reverse is called as foo (345, 10)?
order. Assume that the input string is terminated by (a) 345 (b) 12 (c) 5 (d) 3
a newline character.
void reverse (void) 19. What is the return value of the function foo when it
{ is called as foo (513, 2)?
int c; (a) 9 (b) 8 (c) 5 (d) 2
if (?1)
reverse ( ) ; 20. Consider the following C function.
?2
int fun(int n)
}
{
main ( )
int x = 1, k;
{
printf(“Enter Text”) ; if (n = = 1) return x;
printf(“\ n” ); for (k =1; k<n; ++k)
reverse ( ); x = x + fun(k) * fun(n–k);
printf (“\ n”); return x;
}
}
(a) ?1 is (getchar ( ) ! = ‘\ n’) The return value of fun(5) is _____.
?2 is getchar (c);
(b) ?1 is ((c = getchar ( ) )! = ‘\ n’)
Storage Classes
?2 is getchar (c);
(c) ?1 is (c ! = ‘\ n’)
21. The value of j at the end of the execution of the
?2 is putchar (c); following C program
(d) ?1 is ((c = getchar ( ) )! = ‘\ n’) int incr (int i)
?2 is putchar (c); {
static int count = 0;
Common Data for Questions 18 & 19 count = count + i;
return (count);
Consider the following recursive C function that }
takes two arguments: main ( )
Unsigned int foo (unsigned int n, unsigned int r) {
{ int i, j;
if (n > 0 ) for (i = 0; i <=4; i++)
return((n%r) + foo(n/r,r)); j = incr(i);
else return 0; }
} is:
(a) 10 (b) 4 (c) 6 (d) 7
India’s Best Online Coaching Platform for GATE, ESE, PSUs, SSC-JE, RRB-JE, SSC, Banks, Groups & PSC Exams
Enjoy a smooth online learning experience in various languages at your convenience
103 Programming Languages

22. Consider the following C function: void prtFun(void)


int f(int n) {
{
static int a = 2 ;// Line 2
static int i = 1 ;
if (n > 5) return n; int b = 1;
n = n+i; a += ++b;
i++;
printf (“ \n %d %d” , a, b);
return f(n);
} }
The value returned by f(1) is
(a) 5 (b) 6 (c) 7 (d) 8 24. What output will be generated by the given code
segment?
23. Consider the following C function: (a) (b) (c) (d)
int f(int n) 3 1 4 2 4 2 3 1
{ 4 1 6 1 6 2 5 2
static int r = 0; 4 2 6 1 2 0 5 2
if (n <= 0) return 1;
if (n > 3) 25. What output will be generated by the given code
{ segment if:
r = n; Line 1 is replaced by auto int a = 1;
return f(n−2)+2; Line 2 is replaced by register int a =2;
}
(a) (b) (c) (d)
return f(n−1)+r;
} 3 1 4 2 4 2 4 2
What is the value of f (5)? 4 1 6 1 6 2 4 2
(a) 5 (b) 7 (c) 9 (d) 18 4 2 6 1 2 0 2 0

Common Data for Questions 24 & 25 26. Consider the C function given below.
Consider the following C code segment. int f ( int j)
int a, b, c = 0 ; { static int i = 50 ;
void prtFun (void) ; int k ;
main ( ) if ( i = = j )
{ {
static int a = 1; // Line1 printf ( “ something”) ;
prtFun( ); k = f(i) ;
a+=1; return 0 ;
prtFun( ); }
printf (“ \n %d %d” , a, b) ; else return 0 ;
} }
Regular Live Doubt clearing Sessions | Free Online Test Series | ASK an expert
Affordable Fee | Available 1M | 3M | 6M |12M|18M and 24 Months Subscription Packages
104 Programming Languages

Which one of the following is TRUE? void main ( )


(a) The function returns 0 for all values of j. {
count (3);
(b) The function prints the string something for all }
values of j.
(a) 3 1 2 2 1 3 4 4 4 (b) 3 1 2 1 1 1 2 2 2
(c) The function returns 0 when j = 50.
(c) 3 1 2 2 1 3 4 (d) 3 1 2 1 1 1 2
(d) The function will exhaust the runtime stack or
run into an infinite loop when j = 50.
Pointers
27. Consider the following C program.
#include<stdio.h> 29. The most appropriate matching for the following
int f1(void); pairs
int f2(void); List - I
int f3(void); X: m= malloc(5); m= NULL;
int x = 10; Y: free(n); n→value=5;
int main ( ) Z: char *p; *p= ‘a’;
{ List - II
int x = 1; 1: using dangling pointers
2: using uninitialized pointers
x + = f1 ( ) + f2 ( ) + f3( ) + f2( );
3. lost memory
printf (“%d”, x);
is
return 0;
(a) X-1, Y-3, Z-2 (b) X-2, Y-1, Z-3
}
(c) X-3, Y-2, Z-1 (d) X-3, Y-1, Z-2
int f1 ( ) { int x = 25; x++; return x;}
int f2 ( ) { static int x = 50; x++; return x;} 30. The following C declaration
int f3 ( ) {x *= 10; return x;} struct node
The output of the program is ______. {
int i;
28. What will be the output of the following C program? float j;
};
void count (int n)
struct node *s[10];
{
define s to be
static int d = 1;
(a) An array, each element of which is a pointer to
printf(“%d” , n); a structure of type node
printf(“%d”,d); (b) A structure of 2 fields, each field being a pointer
d++; to an array of 10 elements
(c) A structure of 3 fields: an integer, a float, and
if (n >1) count(n –1);
an array of 10 elements
printf(“%d”, d);
(d) An array, each element of which is a structure
} of type node
India’s Best Online Coaching Platform for GATE, ESE, PSUs, SSC-JE, RRB-JE, SSC, Banks, Groups & PSC Exams
Enjoy a smooth online learning experience in various languages at your convenience
105 Programming Languages

31. Consider the following three C functions: (a) What will be the output of the program?
[P1] int * g(void) (b) If abc(s) is called with a null-terminated string
{ s of length n characters (not counting the null
int x =10; (‘\0’) character), how many characters will be
return (&x); printed by abc(s)?
}
33. Consider the C program shown below.
[P2] int * g(void) #include <stdio.h>
{ #define print(x) printf (“%d”, x)
int * px; int x;
* px = 10; void Q(int z)
return px; {
} z += x; print(z);
}
[P3] int * g(void) void P(int *y)
{ {
int * px; int x = *y+2;
px=(int*)malloc(sizeof (int)); Q(x); *y = x–1;
*px=10; print(x);
return px; }
} main(void)
Which of the above three functions are likely to {
cause problems with pointers? x = 5;
(a) Only P3 (b) Only P1 and P3 P(&x);
(c) Only P1 and P2 (d) P1, P2 and P3 print(x);
}
32. Consider the following C program: The output of this program is
void abc(char *s) (a) 12 7 6
{ (b) 22 12 11
if(s[0]== ‘\0’) return; (c) 14 6 6
abc(s+1); (d) 7 6 6
abc(s+1);
printf(“%c”,s[0]); 34. Assume the following C variable declaration
} int * A[10], B[10] [10];
main( ) Of the following expressions
{ I. A[2]
abc(“123”); II. A[2] [3]
} III. B[1]
IV. B[2] [3]
Regular Live Doubt clearing Sessions | Free Online Test Series | ASK an expert
Affordable Fee | Available 1M | 3M | 6M |12M|18M and 24 Months Subscription Packages
106 Programming Languages

Which will not give compile-time errors if used 37. What is printed by the following C program?
as left hand sides of assignment statements in a C int f (int x, int *py, int * *ppz)
program? {
int y, z;
(a) I, II, and IV only * *ppz +=1; z =**ppz;
(b) II, III, and IV only *py +=2; y =*py;
x +=3;
(c) II and IV only
return x +y+ z;
(d) IV only }
void main ( )
35. Consider the following C program segment: {
int c, *b, * *a;
char p [20];
c =4; b =&c; a =&b;
char * s = “string”; printf( “%d”, f (c,b,a) ) ;
}
int length = strlen (s);
(a) 18 (b) 19 (c) 21 (d) 22
for (i = 0 ; i < length; i++)
38. What does the following program print?
p[i] = s[length – i];
#include <stdio.h>
printf(“%s”,p); void f (int *p, int *q)
The output of the program is {

(a) gnirts p = q;
*p = 2;
(b) string
}
(c) gnirt
int i = 0, j =1;
(d) no output is printed
int main ( )
{
36. What does the following C-statement declare? f(&i, & j);
int ( * f) (int * ) ; printf (“%d %d \ n”, i, j) ;
(a) A function that takes an integer pointer as return 0;
argument and returns an integer }
(b) A function that takes an integer as argument (a) 2 2 (b) 2 1 (c) 0 1 (d) 0 2

and returns an integer pointer


39. What does the following fragment of C-program
(c) A pointer to a function that takes an integer
print?
pointer as argument and Returns an integer.
Char c [ ] = “GATE2011”;
(d) A function that takes an integer pointer as
char *p = c;
argument and returns a function pointer
printf (“%s”, p + p [ 3 ] – p[1] );
India’s Best Online Coaching Platform for GATE, ESE, PSUs, SSC-JE, RRB-JE, SSC, Banks, Groups & PSC Exams
Enjoy a smooth online learning experience in various languages at your convenience
107 Programming Languages

(a) GATE2011 (b) E2011 42. What is the output of the following C code? Assume
(c) 2011 (d) 011 that the address of x is 2000 (in decimal) and an
integer requires four bytes of memory?
40. Consider the following program in C language int main( )
# include <stdio.h>
{
main( )
unsigned int x[4] [3] = {(1,2,3), {4,5,6}, {7,8,9},
{
{10, 11, 12}};
int i ;
printf(“%u,%u,%u”, x+3,*(x+3), *(x+2)+3);
int *pi = &i;
}
scanf(“%d”, pi);
(a) 2036, 2036, 2036
printf(“%d\n”, i + 5);
(b) 2012, 4, 2204
}
Which one of the following statements is TRUE? (c) 2036, 10, 10
(a) Compilation fails (d) 2012, 4, 6
(b) Execution results in a runtime error.
(c) On execution, the value printed is 5 more than 43. Consider the following function written in the C
the address of variable i. programming language.
(d) On execution, the value printed is 5 more than void foo(char *a)
the integer value entered.
{

41. The output of the following C program is ______. if(*a && *a != ‘ ’)

void f1(int a, int b) {


{ foo(a+1);
int c;
c=a; a=b; b=c; putchar(*a);
} }
void f2(int *a, int *b) }
{
The output of the above function on input “ABCD
int c;
EFGH” is
c=*a; *a=*b; *b=c;
} (a) ABCD EFGH
int main ( ) (b) ABCD
{
(c) HGFE DCBA
int a=4, b=5, c=6;
(d) DCBA
f1(a, b);
f2(&b, &c);
printf(“%d”, c–a–b);
}
Regular Live Doubt clearing Sessions | Free Online Test Series | ASK an expert
Affordable Fee | Available 1M | 3M | 6M |12M|18M and 24 Months Subscription Packages
108 Programming Languages

44. Consider the C program below. 46. Consider the following C program.
#include <stdio.h> #include<stdio.h>
int *A, stkTop; int main( )
int stkFunc(int opcode, int val) {
static int a[ ] = {10, 20, 30, 40, 50};
{
static int size = 0, stkTop = 0; static int *p[ ] = {a, a+3, a+4, a+1, a+2};

switch (opcode) int **ptr = p;


{ ptr++;
case –1: size = val; break; printf(“%d%d”, ptr-p, **ptr);
case 0: if(stkTop<size)A[stkTop++]=val; }
break; The output of the program is ______.
default: if (stkTop) return A[– –stkTop];
} 47. Consider the following C program.
#include<stdio.h>
return – 1;
void mystery(int *ptra, int *ptrb)
}
{
int main( ) int *temp;
{ temp = ptrb;
int B[20]; A = B; stkTop = –1; ptrb = ptra;
stkFunc(–1, 10); ptra = temp;
stkFunc(0, 5); }
int main( )
stkFunc(0, 10); {
printf(“%d\n”, stkFunc(1,0)+stkFunc(1,0)); int a = 2016, b = 0, c = 4, d = 42;
} mystery (&a, &b);
The value printed by the above program is ______. if (a < c)
mystery(&c, &a);
45. Consider the following C program segment.
mystery(&a, &d);
#include <stdio.h> printf(“%d\n”, a);
int main( ) }
{ The output of the program is _________.
char s1[7] = “1234”, *p;
p = s1 + 2; 48. The value printed by the following program
*p = ‘0’; is_______.
printf (“%s”, s1); void f(int* p, int m)
} {
What will be printed by the program? m = m + 5;
*p = *p + m;
(a) 12 (b) 120400
return;
(c) 1204 (d) 1034 }
India’s Best Online Coaching Platform for GATE, ESE, PSUs, SSC-JE, RRB-JE, SSC, Banks, Groups & PSC Exams
Enjoy a smooth online learning experience in various languages at your convenience
109 Programming Languages

void main( ) 51. main( )


{ {
int i=5, j=10; struct a
f(&i, j); {
printf(“%d”, i+j); char ch[7];
} char *str;
};
49. Consider the following program: struct b
int f(int *p, int n) {
{ char *c;
if (n <= 1) return 0; struct a ss1;
else };
return max(f(p+1,n–1), p[0] – p[1]); sturct b s2={“Raipur”,“Kanpur”, “Jaipur”};
} printf(“\n %s %s”, s2.c, s2.ss1.str);
int main() printf(“\n %s %s”, ++s2.c,++s2.ss1.str);
{ }
int a[ ] = {3,5,2,6,4}; (a) Raipur Raipur (b) Raipur Jaipur
printf(“%d”, f(a,5)); aipur aipur aipur aipur
} (c) Jaipur Kanpur (d) None of these
Note: max(x,y) returns the maximum of x and y. aipur aipur
The value printed by this program is___________.
52. main( )
50. main( ) {
{ struct s1
{
static char *s[ ] = {
char *z;
“ice”,
int i;
“green”, struct s1 *p;
“cone”, };
“please”, static struct s1 a[ ] = {
}; {“Nagpur”, 1,a+1},
static char **ptr[ ] = {s+3, s+2, s+1, s}; {“Raipur”, 2, a+2},
char ***p = ptr; {“Kanpur”, 3, a}};
printf (“\n %x”, **++p); struct s1 *ptr = a;
printf(“\n %s”, *– –*++p+3); printf(“\n %s %s %s”, a[0].z, ptr→z, a[2].p→z);
printf(“\n %s”, *p[–2] + 3); }
printf(“\n %s”, p[–1] [–1] +1); (a) Raipur, Raipur, Raipur
} (b) Nagpur, Nagpur, Nagpur
(a) cone, ase, reen (b) ice, green, cone (c) Raipur, Nagpur, Jaipur
(c) green, cone, please (d) none of the above (d) None of these
Regular Live Doubt clearing Sessions | Free Online Test Series | ASK an expert
Affordable Fee | Available 1M | 3M | 6M |12M|18M and 24 Months Subscription Packages
110 Programming Languages

53. main( ) printf(“%s”, p + + → c);


{ printf(“%c”, *+ + p → c);
struct s1 printf(“% d”, p[0] .i );
{ char *z;
printf(“%s\n”, p → c);
int i;
}
struct s1 *p;
(a) jungle, n, 8, ncestor
};
static struct s1 a[ ] = { (b) etter, u,6, ungle
{“Nagpur”, 1, a+1}, (c) cetter, k,6, jungle
{“Raipur”, 2, a+2}, (d) etter,u,8, ncestor
{“Kanpur”, 3, a}
}; 55. Which one of the choices given below would be
struct s1 *ptr = a;
printed when the following program is executed?
printf(“\n %s”, ++(ptr→z));
#include <stdio.h>
printf(“\n%s”, a[(++ptr)→i].z);
int a1[ ] = {6,7,8,18,34,67};
printf(“\n%s”, a[--(ptr→p→i)]. z);
} int a2[ ] = {23,56,28,29};
(a) Raipur, Raipur, Raipur int a3[ ] = {–12,27, –31};
(b) Jaipur, Jaipur, Jaipur int *x[ ] = {a1,a2,a3};
(c) Raipur, Jaipur, Kanpur void print (int *a[ ])
(d) None of these {
printf(“%d”, a[0] [2]);
54. Which one of the choices given below would be
printf(“%d”, *a[2]);
printed when the following program is executed?
printf(“%d”, *++a[0]);
#include <stdio.h>
struct test { printf(“%d”, *(++a) [0]);
int i; printf(“%d\n”, a[–1] [+1]);
char *c; }
}st[ ]={ main( )
5, “become”, {
4, “better”,
print (x);
6,“jungle”,
}
8,“ancestor”,
7, “brother”};
main( ) (a) 8, –12, 7, 23, 8
{ (b) 8, 8, 7, 23, 7
struct test *p = st; (c) –12, –12, 27, –31, 23
p+ = 1; (d) –12, –12, 27, –31, 56
++p →c;

India’s Best Online Coaching Platform for GATE, ESE, PSUs, SSC-JE, RRB-JE, SSC, Banks, Groups & PSC Exams
Enjoy a smooth online learning experience in various languages at your convenience
111 Programming Languages

56. #include<stdio.h>
Parameter Passing Techniques
main( )
{
char s[ ]={‘a’, ‘b’, ‘c’, ‘\n’, ‘c’, ‘\0’}; 58. Consider the program below in a hypothetical
char *p, *str,*str1; language which allows global variables and a
p = &s[3]; choice of call by reference or call by value methods
str=p; of parameter passing.
str1=s; int i;
printf(“%c”, ++*p + ++*str1–32); Program main( )
{
}
int j = 60;
i = 50;
(a) M (b) A call f(i,j);
(c) B (d) C print i,j;
}
57. consider the program below: Procedure f(x,y)
#include <stdio.h> {
i = 100;
int fun(int n, int *fp)
x = 10;
{
y = y + i;
int t, f; }
if (n < = 1) Which one of the following options represents the
{ correct output of the program for the two parameter
*fp = 1; passing mechanisms?
return 1; (a) Call by value: i=70, j = 10;
} Call by reference: i=60, j=70
t = fun (n–1, fp); (b) Call by value: i=50, j = 60;
f = t + *fp; Call by reference: i=50, j =70
*fp = t; (c) Call by value: i=10, j = 70;
return f; Call by reference: i=100, j =60
} (d) Call by value: i=100, j = 60;
int main( ) Call by reference: i =10, j=70
{
int x = 15; 59. Consider the following C function
printf (“%d\n”, fun(5, &x)); void swap (int a, int b)
return 0; {
} int temp;
The value printed is: temp = a ;
a = b ;
(a) 6 (b) 8 b = temp ;
(c) 14 (d) 15 }
Regular Live Doubt clearing Sessions | Free Online Test Series | ASK an expert
Affordable Fee | Available 1M | 3M | 6M |12M|18M and 24 Months Subscription Packages
112 Programming Languages

In order to exchange the values of two variables x Determine its output, if the parameters are passed to
and y the procedure P by
(a) call swap(x, y) i. value
(b) call swap(&x, &y)
ii. reference
(c) swap(x, y) cannot be used as it does not return
any value
62. Consider the following program in pseudo pascal
(d) swap(x, y) cannot be used as the parameters
syntax.
are passed by value
program what:
60. What is the return value of f (p, p), if the value of p var z: integer;
is initialized to 5 before the call? procedure recur (x):
Note that the first parameter is passed by reference, begin if x ≤ 40 then
whereas the second parameter is passed by value.
begin x: =x + z;
int f (int &x, int c)
{ recur (x);
c = c – 1; z:=x+10;
if (c = = 0) return 1; end
x = x + 1; end(*recur*);
return f(x, c) * x;
begin (*what*)
}
(a) 3024 (b) 6561 z : =10;
(c) 55440 (d) 161051 recur(z);
writeln(z);
61. Consider the following pseudo-code (all data items end
are of type integer):
(a) Suppose the parameter to the procedure ‘recur’
procedure P (a, b, c); is passed by value.
a:=2; (i) What value is printed by the program?
c:=a+b; (ii) How many times is ‘recur’ called?
end {P};
begin (b) What value is printed by the program if the
x:=1; parameter is passed by reference?

y:=5;
z:=100; 63. What will be the output of the following program
P(x,x*y,z); assuming that parameter passing is
write (‘x=’,x, ‘z=’,z) (i) call by value
(ii) call by reference
end:
India’s Best Online Coaching Platform for GATE, ESE, PSUs, SSC-JE, RRB-JE, SSC, Banks, Groups & PSC Exams
Enjoy a smooth online learning experience in various languages at your convenience
113 Programming Languages

procedure P(x,y,z);
Scope
begin y: = y + 1; z: = z + x; end;
begin
Common Data for Questions 65 & 66
a: = 2; b: =3;
P(a+b,a,a); Study the following program written in a
Print (a); block − structured language:
var x,y :integer;
end.
procedure P (n:integer);
begin
64. What is printed by the print statements in the
program P1 assuming call by reference parameter x: = (n+2)/(n−3);
passing? end;
Program P1( ) procedure Q
{ var x,y :integer;

x=10; begin
x: =3;
y=3;
y: =4;
func1(y, x, x);
P(y);
print x;
Write (x) ……………… (1)
print y;
end;
}
begin
func1(x, y, z)
x : =7;
{ y : =8;
y= y + 4; Q:
z= x+ y + z; Write (x) ……………….. (2)
} end;

(a) 10, 3 65. What will be printed by the write statements marked
(b) 31, 3 (1) and (2) in the program if variables are statically
scoped?
(c) 27, 7
(a) 3, 6 (b) 6,7
(d) None of the above
(c) 3, 7 (d) None

66. For the program given Q65 in what will be printed


by the write statements marked (1) and (2) if the
variables are dynamically scoped:
(a) 3, 6 (b) 6,7
(c) 3,7 (d) None
Regular Live Doubt clearing Sessions | Free Online Test Series | ASK an expert
Affordable Fee | Available 1M | 3M | 6M |12M|18M and 24 Months Subscription Packages
114 Programming Languages

67. Consider the following program selection and var a, b : integer;


given figure which shows activation records of P;
procedures involved in the calling sequence. end {Q};
P→ s→q→r→q
begin
Write the access links of the activation records to a:=1; b:=2;
enable correct access of variables in the procedures
Q;
from other procedures involved in the calling
write (‘a =’, a, ‘b=’,b)
sequence.
end.
Procedure
P; 69. Consider the program below:
access link (a. 1)

P program main;

Procedure q; a.1 var r:integer;
s procedure two;
Procedure r;
a.1 begin write (r) end;
begin
q
q; procedure one;

a.1 end r; var r:integer;
r
begin begin r:=5; two;end;
a.1
q begin r:=2;
r;
end q; two; one; two;
Procedure s; end;
begin What is printed by the above program if
q; i. Static scoping is assumed for all variables;
end s; ii. Dynamic scoping is assumed for all variables.
end P; Give reasons for your answer.

68. For the following pseudo-code, indicate the output, 70. Consider the following program in a language that
if has dynamic scoping:
var x: real;
(i) static scope rules and
procedure show;
(ii) dynamic scope rules are used begin print(x);end;
var a, b : integer; procedure small;
Procedure P; var x: real;
begin x: = 0.125; show; end;
a:=5; b:=10;
begin x:=0.25;
end {P};
show; small;
procedure Q; end.
India’s Best Online Coaching Platform for GATE, ESE, PSUs, SSC-JE, RRB-JE, SSC, Banks, Groups & PSC Exams
Enjoy a smooth online learning experience in various languages at your convenience
115 Programming Languages

Then the output of the program is: 72. Consider the following program
(a) 0.125 0.125 Program P2
(b) 0.25 0.25 var n: int;
(c) 0.25 0.125 procedure W(var x: int)
(d) 0.125 0.25 begin
x=x+1;
71. Consider the following program in pseudo-Pascal print x;
syntax. end
program main procedure D
begin
var x: integer;
var n: int;
procedure Q (z: integer);
n=3;
begin W(n);
z: = z + x; end
writeln(z) begin \\begin P2
end; n = 10;
procedure P (y: integer); D;
var x: integer;
end;
begin
If the language has dynamic scoping and parameters
x: = y + 2; are passed by reference, what will be printed by the
Q(x); program?
writeln(x) (a) 10 (b) 11
end; (c) 3 (d) None of the above
begin
x:=5; 73. Consider the program given below, in a block-
P(x); structured pseudo-language with lexical scoping
and nesting of procedures permitted
Q(x);
Program main;
writeln(x)
end. var …

What is the output of the program, when Procedure A1;


(a) The parameter passing mechanism is call-by- var …
value and the scope rule is static scoping? Call A2;
(b) The parameter passing mechanism is call-
End A1
by-reference and the scope rule is dynamic
scoping? Procedure A2;
var …
Regular Live Doubt clearing Sessions | Free Online Test Series | ASK an expert
Affordable Fee | Available 1M | 3M | 6M |12M|18M and 24 Months Subscription Packages
116 Programming Languages

Procedure A21; 75. Heap allocation is required for languages.


Var … (a) That support recursion
(b) That support dynamic data structures
Call A1;
(c) That use dynamic scope rules
End A21 (d) None of the above
call A21;
76. Which languages necessarily need heap allocation
End A2
in the runtime environment?
call A1; (a) Those that support recursion
End main. (b) Those that use dynamic scoping
Consider the calling chain: (c) Those that allow dynamic data structures
(d) Those that use global variables
Main →A1→ A2 → A21 → A1
The correct set of activation records along with
77. Which of the following is not a C variable?
their access links is given by
(a) Count123 (b) Count_123
(a) (b)
Main Main
(c) Count@123 (d) @Count
A1 A1
A2 A2 78. Which of the following is/are correct statements?
A21 A21 int a=7
FRAME FRAME ACCESS
POINTER
A1 ACCESS POINTER
A1
LINKS 1) if(a = = 7) printf(“IncludeHelp”);
LINKS
(c) (d)
Main
2) if(7 = = a) printf(“IncludeHelp”);
Main
A1
3) if(a = 7) printf(“IncludeHelp”);
FRAME
A1
POINTER A2 4) if(7 = a) printf(“IncludeHelp”);
A2
A21
A21 ACCESS FRAME
LINKS POINTER
A1 ACCESS (a) 1 only (b) 2 only
LINKS
(c) 4 only (d) 3 only

79. Consider the following C function.


74. What will be the output of the following pseudo-
void convert (int n ) {
code when parameters are passed by reference and
if (n<0)
dynamic scoping is assumed?
printf(“%d”, n);{
a = 3;
else
void n(x) {x = x * a; print(x);}
convert(n/2);
void m(y) {a = 1; a = y − a; n(a); print(a);}
printf(“%d”, n%2);
void main() {m(a);}
}
(a) 6, 2 (b) 6, 6
}
(c) 4, 2 (d) 4, 4
Which one of the following will not happen when
the function convert is called with any positive
integer nn as argument?

India’s Best Online Coaching Platform for GATE, ESE, PSUs, SSC-JE, RRB-JE, SSC, Banks, Groups & PSC Exams
Enjoy a smooth online learning experience in various languages at your convenience
117 Programming Languages

(a) It will print the binary representation of nn and terminate


(b) It will print the binary representation of nn in the reverse order and terminate
(c) It will print the binary representation of nn but will not terminate
(d) It will not print anything and will not terminate

Key for Practice Questions


01. (c) 02. (a) 03. (b) 04. 10 05. (c) 06. (b) 07. (d) 08. 9 09. (d) 10. (d)

11. (c) 12. 3 13. (c) 14. (c) 15. (b) 16. (d) 17. (d) 18. (b) 19. (d) 20. 51

21. (a) 22. (c) 23. (d) 24. (c) 25. (d) 26. (d) 27. 230 28. (a) 29. (d) 30. (a)

31. (c) 33. (a) 34. (a) 35. (d) 36. (c) 37. (b) 38. (d) 39. (c) 40. (d) 41. –5

42. (a) 43. (d) 44. 15 45. (c) 46. 1, 40 47. 2016 48. 30 49. 3 50. (a) 51. (b)

52. (b) 53. (d) 54. (b) 55. (a) 56. (a) 57. (b) 58. (d) 59. (d) 60. (b) 64. (b)

65. (a) 66. (b) 70. (c) 71. (A) 12, 7, 10, 5 (B) 14, 14, 10, 10 72. (d) 73. (d) 74. (d)

75. (b) 76. (c) 77. (c), (d) 78. (a), (b) & (d) 79. (a), (b) & (d)

Regular Live Doubt clearing Sessions | Free Online Test Series | ASK an expert
Affordable Fee | Available 1M | 3M | 6M |12M|18M and 24 Months Subscription Packages

You might also like