TCS NQT frequently asked Coding Questions

In this article you will get all sample and frequently asked coding questions, which will help you to solve coding section in TCS NQT as well as Interview coding questions during Interview.

TCS NQT coding section pattern-

Provided pattern is according to Previous year National Qualifier Test(NQT).

  • Two questions to be completed in 45 minutes.
  • Languages allowed are C/ C++/ Java/ Python/ PERL.
  • Strong knowledge in Fundamentals of Programming has ensured that students are able to crack this section.
  • To gain max opportunities of cracking this section, students must try to clear all of the possible test cases.

Programming Questions-

  1. Swap two numbers without using the third variable.
#include <stdio.h> 
int main()
{
    int x, y;   
    printf("Input two integers to swap\n");
    scanf("%d%d", &x, &y);
    //logic   
    x = x + y;
    y = x - y;
    x = x - y; 
    printf("%d %d",x,y);
    return 0
} 

Input-2 5

Output-5 2

2. Find the factorial of number using recursion.

#include <stdio.h>
long int fact(int n);
int main()
{
    int n;
    printf("Enter a positive integer: ");
    scanf("%d", &n);
    printf("%d",fact(n));
    return 0;
}
long int fact(int n)
{
    if (n >= 1)
        return n*fact(n-1);
    else
        return 1;
}

Input– 4

Output– 24

3. Calculate the sum of even elements of array.

#include <stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    int array[n];
    int sum = 0;
    for(int i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    for(int i=0; i<n; i++)
    {
        if (array[i]%2==0)
            sum = sum + array[i];
    }
    printf("%d",sum);
    return 0;
}

Input– 5

1 2 3 4 5

Output– 6

4. Print Fibonacci Series upto n terms.

#include<stdio.h>
int main()
{
    //To initialize variables 
    int n1=0,n2=1,n3,limit,i;
    //To take user input
    printf("enter a limit of series \n");
    scanf( "%d",&limit);
    printf("Fibonacci series %d %d ",n1,n2);
    //To use this loop for given length
    for(i=2;i<limit;i++)
    {
        //n1 and n2 sum  store in new variable n3
        n3=n1+n2;   
        n1=n2;
        n2=n3;
        //display serious 
        printf("%d ",n3);
    }
    return 0;
}

Input– 4

Output– 0 1 1 2

5. Program to find the sum of each row of 2d matrix.

#include<stdio.h>
int main()
{
    int n, m;
    scanf("%d %d",&n,&m);
    int sum = 0;
    int array[n][m];
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            scanf("%d",&array[i][j]);
        }
    }
    for(int i=0; i<n; i++)
    {
        sum = 0;
        for(int j=0; j<m; j++)
        {
            sum = sum + array[i][j];
        }
        printf("%d ",sum);
    }
    return 0;
}

6. Program to Convert Octal number into Binary Number.

#define MAX 1000
int main()
{
    char octalnum[MAX];
    //For initialize 
    long i = 0;
    //taking user input of octalnum
    printf("Insert an octal number: ");
    scanf("%s", octalnum);
    printf("Equivalent binary number: ");
    while (octalnum[i])
    { 
       //use switch case for multiple condition
        switch (octalnum[i])
        {
          case '0':
               printf("000"); break;
          case '1':
               printf("001"); break;
          case '2':
               printf("010"); break;
          case '3':
               printf("011"); break;
          case '4':
               printf("100"); break;
          case '5':
               printf("101"); break;
          case '6':
               printf("110"); break;
          case '7':
               printf("111"); break;
         //for invalid case 
          default:
               printf("\n Invalid octal digit %c ", octalnum[i]);
               return 0;
        }
        i++;
    }
    return 0;
}

7. Program to count vowels and consonant in a sentence.

#include <stdio.h>
int main()
{
    char str[100];
    scanf("%[^\n]s",str);
    int i = 0;
    int vowels=0, consonant=0, space=0;
    while(str[i]!='\0')
    {
        if(str[i]=='a' ||str[i]=='e' ||str[i]=='i' ||str[i]=='o' ||str[i]=='u')
        {
            vowels++;
        }
        else if(str[i]==32)
        {
            space++;
        }
        else
        {
            consonant++;
        }
        i++;
    }
    printf("vowels : %d\nconsonant : %d",vowels,consonant);
    return 0;
}

Input– GaneshJondhalekar

Output– vowels:6

consonant:11

8. Finding the frequency of elements in an array.

#include <stdio.h>
int main()
{
    int n;
    int i, j, count=0, unique = 0;;
    scanf("%d",&n);
    int array[n];
    int newarray[n];
    for(i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    for(i=0; i<n; i++)
    {
        count = 0;
        for(j=0;j<=i;j++)
        {
            if(array[i]==array[j])
            {
                count++;
            }
        }
        if(count==1)
        {
            newarray[unique] = array[i];
            unique++;
        }
    }
    for(i=0; i<unique; i++)
    {
        count = 0;
        for(j=0; j<n; j++)
        {
            if(newarray[i]==array[j])
            {
               count++;
            }
        }
        printf("%d element is present %d times\n",array[i],count);
    }
    return 0;
}

9. To check whether the number is prime by not using recursion function.

#include<stdio.h>
// declaring the recursive function
int isPrime(int, int);
int main()
{
    int num, prime;
    printf("Enter number : ");
    scanf("%d", &num);
    prime = isPrime(num, num/2);
    if(prime == 1)
    {
        printf("\n%d is a prime number\n", num);
    }
    else
    {
        printf("\n%d is not a prime number\n", num);
    }
    return 0;
}
// function definition
int isPrime(int n, int i)
{
    if(i == 1)
        return 1;   
    else
    {
        if(n%i == 0)
            return 0;
        else
            isPrime(n, i-1);    
    }
}

Input– 3

Output– 3 is a Prime Number.

10. To check whether the given year is leap or not leap.

#include <stdio.h>
int checkYear(int year) 
{ 
    if (year % 400 == 0) //condition for leap year
        return 1; 
    if (year % 100 == 0) //condition for leap year
        return 0; 
    if (year % 4 == 0) //condition for leap year
        return 1; 
    return 0; 
} 
  
int main() 
{ 
    int year = 2000; 
    checkYear(year)? printf("Leap Year"): printf("Not a Leap Year"); 
    return 0; 
} 

11. One programming language has the following keywords that cannot be used as identifiers:

break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type, var.

Write a program to find if the given word is a keyword or not.

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
  
    char str[16][10] = {"break", "case", "continue", "default", "defer", "else","for","func", "goto", "if", "map", "range", "return", "struct", "type", "var"};    
    char input[20];    
    int flag = 0;
    cin >> input;   
    for(int i = 0; i<16;i++)
    {
        if(strcmp(input,str[i]) == 0)
        {
            flag = 1;
            break;
        }
    }  
    if(flag==1)
    {
        cout << input << " is a keyword";
    }
    else
    {
        cout << input << " is not a keyword";
    }
    return 0;
}

12. Write a program to find Palindrome of a given number.

#include<stdio.h>
int main()
{
     //Initialization of variables where rev='reverse=0'
      int number, rev = 0,store, n1,left;

     //input a numbers for user
     printf("Enter the number\n");
     scanf("%d", &number);

     //for duplicacy of number
      n1=number;

      store= number;
      //use this loop for check true condition
      while (number > 0)
      {
           //left is for remider are left
            left= number%10;

            //for reverse of no.
             rev = rev * 10 + left;

             //number /= 10;
              number=number/10;
       }
        //To check reverse no is a Palindrome 
        if(n1==rev)
            printf("It is a Palindrome number");
        else
            printf("It is Not a Palindrome Number");
    return 0;
}

Input– 42145

Output– It is Not a Palindrome Number

Data structure interview questions

Interview Questions On Object oriented programming

SQL Interview questions

Snake game using python

c interview questions

Leave a Comment