Table of Contents
Accenture Pseudo Code Question asked in accenture hiring in previous years related to Programming Fundamentals, Loops, Arrays, Recursion, Functions, Bitwise Operators, Increment & Decrement Operators and more.
Programming Fundamentals
Q: What will be the output of the following pseudocode?
Integer x
Set x = 5
x = x + 5
Print x
Answer: 10
Explanation: The value of x is initially set to 5, then increased by 5, resulting in 10.
Q: What does the following pseudocode do?
Integer x
Set x = 10
Print x
Answer: It prints the value 10.
Explanation: The pseudocode sets the variable x to 10 and then prints it.
Loops
Q: What will be the output of the following pseudocode?
Integer i
for(i = 0 to 4)
Print "Hello"
end for
Answer: “Hello” is printed 5 times.
Explanation: The loop runs from 0 to 4, executing 5 iterations.
Q: What is the value of sum after executing the following pseudocode?
Integer i, sum
Set sum = 0
for(i = 1 to 5)
sum = sum + i
end for
Answer: 15
Explanation: The loop calculates the sum of integers from 1 to 5.
Arrays
Q: What will be the output of the following pseudocode?
Integer arr[3] = {2, 4, 6} Print arr[1]
Answer: 4
Explanation: Arrays are zero-indexed, so arr[1] accesses the second element.
Q: What is the value of arr[2] after the following operations?
Integer arr[3] = {1, 2, 3}
arr[2] = arr[0] + arr[1]
Answer: 3
Explanation: arr[0] is 1 and arr[1] is 2, so arr[2] becomes 3.
Recursion
Q: What will be the output of the following pseudocode when fun(2) is called?
Integer fun(Integer n)
if (n <= 0) return 1
return n * fun(n - 1)
Answer: 2
Explanation: The function calculates the factorial of n . For n = 2 , it returns 2 fun(1) which results in 2.
Q: What does the following pseudocode do?
Integer fun(Integer n)
if (n == 0) return 1
else return n + fun(n - 1)
Answer: Computes the sum of integers from n to 1.
Explanation: This recursive function adds the current n to the result of fun(n-1) until n is 0.
Functions
Q: What is the result of the following pseudocode when compute(2) is called?
Integer compute(Integer x)
return x * x
Answer: 4
Explanation: The function compute squares the input value.
Q: What does the following pseudocode return when add(3, 4) is called?
Integer add(Integer a, Integer b)
return a+b
Answer: 7
Explanation: The function adds the two input values.
Bitwise Operators
Q: What will be the value of the following pseudocode?
Integer a = 5, b = 3
Print a & b
Answer: 1
Explanation: The bitwise AND of 5 (0101) and 3 (0011) is 1 (0001).
Q: What will be the output of the following pseudocode?
Integer x = 8
x = x << 1
Print x
Answer: 16
Explanation: The bitwise left shift operation << moves all bits in x one place to the left, doubling the value.
Q: What will be the output of the following pseudocode?
Integer x = 12
x = x >> 2
Print x
Answer: 3
Explanation: Bitwise right shift (>>) shifts bits to the right by the specified number of positions.
Q: What will be the output of the following pseudocode?
Integer a = 9, b = 5
Print a ^ b
Answer: 12
Explanation: Bitwise XOR (^) returns 1 if the corresponding bits are different.
Q: What will be the output of the following pseudocode?
Integer x = 7
x = ~x
Print x
Answer: -8
Explanation: Bitwise NOT (~) inverts all bits of the number.
Q: What will be the output of the following pseudocode?
Integer a = 4, b = 7
Print a & ~b
Answer: 0
Explanation: Bitwise AND (&) with the complement of b results in all zeros.
Q: What will be the output of the following pseudocode?
Integer x = 15
x = x << 3
Print x
Answer: 120
Explanation: Left shift by 3 positions multiplies the value by 2^3.
Q: What will be the output of the following pseudocode?
Integer a = 10, b = 15
Print a | ~b
Answer: -6
Explanation: OR with the complement of b results in a negative number.
Q: What will be the output of the following pseudocode?
Integer x = 24
x = x >> 4
Print x
Answer: 1
Explanation: Right shift by 4 positions divides the value by 2^4.
Q: What will be the output of the following pseudocode?
Integer a = 3, b = 6
Print a ^ ~b
Answer: -10
Explanation: XOR with the complement of b results in a negative number.
Q: What will be the output of the following pseudocode?
Integer x = -8
x = ~x
Print x
Answer: 7
Explanation: Inverting a negative number can lead to unexpected results due to two’s complement representation.
Q: What will be the output of the following pseudocode?
Integer a = 12, b = 8
Print a & ~b
Answer: 4
Explanation: AND with the complement of b isolates some bits.
Q: What will be the output of the following pseudocode?
Integer x = 64
x = x >> 6
Print x
Answer: 1
Explanation: Right shift by 6 positions divides the value by 2^6.
Q: What will be the output of the following pseudocode?
Integer a = 5, b = 10
Print a | ~b
Answer: -6
Explanation: OR with the complement of b results in a negative number.
Increment & Decrement Operators
Q: What will be the value of y after the following pseudocode?
Integer x = 5, y
y = ++x
Print y
Answer: 6
Explanation: The pre-increment operator ++x increases x by 1 before assigning it to y .
Q: What is the value of z after the following pseudocode?
Integer z = 5
z--
Print z
Answer: 9
Explanation: The post-decrement operator z– decreases z by 1 after its current value is used.
Conditional Statements
Q: What will be the output of the following pseudocode?
Integer a = 10,
b = 20
if (a > b)
Print "a is greater"
else
Print "b is greater"
Answer: “b is greater”
Explanation: Since a is not greater than b , the else block is executed.
Q: What does the following pseudocode do?
Integer n = 15
if (n % 2 == 0)
Print "Even"
else
Print "Odd"
Answer: Prints “Odd”
Explanation: The code checks if n is even or odd and prints the appropriate message. Since 15 is odd, “Odd” is printed.
Basics of Data Structures
Q: What does the following pseudocode do?
Stack S
S.push(10)
S.push(20)
S.pop()
Answer: Pushes 10 and 20 onto the stack, then pops 20.
Explanation: Stack operations follow Last-In-First-Out (LIFO) principle. After the pop , the stack contains only 10.
Q: How many elements are in the stack after executing the following pseudocode?
Stack s
s.push(1)
s.push(2)
s.pop()
s.push(3)
Answer: 2
Explanation: The stack contains the elements 1 and 3 after the operations.
Q: What does the following pseudocode do?
Stack S
S.push(5)
S.push(15)
S.peek()
Answer: Returns 15.
Explanation: Peeks at the top element without removing it.
Q: What does the following pseudocode do?
Stack S
S.push(10)
S.push(20)
S.pop()
S.isEmpty()
Answer: Returns false.
Explanation: After pop, the stack contains 10, so it’s not empty.
Q: What does the following pseudocode do?
Stack S
S.push(30)
S.push(40)
S.pop()
S.push(50)
S.size()
Answer: Returns 2.
Explanation: The stack contains 30 and 50 after the operations.
Q: What does the following pseudocode do?
Stack S
S.push(60)
S.push(70)
S.pop()
S.pop()
S.push(80)
S.peek()
Answer: Returns 80.
Explanation: The stack contains only 80 after the operations.
Q: What does the following pseudocode do?
Stack S
S.push(90)
S.push(100)
S.pop()
S.push(110)
S.pop()
S.isEmpty()
Answer: Returns false.
Explanation: The stack contains 90 after the operations.
Q: What does the following pseudocode do?
Stack S
S.push(120)
S.push(130)
S.peek()
S.pop()
S.push(140)
S.size()
Answer: Returns 2.
Explanation: The stack contains 120 and 140 after the operations.
Q: What does the following pseudocode do?
Stack S
S.push(150)
S.push(160)
S.pop()
S.pop()
S.push(170)
S.isEmpty()
Answer: Returns true.
Explanation: The stack is empty after the operations.
Q: What does the following pseudocode do?
Stack S
S.push(180)
S.push(190)
S.peek()
S.pop()
S.push(200)
S.peek()
Answer: Returns 200.
Explanation: The stack contains 180 and 200 after the operations.
Q: What does the following pseudocode do?
Stack S
S.push(210)
S.push(220)
S.pop()
S.push(230)
S.pop()
S.push(240)
S.size()
Answer: Returns 2.
Explanation: The stack contains 220 and 240 after the operations.
Q: What does the following pseudocode do?
Stack S
S.push(250)
S.push(260)
S.pop()
S.pop()
S.push(270)
S.push(280)
S.peek()
Answer: Returns 280.
Explanation: The stack contains 270 and 280 after the operations.
Additional Questions
Q: What will be the value of the following pseudocode?
Integer x = 7
Print x % 3
Answer: 1
Explanation: The modulo operator % returns the remainder of the division of x by 3.
Q: What will the following pseudocode output if n = 5 ?
Integer n
Print n * (n - 1)
Answer: 20
Explanation: The expression n * (n – 1) calculates the product of n and (n – 1).
Q: What will the following pseudocode output?
Integer x = 4,
y = 5
if (x == y)
Print "Equal"
else
Print "Not Equal"
Answer: “Not Equal”
Explanation: Since x is not equal to y , “Not Equal” is printed.
Q: What will be the output of the following pseudocode?
Integer i
for(i = 0 to 2)
Print "A"
end for
Answer: “A” is printed 3 times.
Explanation: The loop runs 3 times (i = 0, 1, 2), printing “A” each time.
Q: What will be the output of the following pseudocode?
Integer arr[5] = {1, 2, 3, 4, 5}
Integer i, sum = 0
for(i = 0 to 4)
sum = sum + arr[i]
end for
Print sum
Answer: 15
Explanation: The code calculates the sum of the array elements.
Q: What will be the output of the following pseudocode?
Integer x = 0
while (x < 3)
x = x + 1
Print x
Answer: 1 2 3
Explanation: The while loop increments x and prints it until x is no longer less than 3.
Integer a = 5
Integer b = a * a
Print b
Answer: 25
Explanation: The code calculates the square of `a` and as signs it to `b`.
Q: What will be the output of the following pseudocode?
Integer x = 3, y = 4
if (x < y)
Print x
else
Print y
Answer: 3
Explanation: Since x is less than y , the code prints x .
Q: What does the following pseudocode do?
Integer arr[3] = {10, 20, 30}
Integer i, sum = 0
for(i = 0 to 2)
sum = sum + arr[i]
end for
Print sum
Answer: Prints the sum of the array elements, which is 60.
Explanation: The loop iterates through the array, adding each element to sum.
Q: What is the result of the following pseudocode?
Integer x = 4
x = x << 2
Print x
Answer: 16
Explanation: Left shifting x by 2 bits is equivalent to multiplying by 4.
4. Q: What does the following pseudocode print?
Integer x = 5
x += 2
Print x
Answer: 7
Explanation: The += operator increments x by 2.
5. Q: What will the following pseudocode output?
Integer x = 7
if (x % 2 == 0)
Print "Even"
else
Print "Odd"
Answer: “Odd”
Explanation: The code checks if x is even or odd and prints the appropriate message.
6. Q: What does the following pseudocode do?
Integer x = 3
Print x * 2
Answer: Prints 6
Explanation: The code multiplies x by 2 and prints the result.
Q: What will the following pseudocode output?
Integer x = 1
while (x <= 5)
Print x
x = x + 1
Answer: 1 2 3 4 5
Explanation: The loop prints the values of x from 1 to 5.
Q: What is the value of z after the following pseudocode?
Integer x = 2, y = 3, z
z = x * y
Answer: 6
Explanation: The code multiplies x and y and assigns the result to z .
Q: What will the following pseudocode output?
Integer arr[3] = {1, 2, 3}
Integer i
for(i = 0 to 2)
Print arr[i]
Answer: 1 2 3
Explanation: The loop iterates through the array, printing each element.
Q: What will the following pseudocode output?
Integer x = 10
Print x / 2
Answer: 5
Explanation: The code divides x by 2 and prints the result.
Q: What is the result of the following pseudocode?
Integer x = 4, y = 3
Integer z = x % y
Print z
Answer: 1
Explanation: The modulo operator % returns the remainder of x divided by y .
Q: What is the result of the following pseudocode?
Integer arr[5] = {5, 4, 3, 2, 1}
Integer i, temp
for (i = 0 to 4)
temp = arr[i]
arr[i] = arr[4 - i]
arr[4 - i] = temp
Print arr
Answer: 1 2 3 4 5
Explanation: Reverses the array.
Q: What is the result of the following pseudocode?
Integer num = 1234
Integer sum = 0, digit
while (num > 0)
digit = num % 10
sum = sum + digit
num = num / 10
Print sum
Answer: 10
Explanation: Calculates the sum of digits of a number.
Q: What is the result of the following pseudocode?
Integer n = 5
Integer factorial = 1
for (i = 1 to n)
factorial = factorial * i
Print factorial
Answer: 120
Explanation: Calculates the factorial of a number.
Q: What is the result of the following pseudocode?
Integer num = 13
Boolean isPrime = true
for (i = 2 to sqrt(num))
if (num % i == 0)
isPrime = false
break
Print isPrime
Answer: true
Explanation: Checks if a number is prime.
Q: What is the result of the following pseudocode?
Integer arr[6] = {2, 3, 5, 7, 11, 13}
Integer n = 6
Integer i, j, temp
for (i = 0 to n-2)
for (j = 0 to n-i-2)
if (arr[j] > arr[j+1])
temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
Print arr
Answer: 2 3 5 7 11 13
Explanation: Sorts an array in ascending order using bubble sort.
Q: What is the result of the following pseudocode?
Integer n = 15
Integer count = 0
for (i = 1 to n)
if (n % i == 0)
count = count + 1
if (count == 2)
Print "Prime"
else
Print "Not Prime"
Answer: Prime
Explanation: Checks if a number is prime using divisibility test.
Q: What is the result of the following pseudocode?
Integer fib1 = 0, fib2 = 1, next, count = 10
Print fib1
Print fib2
for (i = 3 to count)
next = fib1 + fib2
Print next
fib1 = fib2
fib2 = next
Answer: 0 1 1 2 3 5 8 13 21 34
Explanation: Prints the first n Fibonacci numbers.
Q: What is the result of the following pseudocode?
Integer num = 1234
Integer reversed = 0, digit
while (num > 0)
digit = num % 10
reversed = reversed * 10 + digit
num = num / 10
Print reversed
Answer: 4321
Explanation: Reverses a number.
Q: What is the result of the following pseudocode?
Integer n = 5
for (i = 1 to n)
for (j = 1 to i)
Print "*"
Print
Answer:
*
**
***
****
*****
Explanation: Prints a right-angled triangle pattern.
Q: What is the result of the following pseudocode?
Integer n = 5
for (i = n; i >= 1; i--)
for (j = 1; j <= i; j++)
Print "*"
Print
Answer:
*****
****
***
**
*
Explanation: Prints an inverted right-angled triangle pattern.