TCS NQT - Programming Logic

Reet Maggo
Last Updated: May 13, 2022

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:

#include <stdio.h>

int main()

{

   int i=3;

   switch(i)

   {

      case 0: printf("Purple");

      break;

      case 1+1: printf("Blue");

      break;

      case 7/2: printf("Yellow");

      break;

      case 3%2: printf("Black");

      break;

   }

   return 0;

}

1. Black

2. 3%2

3. error

4. Yellow

Answer and explanation: 

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: 

The range of a 10-bit unsigned integer is:

1. 0-1024

2. 0-1000

3. 0-1048

4. 0-1023

Answer and explanation: 

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.

# include<stdio.h> //line 1

 # include<conio.h> //line 2

 void main() //line 3

 { //line 4

    float num1 = 100.00; // line 5

   { // line 6

      auto float num1 = 675.29; // line 7

    { // line 8

           auto float num2 = 325; //line 9

          printf("\n%f %f", num1); //line 10

          num2++; // line 11

     } // line 12

     num1++; // line 13

     printf("\n%f %f", num2, num1); // line 14

     num1++; // line 15

  } // line 16

 printf("\n%f", num1); //line 17

 } // line 18

1. Line 2

2. Line 9

3. Line 12

4. Line 10

Answer and explanation: 

2. Line 9

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: 

#include <stdio.h>

int main()

  {

    double num = (0.4, 4.7, 7.4);  

    printf("%f", num);

    return 0;

  }

What will the output be?

1. 4.7

2. error 

3. 0.004 

4. 7.400000 

Answer and explanation: 

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: 

The in-order traversal of a binary tree is M J N H R K S, and its pre-order traversal is H J M N K R S. According to this, what would be the post-order traversal of the binary tree?

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

Answer and explanation: 

3. M N J R S K H

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: 

What will the given code result in?

#include <stdio.h>

int main()

  {

   int packets;

   for( ; ; packets++)

   {

      printf("%d", packets);

      if(packets++==6)

      break;

   }

}

1. 0 1 2 3 4 5 6

2. error

3. 0 2 4 6

4. infinite loop

Answer and explanation: 

0 2 4 6

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: 

What type of output would the following code produce?

#include <stdio.h>

int main()

{

    int num = 5;

    printf("%p\n", &num);

    return 0;

}

1. integer

2. boolean

3. error

4. variable address

Answer and explanation: 

Variable address

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

Question 8: 

Predict the output:

#include <bits/stdc++.h>

int main()

{

   int a=0;

   if(a++)

   {

      std::cout<<"Hello";

   }

   else{

   std::cout<<"Goodbye";

   }

   return 0;

}

1. Syntax error

2. Runtime error

3. Hello

4. Goodbye

Answer and explanation: 

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: 

What is the syntax for command-line arguments?

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[])

Answer and explanation: 

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

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[0] is the program name and all elements till argv[var-1] are command-line arguments.

Question 10: 

Arrays are also known as:

1. Identical variables

2. Variable collectives

3. Similar quantity variables

4. Subscripted variables

Answer and explanation: 

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: 

A user is trying to pop an element from some empty stack. Such a condition is exclusively called:

1. Underflow condition

2. Overflow condition

3. Garbage collection

4. Empty collection

Answer and explanation: 

1. Underflow condition

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: 

A given tree’s height starts from 0. Trisha wants to know the number of nodes in that tree. What do you think would be the number of nodes in a tree with ‘h’ height?

1. 2^h

2. 0

3. (h^2 – 1)

4. (2^h+1 – 1)

Answer and explanation: 

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: 

What is the time complexity for a merge-sort algorithm?

1. O(n)

2. O(log n)

3. O(n log(n))

4. O(n^2)

Answer and explanation: 

3. O(n log n)

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: 

How many moves are needed to solve the Tower of Hanoi puzzle?

1. 2^(n)-1

2. 2^(n)+1

3. 2^n

4. n^2

Answer and explanation: 

1. 2^(n)-1

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: 

What type of value does a dangling pointer point to?

1. Points to a null value

2. Points to 0 value

3. Points to a garbage value

4. Both 1 and 2

Answer and explanation: 

3. Points to a garbage value

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

Frequently Asked Questions

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.

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think