Java Pseudocode Questions
Java Pseudocode Questions
1) Given the pseudocode for performing Binary search in an array of elements sorted in
ascending order. What operation must be implemented in the 3rd so that the execution of binary
search is successful?
1. Middle element should be compared with x.
2. If x and the middle element match, we return the middle index.
3. _ _ _ _ _ _ _ _ _ _ _
4. Else (x is smaller than the middle element) recur for the left half
A. Else if x is greater than the middle element, then x can only lie in the right half subarray after
the middle element. So we recursively search for the right
B. Else if x is greater than the middle element, then x can only lie in the left half subarray after
the middle element. So we recursively search for left
C. None of the above
D. Else if x is less than the middle element, then x can only lie in the right half subarray after the
middle element. So we recursively search for the right
2) Predict the output of the given pseudo code if the value of the number is 6:
Read number
k=2
i=2
while i <= number:
k=k*i
i = i +1
end while
write k
A. 1440.0
B. 1340.0
C. 1700.0
D. 1560.0
6) Count the number of " * " printed by the given pseudo code when the input is 25.
Write "Please enter a number"
Read input
Repeat while input > 0
if( input > 0 and input <=12 )
Write *
else if ( input >12 and input <=24 )
Write **
else if ( input >24 and input< = 30 )
Write ***
input --
end if
End while
A. 39.0
B. 57.0
Java Pseudocode Questions
C. 35.0
D. 29.0
10) What will be the output of the given pseudo code if n = 10?
Read n
Initialize i to 5, sum to 0
while i < n do
increase sum by i
increment i
end while
Write sum
A. 35.0
B. 25.0
C. 45.0
D. 55.0
12) Predict the output of the given pseudo code if the value of n is 35 :
Read n
i=0
While n % 10 != 0
n=n+3
i++
end while
n = n+i
print n
A. 55.0
B. 45.0
C. 50.0
Java Pseudocode Questions
D. 43.0
14) Predict the output of the given pseudo code if the value of n is 26 :
Read n
i=0
While n % 8 != 0
n=n+5
i++
end while
n=n+i
print n
A. 16
B. 32
C. 10
D. 62
17) Predict how many times the value of res will be displayed.
Set Integer res = 0
do
++res
display res
while(res <= 5)
End do-while
A. The program will not enter the loop.
B. Code will run infinite number of times.
C. Code will execute and the value of res will be displayed six times.
D. Code will execute and the value of res will be displayed five times.
21) What will be the output of the following pseudocode for p = 3 and q = 4?
int fun1(int p, int q)
if(q EQUALS 0)
return 0
if(q mod 2 EQUALS 0)
return fun1(p + p, q/2) .
return fun1(p + p, q/2) + p
End function fun1()
A. None of the options
B. 7
C. 12
D. 8
A) 15
B) 1
C) 2
D) 8
A. 0
B. 3
C. 1
D. 2
A. SevenTwoHello
B. OneHello
C. SevenTwo
D. Seven
45) Consider the following piece of code. What will be the space required for this code?
int sum(int A[], int n)
{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
} // sizeof(int) = 2 bytes
A. 2n + 8
B. 2n + 4
C. 2n + 2
D. 2n