# TCS NQT - Programming Logic

## Introduction

Programming logic is usually based on both skill and conceptual understanding. While computer programming skills and knowing the languages efficiently is beneficial, having a solid foundation in CS is of utmost importance. To get the best results, we must learn to understand the most suitable inputs, the most precise output, and what arithmetic, conditional or iterative operations we must perform. Let us look at what the Programming Logic in TCS NQT would look like.

## Programming Logic in TCS NQT

TCS NQT Programming Logic definitely tests you for your C, C++, and Java skills, but that is not it. You must have a firm understanding of Computer Science fundamentals to answer questions in OOPS, Recursion, Threads, DSA, etc. There are an adequate number of questions based on Data Structures. Let us first revise the basic rules so that you are fully equipped to take the test:

• The programming logic section is a compulsory section of the TCS NQT test, even for non-CS or IT students.
• There are 10 questions in this section, with 15 minutes allotted for the same.
• This section has no negative marking but is considered fairly difficult.

The cut off for the programming logic section is 6 to 7 questions with about 8-9 topics, which include:

### Programming Logic section format:

• Data typesThere is either no or one question from data types which is relatively easy to answer if you have a clear understanding of the concept.
• Functions and loops- One question with average difficulty is asked for functions and loops. They have usually coded snippets that you will have to predict output for.
• Recursion, iteration- These too are traditionally mildly challenging questions that either require guessing the output or finding errors in the code.
• Arraysstacks and queues- These three closely related topics are reasonably hard to answer and need practice. However, there won’t be more than 1 question for the topic.
• Trees- About 1-2 relatively tricky questions that can be solved with good practice.
• Graphs, Command line programming and Greedy algorithms-1 challenging questions for all three topics. Solving practice questions for them helps understand them better.

## Sample Question Paper

We looked at a few recent tests and tried to frame similar questions for you, for which you can check your answers and read the explanations to have a clear understanding of the concept used.

Question 1:

Predict the output of the following code snippet:

1. Black

2. 3%2

3. error

4. Yellow

Initially, the value of i is 3. We perform a switch action, which can only be performed on char or int data types. For any more complex case than 0, the case takes anything up to the colon as a single entity.

Therefore, 1+1=2, 7/2=3(quotient) since it is of integer data type, 3%2=1(remainder). Now, there is a break after any case that is acted upon. This takes the user out of the switch once the case is executed so that there is no repetition.

Since the value of i is 3, it will go to case 3, i.e., “case 7/2:” and then the switch will break. The simple output for this code would thus be Yellow.

Question 2:

1. 0-1024

2. 0-1000

3. 0-1048

4. 0-1023

We know that the number of bits here is 10 and for every bit, there are 2 possible values(0 and 1) Since it is an unsigned integer, no bit is used to represent the sign and all 10 bits will represent the magnitude. Now, the total possible number would be 2^n=2^10=1024. Starting from 0, that gives us the range: 0-1023.

Question 3:

In the following code, identify the line with an error.

1. Line 2

2. Line 9

3. Line 12

4. Line 10

Since ‘num2’ is declared locally in an inner block, its scope doesn’t extend outside the block where it is accessed. To fix the code, we must declare ‘num2’ globally to give it global scope.

Question 4:

What will the output be?

1. 4.7

2. error

3. 0.004

4. 7.400000

The bracket operator is executed before the assignment operator due to higher precedence. The result will always be from the last expression assigned when evaluated left to right.

Question 5:

1. M J N R K S H

2. H J K M N R S

3. M N J R S K H

4. R S K H M N J

The binary tree is shown according to the in-order and preorder traversal. We can build the binary tree by considering its inorder transversal and pre-order transversal. The creation is as follows:

Pre-order traversal: Root->Left->Right

Inorder traversal: Left->Root->Right , Using the mentioned pattern we could construct the binary tree. For great detail refer to this article

Question 6:

1. 0 1 2 3 4 5 6

2. error

3. 0 2 4 6

4. infinite loop

We will need to assume that the initial value of packets is 0. Now, there is no initialisation or condition meaning that the for loop will run forever. However, a break statement later in the loop shows that if the ‘if’ condition is true, then the loop will terminate. First, the loop will print 0. Now, the next step is crucial.

Since there is a post-increment inside the if loop, we will check the condition first and then increment. The value of packets is 0, which is not equal to 6. The loop will perform a post-increment to 1 from the ‘if’ condition and go for iteration again. Now it will be incremented from the ‘for’ loop, too. Therefore, the value of packets will become 2. Again, we check the if condition and perform post-increment such that packages=3. It will go back to the for loop, increment and print 4. The loop will repeat in this manner until packets become precisely 6. Therefore the output would be 0, 2, 4 and 6.

Question 7:

1. integer

2. boolean

3. error

Since %p prints a pointer value, the following code will print the variable address.

Question 8:

1. Syntax error

2. Runtime error

3. Hello

4. Goodbye

People tend to get confused since there is no condition for ‘if’. However, it is straightforward. The value of ‘a’ is initialised as 0. Now, if the result of whatever we write is 0, it is considered false, while every non-zero result will be true. In post-increment, the value is checked before updation. So the condition will be false, and the else condition will print Goodbye.

Question 9:

1. int main(char c, int arg)

2. int main(int var, char*argv[])

3. int main(int v, char c)

4. int main(char*arv[], int arg[])

The syntax for command-line arguments is int main(int var, char*argv[]), where var is a positive integer variable to store the number of arguments passed by users (including the program name). The ‘argv’ or the argument vector, on the other hand, is an array for character pointers and lists all arguments. ‘argv is the program name and all elements till argv[var-1] are command-line arguments.

Question 10:

1. Identical variables

2. Variable collectives

3. Similar quantity variables

4. Subscripted variables

Since all elements in the array can be identified under the same name, with the index value (subscript value), arrays can also be called subscripted variables.

Question 11:

1. Underflow condition

2. Overflow condition

3. Garbage collection

4. Empty collection

In case a user tries to delete an element from a stack containing no elements at all, the condition is termed an underflow condition.

Question 12:

1. 2^h

2. 0

3. (h^2 – 1)

4. (2^h+1 – 1)

We know from the formula itself that: if h starts from 0, there will be (2^h+1 – 1) nodes; if h starts from 1, there will be (2^h – 1)nodes in the tree.

Question 13:

1. O(n)

2. O(log n)

3. O(n log(n))

4. O(n^2)

Merge sort divides the array into two halves and then merges both halves in linear time. Therefore the worst, average and best case time complexity will be O(n*Log n).

Question 14:

1. 2^(n)-1

2. 2^(n)+1

3. 2^n

4. n^2

In the Tower of Hanoi, for disc 1, we need 1 move. For disc 2, we need 3 moves and for disc 3 we need 5 moves. Hence, for n discs, we need 2^(n)-1 moves.

Question 15:

1. Points to a null value

2. Points to 0 value

3. Points to a garbage value

4. Both 1 and 2

Unreferenced objects or garbage value is pointed at by a dangling pointer.

Q1. How many times can you appear for TCS NQT?
Ans:  There is no limit as to how many times one can appear for the TCS NQT exam, but failing the written exam will mean not being able to write the exam again for the next 6 months. After that period, you can retake the exam.

Q2. Can we use Python in TCS NQT?
Ans: While Python is definitely allowed in the TCS NQT exam, it is discouraged because the TCS Python compiler usually takes very long to compile the code, which is a problem since it is not time efficient.

Q3. Is there negative marking in the Programming Logic section in TCS NQT?
Ans: No, there is no negative marking in Programming Logic TCS NQT. In fact, there is no negative marking in any section of the entire TCS NQT exam in 2022.

Q4. What is the average difficulty of the Programming Logic section?
Ans: The programming logic section in TCS NQT is considered fairly complex and requires good practice before taking the test. However, with an adequate understanding of concepts, one can certainly clear the cut off of 7 questions in this section.

## Key takeaways

In this article, we learned about the format, topics and concepts for the Programming logic section in TCS NQT. We solved questions similar to the previous papers and explanations for the answers so that the concepts are concrete in our knowledge.

You can find a lot of interesting articles and questions on all other topics of TCS NQT at Coding Ninjas. Visit CodeStudio to try and solve more problems for practice. Share this blog with your friends if you found it helpful! Until then, All the best for your future endeavours, and Keep Coding. 