Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

TCS Ninja Hands On Coding Questions with Solutions

TCS Ninja 2022 Hands on Coding Questions

TCS Ninja 2022 hands-on coding questions answers  with pattern has been announced as a part of TCS Test. TCS Ninja Hands on Coding Questions Paper with Solutions are given here on this page. Also, you will find TCS ninja coding questions with solved papers. There are 2 questions that you need to solve in 45 mins(30 + 15 mins)

  • No Of Questions:- 2 Question(1 Easy + 1 Hard)
  • Time – 45 mins(15 mins + 30  mins)
IMPORTANT NOTE:
  1. There will be no negative marking.
  2. TCS NQT is adaptive this year
  3. You will not get any extra rough paper in the exam as a calculator and Rough Paper will be available on your Desktop Screen. You are not allowed to move your eyes down while giving the examination.
Coding Question For
TCS Ninja
Number of Questions2
Time Allotted45 Mins
Negative MarkingNo
Cut Off
Positive Testcases are Necessary

Marking Scheme for TCS Hands on Coding Questions

Coding Marks
Marks
Type
0 Test Case
0 Marks
NA
1 Test Case
5 Marks
Shown
2 Test Case
8 Marks
Shown
3 Test Case
12 Marks
Hidden
4 Test Case
18 Marks
Hidden
5 Test Case
22 Marks
Hidden

Program 1

Word is the Key

Word is the Key 💖 (Link to this question)

 

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

Test cases

Case 1

  • Input – defer
  • Expected Output – defer is a keyword

Case 2

  • Input – While
  • Expected Output – while is not a keyword
#include<stdio.h>
#include<string.h>

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;
    scanf("%s",input);
    
    for(int i = 0; i<16;i++){
        if(strcmp(input,str[i]) == 0){
            flag = 1;
            break;
        }
    }
    
    if(flag==1){
        printf("%s is a keyword",input);
    }
    else{
        printf("%s is not a keyword",input);
    }
    return 0;
}
#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;
}
keyword = {"break", "case", "continue", "default", "defer", "else", "for", 
"func", "goto", "if", "map", "range", "return", "struct", "type", "var"}

input_var = input()
if input_var in keyword:
    print(input_var+ " is a keyword")
else:
    print(input_var+ " is a not keyword")
import java.util.Scanner;
public  class Prep
{
    public static voidmain(String args[])
    {

     String str[]= {"break", "case", "continue", "default", "defer", "else","for", "func", "goto", 
     "if", "map", "range", "return", "struct", "type", "var"};

    int flag = 0;
    Scanner sc = new Scanner(System.in);
    String input=sc.nextLine();

    for(int i = 0; i<16;i++){

        if(str[i].equals(input)){
            flag = 1;
            break;
        }
    }

    if(flag==1){
        System.out.println(input+" is a keyword");
    }
    else{
        System.out.println(input+" is not a keyword");
    }

}
}
//The above is code is contributed by Jonty Chakraborty (PrepInsta Placement Cell Student)

Program 2

Sweet Seventeen

Sweet Seventeen 💖 (Link to this question)

 

Given a maximum of four digit to the base 17 (10 – A, 11 – B, 12 – C, 13 – D … 16 – G} as input, output its decimal value.

Test Cases

Case 1

  • Input – 1A
  • Expected Output – 27

Case 2

  • Input – 23GF
  • Expected Output – 10980
#include <stdio.h>
#include <math.h>
#include <string.h>

int main(){

  char hex[17];
  long long decimal, place;

  int i = 0, val, len;
  decimal = 0;
  place = 1;

  scanf("%s",hex);

  len = strlen(hex);
  len--;

  for(i = 0;hex[i]!='\0';i++)
  {
    if(hex[i]>='0'&& hex[i]<='9'){

      //48 to 57 are ascii values of 0 - 9
      //say value is 8 its ascii will be 56
      //val = hex[i] - 48 => 56 - 48 => val = 8

      val = hex[i] - 48;
    }
    else if(hex[i]>='a'&& hex[i]<='g'){

      //97 to 103 are ascii values of a - g
      //say value is g its ascii will be 103
      //val = hex[i] - 97 + 10 => 103 - 97 + 10=> val = 16
      //10 is added as g value is 16 not 6 or a value is 10 not 0

      val = hex[i] - 97 + 10;
    }
    else if(hex[i]>='A'&& hex[i]<='G'){

      //similarly, 65 to 71 are values of A - G
      val = hex[i] - 65 + 10;
    }

    decimal = decimal + val * pow(17,len);
    len--;
  }

  printf("%lld",decimal);

  return 0;
}
#include <iostream>
#include <math.h>
#include <string.h>

using namespace std;
int main(){

  char hex[17];
  long long decimal, place;

  int i = 0, val, len;
  decimal = 0;
  place = 1;

  cin>> hex;

  len = strlen(hex);
  len--;

  for(i = 0;hex[i]!='\0';i++)
  {
    if(hex[i]>='0'&& hex[i]<='9'){

      //48 to 57 are ascii values of 0 - 9
      //say value is 8 its ascii will be 56
      //val = hex[i] - 48 => 56 - 48 => val = 8

      val = hex[i] - 48;
    }
    else if(hex[i]>='a'&& hex[i]<='g'){

      //97 to 103 are ascii values of a - g
      //say value is g its ascii will be 103
      //val = hex[i] - 97 + 10 => 103 - 97 + 10=> val = 16
      //10 is added as g value is 16 not 6 or a value is 10 not 0

      val = hex[i] - 97 + 10;
    }
    else if(hex[i]>='A'&& hex[i]<='G'){

      //similarly, 65 to 71 are values of A - G
      val = hex[i] - 65 + 10;
    }

    decimal = decimal + val * pow(17,len);
    len--;
  }

  cout<< decimal;

  return 0;
}
'''The int() function converts the specified value into an integer number.
We are using the same int() method to convert the given input.
int() accepts two arguments, number and base.
Base is optional and the default value is 10.
In the following program we are converting to base 17'''

num = str(input())
print(int(num,17))
import java.util.*;
public class Main
{
	public static void main(String[] args) {
		HashMap<Character,Integer> hmap = new HashMap<Character,Integer>();
		hmap.put('A',10);
		hmap.put('B',11);
		hmap.put('C',12);
		hmap.put('D',13);
		hmap.put('E',14);
		hmap.put('F',15);
		hmap.put('G',16);
		hmap.put('a',10);
		hmap.put('b',11);
		hmap.put('c',12);
		hmap.put('d',13);
		hmap.put('e',14);
		hmap.put('f',15);
		hmap.put('g',16);
		Scanner sin = new Scanner(System.in);
		
                String s = sin.nextLine();
		long  num=0;
		int k=0;
		
                for(int i=s.length()-1;i>=0;i--)
		{
		    if((s.charAt(i)>='A'&&s.charAt(i)<='Z')||(s.charAt(i)>='a' &&s.charAt(i)<='z'))
		       {
		           num = num + hmap.get(s.charAt(i))*(int)Math.pow(17,k++);
		       }
		    else
		       {
		        num = num+((s.charAt(i)-'0')*(int)Math.pow(17,k++));
		}
		}
		System.out.println(num);
	}
}

Program 3

Oddly Even

The Oddly even 💖 (Link to this question)

 

Problem

Given a maximum of 100 digit numbers as input, find the difference between the sum of odd and even position digits

Test Cases

Case 1

  • Input: 4567
  • Expected Output: 2

Explanation : Odd positions are 4 and 6 as they are pos: 1 and pos: 3, both have sum 10. Similarly, 5 and 7 are at even positions pos: 2 and pos: 4 with sum 12. Thus, difference is 12 – 10 = 2

Case 2

  • Input: 5476
  • Expected Output: 2

Case 3

  • Input: 9834698765123
  • Expected Output: 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    int a = 0,b = 0,i = 0, n;
    char num[100];
   
    printf("Enter the number:");
    scanf("%s",num);    //get the input up to 100 digit
    n = strlen(num);
    while(n>0)
    {
        if(i==0)         //add even digits when no of digit is even and vise versa
        {
            a+=num[n-1]-48;
            n--;
            i=1;
        }
        else            //add odd digits when no of digit is even and vice versa
        {
            b+=num[n-1]-48;
            n--;
            i=0;
        }
    }
    printf("%d",abs(a-b)); //print the difference of odd and even

    return 0;
}
#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std; int main() { int a = 0,b = 0,i = 0, n; char num[100]; cout<< "Enter the number:"; cin>> num; //get the input up to 100 digit n = strlen(num); while(n>0) { if(i==0) //add even digits when no of digit is even and vise versa { a+=num[n-1]-48; n--; i=1; } else //add odd digits when no of digit is even and vice versa { b+=num[n-1]-48; n--; i=0; } } cout<< abs(a-b); //print the difference of odd and even return 0; }
num = [int(d) for d in str(input("Enter the number:"))]
even,odd = 0,0
for i in range(0,len(num)):
    if i % 2 ==0:
        even = even + num[i]
    else:
        odd = odd + num[i]

print(abs(odd-even))

# code contributed by Shubhanshu Arya PrepInsta Placement Cell Student
import java.util.*;
public class Main
{
	public static void main(String[] args) {
		Scanner sin = new Scanner(System.in);
		String s=sin.nextLine();
		
                long num = 0, num1 = 0;
		num=num + s.charAt(0)-'0';
		
                for(int i=1;i<s.length();i++)
		{
		    if(i%2==0)
		        num = num + s.charAt(i)-'0';
		   else
		     num1 = num1 + s.charAt(i)-'0';
		}
		System.out.println(Math.abs(num-num1));
	}
}

Program 4

Prime Number with a Twist

Ques. Write a code to check whether no is prime or not. Condition use function check() to find whether entered no is positive or negative ,if negative then enter the no, And if yes pas no as a parameter to prime() and check whether no is prime or not?

 

#include<bits/stdc++.h>
using namespace std;

void prime(int num){
int count=0;
for(int i=2;i<num;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0){
cout<<"prime"<<endl;
}
else{
cout<<"Not Prime"<<endl;
}
}

int main(){
int n;
cout<<"Enter the number: ";
cin>>n;
if(n>0){
prime(n);
}
else{
cout<<"negative number.Please enter a postive number"<<endl;
}

return 0;
}
#include 
using namespace std;

void enter();
void check(int);
void prime(int);

int main()
{
enter();
return 0;

}

void check(int num)
{
if(num<0)
{
cout<<"invalid input enter value again"<<endl;
enter();
}
else
{
prime(num);
}
}
void enter()
{
int num;
cout<<"Enter number:";
cin>>num;
check(num);
}

void prime(int num)
{
int i,div=0;
for(i=1;i<=num;i++)
{
if(num%i==0)
{
div++;
}
}
if(div==2)
{
cout<<num<<" is a prime number";
}
else
{
cout<<" is not a prime number";
}
}
/* Write a progam in java and check if a number which is entered is prime number or not. However if the number
happens to be negative then we must ask the user to enter a positive number again*/


import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//First we will ask the user to enter a number
System.out.println("Enter value to be evaluated : ");
int n = sc.nextInt();
//create object of class CheckPrime
Main prime=new Main();
//calling function with value n, as parameter
prime.verify(n);
}
//function for checking number is positive or negative
void verify(int n)
{
if(n<0)
System.out.println("Negative number detected enter positive number");
else
calc(n);
}
//creating function for checking prime or not
void calc(int n)
{
int x=0;
for(int i=2;i<n;i++)
{
if(n%i==0)
++x;
}
if(x>=1)
System.out.println("The number that you have entered is not prime");
else
System.out.println("The number that you have entered is prime");
}
}

Program 5

Leap Year or not

Program to check if a year is Leap Year or not 💖

 

#include<stdio.h>

int main()

{

  int year;

  printf("Enter a year to check if it is a leap year\n");

  scanf("%d", &year);

  if (year%400 == 0) // Exactly divisible by 400 e.g. 1600, 2000

    printf("%d is a leap year.\n", year);

  else if (year%100 == 0) // Exactly divisible by 100 and not by 400 e.g. 1900, 2100

    printf("%d isn't a leap year.\n", year);

  else if (year%4 == 0) // Exactly divisible by 4 and neither by 100 nor 400 e.g. 2016, 2020

    printf("%d is a leap year.\n", year);

  else // Not divisible by 4 or 100 or 400 e.g. 2017, 2018, 2019

    printf("%d isn't a leap year.\n", year);  

  return 0
}
#include
using namespace std;
int main() { 
   int year = 2016;
   if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
      cout<<year<<" is a leap year";
   else
      cout<<year<<" is not a leap year";
   return 0; 
}

Output

2016 is a leap year

In the above program, if a year is divisible by 4 and not divisible by 100, then it is a leap year. Also, if a year is divisible by 400, it is a leap year.

This is demonstrated by the following code snippet.

if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
   cout<<year<<" is a leap year";
else
   cout<<year<<" is not a leap year";

The program to check if a year is leap year or not can also be written using nested if statements. This is given as follows −

import java.util.Scanner;
public class Demo {

    public static void main(String[] args) {

    	int year;
    	Scanner scan = new Scanner(System.in);
    	System.out.println("Enter any Year:");
    	year = scan.nextInt();
    	scan.close();
        boolean isLeap = false;

        if(year % 4 == 0)
        {
            if( year % 100 == 0)
            {
                if ( year % 400 == 0)
                    isLeap = true;
                else
                    isLeap = false;
            }
            else
                isLeap = true;
        }
        else {
            isLeap = false;
        }

        if(isLeap==true)
            System.out.println(year + " is a Leap Year.");
        else
            System.out.println(year + " is not a Leap Year.");
    }
}
# perl Script
# leap year
print "Enter Year: ";
$year=;
# condition to check for leap year
if( (0 == $year % 4) && (0 != $year % 100) || (0 == $year % 400) )
{
print "Leap year";
}
else
{
print "Not a leap year";
}
Output
Enter Year: 2016
Leap year
# Python Program to Check Leap Year using If Statement

year = int(input("Please Enter the Year Number you wish: "))

if (( year%400 == 0)or (( year%4 == 0 ) and ( year%100 != 0))):
    print("%d is a Leap Year" %year)
else:
    print("%d is Not the Leap Year" %year)
The Words Like “Placement Papers” and “Previous Year Papers” are used here for Google Search Purposes only and may not be. All these questions could be freely available on the internet, we are only charging students for the PrepInsta’s Mock Test experiences and Analytics as well as preparation for the exam. Prepinsta does not guarantee any recurrence of the questions in the exam however we believe that from our practise questions the exam should atleast be similar in pattern as per syllabus or difficulty. These are only practise mock questions. PrepInsta has compiled these from various internet sources and made them as per mock experience for students ease and are offering analytics as per performance

Comments