Basic of Programming Language · Programming Languages · GATE CSE
Marks 1
Consider the following C program:
#include <stdio.h>
int main() {
int a = 6;
int b = 0;
while(a < 10) {
a = a / 12 + 1;
a += b;}
printf("%d", a);
return 0;}
Which one of the following statements is CORRECT?
int fun ( int n ) {
int x = 1, k ;
if ( n == 1) return x ;
for( k = 1 ; k < n ; ++ k )
x = x + fun( k ) * fun( n - k ) ;
return x ;
}
The return value of fun (5) is ________. int func(int num)
{
int count = 0;
while(num)
{
count++;
num >>= 1;
}
return (count);
}
The value returned by func(435) is _________.
1) Static allocation of all data areas by a compiler makes it impossible to implement recursion.
2) Automatic garbage collection is essential to implement recursion.
3) Dynamic allocation of activation records is essential to implement recursion.
4) Both heap and stack are essential to implement recursion.
C = 100;
for i = 0 to n do
for j = 1 to n do
{
Temp = A[ i ][ j ] + C ;
A[ i ][ j ] = A[ j ][ i ] ;
A[ j ][ i ] = Temp - C ;
}
for i = 0 to n do
for j = 1 to n do
output(A[ i ][ j ]);
#include < stdio.h >
main()
{
int i;
int *pi = &i;
scanf("%d", pi);
printf("%d\n", i + 5);
}
Which one of the following statements is TRUE?
Group 1
P. Functional
Q. Logic
R. Object-oriented
S. Imperative
Group 2
1. Command-based, procedural
2. Imperative, abstract data types
3. Side-effect free, declaretive, expression Evaluation
4. Declaretive, clausal representation, theorem proving
macro Add x,y
Load y
Mul x
Store y
end macro
Although C does not support call by name parameter passing, the effect can be correctly simulated in C.
A programming language not supporting either recursion or pointer type does not need the support of dynamic memory allocation.
Marks 2
Consider an array X that contains n positive integers. A subarray of X is defined to be a sequence of array locations with consecutive indices.
The C code snippet given below has been written to compute the length of the longest subarray of X that contains at most two distinct integers. The code has two missing expressions labelled (P) and (Q).
int first=0, second=0, len1=0, len2=0, maxlen=0; for (int i=0; i < n; i++) { if (X[i] == first) { len2++; len1++; } else if (X[i] == second) { len2++; len1 = (P); second = first; } else { len2 = (Q); len1 = 1; second = first; } if (len2 > maxlen) { maxlen = len2; } first = X[i]; }
Which one of the following options gives the CORRECT missing expressions?
(Hint: At the end of the i-th iteration, the value of len1 is the length of the longest subarray ending with X[i] that contains all equal values, and len2 is the length of the longest subarray ending with X[i] that contains at most two distinct values.)
Consider the following program:
int main ( ) { f1( ); f2(2); f3( ); return (0); } |
int f1( ) { return (1); } |
int f2 (int X) { f3( ); if (X==1) return f1 ( ); else return (X*f2(X$$-$$1)); } |
int f3( ) { return (5); } |
Which one of the following options represents the activation tree corresponding to the main function?
What is printed by the following ANSI C program?
#include <stdio.h>
int main(int argc, char *argv[]) {
char a = 'P';
char b = 'x';
char c = (a & b) + '*';
char d = (a | b) - '-';
char e = (a ^ b) + '+';
printf("%c %c %c\n", c, d, e);
return 0;
}
ASCII encoding for relevant characters is given below :
Consider the following C program :
#include <stdio.h>
int main(){
float sum = 0.0, j = 1.0, i = 2.0;
while (i/j > 0.0625){
j = j + j;
sum = sum + i/j;
printf("%f\n", sum);
}
return 0;
}
The number of times the variable sum will be printed, when the above program is executed, is _________.
begin
q := 0
r := x
while r ≥ y do
begin
r := r - y
q := q + 1
end
end
The post condition that needs to be satisfied after the program terminates isint f(int j)
{
static int i = 50;
int k;
if (i == j)
{
printf("something");
k = f(i);
return 0;
}
else return 0;
}
Which one of the following is TRUE?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:______.int f (int &x, int c) {
c = c - 1;
if (c==0) return 1;
x = x + 1;
return f(x,c) * x;
}
I. A programming language which does not permit global variables of any kind and has no nesting of procedures/functions, but permits recursion can be implemented with static storage allocation
II. Multi-level access link (or display) arrangement is needed to arrange activation records only if the programming language being implemented has nesting of procedures/functions
III. Recursion in programming languages cannot be implemented with dynamic storage allocation
IV. Nesting procedures/functions and recursion require a dynamic heap allocation scheme and cannot be implemented with a stack-based allocation scheme for activation records
V. Programming languages which permit a function to return a function as its result cannot be implemented with a stack-based storage allocation scheme for activation records
global int i = 100, j = 5;
void P(x) {
int i = 10;
print(x + 10);
i = 200;
j = 20;
print (x);
}
main() {
P(i + j);
}
If the programming language uses dynamic scoping and call by name parameter
passing mechanism, the values printed by the above program areglobal int i = 100, j = 5;
void P(x) {
int i = 10;
print(x + 10);
i = 200;
j = 20;
print (x);
}
main() {
P(i + j);
}
If the programming language uses static scoping and call by need parameter
passing mechanism, the values printed by the above program areProgram P1()
{
x=10;
y=3;
func1(y,x,x);
print x;
print y;
}
func1(x,y,z)
{
y=y+4;
z=x+y+z;
}
Program P2
var n:int;
procedure W(var x:int)
begin
x=x+1;
print x;
end
procedure D
begin
var n:int;
n=3;
W(n);
end
begin \\begin P2
n = 10;
D;
end
If the language has dynamic scooping and parameters are passed by reference,
what will be printed by the program?(i) assignment
(ii) for loops where the loop parameter cannot be changed within the loop
(iii) if-then-else
(iv) forward go to
(v) arbitrary go to
(vi) non-recursive procedure call
(vii) recursive procedure/function call
(viii) repeat loop,
which constructs will you not include in a programming language such that it should be possible to program the terminates (i.e., halting) function in the same programming language.
var x: real;
procedure show;
begin print(x); end;
procedure small;
var x: real;
begin x: = 0.125; show; end;
begin x:=0.25;
show; small
end;
Then the output of the program is:Given the following Pascal like program segment:
Procedure A;
x,y:intger;
Procedure B;
x,z:real;
S1
end B;
Procedure C;
i:integer;
S2;
end C;
end A;
The variables accessible in S1 and S2 are
List - I
(A) Activation record
(B) Location counter
(C) Reference counts
(D) Address relocation
List - II
(1) Linking loader
(2) Garbage collection
(3) Subroutine call
(4) Assembler
List - I
(A) Small talk(B) LISP
(C) Prolog
(D) VAL
List - II
(p) Logic programming(q) Data flow programming
(r) Functional programming
(s) Object-Oriented programming
List - I
(A) Pointer data type(B) Activation record
(C) Repeat-until
(D) Coercion
List - II
(p) Type conversion(q) Dynamic data structure
(r) Recursion
(s) Nondeterministic loop