PSEUDOCODE-1
Presented by : Prashant Soni ‘Assistant Professor, UCER‘
Objectives
After completing this session, you should be able to work
with pseudocode:
• Pseudo Code is a normal representation of algorithm code in C,
C++ or any other language.
• In Pseudo Code round there will be a total of 5 questions that
we need to answer within 10 minutes.
• The Difficulty level of the paper goes from Moderate to High.
• Pseudo-Code are not the machine-readable codes it is just as
same as algorithm written in coding format.
1. What is the output?
#include<stdio.h>
int main()
{
for (int x = 10; x >= 0; x--) {
int z = x & (x >> 1);
if (z)
printf("%d ", x);
}
}
A) 763 B)769
C)678 D)679
Explanation
• Here, in the given code, >> is the bitwise right shift operator and x>>y => x/(2^y)
and & is the bitwise and operator and gives 1 only if both bits are same.
• Now, for x = 10, (10>=0) z = 10 & (10>>1) => z = 10 & 5 => z=10.
• Similarly for x= 9, 8, 5, 4, 2, 1 and 0 , z will be zero and for x=7, z will be 3 , for
x= 6, z will be 2 and for x=3, z will be 1 for x=7, 6, and 3 z is non zero so the will
get printed on the output screen.
• Hence, we get output as 7 6 3.
2. What is the output?
#include<stdio.h>
int main()
{
int a = 100;
printf("%0 %x", a);
}
A) %a B) %b
C) 100 D)None.
5
Explanation
•Inthis question %0 will cancel out the a, while
%x will get printed
•but,
•ifwe only write printf("%x"); without passing
any parameter for x,
•it will show an error.
6
3. What is the output?
#include<stdio.h>
int main()
{ typedef int num;
num bunk = 0.00;
printf("%d", bunk);
return 0;
}
A) ZERO B)0.0
C)Garbage D) Logical Error
Explanation
• Integer part of 0.00 is equal to 0.
• Hence only zero will be printed with printf("%d", bunk), when
0.00 is passed, because %d stands for integer.
8
4. What is the output?
#include<stdio.h>
int main(){
float x = 0.0;
long int y = 10;
printf("%d", sizeof(y) == sizeof(x+y));
return 0;
}
A) 1 B)4
C) Zero D)8
9
Explanation
• sizeof(x), that is float, is 4
• sizeof(y), that is long int, is 8
• sizeof(x+y) that also comes out to be float will have size = 4
• here it is asking sizeof(y) == sizeof(x+y)
• i.e 8 == 4
• "==" operator gives result in either 0 or 1
• so answer is zero.
10
5. What is the output?
#include<stdio.h>
int main()
{
int any = ' ' * 10;
printf("%d", any);
return 0;
}
A) 320 B)360
C) 340 D)380
11
Explanation
• Ascii value of space (' ') is 32 and 32 * 10 is 320.
12
6. What will be the output of the following pseudocode:
#include<stdio.h>
int main()
{
int go = 5.0, num = 1*10;
do
{
num /= go;
} while(go--);
printf ("%d\n", num);
return 0;
}
A) Floating Point Exception B)Compile time error
C) 3 6 7 D)None
Explanation
• In
C when you divide the integer with a float without the type
casting it gives "Floating Point Exception" error
8. What will be the output of the following pseudocode:
#include <stdio.h>
int main()
{
int num = 987;
int rem;
while(num!=0)
{
rem = num % 4;
num = num / 10;
}
printf("%d",rem);
}
A) Zero B)1
C) 2 D)3
Explanation
• Iteration 1 : rem = 3(987 % 4), num = 98
• Iteration 2 : rem = 2(98 % 4), num = 9
• Iteration 3 : rem = 1(9 % 4), num = 0
9. what will be the output of the following Pseudo Code.
#include<stdio.h>
int main()
{
float i;
i = 1;
printf("%d",i);
return 0;
}
A) 1 B)Garbage Value
C) 1.000000 D)Error
Explanation
• If float variable is declared as an integer type
• then, it will always give garbage value.
10. What will be the output of the following pseudocode:
public class Main
{ public static void main(String[] args)
{ String names[] = new String[5];
for(int x=0; x<args.length; x++)
names[x] = args[x];
System.out.println(names[2]);
}
}
A)Names B)NULL
C)Compilation Fails D)An exception thrown at run time
Explanation
• valueof args.length is 0
So, the loop will not run either of the time
Hence, null will be printed.
Lets discuss Example
#include <stdio.h>
int main()
{
int x=5; int y=3;
printf("\n Result with AND %d",x&y);
printf("\n Result with OR %d",x|y);
printf("\n Result with XOR %d",x^y);
return 0;
}
Result with AND 1
Result with OR 7
Result with XOR 6
21
Example 2
#include <stdio.h>
int main()
{ int x=2; int y=16;
printf("\n Result with left shift %d",x<<3);
printf("\n Result with right shift %d",y>>3);
return 0;
}
Result with left shift 16
Result with right shift 2
22
Example 3
Q .WAP to check if a given number is a power of 2 ?
bool isPowerOfTwo(int x)
{
if(x == 0)
return false;
else
{
while(x % 2 == 0) x /= 2;
return (x == 1);
}
}
using bit manipulation.
•The same problem can be solved using bit manipulation.
•Consider a number x that we need to check for being a
power for 2.
• Now think about the binary representation of (x-1). (x-1)
will have all the bits same as x, except for the rightmost 1
in x and all the bits to the right of the rightmost 1.
•Now think about x & (x-1). x & (x-1) will have all the
bits equal to the x except for the rightmost 1 in x.
Let, x = 4 = (100)2
x - 1 = 3 = (011)2
x & (x-1) = 4 & 3 = (100)2 & (011)2 = (000)2
Let, x = 6 = (110)2
x - 1 = 5 = (101)2
x & (x-1) = 6 & 5 = (110)2 & (101)2 = (100)2
bool isPowerOfTwo(int x)
{ // x will check if x == 0
// and !(x & (x - 1)) will check if x is a power of 2
or not
return (x && !(x & (x - 1))); }
Example
Q. WAP to generate all the possible subsets of a set ?
Algo
• Consider a set A of 3 elements.
A = {a, b, c}
• Now, we need 3 bits, one bit for each element. 1 represent that the corresponding element is
present in the subset, whereas 0 represent the corresponding element is not in the subset.
Let’s write all the possible combination of these 3 bits.
• 0 = (000)2 = {}
1 = (001)2 = {c}
2 = (010)2 = {b}
3 = (011)2 = {b, c}
4 = (100)2 = {a}
5 = (101)2 = {a, c}
6 = (110)2 = {a, b}
7 = (111)2 = {a, b, c}
Pseudo Code:
possibleSubsets(A, N):
for i = 0 to 2^N:
for j = 0 to N:
if jth bit is set in i:
print A[j]
print ‘\n’
Implementation:
void possibleSubsets(char A[], int N)
{ for(int i = 0;i < (1 << N); ++i)
{ for(int j = 0;j < N;++j)
if(i & (1 << j))
cout << A[j] << ‘ ‘;
cout << endl; }
}
IMPORTANT
Q1.What is the difference between Graphs and tree? (Asked by INFOSYS, TCS in
2017,18,19)
Q2. A Single node is, A graphs and A tree? (Asked by INFOSYS
in 2019)
Thank You !