Explore
Placement Prep

Edit

Edit

# TCS NQT coding questions and answers for TCS NQT Coding test | Face Prep

Published on 11 Mar 2020

The coding question asked in slot 1 of the exam was of this sorts.

1) One programming language has the following keywords that cannot be used as identifiers - break, case, continue, default, defer else, for, func, goto, if, return, struct, type. Now write a program to check if the given word is a keyword or not.

TCS NQT coding questions are a must to prepare to clear the TCS NQT test. We are going to discuss all the previously asked TCS Ninja coding questions in this article. These will help you prepare for any kind of coding questions asked in the exam

## TCS NQT Coding questions - Important instructions

### Instructions

1) Only One question, 20 minutes.

2) Choice of C / C++ / Java / Perl / Python 2.7.

3) Provided an IDE to debug.

4) For Java, the class name should be named Maze.

5)Input to the program either through STDIN / Command line arguments, as per the instructions.

6)Program should write the output to STDOUT.

7) Public and private test cases based evaluation.

### Points to note

1) While printing the output no leading or trailing spaces should be printed.

2) Other than the required output, no other text should be printed.

3) If the output is a number, no leading sign must be printed unless it is a negative number.

4) No scientific notation (3.9265E + 2).

5) All floating point numbers must contain that many decimal places as mentioned in the question.

## TCS Ninja Coding Questions 2019 with solutions

Note: The below given questions were asked in TCS Ninja National Qualifier test 2018.

### TCS Ninja coding questions - 1

Consider the below series:

1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 13, 17, ...

This series is a mixture of 2 series - all the odd terms in this series form a Fibonacci series and all the even terms are the prime numbers in ascending order.

Write a program to find the Nth term in this series.

The value N is a Positive integer that should be read from STDIN. The Nth term that is calculated by the program should be written to STDOUT. Other than the value of Nth term, no other characters/strings or message should be written to STDOUT.

For example, when N = 14, the 14th term in the series is 17. So only the value 17 should be printed to STDOUT.

Solution in C

#include<stdio.h>
#define MAX 1000
void fibonacci(int n)
{
int i, t1 = 0, t2 = 1, nextTerm;
for (i = 1; i<=n; i++)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
printf("%d", t1);
}
void prime(int n)
{
int i, j, flag, count =0;
for (i=2; i<=MAX; i++)
{
flag = 0;
for (j=2; j<i; j++)
{
if(i%j == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
if(++count == n)
{
printf("%d", i);
break;
}
}
}
int main()
{
int n;
scanf("%d", &n);
if(n%2 == 1)
fibonacci (n/2 + 1);
else
prime(n/2);
return 0;
}



Solution in C++

#include<iostream>
using namespace std;
#define MAX 1000

void fibonacci(int n)
{
int i, t1 = 0, t2 = 1, nextTerm;
for (i = 1; i<=n; i++)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
cout << t1;
}
void prime(int n)
{
int i, j, flag, count =0;
for (i=2; i<=MAX; i++)
{
flag = 0;
for (j=2; j<i; j++)
{
if(i%j == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
if(++count == n)
{
cout << i;
break;
}
}
}
int main()
{
int n;
cin >> n;
if(n%2 == 1)
fibonacci (n/2 + 1);
else
prime(n/2);
return 0;
}


Output:

Input: 14
Output: 17


### TCS Ninja coding questions - 2

Given a series whose even term creates a separate geometric series and odd term creates another geometric series.

Write a program to generate such series.For example,1, 1, 2, 2, 4, 4, 8, 8, 16, 16,......

Solution in C

#include

int main()
{
int n, i, r1, r2;
printf("nEnter the total number of terms : ");
scanf("%d", &n);
printf("nEnter the common ratio for GP - 1 : ");
scanf("%d", &r1);
printf("nEnter the common ratio for GP - 2 : ");
scanf("%d", &r2);
printf("nThe series isn");
int a = 1, b = 1;
if(n % 2 == 0)
{
for(i = 0; i < n/2; i++)
{
printf("%d ", a);
a = a * r1;
printf("%d ", b);
b = b * r2;
}
}
else
{
for(i = 0; i < n/2; i++)
{
printf("%d ", a);
a = a * r1;
printf("%d ", b);
b = b * r2;
}
printf("%d ", a);
}
printf("n");

}


Solution in C++

#include<iostream>
using namespace std;

int main()
{
int n, i, r1, r2;
cout << "nEnter the total number of terms : ";
cin >> n;
cout << "nEnter the common ratio for GP - 1 : ";
cin >> r1;
cout << "nEnter the common ratio for GP - 2 : ";
cin >> r2;
cout << "nThe series isn";
int a = 1, b = 1;
if(n % 2 == 0)
{
for(i = 0; i < n/2; i++)
{
cout << a << " ";
a = a * r1;
cout << b << " ";
b = b * r2;
}
}
else
{
for(i = 0; i < n/2; i++)
{
cout << a << " ";
a = a * r1;
cout << b << " ";
b = b * r2;;
}
cout << a << " ";
}
cout << endl;
}


Input:﻿

Enter the number of terms : 10
Enter the common ratio for G.P - 1 : 2
Enter the common ratio for G.P - 2 : 3

Output:

The series is
1 1 2 3 4 9 8 27 16 81


## Most asked TCS Ninja Coding questions so far

TCS Ninja coding questions range from easy to medium difficulty level. TCS Ninja coding test questions generally get repeated from the same set of concepts and programs. Here are some of the most asked TCS Coding questions with solutions in all possible languages are given.

Practice more TCS Ninja coding questions