WorksheetsPSUEDOCODE 2 CAPGEMINI
Total questions: 25
Worksheet time: 25mins
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
2n+8
2n+2
2n+4
2n
Question: Point out the error in the program?
#include<stdio.h>
int main()
{
char ch;
int i;
scanf("%c", &i);
scanf("%d", &ch);
printf("%c %d", ch, i);
return 0;
}
Error: suspicious char to in conversion in scanf()
Error: we may not get input for second scanf() statement
No error
None of above
Question: Consider the following iterative implementation to find the factorial of a number:
int main()
{
int n = 6, i;
int fact = 1;
for(i=1;i<=n;i++)
_________;
printf("%d",fact);
return 0;
}
Which of the following lines should be inserted to complete the above code?
fact = fact + i
fact = fact * i
i = i * fact
i = i + fact
Question: What is the output of this C code?
#include <stdio.h>
int main()
{
int i=12;
int *p =&i;
printf(“%d\n”,*p++);
}
Address of i++
12
Garbage value
Address of i
Question: Convert the following 211 decimal number to 8-bit binary?
11011011
11001011
11010011
11010011
Question: A NAND gate has:
LOW inputs and a HIGH output
LOW inputs and a LOW output
HIGH inputs and a HIGH output
None of these
Question: Comment on the output of this C code?
#include <stdio.h>
int main()
{
int a = 1;
switch (a)
case 1:
printf("%d", a);
case 2:
printf("%d", a);
case 3:
printf("%d", a);
default:
printf("%d", a);
}
No error, output is 1111
No error, output is 1
Compile time error, no break statements
Compile time error, case label outside switch statement
Question: How will you find the maximum element in a binary search tree?
a)
public void max(Tree root)
{
while(root.left() != null)
{
root = root.left();
}
System.out.println(root.data());
}
b)
public void max(Tree root)
{
while(root != null)
{
root = root.left();
}
System.out.println(root.data());
}
c)
public void max(Tree root)
{
while(root.right() != null)
{ root = root.right();
}
System.out.println(root.data());
}
d)
public void max(Tree root)
{
while(root != null)
{
root = root.right();
}
System.out.println(root.data());
}
Question: What is the output of the following code?void my_recursive_function(int n)
{
if(n == 0)
return;
printf("%d ",n);
my_recursive_function(n-1);
}
int main()
{
my_recursive_function(10);
return 0;
}
10
1
10 9 8 … 1 0
10 9 8 … 1
For input a = 8 & b = 9.
function (input a, input b)
If (a < b)
return function (b, a)
elseif (b != 0)
return (a + function (a, b - 1))
else
return 0
56
78
82
72
Input m = 9, n = 6 ,
m = m + 1 ;
N = n - 1 ;
m = m + n
if (m > n)
print m
else
print n
6
5
10
15
Input f = 6, g = 9 and set sum = 0
Integer n if (g > f)
for (n = f; n < g; n = n + 1)
sum = sum + n
End for loop
else
print error message print sum
21
15
9
6
Consider a hash table with 9 slots. The hash function is h(k) = k mod 9. The collisions are resolved by chaining. The following 9 keys are inserted in the order: 5, 28, 19, 15, 20, 33, 12, 17, 10. The maximum, minimum, and average chain lengths in the hash table, respectively, are
3,0,1
3,3,1
4,0,1
3,0,2
You have an array of n elements. Suppose you implement a quick sort by always choosing the central element of the array as the pivot. Then the tightest upper bound for the worst case performance is:
O(n2)
O(nlogn)
O(logn)
O(n3)
Let G be a graph with n vertices and m edges. What is the tightest upper bound on the running time on Depth First Search of G? Assume that the graph is represented using adjacency matrix.
O(n2)
O(nlogn)
O(logn)
O(n3)
Let P be a Quick Sort Program to sort numbers in ascending order using the first element as a pivot. Let t1 and t2 be the number of comparisons made by P for the inputs {1, 2, 3, 4, 5} and {4, 1, 5, 3, 2} respectively. Which one of the following holds?
t1=5
t1>t2
t1<t2
t1=t2
public void func (Tree root)
{
func (root.left ());
func (root.right ());
System.out.println (root.data ());
}
preorder traversal
postorder traversal
inorder traversal
level order traversal
How will you find the minimum element in a binary search tree?
a) public void min (Tree root)
{
while (root.left () != null)
{
root = root.left ();
}
System.out.println (root.data ());
}
b) public void min (Tree root)
{
while (root != null)
{
root = root.left ();
}
System.out.println (root.data ());
}
c) public void min (Tree root)
{
while (root.right () != null)
{
root = root.right ();
}
System.out.println (root.data ());
}
d) public void min (Tree root)
{
while (root != null)
{
root = root.right ();
}
System.out.println (root.data ());
}
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]
A) 6 6 6
B) 6 5 6
C) 5 5 5
D) 6 5 4
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]
A) 0
B) 4
C) 5
D) 3
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
A) 15
B) 7
C) 2
D) 0
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]
A) 2
B) 13
C) 26
D) 5
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.
A. Only 1 and 3
B. Only 2 and 3
C. Only 1
D. Only 1 and 2
Consider an array of float. Calculate the difference between the address of the 1st and 4th element, assuming float occupies 4 bytes of memory.
A. 16
B. 4
C. 12
D. 8
What is the second part of a node in a linked list that contains the address of the next node called?
A. data
B. pointer
C. element
D. Link
