Q: What will be the output of the following pseudocode? Integer i Set i = 3 do print i + 3 i = i - 1 while(i not equals 0) end while [Note: A do-while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the given Boolean condition at the end of the block]
Solution: In this program, one variable declared as i, and the value initialized as 3. We are moving with do-while(Do while will execute the statement once and then it will check the condition). Step 1: It will print i+3, here i value is 3. So i+3 is 6. On the next line, i will be decremented by 1. Then checking the conditions in do-while() i!=0. Here updated i value is 2 (2!=0),so condition is true. The loop continues. Step 2: It will print i+3, here the updated i value is 2. So i+3 is 5. On the next line, i will be decremented by 1. Then checking the conditions in do-while() i!=0. Here updated i value is 1 (1!=0),so condition gets true. The loop continues Step 3: It will print i+3, here the updated i value is 1. So i+3 is 4. On the next line, i will be decremented by 1. Then check the condition in do-while() i!=0. Here updated i value is 0 (0!=0),so condition gets false. Thus the loop gets terminated!
Q: What would be the output of the following pseudocode? Integer a String str1 Set str1 = “goose” a = stringLength(str1) Print (a ^ 1) [Note- string-length(): string-length() function counts the number of characters in a given string and return the integer value. ^ is the bitwise exclusive OR operator that compares each bit of its first operand to the corresponding bit of its equal operand. If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0]
Solution: There are two variables a and str1. Value initialized for str1 is “goose”. On the next line, we are finding the length of str1 that is 5. Finally, printing the output of a bitwise exclusive OR operator with 1. And the answer is 4.
Q: What would be the output of the following pseudocode? Integer a, b, c Set a = 8, b = 51, c = 2 c = (a ^ c)^ (a) b = b mod 4 Print a + b + c [Note- mod finds the remainder after the division of one number by another. For example, the expression “5 mod 2” would evaluate to 1 because 5 divided by 2 leaves a quotient of 2 and a remainder of 1 ^ is the bitwise exclusive OR operator that compares each bit of its first operand to the corresponding bit of its equal operand. If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0]
Solution: There are three variables a, b and c declared. Value initialized for a is 8, b is 51 and c is 2. When we do a bitwise exclusive OR of (8^2), the answer is 10. Again 10 bitwise exclusive OR of a i.e (10 ^ 8) is 2, which will be stored in variable c. Then taking modulo operation for b by 4 (b%4) the answer is 3 Finally adding all the updated values of a,b, and c (8+2+3 ) and the output of Pseudocode is 13.
Q: Consider an array A = {1, 2, 4, 5, 6, 11, 12} and a key which is equal to 10. How many comparisons would be done to find the key element in the array using the binary search?Solution: There is an Integer Array A = {1, 2, 4, 5, 6, 11, 12} and the key value is 10.
Binary search applies only to the sorted ordered list.
We know that binary search takes log n base 2 times to search for a particular element.
If there are N elements in the set you have chosen binary search then take log n base 2 times.
First, you check how many elements in your array. if an element in your array is greater than 1 then you go for the next step.
Binary search divides the problem into two parts using the mean of the total number of an element which is sorted mean=(0+n)/2
And compare the searching element which is either greater than the mean or lesser or equal to the mean .then after comparison skips the one part either greater part or small part depends on the result of searching is done if the mean is equal to the searching element. So that problem is divide into n/2 and goes until searching is done.If you apply the recursive equation for the binary search algorithm then
T(n)=T(nQ: What would be the output of the following pseudocode?
Integer i, j, k
Set k = 8
for(each i from 1 to 1)
for(each j from the value of i to 1)
print k+1
end for
end forSolution: There are three variables i, j, and k declared. Value initialized for k is 8, In this code, we are moving with nested for loop. Here I value is 1, for loop will check the condition i<=1 condition gets true. Now, moving with inner for loop j value will be 1 condition gets true j<=1.so, it prints K+1. Then j value will be incremented by 1(2<+1) inner for loop condition gets false. On the next iteration, i value will be incremented by 1, here the updated i value is 2 (2<=1) condition get false. So the answer is 9.
Q: What will be the output of the following pseudocode? Integer a, b Set a = 15, b = 7 a = a mod (a - 3) b = b mod (b – 3) a = a mod 1 b = b mod 1 Print a + b
Solution: There are two variables a and b declared. Value initialized for a is 15 and b is 7. Taking mod operation for a by 12(a%12) and the answer is 3 will stored in a. The next mod operation for b is 7 mod (7%4). The answer is 3 will be stored in b. The next line takes the updated value of a and mods it by 1(3%1). Then the answer becomes 0 will be stored in a. The next line takes the updated value of b mod by 1 (3%1) then the answer is 0. Finally adding all the updated values of a and b (0+0 ) and the output of Pseudocode is 0.
Q: What will be the output of the following pseudocode? Integer a, b, c Set b = 5, a = 2, c = 2 if(b>a && a>c && c>b) b = a + 1 Else a = b + 1 End if Print a + b + c [Note-&&: Logical AND - The logical AND operator (&&) returns the Boolean value true(or 1) if both operands--. If (x) gets executed if the value if(), i.e., x is not zero]
Solution: There are three variables a, b and c declared. Value initialized for a is 2, b is 5 and c is 2. Checking the condition using if, b >a and a>c and c>b here if conditions get false. Now else part will execute b value will be incremented by 1 and stored in a, Finally adding all the updated values of a, b and c (6+5+2 ) and the output of Pseudocode is 13.
Q: For which of the following applications can you use hashing? 1. To construct a message authentication code. 2. For Timestamping 3. For detecting a cycle in a graph Choose the correct answer from the options given below.
Solution: Constructing a message authentication code and Timestamping are the real-time applications for hashing.
| You Have Score    | /8 |