TCS Ninja Programming Logic Questions
TCS Ninja Programming Logic Questions
Integer n
String s
Set s =”readwrite”
n = stringofLength(s)
Print (n ^ 1)
A. Error
B. 8
C. 1
D. 9
Answer: B
Explanation:
The given pseudocode takes a string s with the value "readwrite". It calculates
the length of the string, which is 8 characters. Then, it performs a bitwise XOR
operation (^) between the length n (which is 8) and 1. In binary, 8 is
represented as 1000, and XORing it with 1 flips the least significant bit,
resulting in 1001, which is 9 in decimal. Finally, it prints the result, which is 9.
C. Backtracking Algorithm.
D. Greedy Algorithm.
Answer: D
Explanation:
#include <stdio.h>
int main()
int i, j;
printf(“%d”,(i));
return 0;
A. Compilation Error.
Answer: C
Explanation:
The given C code initializes two variables i and j to 1. It enters a for loop with
the condition i<=j. However, both i and j are never modified inside the loop. As
a result, the loop body executes once, printing the value of i (which is 1) and
then terminates. Therefore, the output of the code is 1. Option C is correct.
#include <stdio.h>
int main()
while(!0 || !1)
if(~1)
printf(“A”);
break;
else
printf(“B”);
continue;
}
}
return 0;
A. A
D. B
Answer: A
Explanation:
The given C code contains a while loop with a condition while(!0 || !1). In C, !0
is true (represented as 1), and !1 is false (represented as 0). Therefore, the
loop condition (!0 || !1) is always true. Inside the loop, there's an if statement
with the condition if(~1), where ~1 is -2 in two's complement representation,
which is considered true in C. As a result, the code prints "A" and then breaks
out of the loop. Option A is the correct answer, and the output of the code is
"A".
Q5. What is the average case time complexity of Radix Sort, if n is the number
of elements in the array, k is
A. O(n+k)
B. Θ(n+k)
C. Θ(nk)
D. O(nk)
Answer: C
Explanation:
Radix Sort is a non-comparative sorting algorithm that processes the digits of
numbers to sort them. In the average case, the time complexity of Radix Sort is
Θ(nk), where n represents the number of elements in the array, and k is the
maximum number of digits in any element.
During each pass, the algorithm examines all n numbers and performs
operations based on their digits, iterating through k digits. Since Radix Sort
performs k passes and processes n elements in each pass, the total number of
operations is nk. This complexity holds true for the average case scenario,
making Radix Sort efficient when dealing with integers with a fixed number of
digits.
Q6. In which stage are the master test plan artifacts required to be completed
and reviewed in SDLC model?
Page 23 | 29
Answer: B
Explanation :
In the Software Development Life Cycle (SDLC) model, the master test plan
artifacts are typically required to be completed and reviewed during the
Testing sign-off stage. During this stage, the testing team evaluates the master
test plan to ensure that all necessary testing activities, resources, and
schedules are in place before moving forward with the testing process. The
correct answer is B. Testing sign-off stage.
#include<stdio.h>
int main()
int x = 25;
if(x == 30);
x = 50;
if(x == 25)
x = x + 1;
else
x = x - 1;
printf(“%d”, x);
return 0;
Answer: 49
Explanation:
The given C code initializes a variable x to 25. In the first if statement, if(x ==
30);, the condition is true (25 is not equal to 30), but it does nothing due to the
presence of a semicolon, making the statement an empty one. Consequently, x
is set to 50 in the next line. The second if statement, if(x == 25), is false
because x is now 50. The else block is executed, decreasing the value of x by 1.
Finally, the printf statement prints the current value of x, which is 49 after the
decrement. Therefore, the output of the code is 49.
Q8. How can you prevent memory leak in ‘C’ which you would have allocated
using dynamic memory
allocation?
A. erase(ptr)
B. free(ptr)
C. deallocate(ptr)
D. end
Answer: B
Explanation:
B. free(ptr)
After you allocate memory using functions like malloc(), calloc(), or realloc(),
it's essential to use the free() function to release that memory back to the
operating system once you're done using it. This prevents memory leaks and
ensures efficient memory management in C programs.
#include<stdio.h>
1. int main()
2. {
3. float num=2.0;
4. switch(num+5.0)
5. {
6. case 1:
8. case2;
10. case 3;
return 0;
The above program doesn’t compile and execute to give the output. Identify
which line has the error.
Answer: Line 4
Explanation:
The given C code snippet contains several errors that prevent it from compiling
and executing successfully. The main issues are:
Incorrect Format Specifier in printf: The printf statements inside the switch
cases have formatting errors. The placeholder %1 in the printf statements is
incorrect. The correct format specifier for floating-point numbers is %f.
Missing break Statements: Each case block inside a switch statement should
end with a break statement to prevent falling through to subsequent cases.
Without break statements, the program would continue executing the code in
the subsequent cases even if a matching case is found.
Default Case Formatting Error: The default case also lacks a colon at the end,
which is syntactically incorrect.
Incorrect Case Labels: The case labels don't match the value of num + 5.0
properly, leading to potential logical issues in the program logic.
The corrected version of the code addresses these issues by fixing the syntax
errors in the switch statement, providing correct format specifiers in printf
statements, adding break statements to terminate each case block, and
ensuring the case labels are appropriately matched.
Step 2 : Set i =0
Page 25 | 29
Go To Step 8
End if
[Missing Steps]
Step 8 : EXIT
write i+1
End if
Step 6 : i = i + 1
[End Loop]
write i+1
End if
Step 6 : i = i + 1
Step 7 : ptr = ptr -> next
[End Loop]
write i+1
End if
Step 6 : i = i + 1
[End Loop]
write i+1
End if
Step 6 : i = i + 1
[End Loop]
Answer: B
Explanation:
In a typical search algorithm for a linked list, you need to traverse the list until
you find the desired item or reach the end of the list (i.e., ptr != null). The
missing steps should include these traversal steps.
Option B contains the correct missing steps that ensure the algorithm
continues searching until ptr reaches the end of the list (ptr != null).
Specifically:
Step 6: Increment i by 1.
These steps ensure that the search continues until the end of the list is
reached, allowing the algorithm to properly find the desired item or indicate
that the item is not in the list.