Programming
Question 1 
for (int k=0; k<20; k=k+2)
{
if (k % 3 == 1)
system.out.print(k+ " ")
}
What is printed as a result of executing the code segment?
4 16  
4 10 16  
0 6 12 18  
1 4 7 10 13 16 19 
k = 0 % 3 = 0
k = 2 % 3 = 2
k = 4 % 3 = 1 // prints 4
k = 6 % 3 = 0
k = 8 % 3 = 2
k = 10 % 3 = 1 // prints 10
k = 12 % 3 = 0
k = 14 % 3 = 2
k = 16 % 3 = 1 // prints 16
k = 18 % 3 = 0
So, Output is 4 10 16
Question 2 
function F(K : integer)
integer;
begin
if (k<3) then F:=k
else F:=F(k1)*F(k2)+F(k3)
end;
5  
6  
7  
8 
F(4) = F(3)*F(2)+F(1) = 5
F(3) = F(2)*F(1)+F(0) = 2
F(2) = 2
F(1) = 1
F(0) = 0
Question 3 
the values are displayed right justified  
the values are displayed centered  
the values are displayed left justified  
the values are displayed as negative numbers 
Controlling integer width with printf
The %3d specifier is used with integers, and means a minimum width of three spaces, which, by default, will be rightjustified:
Leftjustifying printf integer output
To leftjustify integer output with printf, just add a minus sign (  ) after the % symbol, like this:
The printf integer zerofill option
To zerofill your printf integer output, just add a zero (0) after the % symbol, like this:
Question 4 
subroutine swap(ix,iy)
it = ix
L1 : ix = iy
L2 : iy = it
end
ia = 3
ib = 8
call swap (ia, ib+5)
print *, ia, ib
end
S1: The compiler will generate code to allocate a temporary nameless cell, initialize it to 13, and pass the address of the cell to swap
S2: On execution the code will generate a runtime error on line L1
S3: On execution the code will generate a runtime error on line L2
S4: The program will print 13 and 8
S5: The program will print 13 and 2
Exactly the following set of statement(s) is correct:
S1 and S2  
S1 and S4  
S3  
S1 and S5 
Swap (8, 13)
⇒ ia will returns value with 13.
⇒ ib is unchanged, because here we using pass by reference value.
➝ Temporary nameless is initialized to 13.
➝ There is No runtime error.
Question 5 
7  
8  
7.4  
7.0 
But here the argument is negative value.
Floor of 7.4 will return the lower limit, i.e. 8.
Question 6 
x:=1;
i:=1;
while (x ≤ 500)
begin
x:=2x ;
i:=i+1;
end
What is the value of i at the end of the pseudocode?
4  
5  
6  
7 
After completion of second iteration x and i values are : x = 4 and i = 3
After completion of third iteration x and i values are : x = 16 and i = 4
After completion of fourth iteration x and i values are : x =256 and i = 5
After completion of fifth iteration x and i values are : x = 65536 and i = 6(Condition is false)
Then the value of “i” is 5
Question 7 
What is the output of the following C code?
#include
int main()
{
int index;
for(index=1; index<=5; index++)
{
printf("%d", index);
if (index==3)
continue;
}
}
1245  
12345  
12245  
12354 
In the given code, there are no statements after continue statement , So it won’t effect on the output.
The loop will executes for five iterations,For each iteration it will print corresponding value i.e; 12345.
Question 8 
p = getnode()
info (p) = 10
next (p) = list
list = p
result in which type of operation?
pop operation in stack  
removal of a node  
inserting a node  
modifying an existing node 
info (p) = 10 // Storing the value of 10 into the info field of new node
next (p) = list // adding new node to the existing list.
list=p // the starting address of the list will
point to the new node
Question 9 
#include
#include
void main()
{
double pi = 3.1415926535;
int a = 1;
int i;
for(i=0; i < 3; i++)
if(a = cos(pi * i/2) )
printf("%d ",1);
else printf("%d ", 0);
}
What would the program print?
000  
010  
101  
111 
→For a given i = 0:
a = cos(pi * 0/2) [ PI*0 is 0]
a = cos(0) = 1, if condition true and it will print 1
→For a given i = 1
a = cos (pi/2) [pi*1 is pi]
a=cos(1.57075) whose value approximately equal to zero
a = 0, Here the condition is false then else part will execute and it will print 0
→For i = 2
a = cos(pi) [pi*2/2 is nothing but pi]
a = 1, ,Here also condition is true and it will execute if part and it will print “1”
→Finally it will print 101.
Question 10 
Class Test
{
public static void main (String [] args)
{
int x = 0;
int y = 0;
for (int z = 0; z < 5; z++)
{
if((++x > 2)  (++y > 2))
{
x++;
}
}
System.out.println( x + " " + y);
}
}
8 2  
8 5  
8 3  
5 3 
For the given code, the loop will execute 5 times.
Step1: For z = 0,
The following condition will execute
if((++x > 2)  (++y > 2))
Here preincrement operation will be performed on x and y. Then x and y values are 1 and the condition false
Similar operation will be performed for z values 1,2,3 and 4
Step2: For z = 1, Then x and y values become 2 and the condition false.
Step3: For z = 2, “” operator is present in the expression, if first operand is true then no need to check the second operand.x value is 3, if condition true and ++y is not evaluated,again “x” value is incremented then x value is “4”.
Perform the step3 procedure for z value 3 and 4 . Here x value is incremented “4” times. Then the final value of “x” is 8 and “y” is 2.
Question 11 
2  
3  
4  
5 
The “current” binding for a given name is the one encountered most recently during execution
Dynamic scoping :
The scope of bindings is determined at run time not at Compile time .
→ For deep binding, the referencing environment is bundled with the subroutine as a closure and passed as an argument. A subroutine closure contains
– A pointer to the subroutine code
– The current set of nametoobject bindings
→ By considering dynamic scoping with deep binding when add is passed into second the environment is x = 1, y = 3 and the x is the global x so it writes 4 into the global x, which is the one picked up by the write_integer.
Question 12 
Consider the following pseudocode fragment, where m is a nonnegative integer that has been initialized:
p = 0; k = 0; while(k < m) p = p + 2^{k}; k = k + 1; end while
Which of the following is a loop invariant for the while statement?
(Note: a loop variant for a while statement is an assertion that is true each time guard is evaluated during the execution of the while statement).
Options:p = 2^{k}  1 and 0≤k  
p = 2^{k+1}  1 and 0≤k  
p = 2^{k}  1 and 0≤k≤m  
p = 2^{k+1}  1 and 0≤k≤m 
k=1, P = 1+21 = 3
k=2, P = 3+22 = 7
k=3, P = 7+23 = 15
Only the option3 satisfied to the loop variant.
Ex: m=2, The loop will execute for 3 times for k value is 0,1 and 2.
Then options3 gives P=1, P=3 and P=7 for the k values 0,1 and respectively.
Question 13 
Consider the C/C++ function f() given below:
void f(char w []) { int x = strlen(w); //length of a string char c; For (int i = 0; i < x; i++) { c = w[i]; w[i] = w[x  i  1]; w[x  i  1] = c; } }
Which of the following is the purpose of f() ?
It output the content of the array with the characters rearranged so they are no longer recognized a the words in the original phrase.
 
It output the contents of the array with the characters shifted over by one position.
 
It outputs the contents of the array in the original order.
 
It outputs the contents of the array in the reverse order.

Question 14 
Consider the following two C++ programs P_{1} and P_{2} and two statements S_{1} and S_{2} about the programs :
 S_{1}: P_{1} prints out 3
S_{2}: P_{2} prints out 4:2
What can you say about the statement S_{1} and S_{2} ?
Code:Neither S_{1} nor S_{2} is true
 
Only S_{1} is true
 
Only S_{2} is true
 
Both S_{1} and S_{2} are true 
The definition of the function first modifies “i” value by 2 and later by “3” and that modification of the value is automatically reflects in the main program as “c” is reference variable.
The program code P_{2} will gives the output of 4:5 as the function will take reference variable as parameter and return type also reference object.
Question 15 
What does the following java function perform ? (Assume int occupies four bytes of storage)
Public static int f(int a) { //Preconditions : a>0 and no overflow/underflow occurs int b=0; for(int i=0; i<32; i++) { b=b <<1; b= b  (a & 1); a = a >>> 1; // This is a logical shift } Return b; }
Return the int that represents the number of 0’s in the binary representation of integer a.  
Return the int that represents the number of 1’s in the binary representation of integer a.  
Return the int that has the reversed binary representation of integer a.
 
Return the int that has the binary representation of integer a.

The initial value of b is 0 and b<<1 means b will get value of 2
b=b(a&1) which means 2(5&1) then b will get value “2”
a=a<<1 means value a will reduce to 3
→ Repeat the above procedure for 31 iterations.
→ Iteration2: b=b<<1 then b is 4 and b=b(a&1)= 4(3&1) then b is 5 and a becomes 1
→ Iteration3: b=b<<1 then b is 10 and b=b(1&1) = 4(3&1) then b is 11 and a becomes 0
→ And this procedure will repeat until “i” value is 32.
→ The final value is 11 (Number of 1’s in the result is 2) and the number of 1’s in the input 5 are 2.
Question 16 
Consider the following recursive Java function f that takes two long arguments and returns a float value :
public static float f(long m, long n) { float result = (float) m / (float) n; if (m < 0  n < 0) return 0⋅0f; else result += f(m*2, n*3); result result; }
Which of the following integers best approximates the value of f(2,3) ?
0  
3  
1  
2 
Question 17 
Consider the following method :
int f(int m, int n, boolean x, boolean y) { int res=0; if(m<0) {res=nm;} else if(x  y) { res= 1; if( n==m) { res =1; } } else {res=n; } return res; } /*end of f */
If P is the minimum number of tests to achieve full statement coverage for f() and Q is the minimum number of tests to achieve full branch coverage for f(), then (P,Q) =
(3,4)  
(3,2)
 
(2,3)  
(4,3)

→ Statement coverage is a white box test design technique which involves execution of all the executable statements in the source code at least once. It is used to calculate and measure the number of statements in the source code which can be executed given the requirements.
→ Branch coverage is a testing method, which aims to ensure that each one of the possible branch from each decision point is executed at least once and thereby ensuring that all reachable code is executed.
→ In the branch coverage, every outcome from a code module is tested. For example, if the outcomes are binary, you need to test both True and False outcomes.
Question 18 
A function returning a pointer to an array of integers.  
Array of functions returning pointers to integers.  
A function returning an array of pointers to integers.  
An illegal statement. 
Question 19 
x * y < i + j  k
1  
0  
1  
2 
Step1: Evaluate x * y because multiplication has more priority than remaining operators
x * y→ 0
Step2: i + j is 1
Step3: (x*y) < (i+j) is 1. Because relational operators only return 1(TRUE) or 0(FALSE).
Step4: ((x*y) < (i+j))  k is logical OR operator.
1  1 will returns 1.
Note: The precedence is ((x*y) < (i+j))  k
Question 20 
int f (int n)
{
if (n = = 0) then return n;
else return n + f(n2);
}
2550  
2556  
5220  
5520 
Step1: int f(100)
{ if (100 = = 0) then return 100; /*false*/
else return 100 + f(1002); /* return 198 */
}
Step2: int f(98)
{ if (98 = = 0) then return 98; /*false*/
else return 98 + f(96); /* return 194 */
}
Step3: int f(96)
{ if (96 = = 0) then return 96; /*false*/
else return 96 + f(94); /* return 190 */
}
;
;
Stepfinal:
int f(0)
{ if (0 = = 0) then return 0; /*TRUE*/
else return 0 + f(0); /* FALSE */
}
Actual process:
The above series is nothing but sum of Arithmetic Progression(A.P)
= 2+4+6+8+ ... +98+100
= n(n+1)
/* It is nothing but sum of even numbers upto 'n' */
= 50*51
= 2550
Question 21 
#include
main( )
{
int i, inp;
float x, term=1, sum=0;
scanf(“%d %f ”, &inp, &x);
for(i=1; i<=inp; i++)
{
term = term * x/i;
sum = sum + term ;
}
printf(“Result = %f\n”, sum);
}
The program computes the sum of which of the following series?
x + x ^{2} /2 + x ^{3} /3 + x^{ 4} /4 +...  
x + x ^{2} /2! + x^{ 3} /3! + x^{ 4} /4! +...  
1 + x ^{2} /2 + x^{ 3} /3 + x^{ 4} /4 +...  
1 + x ^{2} /2! + x^{ 3} /3! + x^{ 4} /4! +... 
Let x=5, inp=5
Iteration1:
for(i=1; i<=inp; i++)
/* condition true 1<=5 */
{
term = term * x/i;
/* (1*5)/1=5. Here, 5 nothing but ‘x’ */
sum = sum + term ; /* 0+5=5 */
}
Iteration2: Here, ‘i’ becomes 2.
for(i=2; i<=inp; i++)
/* condition true 2<=5 */
{
term = term * x/i;
/* (5*5)/2 is nothing but x 2 /i */
sum = sum + term ; /* 5+12=17 */
}
;;;
;;;
Iteration5: Here, ‘i’ becomes 5.
for(i=5; i<=inp; i++)
/* condition true 5<=5 */
{
term = term * x/i;
/* (625*5)/5! is nothing but x ^{5} /i! */
sum = sum + term ;
}
So, OptionB is correct answer.
Question 22 
main( )
{
int x=128;
printf (“\n%d”, 1 + x++);
}
128  
129  
130  
131 
printf(“\n%d”, 1 + x++); /* x=128 */
Here, 1+128=129
Question 23 
a pointer to a function returning array of n pointers to function returning character pointers.  
a function return array of N pointers to functions returning pointers to characters  
an array of n pointers to function returning pointers to characters  
an array of n pointers to function returning pointers to functions returning pointers to characters. 
Question 24 
p=0;
k=0;
while(k < m)
p = p + 2^{k};
k=k+1;
end while
Which of the following is a loop invariant for the while statement?
(Note: a loop variant for a while statement is an assertion that is true each time guard is evaluated during the execution of the while statement).
p = 2 ^{k} − 1 and 0≤k < m  
p = 2 ^{k+1} − 1 and 0≤k < m  
p = 2 ^{k − 1} and 0≤k≤m  
p = 2 ^{k+1} − 1 and 0≤k≤m 
k=1, P=1+2 ^{1} =3
k=2,P=3+2 ^{2} =7
k=3,P=7+2 ^{3} =15
Only the option3 satisfied to the loop variant.
Ex: m=2, The loop will execute for 3 times for k value is 0,1 and 2.
Then options3 gives P=1,P=3 and P=7 for the k values 0,1 and respectively
Question 25 
void f(char w [ ] )
{
int x = strlen(w); //length of a string
char c;
For (int i = 0; i < x; i++)
{
c = w[i];
w[i] = w[x  i  1];
w[x  i  1] = c;
}
}
Which of the following is the purpose of f() ?
It output the content of the array with the characters rearranged so they are no longer recognized a the words in the original phrase.  
It output the contents of the array with the characters shifted over by one position.  
It outputs the contents of the array in the original order.  
It outputs the contents of the array in the reverse order. 
Question 26 
Public static int f(int a)
{
//Preconditions : a>0 and no overflow/underflow occurs
int b=0;
for(int i=0; i<32; i++)
{
b=b <<1;
b= b  (a & 1);
a = a >>> 1; // This is a logical shift
}
Return b;
}
Return the int that represents the number of 0’s in the binary representation of integer a.  
Return the int that represents the number of 1’s in the binary representation of integer a.  
Return the int that has the reversed binary representation of integer a.  
Return the int that has the binary representation of integer a. 
The initial value of b is 0 and b<<1 means b will get value of 2
b=b(a&1) which means 2(5&1) then b will get value “2”
a=a<<1 means value a will reduce to 3
→ Repeat the above procedure for 31 iterations .
→ Iteration2: b=b<<1 then b is 4 and b=b(a&1)= 4(3&1) then b is 5 and a becomes 1
→ Iteration3: b=b<<1 then b is 10 and b=b(1&1)= 4(3&1) then b is 11 and a becomes 0
→ And this procedure will repeat until “i” value is 32.
→ The final value is 11 (Number of 1’s in the result is 2) and the number of 1’s in the input 5 are 2.
Question 27 
public static float f(long m, long n)
{
float result = (float) m / (float) n;
if (m < 0  n < 0)
return 0⋅0f;
else
result += f(m*2, n*3);
result result;
}
Which of the following integers best approximates the value of f(2,3) ?
0  
3  
1  
2 
{
float result = (float) m / (float) n;
if (m < 0  n < 0)
return 0⋅0f;
else
result += f(m*2, n*3);
result result;
}
function call→ f(2,3)
→ result=2.0/3.0
=0.666
if condition is false, else will be executed result = 0.666+f(4,9) function call→ f(4,9)
→ result=4.0/9.0
=0.444
result=0.4444+f(8,27)
function call→ f(8,27)
→ result=8.0/27.0
=0.2962
result=0.2962+f(16,54)
function call→ f(16,81)
→ result=16.0/81.0
=0.1975
result=0.1975+f(32,243)
function call→ f(32,243)
→ result= 32.0/243.0
= 0.13168
result=0.13168+f(64,729)
and so on
f(128,2187),f(256,6561),f(512,19683),f(1024,59049),f(2048,177147)
result=0.6666+0.4444+0.2962+0.1975+0.13168+0.08779+0.0390+0.026+0.0173+0.0115
The best approximate value is sum of all the function call result values which is equal 2.
Question 28 
int f(int m, int n, boolean x, boolean y)
{
int res=0;
if(m<0) {res=nm;}
else if(x  y) {
res= 1;
if( n==m) { res =1; }
}
else {res=n; }
return res;
} /*end of f */
If P is the minimum number of tests to achieve full statement coverage for f() and Q is the minimum number of tests to achieve full branch coverage for f(), then (P,Q) =
(3,4)  
(3,2)  
(2,3)  
(4,3) 
→ Statement coverage is a white box test design technique which involves execution of all the executable statements in the source code at least once. It is used to calculate and measure the number of statements in the source code which can be executed given the requirements.
→ Branch coverage is a testing method, which aims to ensure that each one of the possible branch from each decision point is executed at least once and thereby ensuring that all reachable code is executed.
→ In the branch coverage, every outcome from a code module is tested. For example, if the outcomes are binary, you need to test both True and False outcomes.