0% found this document useful (0 votes)
4 views

TCS Ninja Programming Logic Questions

The document contains a series of programming logic questions and their explanations, primarily focused on C programming and algorithm techniques. It covers topics such as pseudocode output, algorithm suitability for the Traveling Salesman Problem, time complexity of Radix Sort, and memory management in C. Each question is followed by a detailed explanation of the correct answer and the reasoning behind it.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

TCS Ninja Programming Logic Questions

The document contains a series of programming logic questions and their explanations, primarily focused on C programming and algorithm techniques. It covers topics such as pseudocode output, algorithm suitability for the Traveling Salesman Problem, time complexity of Radix Sort, and memory management in C. Each question is followed by a detailed explanation of the correct answer and the reasoning behind it.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

TCS Ninja Programming Logic Questions:

Q1. What is the output of the following pseudocode?

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.

Q2. Which algorithm technique is most suitable to implement to solve


travelling salesman problem?

A. Divide and Conquer.

B. Floyd Warshall Algorithm.

C. Backtracking Algorithm.

D. Greedy Algorithm.
Answer: D

Explanation:

The Greedy Algorithm is a technique used to solve the Traveling Salesman


Problem (TSP) by making locally optimal choices at each step. It starts with an
arbitrary city as the initial point and repeatedly selects the nearest unvisited
city to the current city. This process continues until all cities have been visited,
and the salesman returns to the initial city, forming a closed tour. While the
greedy approach does not guarantee the optimal solution, it provides a
relatively quick and feasible solution for large datasets. However, it may not
always produce the shortest possible route, as it lacks a global perspective and
can get stuck in local optima. Despite this limitation, it is widely used due to its
simplicity and efficiency in finding approximate solutions to TSP instances.

Q3. What is the output of the below code?

#include <stdio.h>

int main()

int i, j;

for(i=1, j=1; i<=j; i++, j--)

printf(“%d”,(i));

return 0;

A. Compilation Error.

B. Nothing will print.


C. 1

D. Loop will execute infinite time.

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.

Q4. . What is the output of the below code?

#include <stdio.h>

int main()

while(!0 || !1)

if(~1)

printf(“A”);

break;

else

printf(“B”);

continue;

}
}

return 0;

A. A

B. Nothing will print.

C. B will print infinite time.

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

the maximum number of digits in any element of the array?

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?

A. Deployment into Production stage

B. Testing sign-off stage

C. Exit Criteria stage of design phase

D. Brainstorming stage of Requirement analysis

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.

Q7. What is the output of the code below?

#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:

To prevent memory leaks in C, which occur when dynamically allocated


memory is not properly deallocated, you need to use the free() function. So
the correct answer is:

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.

Q9. What is the output of the code below?

#include<stdio.h>

1. int main()

2. {

3. float num=2.0;

4. switch(num+5.0)

5. {

6. case 1:

7. printf(“Case1: Value is : %1”, num);

8. case2;

9. printf(Case1: Value is: %1”, num);

10. case 3;

11. printf(“Case1: Value is: %1”, num);


12. default;

13. printf(“Default: Value is: %1”, num);

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:

Syntax Error in Switch Statement: The switch statement lacks proper


formatting of case labels. Each case label should end with a colon (:) but in the
original code, they end with semicolons (;), which is incorrect.

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.

Q10. What will be the missing steps of the algorithm to search.

Step 1: set ptr = head

Step 2 : Set i =0

Page 25 | 29

Step 3 : If ptr = null

write “EMPTY LIST”

Go To Step 8

End if

[Missing Steps]

Step 8 : EXIT

A. Step 4 : Repeat Step 5 to 7 until ptr !=null

Step 5: if ptr -> data=item

write i+1

End if

Step 6 : i = i + 1

Step 7 : ptr = next -> ptr

[End Loop]

B. Step 4 : Repeat Step 5 to 7 until ptr !=null

Step 5: if ptr -> data=item

write i+1

End if

Step 6 : i = i + 1
Step 7 : ptr = ptr -> next

[End Loop]

C. Step 4 : Repeat Step 5 to 7 until ptr =null

Step 5: if ptr -> data=item

write i+1

End if

Step 6 : i = i + 1

Step 7 : ptr = ptr -> data

[End Loop]

D. Step 4 : Repeat Step 5 to 7 until ptr ->data = item

Step 5: if ptr -> data=item

write i+1

End if

Step 6 : i = i + 1

Step 7 : ptr = ptr -> next

[End Loop]

Answer: B

Explanation:

Here's the 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 4: Repeat Steps 5 to 7 until ptr != null.

Step 5: If ptr->data == item, write i+1.

Step 6: Increment i by 1.

Step 7: Move to the next node: ptr = ptr->next.

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.

You might also like