Subject-wise Programming Questions asked frequently
Question 1 |
Consider the following code segment
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 |
Question 1 Explanation:
for loop starts from 0 to 20 and in each iteration k is incremented by 2
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 = 0Therefore, Output for above code segment is 4 10 16
Question 2 |
What is the value of F(4) using the following procedure:
function F(K : integer) integer; begin if (k<3) then F:=k else F:=F(k-1)*F(k-2)+F(k-3) end;
5 | |
6 | |
7 | |
8 |
Question 2 Explanation:
As per the above code snippet
f(0) = 0
f(1) = 1
f(2) = 2
f(0) = 0
f(1) = 1
f(2) = 2
f(K) = F(k-1)*F(k-2)+F(k-3) f(4) = f(3)*f(2)+f(1) f(3) = f(2)*f(1)+f(0) = 2*1+0 = 2 So, We know that f(1) = 1 f(2) = 2 f(3) = 2 f(4) = f(3) * f(2) + f(1) f(4) = 2 * 2 + 1 f(4) = 5
Question 3 |
In C, what is the effect of a negative number in a field width specifier?
The values are displayed right justified | |
The values are displayed centered | |
The values are displayed left justified | |
The values are displayed as negative numbers |
Question 3 Explanation:
To left justify, we use a negative number in the field width


Question 4 |
Consider the following code written in a pass-by-reference language like FORTRAN and these statements about the code.
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:
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 endS1: 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 |
Question 4 Explanation:
Statement 1 : True
1b+5 is an expression, which can be evaluated to a value 13, compiler itself will generate code to define and declare an unnamed temporary cell with value 13 and pass it to swap subroutine.
Statement 2 : Flase
There won't be any runtime error for the given code using pass by reference.
Statement 3 : False
There won't be any runtime error for the given code using pass by reference.
Statement 4 : True
Swap operation perform between the ia and temporary nameless cell, therefore the value of ib i.e. 8 is remains unchanged. Due to the pass by reference nature of the language, the cell bound to variable 'ia' will get value 13 and the temporary unnamed cell which was allocated and passed to the swap subroutine will get value 3 and cell bound to variable 'ib' is unchanged, therefore printing 13 and 8 at the end of this routine.
Statement 5 : False
The program will print 13 and 8
1b+5 is an expression, which can be evaluated to a value 13, compiler itself will generate code to define and declare an unnamed temporary cell with value 13 and pass it to swap subroutine.
Statement 2 : Flase
There won't be any runtime error for the given code using pass by reference.
Statement 3 : False
There won't be any runtime error for the given code using pass by reference.
Statement 4 : True
Swap operation perform between the ia and temporary nameless cell, therefore the value of ib i.e. 8 is remains unchanged. Due to the pass by reference nature of the language, the cell bound to variable 'ia' will get value 13 and the temporary unnamed cell which was allocated and passed to the swap subroutine will get value 3 and cell bound to variable 'ib' is unchanged, therefore printing 13 and 8 at the end of this routine.
Statement 5 : False
The program will print 13 and 8
Question 5 |
Find the output of the following Java code line
System.out.println( math.floor(-7.4) )
-7 | |
-8 | |
-7.4 | |
-7.0 |
Question 5 Explanation:
In java, floor function works as usual it will give the lower limit value of any number.
Example :
Example :
- Floor of -7.4 will return the lower limit, i.e. -8.
- Floor of 94.69 will return the lower limit, i.e. 94.
- Floor of -39.28; will return the lower limit, i.e.40.
Question 6 |
Consider the following pseudocode:
x:=1; i:=1; while (x ≤ 500) begin x:=2x; i:=i+1; endWhat is the value of i at the end of the pseudocode?
4 | |
5 | |
6 | |
7 |
Question 6 Explanation:
In every iteration we are incrementing the i value by 1 and x value by 2x
In Iteration 1: x = 2 and i = 2 In iteration 2: x = 4 and i = 3 In iteration 3: x = 16 and i = 4 In iteration 4: x = 16,536 and i = 5 In iteration 5: Condition fails as x value > 500.Therefore, i value = 5 option(B) is the correct answer.
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 |
Question 7 Explanation:
Continue statement is used inside loops. When a continue statement is encountered inside a loop, the code inside the loop following the continue statement will be skipped and next iteration of the loop will begin.
In this code, continue statement encounter at last line. so there would be no effect on the execution of the program. Hence, the code will print 12345 as output.
Therefore, Option(B) is the correct answer.
In this code, continue statement encounter at last line. so there would be no effect on the execution of the program. Hence, the code will print 12345 as output.
Therefore, Option(B) is the correct answer.
Question 8 |
The following steps in a linked list
p = getnode() info (p) = 10 next (p) = list list = presult in which type of operation?
Pop operation in stack | |
Removal of a node | |
Inserting a node | |
Modifying an existing node |
Question 8 Explanation:

P = get node () ; means allocating a space for a new node that we want to create and sets p as its address
Question 9 |
Consider the following C code.
#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 |
Question 9 Explanation:
- i = 0 :
a = cos(pi * 0/2) = cos(0) = 1
; Condition True. So, It print 1. - i = 1 :
a = cos (pi/2) = 0
; Condition False. So else part print 0. - i = 2 :
a = cos(pi) = -1
; Condition True. So, It print 1.
So, Option(C) is the correct answer
Note : Other then 0 all values consider it as True in if-condition.
Question 10 |
What is the output of the following Java program?
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 |
Question 10 Explanation:
Short-circuit :
The evaluation of a logical expression exit in between before complete evaluation, then it is known as Short-circuit. A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing.
By using short circuit technique
Therefore, option (A) is the correct answer.
The evaluation of a logical expression exit in between before complete evaluation, then it is known as Short-circuit. A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing.
By using short circuit technique
- z = 0: x = 1, y = 1, if condition false
- z = 1: x = 2, y = 2, if condition false
- z = 2: x = 3, if condition true and due to short circuiting ++y is not evaluated and y value still 2.
x++ // x = 4 - z = 3: x = 5, if condition is true and due to short circuiting ++y is not evaluated and y value still 2.
x++ // x = 6 - z = 4: x = 7, if condition true and due to short circuiting ++y is not evaluated and y value still 2.
x++ // x = 8 - z = 5 ; for loop condition fails and terminate hear
Therefore, option (A) is the correct answer.
Question 11 |
The following ‘C’ statement :
int *f [ ] ( ) ;
declares:
int *f [ ] ( ) ;
declares:
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 11 Explanation:
int * f [ ] ( ) // function f is an array of functions returning pointers to integers.
Declaring integer pointer
int * ptr_integer; // ptr_integer is a pointer to integer
Declare a function
int f(int); // f is a function that returns int and takes one argument of int type
int * f(int);
Declaring integer pointer
int * ptr_integer; // ptr_integer is a pointer to integer
Declare a function
int f(int); // f is a function that returns int and takes one argument of int type
int * f(int);
- function f with one argument of int type and return value of int * i.e. integer pointer.
- According to operator precedence operator () will take priority over operator *.
- function f with one argument of int type and return value of int * i.e. integer pointer.
- A pointer to a function that takes an integer pointer as argument and returns an integer.
Question 12 |
Given
i = 0,
j = 1,
k = –1,
x = 0.5,
y = 0.0
What is the output of the following expression in C language?
i = 0,
j = 1,
k = –1,
x = 0.5,
y = 0.0
What is the output of the following expression in C language?
x * y < i + j || k
-1 | |
0 | |
1 | |
2 |
Question 12 Explanation:
Given data,
i = 0,
j = 1,
k = –1,
x = 0.5,
y = 0.0,
We need to caluculate :
x * y < i + j || k
According to operator precedence
x * y < i + j || k = ( ( (x*y) < ( i + j) ) || k )
= ( ( ( 0.5 * 0.0 ) < ( 0 + 1) ) || -1 )
= ( ( 0 < 1 ) || -1 )
= ( 1 || -1 )
= 1
i = 0,
j = 1,
k = –1,
x = 0.5,
y = 0.0,
We need to caluculate :
x * y < i + j || k
According to operator precedence
- "*" Has higher precedence among all the operators and associativity is from "left to right"
- "+" Has next higher precedence among all the operators and associativity is from "left to right"
- "<" Has next higher precedence among all the operators.
- "||" Has low precedence among all the operators.
x * y < i + j || k = ( ( (x*y) < ( i + j) ) || k )
= ( ( ( 0.5 * 0.0 ) < ( 0 + 1) ) || -1 )
= ( ( 0 < 1 ) || -1 )
= ( 1 || -1 )
= 1
Question 13 |
What is the value returned by the function f given below when n = 100?
int f (int n) { if (n = = 0) then return n; else return n + f(n-2); }
2550 | |
2556 | |
5220 | |
5520 |
Question 13 Explanation:
Base condition: n == 0 then execution stops and return n value
= 2+4+6+8+ ........... +98+100
= n(n+1) // It is nothing but sum of even numbers upto 'n'
= 50 × 51
= 2550
int f(100) ——————————— 100 + f(98) ————— 98 + f(96) ————— 96 + f(94) ————— 94 + f(92) ————— 92 + f(90) ————— ' ' ' 2 + f(0)So, The above series is in 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 14 |
Consider the following program :
#include<stdio.h> 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! +... |
Question 14 Explanation:
Intially : term=1, sum=0;
i=1 :
term =
sum =
i = 2 :
term =
sum =
i = 3 :
term =
sum =
and so on . . . . . . . . .
Which is equl to x + x2/2! + x3/3! + x4/4! +........
Therefore, option(B) is the right answer.
i=1 :
term =
term * x / i
= 1 * x / 1 = x.sum =
sum + term
= 0+x= x.i = 2 :
term =
term * x / i
= x * x / 2 = x2 / 2.sum =
sum + term
= x + x2 / 2.i = 3 :
term =
term * x / i
= x2 / 2 * x / 3 = x3 / 3! .sum =
sum + term
= x + x2 / 2 + x3 / 3! .and so on . . . . . . . . .
Which is equl to x + x2/2! + x3/3! + x4/4! +........
Therefore, option(B) is the right answer.
Question 15 |
What will be the output of the following ‘C’ code ?
main( ) { int x=128; printf ("\n%d", 1 + x++); }
128 | |
129 | |
130 | |
131 |
Question 15 Explanation:
In this program we used post-increment. Post increment operator is used to increment the value of the variable after executing printf statement
printf (“\n%d”, 1 + x++); // hear x value still 128
Hear, x value incremented by 1 now x = 129
printf (“\n%d”, 1 + 128)
output : 129
printf (“\n%d”, 1 + x++); // hear x value still 128
Hear, x value incremented by 1 now x = 129
printf (“\n%d”, 1 + 128)
output : 129
Question 16 |
What does the following expression means ?
char *(*(*a[N]) ( )) ( );
char *(*(*a[N]) ( )) ( );
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 16 Explanation:
Given expression,
char *(*(*a[N]) ( )) ( );
a
Indicates name
a[N]
It is an N-element array
*a[N]
It is an N-element array of pointers
(*a[N])()
It is an N-element array of pointers to functions
*(*a[N])()
It is an N-element array of pointers to functions returning pointers
(*(*a[N])())()
It is an N-element array of pointers to functions returning pointers to functions
*(*(*a[N])())()
It is an N-element array of pointers to functions returning pointers to functions returning pointers
char *(*(*a[N])())()
It is an N-element array of pointers to functions returning pointers to functions returning pointers to char.
∴ An array of n pointers to function returning pointers to functions returning pointers to characters.
Therefore, option(D) is the correct answer
char *(*(*a[N]) ( )) ( );
a
Indicates name
a[N]
It is an N-element array
*a[N]
It is an N-element array of pointers
(*a[N])()
It is an N-element array of pointers to functions
*(*a[N])()
It is an N-element array of pointers to functions returning pointers
(*(*a[N])())()
It is an N-element array of pointers to functions returning pointers to functions
*(*(*a[N])())()
It is an N-element array of pointers to functions returning pointers to functions returning pointers
char *(*(*a[N])())()
It is an N-element array of pointers to functions returning pointers to functions returning pointers to char.
∴ An array of n pointers to function returning pointers to functions returning pointers to characters.
Therefore, option(D) is the correct answer
Question 17 |
Consider the following pseudo-code fragment, where m is a non-negative integer that has been initialized:
(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=0; k=0; while(k < m) p = p + 2k; k=k+1; end whileWhich 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 = 2k − 1 and 0≤k < m | |
P = 2k+1 − 1 and 0≤k < m | |
P = 2k − 1 and 0≤k≤m | |
P = 2k+1 − 1 and 0≤k≤m |
Question 17 Explanation:
Loop invariant definition
A loop invariant is a statement about program variables that is true before and after each iteration of a loop. A good loop invariant should satisfy three properties:
After First iteration:
For k = 0, p =
After Second iteration:
For k =1, p =
After Third iteration:
For k = 2, p =
After Fourth iteration:
For k = 3, p =
Now , check all the option which gives same value of p when k = 0, 1 ,2 ,3
p = 2k - 1 and 0≤k < m
When k = 0, p = 20 - 1 = 0
Option(B): correct
p = 2k+1 - 1 and 0≤k < m
When k = 0, p = 20+1 – 1 = 1
When k = 1, p = 21+1 – 1 = 3
When k = 2, p = 22+1 – 1 = 7
When k = 3, p = 23+1 – 1 = 15
Option(C): incorrect
P = 2 k − 1 and 0≤k≤m
When k = 0, p = 20-1 = 0.5
Option(D): incorrect
This option also giving the same values, but it is not true in the case as k<=m in that, which should be k < m
**Given key is option(C) but option(B) is correct*** Let us know in the discussion form if we miss anything.
A loop invariant is a statement about program variables that is true before and after each iteration of a loop. A good loop invariant should satisfy three properties:
- Initialization: The loop invariant must be true before the first execution of the loop.
- Maintenance: If the invariant is true before an iteration of the loop, it should be true also after the iteration.
- Termination: When the loop is terminated the invariant should tell us something useful, something that helps us understand the algorithm.
After First iteration:
For k = 0, p =
p + 2k
= 0 + 20 = 1After Second iteration:
For k =1, p =
p + 2k
= 1 + 21 = 3After Third iteration:
For k = 2, p =
p + 2k
= 3 + 22 = 7After Fourth iteration:
For k = 3, p =
p + 2k
= 7 + 23 = 15Now , check all the option which gives same value of p when k = 0, 1 ,2 ,3
- k=0, P=1
- k=1, P=3
- k=2, P=7
- k=3, P=15
p = 2k - 1 and 0≤k < m
When k = 0, p = 20 - 1 = 0
Option(B): correct
p = 2k+1 - 1 and 0≤k < m
When k = 0, p = 20+1 – 1 = 1
When k = 1, p = 21+1 – 1 = 3
When k = 2, p = 22+1 – 1 = 7
When k = 3, p = 23+1 – 1 = 15
Option(C): incorrect
P = 2 k − 1 and 0≤k≤m
When k = 0, p = 20-1 = 0.5
Option(D): incorrect
This option also giving the same values, but it is not true in the case as k<=m in that, which should be k < m
**Given key is option(C) but option(B) is correct*** Let us know in the discussion form if we miss anything.
Question 18 |
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 18 Explanation:
Let us take an example
String w = “Era”
Length of the string = 3
int x = strlen(w); // x= 3
Finally, The array will be :
Option(C) is the correct answer.
String w = “Era”
Length of the string = 3
int x = strlen(w); // x= 3
- w[0]= E,
- w[1] = R,
- w[2] = A,
- w[3] = \0,
For ( int i = 0; i < x; i++) // Condition true as, 0< 3 c = w[i]; // c = w[0] = E w[i] = w[x - i - 1]; // w[0] = w[3-0-1]= w[2] = A w[x - i - 1] = c; // w[3- 0 -1 ] = w[2] = EIn iteration 2:
For (int i = 0; i < x; i++) // condition true as, 1< 3 c = w[i]; // c = w[1] = R w[i] = w[x - i - 1]; // w[1] = w[3-1-1]= w[1] = R w[x - i - 1] = c; // w[3- 1 -1 ] = w[1] = CIn iteration 3:
For (int i = 0; i < x; i++) // condition true as, 2< 3 c = w[i]; // c = w[2] = E w[i] = w[x - i - 1]; // w[2] = w[3-2-1]= w[0] = A w[x - i - 1] = c; // w[3- 2 -1 ] = w[0] = E

Finally, The array will be :
- w[0]= E,
- w[1] = R,
- w[2] = A,
- w[3] = null character
Option(C) is the correct answer.
Question 19 |
What does the following java function perform? (Assume int occupies four bytes of storage)
Public static int f(int a) { //Pre-conditions : 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. |
Question 19 Explanation:
Points to Remember :
Initially b = 0
Consider a = 5
Iteration i= 0 :
Hear, value of b will multiply by 2 on every iteration up to i value became 31
when i = 4, b = 20
when i = 5, b = 40
when i = 6, b = 80 = 23 × 10
when i = 7, b = 160 = 24 × 10
'
'
'
'
when i = 29, b = 160 = 226 × 10
when i = 30, b = 227 × 10
but Size of int is 4 byte in java: Range - 231 to 231 – 1
when i = 30, b = 227 × 10 ≡ -1610612736
when i = 31, b = 228 × 10 ≡ -1610612736
a = 5 = (0000 0000 ….... 0101)2
Reverse of a = (1010 0000 0000 …. 0000)2 = -231 + 229 = -1610612736
Hence, the Given code snippet return the int that has the reversed binary representation of integer a.
- Shifting all of a number's bits to the left by 1 bit is equivalent to multiplying the number by 2.
- All of a number's bits to the left by n bits is equivalent to multiplying that number by 2n.
- Logical right shifts are denoted by
>>>
. They shift all bits to the right and fill in vacated places with 0's - When performing AND on two integers, the AND operation is calculated on each pair of bits (the two bits at the same index in each number).
Example : 5 & 6 =4 which gives 4 as result
Initially b = 0
Consider a = 5
Iteration i= 0 :
for(int i=0; i<32; i++) //condition true b = b<<1; // (0 × 2 = 0) ; b=0 b= b | (a & 1); // 0 | (5 & 1);[(101)&(001)=001]; b=1 a = a >>> 1; // logical right shift; a = 101 >>> 1 =010 So, a=2Iteration i= 1 :
for(int i=0; i<32; i++) // 1<32, condition true b = b<<1; // (1 * 2) ; b=2 b= b | (a & 1); // 2 |(2 & 1) ; b=2 a = a >>> 1; // This is a logical right shift; So,a=1Iteration i= 2 :
for(int i=0; i<32; i++) // 2<32, condition true b = b<<1; // (2*2=4) ; b=4 b= b | (a & 1); // 4|(1 & 1) ; 100|(001&001)=100|001=101; b=5 a = a >>> 1; // This is a logical shift; So, a=0Iteration i= 3 :
for(int i=0; i<32; i++) // 3<32, condition true b = b<<1; // b=10 b= b | (a & 1); // 10|(0 & 1) ; b=10 a = a >>> 1; // this is a logical shift; So, a=0
Hear, value of b will multiply by 2 on every iteration up to i value became 31
when i = 4, b = 20
when i = 5, b = 40
when i = 6, b = 80 = 23 × 10
when i = 7, b = 160 = 24 × 10
'
'
'
'
when i = 29, b = 160 = 226 × 10
when i = 30, b = 227 × 10
but Size of int is 4 byte in java: Range - 231 to 231 – 1
when i = 30, b = 227 × 10 ≡ -1610612736
when i = 31, b = 228 × 10 ≡ -1610612736
a = 5 = (0000 0000 ….... 0101)2
Reverse of a = (1010 0000 0000 …. 0000)2 = -231 + 229 = -1610612736
Hence, the Given code snippet return the int that has the reversed binary representation of integer a.
Question 20 |
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); return result; }Which of the following integers best approximates the value of f(2,3)?
0 | |
3 | |
1 | |
2 |
Question 20 Explanation:
Function call: f(2, 3)
Where, m = 2 and n = 3
STEP 1:
Where, m = 2 and n = 3
STEP 1:
float result = (float) m / (float) n;
// result = 2.0/ 3.0 = 0. 66if (m < 0 || n < 0)
// false, else part will executeresult += f(m*2, n*3);
// result = 0.66 + f (4, 9)
float result = (float) m / (float) n;
// result = 4.0/ 9.0 = 0. 44if (m < 0 || n < 0)
// false, else part will executeresult += f(m*2, n*3);
// result = 0.66 + 0.44 + f (8, 27)
float result = (float) m / (float) n;
// result = 8.0/ 27.0 = 0. 296if (m < 0 || n < 0)
// false, else part will executeresult += f(m*2, n*3);
// result = 0.66+ 0.44 + 0.296 + f (16, , 81)
float result = (float) m / (float) n;
// result = 16.0/ 81.0 = 0. 197if (m < 0 || n < 0)
// false, else part will executeresult += f(m*2, n*3);
// result = 0.66+ 0.44 + 0.296 + 0.197 + f (32, 243)
float result = (float) m / (float) n;
// result = 32.0/ 243.0 = 0. 131if (m < 0 || n < 0)
// false, else part will executeresult += f(m*2, n*3);
// result = 0.66+ 0.44 + 0.296 + 0.197 + 0.131 + f (64, 729)
float result = (float) m / (float) n;
// result = 64.0/ 729.0 = 0. 087if (m < 0 || n < 0)
// false, else part will executeresult += f(m*2, n*3);
// result = 0.66+ 0.44 + 0.296 + 0.197 + 0.131 + 0.087 + f (128, 2187)
float result = (float) m / (float) n;
// result =128.0/2187.0= 0.058if (m < 0 || n < 0)
// false, else part will executeresult += f(m*2, n*3);
// result = 1.813 + 0.058 + f (256, 6561)
float result = (float) m / (float) n;
// result = 256/6561 = 0.039if (m < 0 || n < 0)
// false, else part will executeresult += f(m*2, n*3);
// result = 1.871 + 0.039 + f (512, 19683)
float result = (float) m / (float) n;
// result = 512/19683 = 0.0260if (m < 0 || n < 0)
// false, else part will executeresult += f(m*2, n*3);
// result = 1.871 + 0.039 + 0.0260 = 1.91 + f (1024, 59049)
- result = 1.91 + 0.017 + f(2048, 177,147)
Question 21 |
Consider the following method :
int f(int m, int n, boolean x, boolean y) { int res=0; if(m<0) { res=n-m; } 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) |
Question 21 Explanation:
Statement coverage is said to make sure that every statement in the code is executed at least once.
Decision/branch coverage is said 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. That is, every branch taken each way, true and false. It helps in validating all the branches in the code making sure that no branch leads to abnormal behavior of the application.
Note :
Comming to the question :
P : Minimum number of tests to achieve full statement coverage for f() = 3
Total 4 conditions. If any one condition is TRUE remaining automatically will get False except if in else- if i.e. Condition 3
Therefore, the Minimum number of tests to achieve full statement coverage P = 3
Q : Minimum number of tests to achieve full branch coverage for f()
Therefore, Minimum number of tests to achieve full Branch coverage Q = 4
Hence, option(A) is the correct answer.
Decision/branch coverage is said 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. That is, every branch taken each way, true and false. It helps in validating all the branches in the code making sure that no branch leads to abnormal behavior of the application.
Note :
- If there is 100% branch coverage then there is 100% statement coverage
- If there is 100% statement coverage then it does not imply 100% branch coverage
Comming to the question :
P : Minimum number of tests to achieve full statement coverage for f() = 3
Total 4 conditions. If any one condition is TRUE remaining automatically will get False except if in else- if i.e. Condition 3
Condition 1 if(m<0) { res=n-m; } Condition 2 else if(x || y) { res= -1; Condition 3 if( n==m) { res =1; } } Condition 4 else { res=n; }
Condition-1 | Condition-2 | Condition-3 | Condition-4 |
True | False | False | False |
False | True | True | False |
False | False | False | True |
Q : Minimum number of tests to achieve full branch coverage for f()
Condition-1 | Condition-2 | Condition-3 | Condition-4 |
True | False | False | False |
False | True | False | False |
False | True | True | False |
False | False | False | True |
Hence, option(A) is the correct answer.
There are 21 questions to complete.