programming

Question 1
Consider the following ANSI C program.
# include <stdio.h>
int main()
{
            int i, j, count;
            count = 0;
            i = 0;
            for (j = -3; j <= 3; j++)
            {
                        if ((j >= 0) && (i++))
                                 count = count + j;
             }
              count = count + i;
              printf(“%d”, count);
              return 0;
}   
Which one of the following options is correct?
A
The program will compile successfully and output 13 when executed.
B
The program will compile successfully and output 10 when executed.
C
The program will compile successfully and output 8 when executed.
D
The program will not compile successfully.
Question 1 Explanation: 

Input: count=0 , i=0 and j=-3

For(j = -3; j <= 3; j++)   → Condition TRUE then enters into the loop. 

if((j >= 0) && (i++))   → Condition fails because they are given logical AND. 

So, we are not entering the “if” condition and come out of the loop. The count and “i” values remain “0”. 

For(j = -2; j <= 3; j++)   → Condition TRUE then enters into the loop. 

if((j >= 0) && (i++))   → Condition fails because they are given logical AND. 

So, we are not entering the “if” condition and come out of the loop. The count and “i” values remain “0”. 

For(j = -1; j <= 3; j++)   → Condition TRUE then enters into the loop. 

if((j >= 0) && (i++))   → Condition fails because they are given logical AND. 

So, we are not entering the “if” condition and come out of the loop. The count and “i” values remain “0”.

For(j = 0; j <= 3; j++)   → Condition TRUE then enters into the loop. 

if((j >= 0) && (i++))   → Condition TRUE then enters into the loop. 

count=0+0 → Count=0

For(j = 1; j <= 3; j++)   → Condition TRUE then enters into the loop. 

if((j >= 0) && (i++))   → Condition TRUE then enters into the loop. 

count=0+1 → Count=1

For(j = 2; j <= 3; j++)   → Condition TRUE then enters into the loop. 

if((j >= 0) && (i++))   → Condition TRUE then enters into the loop. 

count=1+2 → Count=3

For(j = 3; j <= 3; j++)   → Condition TRUE then enters into the loop. 

if((j >= 0) && (i++))   → Condition TRUE then enters into the loop. 

count=3+3 → Count=6

 

For(j = 4; j <= 3; j++)   → Condition FALSE we are not entering the loop. 

count=6+4 → We are given a condition as a post increment. So, “i” updates the next instruction. 

The above code segment executes successfully and will print value=10.

Question 2
Consider the following ANSI C function:
int SimpleFunction (int Y[], int n, int x)
{
     int total = Y[0], loopIndex;
     for (loopIndex = 1; loopIndex <= n - 1; loopIndex++)
                   total = x * total + Y[loopIndex]
     return total;
}
Let Z be an array of 10 elements with Z[i]=1, for all i such that 0 ≤ i ≤ 9. The value returned by SimpleFunction(Z, 10, 2) is _______
A
1023
Question 2 Explanation: 
Array Z consists of 10 elements and that element's values are 1.
n=10,x=2
Initial total value is 1 => total=1.
For loop will execute 9 times.
loopindex=1, 1<=9 condition is true then
total = x * total + Y[loopIndex]= 2*1+Y[1]=2+1=3
loopindex=2, 2<=9 condition is true then
total=2*3+Y[2]=6+1=7
loopindex=3, 3<=9 condition is true then
total=2*7+Y[3]=14+1 =15
loopindex=4, 4<=9 condition is true then
total= 2*15+Y[4]=30+1=31
loopindex=5, 5<=9 condition is true then
total=2*31+Y[5]=62+1=63
loopindex=6, 6<=9 condition is true then
total=2*63+Y[6]=126+1=127
loopindex=7, 7<=9 condition is true then
Total =2*127+Y[7]=254+1=255
loopindex=8, 8<=9 condition is true then
total=2*255+Y[8]=510+1=511
loopindex=9, 9<=9 condition is true then
total=2*511+Y[9]=1022+1=1023
loopindex=10, 10<=9 condition is false then
Total value is returned which is 1023.
You can also write generalized formulae 210-1=1023
Question 3

The following is an incomplete Pascal function to convert a given decimal integer (in the range -8 to +7) into a binary integer in 2’s complement representation. Determine the expression A, B, C that complete program.

 function TWOSCOMP (N:integer):integer;
 var
 RAM, EXPONENT:integer;
 BINARY :integer;
 begin
 if(N>=-8) and (N<=+7) then
     begin
 if N<0 then
     N : = A;
 BINARY:=0;
 EXPONENT:=1;
 while N<>0 do
     begin
       REM:=N mod 2;
       BINARY:=BINARY + B*EXPONENT;
       EXPONENT:=EXPONENT*10;
       N := C
     end
 TWOSCOMP:=BINARY
 end
 end; 
A
Theory Explanation.
Question 4

(a) Using the scope rules of Pascal determine the declaration that apply to each occurrence of the names A and B in the following program segment.

 procedure T(U, V, X, Y: integer);
 var
    A: record
        A, B : integer
    end; 
    B: record
        B, A : integer
    end;
 begin
    with A do
        begin
          A:=4;
          B:=V
 end;
 with B do
    begin
         A:=X;
         B:=Y
    end
 end; 

(b) Find the lexical errors in the following Pascal statement:

     if A > 1, then B = 2.5A else read (C);  
A
Theory Explanation.
Question 5

Consider the following high level program segment. Give the contents of the memory locations for variables W, X, Y and Z after the execution of the program segment. The values of the variables A and B are 5 CH and 92H, respectively. Also indicate error conditions if any.

 var
     A, B, W, X, Y   :unsigned byte;
     Z               :unsigned integer, (each integer is represented by two bytes)
 begin
     X               :=A+B
     Y               :=abs(bA-b);
     W               :=A-B
     Z               :=A*B
 End;  
A
Theory Explanation.
Question 6

(a) Consider the following Pascal function where A and B are non-zero positive integers. What is the value of GET(3,2)?

 function GET(A,B:integer);integer;
 begin
          if B = 0 then
    GET:=1
 else if A < B then
    GET:=0
 else
    GET:=GET(A-1,B)+GET(A-1,B-1)
 end ; 

(b) The Pascal procedure given for computing the transpose of an N × N (N>1) matrix A of integers has an error. Find the error and correct it.
Assume that the following declaration are made in the main program

 const
     MAXSIZE=20;
 type
     INTARR=array [1.,MAXSIZE,1..MAXSIZE] of integer;
 Procedure TRANSPOSE (var A: INTARR; N : integer);
 var
     I, J, TMP, integer;
 begin
     for I:=1 to NO – 1 do
     for J:=1 to N do
     begin
          TMP: = A[I,J];
          A[I,J]:=A[J,I];
          A(J,I):=TMP
     end
 end; 
A
Theory Explanation.
Question 7

A variant record in Pascal is defined by

                    type varirec      =   record
                                          number : integer;
                                          case (var1,var2) of
                                          var1: (x,y : integer);
                                          var2: (p.q.: real)
                        end
                    end   

Suppose an array of 100 records was declared on a machine which uses 4 bytes for an integer and 8 bytes for a real. How much space would the compiler have to reserve for the array?

A
2800
B
2400
C
2000
D
1200
Question 7 Explanation: 
Note: Out of syllabus.
Question 8

What is the value of X printed by the following program?

 program COMPUTE (input, output);
    var
                  X:integer;
    procedure FIND (X:real);
                  begin
                  X:=sqrt(X);
                  end;
   begin
                  X:=2
                  Find(X)
                  Writeln(X)
   end 
A
2
B
√2
C
Run time error
D
None of the above
Question 8 Explanation: 
Since nothing is said in question. So we will assume by default call by value.
X in the procedure FIND is a local variable. No change will be reflected in global variable X.
Question 9

Assume that X and Y are non-zero positive integers. What does the following Pascal program segment do?

 while X <>Y do
 if X > Y then
    X := X – Y
 else
    Y := Y – X;
 write(X); 
A
Computes the LCM of two numbers
B
Divides the larger number by the smaller number
C
Computes the GCD of two numbers
D
None of the above
Question 9 Explanation: 
Let X=3 and Y=5
1st pass : X=3 and Y=2
2nd pass : X=1 and Y=2
3rd pass : X=1 and Y=1
Write(X), which writes 1. Which is nothing but GCD of 3 & 5.
Question 10

Which of the following strings can definitely be said to be tokens without looking at the next input character while compiling a Pascal program?

 I. begin           II. program           III. <>   
A
I
B
II
C
III
D
All of the above
Question 10 Explanation: 
Note: Out of syllabus.
Question 11

In the following Pascal program segment, what is the value of X after the execution of the program segment?
X:=-10; Y:=20;
If X > Y then if X < 0 then X:=abs(X) else X:=2*X;

A
10
B
-20
C
-10
D
None
Question 11 Explanation: 
X is remains unchanged. As the if condition is becomes false.
X = -10
Question 12
Consider the following C functions.
int tob (int b, int* arr) {
int i;
for (i=0; b>0; i++) {
if (b%2) arr [i] = 1;
else arr [i] = 0;
b = b/2;
}
return (i);
}

int pp (int a, int b) {
int arr [20];
int i, tot = 1, ex, len;
ex = a;
len = tob (b,arr);
for (i=0; i if (arr[i] == 1)
tot = tot * ex;
ex = ex * ex;
}
return (tot);
}
The value returned by pp(3,4) is ________.
A
81
Question 12 Explanation: 
pp(3,4) ⇒
a=3,b=4
tot=1
ex=a=3
len=tob(b,arr) which is 3
[
tob(4,arr)==>
b=4
b%2 =4%2=0 Condition is false then arr[0]=0
=> b=b/2 =4/2 =2
b=2
b%2 =2%2=0 condition is false then arr[1]=0
=>b=b/2=2/2=1
b=1
then b%2=1%2 condition is true then arr[2]=1
=>b=b/2=1/2=0
The i value is 3 [length is 3]
]
i=0,
arr[0] ==1 condition is false
ex=3*3=9
i=1
arr[1]==1 condition is false
then
ex=9*9=81
i=2
then arr[2]==1 condition is true
tot=tot*ex=1*81=81
ex=81*81
Finally it returns tot value which 81.
Question 13

Consider the following C functions.

  int fun1 (int n)  {                              int fun2 (int n)  {
    static int i = 0;                                static int i = 0;  
    if (n > 0)  {                                    if (n > 0)  { 
       ++i;                                              i = i + fun1 (n);   
       fun1 (n-1);                                       fun2 (n-1);
    }                                                 }
    return (i);                                           return (i);
  }                                                 } 

The return value of fun2 (5) is _______.

A
55
Question 13 Explanation: 
#include
int fun1(int n) {
printf("--fun1 call--\n");
static int i = 0;
if(n>0){
++i;
printf("fun1(%d-1)\n",n);
fun1(n-1);
}
printf("fun1(%d)= %d\n",n, i);
return(i);
}
int fun2(int n) {
printf("\n******* fun2 call ********\n");
static int i = 0;
if(n>0){
printf("%d + fun1(%d)\n", i,n);
i=i+fun1(n);
fun2(n-1);
}
printf("fun2(%d)= %d\n",n, i);
return(i);
}
void main()
{
printf("final = %d\n", fun2(5));
}
Check step by step hand run of the code to understand the recursion:
******* fun2 call ********
0 + fun1(5)
--fun1 call--
fun1(5-1)
--fun1 call--
fun1(4-1)
--fun1 call--
fun1(3-1)
--fun1 call--
fun1(2-1)
--fun1 call--
fun1(1-1)
--fun1 call--
fun1(0)= 5
fun1(1)= 5
fun1(2)= 5
fun1(3)= 5
fun1(4)= 5
fun1(5)= 5
******* fun2 call ********
5 + fun1(4)
--fun1 call--
fun1(4-1)
--fun1 call--
fun1(3-1)
--fun1 call--
fun1(2-1)
--fun1 call--
fun1(1-1)
--fun1 call--
fun1(0)= 9
fun1(1)= 9
fun1(2)= 9
fun1(3)= 9
fun1(4)= 9
******* fun2 call ********
14 + fun1(3)
--fun1 call--
fun1(3-1)
--fun1 call--
fun1(2-1)
--fun1 call--
fun1(1-1)
--fun1 call--
fun1(0)= 12
fun1(1)= 12
fun1(2)= 12
fun1(3)= 12
******* fun2 call ********
26 + fun1(2)
--fun1 call--
fun1(2-1)
--fun1 call--
fun1(1-1)
--fun1 call--
fun1(0)= 14
fun1(1)= 14
fun1(2)= 14
******* fun2 call ********
40 + fun1(1)
--fun1 call--
fun1(1-1)
--fun1 call--
fun1(0)= 15
fun1(1)= 15
******* fun2 call ********
fun2(0)= 55
fun2(1)= 55
fun2(2)= 55
fun2(3)= 55
fun2(4)= 55
fun2(5)= 55
final = 55
Question 14

Given the following Pascal-like program segment

Procedure A;
  x,y: integer;
   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

A
x or A, y, x of B and z in S1 and
x of B, y and i in S2
B
x or B, y and z in S1 and
x of B, i and z in S2
C
x or B, z and y in S1 and
x of A, i and y in S2
D
None of the above
Question 14 Explanation: 
Note: Out of syllabus.
Question 15

What value would the following function return for the input x=95?

                 Function fun (x:integer):integer;
      Begin
                 If x > 100 then fun = x - 10
                 Else fun = fun(fun(x + 11))
      End;  
A
89
B
90
C
91
D
92
Question 15 Explanation: 
Value returned by
fun(95) = fun(fun(106))
= fun(96)
= fun(fun(107))
= fun(97)
= fun(fun(108))
= fun(98)
= fun(fun(109))
= fun(99)
= fun(110)
= fun(100)
= fun(fun(111))
= fun(101)
= 91
Question 16

What is the result of the following program?

program side-effect (input, output);
    var x, result: integer;
    function f (var x:integer):integer;
    begin
        x:x+1;f:=x;
    end;
begin
    x:=5;
    result:=f(x)*f(x);
    writeln(result);
end; 
A
5
B
25
C
36
D
42
Question 16 Explanation: 
If it is call by reference then answer is 42.
If it is call by value then answer is 36.
Question 17

Consider the following pascal program skeleton:

program sort(...);
                        var a,x,...;
                        procedure readarray;
                              var i,....;
                              begin
                                             ...:=a...
                              end;
      procedure exchange(...);
                       begin
                                             ...:=a...
                                             ...:=x...
                       end;
      procedure qsort(...);
                       var k,v,...;
                       function partition (...)...;
                              var i,j,...;
                              begin
                              ...:=a...
                              ...:=v...
                              end;
                       begin
                              .
                              .
                       end;
      begin
                       .
                       .
      end; 

Assume that at a given point in time during program execution, following procedures are active: sort, qsort(1,9), qsort(1.3), partition(1,3), exchange(1,3).

Show snapshots of the runtime stack with access links after each of the activations.

A
Theory Explanation.
Question 18

What will be the output of the following program assuming that parameter passing is

    (i) call by value
    (ii) call by reference
    (iii) call by copy restore
      procedure P{x, y, z};
                        begin y:y+1; z: x+x end; 
      begin
                        a:= b:= 3;
                        P(a+b, a, a);
                        Print(a) 
      end. 
A
Theory Explanation.
Question 19

Suppose we have a function HALTS which when applied to any arbitrary function f and its arguments will say TRUE if function f terminates for those arguments and FALSE otherwise. Example, Given the following function definition.
FACTORIAL (N) = IF(N=0) THEN 1 ELSE N*FACTORIAL (N-1)
Then HALTS(FACTORIAL 4) = TRUE and HATS(FACTORIAL - 5) = FALSE

Let us define the function FUNNY(f) = IF HALTS(ff) THEN not(ff) ELSE TRUE
(a) Show that FUNNY terminates for all functions f.
(b) Use (a) to prove (by contradiction) that it is not possible to have a function like HALTS which for arbitrary functions and inputs says whether it will terminate on that input or not.

A
Theory Explanation.
Question 20

Consider the following C function definition.

int Trial (int a, int b, int c)
{
    if ((a >= b) && (c < b) return b;
    else if (a >= b) return Trial(a, c, b);
    else return Trial(b, a, c);
} 

The function Trial:

A
Finds the maximum of a, b, and c
B
Finds the minimum of a, b and c
C
Finds the middle number of a, b, c
D
None of the above
Question 20 Explanation: 
Try for (3,2,2), it will go for infinite loop.
Question 21

Given the programming constructs (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.

A
(ii), (iii), (iv)
B
(v), (vii), (viii)
C
(vi), (vii), (viii)
D
(iii), (vii), (viii)
Question 21 Explanation: 
Arbitrary goto, recursive call and repeat loop may enter infinite loop and the program might never terminate.
Question 22

Consider the following program is pseudo-Pascal syntax.

    program main;
       var x: integer;
       procedure Q [z:integer);
       begin
          z: z + x;
          writeln(z)
       end;
    procedure P (y:integer);
          var x: integer;
       begin
          x: y + 2;
          Q(x);
          writeln(x)
       end;
       begin 
          x:=5;
          P(x);
          Q(x);
          writeln(x)
       end. 

What is the output of the program, when
(a) The parameter passing mechanism is call-by-value and the scope rule is static scooping?

(b) The parameter passing mechanism is call-by-reference and the scope rule is dynamic scooping?

A
Theory Explanation is given below.
Question 22 Explanation: 
Note: Out of syllabus.
Question 23

The value of j at the end of the execution of the following C program.

int incr(int i)
{
   static int count = 0;
   count = count + i;
   return (count);
}
main()
{
   int i,j;
   for (i = 0; i <= 4; i++)
      j = incr(i);
} 

is

A
10
B
4
C
6
D
7
Question 23 Explanation: 
At i=0; count=0
i=1; count=1
i=2; count=3
i=3; count=6
i=4; count=10
It return count value is 10.
Question 24

The following C declarations

struct node
{
   int i;
   float j;
};
struct node *s[10]; 

define s to be

A
An array, each element of which is a pointer to a structure of type node
B
A structure of 2 fields, each field being a pointer to an array of 10 elements
C
A structure of 3 fields: an integer, a float, and an array of 10 elements
D
An array, each element of which is a structure of type node
Question 24 Explanation: 
The given code represents an array of s[ ], in this each element is a pointer to structure of type node.
Question 25

The most appropriate matching for the following pairs

    X: m=malloc(5); m= NULL;        1: using dangling pointers
    Y: free(n); n->value=5;         2: using uninitialized pointers
    Z: char *p; *p = ’a’;           3. lost memory  

is:

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
Question 25 Explanation: 
X → m = NULL will results the loss of memory.
Y → n is pointer to invalid memory, a making it as a dangling pointer.
Z → p is not initialized.
p = malloc (size of(char))p = malloc (size of(char)); should have been used before assigning 'aa' to ∗p.
Question 26

Aliasing in the context of programming languages refers to

A
multiple variables having the same memory location
B
multiple variables having the same value
C
multiple variables having the same identifier
D
multiple uses of the same variable
Question 26 Explanation: 
In computer programming, aliasing refers to the situation where the same memory location can be accessed using different names.
Question 27

Consider the following C declaration

struct {
    short s [5]
    union {
    float y;
    long z;
    } u;
} t; 

Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is

A
22 bytes
B
14 bytes
C
18 bytes
D
10 bytes
Question 27 Explanation: 
short [5] = 5×2 = 10
max[float, long] = max [4, 8] = 8
Total = short[5] + max[float,long] = 10 + 8 = 18
Question 28

Consider the following C program:

           void abc(char*s)
           {
              if(s[0]==’\0’)return;
              abc(s+1);
              abc(s+1);
              printf(“%c”,s[0]);
           }
           main()
           {  abc(“123”)
           }  

(a) What will be the output of the program?
(b) If abc(s) is called with a null-terminated string s of length n characters (not counting the null (‘\0’) character), how many characters will be printed by abc(s)?

A
Theory Explanation is given below.
Question 29

Consider the following program

                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               //beginP2 
                n=10; 
                D; 
            end  

If the language has dynamic scoping and parameters are passed by reference, what will be printed by the program?

A
10
B
11
C
3
D
None of the above
Question 29 Explanation: 
n=3
W(n)=W(3)
Procedure W(var x; int)
begin
x = x+1 = 3+1 = 4
Print x → Print x=4
end
Question 30

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;
}
func1(x,y,z)
{
   y = y+4;
   z = x+y+z;
} 
A
10, 3
B
31, 3
C
27, 7
D
None of the above
Question 30 Explanation: 
Here, variable x of func1 points to address of variable y.
And variable y and z of func1 points to address of variable x.
Therefore, y = y+4 ⇒ y = 10+4 = 14
and z = x+y+z ⇒ z = 14+14+3 = 31
z will be stored back in k.
Hence, x=31 and y will remain as it is (y=3).
Hence, answer is (B).
Question 31

In the C language

A
At most one activation record exists between the current activation record and the activation record for the main
B
The number of activation records between the current activation record and the activation record for the main depends on the actual function calling sequence.
C
The visibility of global variables depends on the actual function calling sequence.
D
Recursion requires the activation record for the recursive function to be saved on a different stack before the recursive fraction can be called.
Question 31 Explanation: 
In C Language a function can create activation record from the created function stack.
Question 32

In the following C program fragment, j, k n and TwoLog_n are interger variables, and A is an array of integers. The variable n is initialized to an integer ≥3, and TwoLog_n is initialized to the value of 2*⌈log2(n)⌉

   for (k = 3; k < = n; k++)
               A[k] = 0;
   for (k = 2; k < = TwoLog_n; k++)
          for (j = k + 1; j < = n; j++)
                    A[j] = A[j] || (j%k);
   for (j = 3; j < = n; j++)
          if (!A[j]) printf("%d", j); 

The set of numbers printed by this program fragment is

A
{m|m ≤ n, (∃i)[m=i!]}
B
{m|m ≤ n, (∃i)[m=i2]}
C
{m|m ≤ n, m is prime}
D
{ }
Question 32 Explanation: 
Take n=4, so Two log_n=4
Now Trace the code,
for (k=3; k<=n; k++)
A[k]=0; // A[3]=0
A[4]=0
for (k=2; k<=Two log_n; k++)
for(j=k+1; j<=n; j++)
A[j] = A[j] // (j%k); // A[3] = 0 // I=1
A[4] = 0 // I=1
for (j=3; j<=n; j++)
if (!A[j]) printf("%d", j);
// if (!1) means if (0), so printf will never execute
Hence, Option (D) is the answer.
Question 33

Consider the C program shown below.

#include 
#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

A
12 7 6
B
22 12 11
C
14 6 6
D
7 6 6
Question 33 Explanation: 
In main function x=5; it is global array
p(&x) it goes to P( ) function
y=5
x=5+2=7;
Q(x)
z=7
z=7+5=12(Print+z→I)
comes to P( )
*y=7-1=6
x=7(Print x→II)
comes to main ( ),
print x=*y=6 (print x→III)
Output: 12 7 6
Question 34

Consider the following class definitions in a hypothetical Object Oriented language that supports inheritance and uses dynamic binding. The language should not be assumed to be either Java or C++, though the syntax is similar.

Class P {
    void f(int i) {
        print(i);
    }
}

Class Q subclass of P {
    void f(int i) {
        print(2*i);
    }
} 

Now consider the following program fragment:

Px = new Q();
Qy = new Q();
Pz = new Q();
x.f(1); ((P)y).f(1); z.f(1); 

Here ((P)y) denotes a typecast of y to P. The output produced by executing the above program fragment will be

A
1 2 1
B
2 1 1
C
2 1 2
D
2 2 2
Question 34 Explanation: 
Because of using dynamic binding it results a values such as 2 2 2.
Note: The given question is not in the present syllabus
Question 35

Consider the following logic program P

A(x) <- B(x, y), C(y) 
     <- B(x,x) 

Which of the following first order sentences is equivalent to P?

A
(∀x) [(∃y) [B(x,y) ∧ C(y)] ⇒ A(x)] ∧ ¬(∃x)[B(x,x)]
B
(∀x) [(∀y) [B(x,y) ∧ C(y)] ⇒ A(x)] ∧ ¬(∃x)[B(x,x)]
C
(∀x) [(∃y) [B(x,y) ∧ C(y)] ⇒ A(x)] ∨ ¬(∃x)[B(x,x)]
D
(∀x) [(∀y) [B(x,y) ∧ C(y)] ⇒ A(x)] ∧ (∃x)[B(x,x)]
Question 35 Explanation: 
Note: This is not in gate syllabus. Please ignore this question.
Question 36

The following program fragment is written in a programming language that allows variables and does not allow nested declarations of functions.

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 static scoping and call by need parameter passing mechanism, the values printed by the above program are

A
115, 220
B
25, 220
C
25, 15
D
115, 105
Question 36 Explanation: 
P(i+j)
P(100+5) = P(105)
→void P(105)
{   
int i=10;   
print (x+10);  ⇒ 105+10=115 prints
  i=200;  
  j = 20;  
  print (x);  ⇒ x=105 prints
}  
115, 105 prints
Question 37

The following program fragment is written in a programming language that allows variables and does not allow nested declarations of functions.

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 are:

A
115, 220
B
25, 220
C
25, 15
D
115, 105
Question 37 Explanation: 
In dynamic,
In void P(x)
{  int i = 10;
   print(x + 10);   ⇒ 105+10 = 115 prints
 
print (x);   ⇒ print x=220;
Question 38

Choose the best matching between the programming styles in Group 1 and their characteristics in Group 2.

      Group-1		        Group-2 
 P. Functional	      1. Command-based, procedural
 Q. Logic	      2. Imperative, abstract data type
 R. Object-oriented   3. Side-effect free, declarative, expression evaluation
 S. Imperative	      4. Declarative, clausal representation, theorem proving 
A
P - 2, Q - 3, R - 4, S - 1
B
P - 4, Q - 3, R - 2, S - 1
C
P - 3, Q - 4, R - 1, S - 2
D
P - 3, Q - 4, R - 2, S - 1
Question 38 Explanation: 
P) Functional programming is declarative in nature, involves expression evaluation and side effect free.
Q) Logic is also declarative but involves theorem proving.
R) Object oriented is imperative statement based and have abstract data types.
S) Imperative programs are made giving commands and follow definite procedure.
Question 39

Consider the following C program

     main()
     {   int x, y, m, n;
         scanf ("%d %d", &x, &y);
         /* Assume x > 0 and y > 0  */
         m = x; n = y;
         while (m! = n)
            {    if (m > n)
                   m = m - n;
                 else
                   n = n - m;
            } 
          print f ("% d", n);
          }

The program computes

A
x + y using repeated subtraction
B
x mod y using repeated subtraction
C
the greatest common divisor of x and y
D
the least common multiple of x and y
Question 39 Explanation: 
Given code is same as Euclid's Algorithm for finding Greatest Common Divisor(GCD).
Question 40

It is desired to design an object-oriented employee record system for a company. Each employee has a name, unique id and salary. Employees belong to different categories and their salary is determined by their category. The functions to get Name, getld and compute salary are required. Given the class hierarchy below, possible locations for these functions are:

    (i) getld is implemented in the superclass
    (ii) getld is implemented in the subclass
    (iii) getName is an abstract function in the superclass
    (iv) getName is implemented in the superclass
    (v) getName is implemented in the subclass
    (vi) getSalary is an abstract function in the superclass
    (vii) getSalary is implemented in the superclass
    (viii) getSalary is implemented in the subclass

Choose the best design

A
(i), (iv), (vi), (viii)
B
(i), (iv), (vii)
C
(i), (iii), (v), (vi), (viii)
D
(ii), (v), (viii)
Question 40 Explanation: 
Name and id are a property of every employee and independent of their category. So these should be implemented in superclass. Every employee has a salary but this is determined by their category. So getsalary must be abstract function in superclass and implemented in subclass.
Question 41

Consider the following C function:

     int f(int n)
     {
        static int i = 1;
        if (n >= 5)
           return n;
        n = n+i;
        i++;
        return f(n);
     } 

The value returned by f(1) is

A
5
B
6
C
7
D
8
Question 41 Explanation: 

The value return by f(1) = 7
Question 42

Consider the following program fragment for reversing the digits in a given integer to obtain a new integer. Let n = d1d2…dm.

     int n, rev;
     rev = 0;
     while (n > 0)
     {
        rev = rev*10 + n%10;
        n = n/10;
     }

The loop invariant condition at the end of the ith iteration is:

A
n = d1d2…dm-i and rev = dmdm-1…dm-i+1
B
n = dm-i+1…dm-1dm or rev = dm-i…d2d1
C
n ≠ rev
D
n = d1d2…dm and rev = dm…d2d1
Question 42 Explanation: 
In each iteration the right most digit of n is going to make to the right end of the reverse.
Question 43

Consider the following C program segment:

     char p[20];
     char *s = "string";
     int length = strlen(s);
     int i;
     for (i = 0; i < length; i++)
          p[i] = s[length — i];
     printf("%s",p); 

The output of the program is

A
gnirts
B
string
C
gnirt
D
no output is printed
Question 43 Explanation: 
Every string is to be end with '\0'.
P[0] = S[7-1] = S[6] = \0.
In P[ ], the first character is '\0'. Then it will results a empty string. If P[0] become '\0', then it doesn't consider about next values in sequence.
Question 44

The goal of structured programming is to:

A
have well indented programs
B
be able to infer the flow of control from the compiled code
C
be able to infer the flow of control from the program text
D
avoid the use of GOTO statements
Question 44 Explanation: 
Structured programming: Which is aimed at improving the clarity, quality and development time of a computer programming by making extensive use of the structured control flow constructs of selection and repetition of block structures and subroutines in contrast to using simple tests and jumps such as goto statements.
Question 45

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.

A
call swap (x, y)
B
call swap (&x, &y)
C
swap (x,y) cannot be used as it does not return any value
D
swap (x,y) cannot be used as the parameters are passed by value
Question 45 Explanation: 
Option A:
Here parameters passed by value in C then there is no change in the values.
Option B:
Here values are not swap.
Here parameters are passed by address in C.
Option C:
It is false. Return value is not valid for exchanging the variables.
Option D:
It is correct.
We cannot use swap(x,y) because parameters are passed by value.
Only exchanging the values (or) variables are passing their address and then modify the content with the help of dereferencing operator(*).
Question 46

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?

A
8, 4, 0, 2, 14
B
8, 4, 0, 2, 0
C
2, 0, 4, 8, 14
D
2, 0, 4, 8, 0
Question 46 Explanation: 
Int a=2048, Sum=0
⇒ foo (a, sum) = foo (2048,0)
⇒ n == 2048
⇒ k = n%10 = 2048%10 = 8
⇒ j = n/10 = 2048/10 = 204
Sum = Sum+k = 0+8 = 8
foo(j, sum) = foo(204, 8)
⇒ n=204
k = n%10 = 204%10 = 4
j = n/10 = 204/10 = 20
sum = sum+k = 12+0 = 12
foo(j, sum) =foo(2,12)
⇒ n=2
k = n%10 = 2%10 = 2
j = n/10 = 2/10 = 0
sum = 14
foo(0,14) ⇒ n==0
printf("%d", k) ⇒ k = 2, 0, 4, 8
After foo( ) statement one more printf statement is there then if print 0 after all digits of n.
2, 0, 4, 8, 0.
Question 47

Consider the following C-program:

double foo (double);    /* Line 1 */
 
int main() {
  double da, db;
 
  // input da
 
  db = foo(da);
}
 
double foo(double a) {
  return a;
} 

The above code compiled without any error or warning. If Line 1 is deleted, the above code will show:

A
no compile warning or error
B
some compiler-warnings not leading to unintended results
C
some compiler-warnings due to type-mismatch eventually leading to unintended results
D
compiler errors
Question 47 Explanation: 
When a function is called before its declaration then it leads to compiler error.
Question 48

Which one of the following are essential features of an object-oriented programming language?

    (i) Abstraction and encapsulation
    (ii) Strictly-typedness
    (iii) Type-safe property coupled with sub-type rule
    (iv) Polymorphism in the presence of inheritance
A
(i) and (ii) only
B
(i) and (iv) only
C
(i), (ii) and (iv) only
D
(i), (iii) and (iv) only
Question 48 Explanation: 
Abstraction, encapsulation and inheritance are three main features of OOP language.
Question 49

What does the following C-statement declare?

    int ( * f) (int * ) ; 
A
A function that takes an integer pointer as argument and returns an integer.
B
A function that takes an integer as argument and returns an integer pointer.
C
A pointer to a function that takes an integer pointer as argument and returns an integer.
D
A function that takes an integer pointer as argument and returns a function pointer.
Question 49 Explanation: 
int ( * f) (int * )
→ A pointer to a function which takes integer as a pointer and return an integer value.
Question 50

A common property of logic programming languages and functional languages is:

A
both are procedural languages
B
both are based on λ-calculus
C
both are declarative
D
both use Horn-clauses
Question 50 Explanation: 
Functional and logic programming languages are also called declarative languages; programs in these languages are said to describe (declaratively) what to do and not (operationally) how to do it.
There are 50 questions to complete.

Access subject wise (1000+) question and answers by becoming as a solutions adda PRO SUBSCRIBER with Ad-Free content

Register Now