Programming

Question 1
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 1 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 2
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 2 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 3

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 3 Explanation: 
X is remains unchanged. As the if condition is becomes false.
X = -10
Question 4

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 4 Explanation: 
Note: Out of syllabus.
Question 5

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 5 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 6

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 6 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 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

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 9

(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 10

(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 11

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 12

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 12 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 13
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 13 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 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

Match the pairs in the following questions:

(a) Small talk      (p) Logic programming
(b) LISP            (q) Data flow programming
(c) Prolog          (r) Functional programming
(d) VAL             (s) Object-oriented programming
A
(a) - (s), (b) - (r), (c) - (p), (d) - (q)
Question 15 Explanation: 
Note: Out of syllabus.
Question 16

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 16 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 17

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 17 Explanation: 
If it is call by reference then answer is 42.
If it is call by value then answer is 36.
Question 18

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 18 Explanation: 
Arbitrary goto, recursive call and repeat loop may enter infinite loop and the program might never terminate.
Question 19

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 19 Explanation: 
Try for (3,2,2), it will go for infinite loop.
Question 20

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 21

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 22

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 23

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 23 Explanation: 
The given code represents an array of s[ ], in this each element is a pointer to structure of type node.
Question 24

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 24 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 25

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 25 Explanation: 
In computer programming, aliasing refers to the situation where the same memory location can be accessed using different names.
Question 26

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 26 Explanation: 
short [5] = 5×2 = 10
max[float, long] = max [4, 8] = 8
Total = short[5] + max[float,long] = 10 + 8 = 18
Question 27

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 27 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 28

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 28 Explanation: 
Note: Out of syllabus.
Question 29

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 29 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 30

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 30 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 31

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 32

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 32 Explanation: 
In C Language a function can create activation record from the created function stack.
Question 33

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 33 Explanation: 
Note: This is not in gate syllabus. Please ignore this question.
Question 34

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 34 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 35

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 35 Explanation: 
In dynamic,
In void P(x)
{  int i = 10;
   print(x + 10);   ⇒ 105+10 = 115 prints
 
print (x);   ⇒ print x=220;
Question 36

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 36 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 37

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 37 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 38

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 38 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 39

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 39 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 40

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 40 Explanation: 

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

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 41 Explanation: 
In each iteration the right most digit of n is going to make to the right end of the reverse.
Question 42

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 42 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 43

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 43 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 44

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 44 Explanation: 
Given code is same as Euclid's Algorithm for finding Greatest Common Divisor(GCD).
Question 45

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 45 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 46

What function of x, n is computed by this program?

 Function what (x, n:integer): integer:
 Var
     value : integer;
     begin
     value:=1
     if n>0 then
 begin
     if n mod 2 = 1 then
     value:=value*x;
     value:=value*what(x*x, n div 2);
     end;
     what:value
     end; 
A
Theory Explanation.
Question 47

Consider the program below:

 Program main;
    var r:integer;
    procedure two;
    begin write (r) end;
    procedure one;
    var r:integer;
    begin r:=5 two; end
    begin r:=2;
    two; one; two;
    end. 

What is printed by the above program if
(i) Static scoping is assumed for all variables;
(ii) Dynamic scoping is assumed for all variables.
Give reasons for your answer.

A
Theory Explanation.
Question 48

An array a contains n integers in non-decreasing order, A[1] ≤ A[2] ≤ ... ≤ A[n]. Describe, using Pascal like pseudo code, a linear time algorithm to find i, j, such that A[i] + A[j] = a given integer M, if such i, j exist.

A
Theory Explanation.
Question 49

(a) Draw a precedence graph for the following sequential code. The statements are numbered from S1 to S6

 S1       read n
 S2       i:=1
 S3       if i>n goto next
 S4       a(i):=i+1
 S5       i:=i+1
 S6       next : Write a(i) 

(b) Can this graph be converted to a concurrent program using parbegin-parend construct only?

A
Theory Explanation.
Question 50
Program PARAM (input, output);
 var m, n : integer;
 procedure P (var, x, y : integer);
    var m : integer;
    begin
      m : = 1;
      x : = y + 1
    end;
 procedure Q (x:integer; vary : integer);
    begin
      x:=y+1;
    end;
 begin
    m:=0; P(m,m); write (m);
    n:=0; Q(n*1,n); write (n)
 end 

The value of m, output by the program PARAM is:

A
1, because m is a local variable in P
B
0, because m is the actual parameter that corresponds to the formal parameter in p
C
0, because both x and y are just reference to m, and y has the value 0
D
1, because both x and y are just references to m which gets modified in procedure P
E
none of the above
Question 50 Explanation: 
0, because global m is not modified, m is just passed to formal argument of P.
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