Post thumbnail
CAREER

Top 9 TCS Xplore Python Coding Questions [DeCode with GUVI]

TCS Xplore is a learner-centric program with a 120-hour progressive induction curriculum. Find the most important TCS Xplore Python Coding questions here.

A tech career in India’s IT trendsetter Tata Consultancy Services (TCS), is one of the top aspirations of most Indian techies. If you are still exploring TCS, let me tell you that TCS was India’s first listed company to hit a $100 billion m-cap. So, rightly called the crown jewel of Indian IT. 

Whether you are a seasoned IT Expert or a beginner just trying to discover your identity on the ever-green IT planet, joining an Indian venture like TCS would open up unlimited opportunities. 

Xplore 1 and Xplore 2 both are self and pre-learning programs for TCS NINJA Fresher’s 2019. All the TCS NINJA freshers are given the option to select either Xplore1 or Xplore2.

TCS Xplore python coding questions

 For your information,

Table of contents


  1. The TCS Xplore program is coupled with a plethora of engagements which include:
  2. Top TCS Xplore Python coding questions & answers:
    • Q1. Write a Python code to print the position or index of a given string (taken as input from a user) from a given list of strings.
    • Q2. Make a function check_palindrome() that takes a list of strings as an argument. It returns the string which is a palindrome.
    • Q3. Create a function count_words() which takes a string as input and creates a dictionary with a word in the string as a key and its value as the number of times the word is repeated in the string. It should return the dictionary.
    • eg: "hello hi hello world hello"
    • dict={'hello':3,'hi':1,'word':1}
    • Q4. Write a Python program to create a class called mobile which contains a method called display which displays the name of the mobile owner, mobile brand, colour and camera pixel.
    • Q5. Write a Python program to calculate the salary of the temporary staff using Multilevel Inheritance.
    • Q6. Write a Python program to check the quantity of petrol in the bike using exception handling.
    • Q7.Write a Python program to display the Passport details of the Person using composition.
    • Q8. Longest Increasing Subsequence
    • Q9. Consider a row of n coins. We play a game against an opponent by alternative turns. In each turn, a player selects either the first or last coin from the row. Now remove it from the row permanently and take the value of a coin. Find the maximum possible amount of money.
  3. Wrapping Up

The TCS Xplore program is coupled with a plethora of engagements which include:

  • AsCEnD: This program will assist you in acing your Digital skills. You will also be certified and then can flaunt your competencies.
  • Hackathons: After mastering digital skills, you might look forward to testing the same. Then comes a fantastic platform to test your skills in the form of hackathons. You can showcase your prowess in Digital Technologies via hackathons.
  • Digital Connect Series: Get insights from leaders on the Organization’s strategy for growth, current industry challenges and expectations from the customers
  • Business Unit Connect Business inclusive learning experience through quizzes, hackathons, Leader Connects etc.
  • Internships: Opportunity to learn and grow at one of the leading IT companies in the world.

So, let us now quickly jump into the

Top TCS Xplore Python coding questions & answers:

If you are aspiring to explore Python through a self-paced course, try GUVI’s Python self-paced certification course with IIT Certification.

Q1. Write a Python code to print the position or index of a given string (taken as input from a user) from a given list of strings.

Ans. The program will take input from the user in the form of a string and will pass the string as an argument to a function. The function will take the strings as arguments and return the Position(or index) of the list if the passed string is present in the list, else it'll return "String not found". If the passed strings are present at multiple indices, in that case, the function should only return The first index of occurrence.

Considering the above scenario into account, build the logic to print the position of the passed string from a given list of strings.

Refer to the below instructions and sample input-Output for more clarity on the requirement.

Input:

4

Hello Good Morning

abcd123Fghy

India

Progoti.c

India

Output:

The position of the searched string is: 2

def isPresent(lis,st):

    for i in range(0, len(lis)):

        if lis[i] == st:

            return i

lis = []

for j in range(int(input())):

    lis.append(input())

st = input()

ind = isPresent(lis,st)

if ind == -1:

    print("String not found")

else:

    print("Position of the searched string is: ",ind)

Q2. Make a function check_palindrome() that takes a list of strings as an argument. It returns the string which is a palindrome.

Input:

3

malayalam

radar

nitish

Output:

malayalam

radar

code:

def check_palindrome(lis):

    palin_lis = []

    for i in lis:

        if i == i[::-1]:

            palin_lis.append(i)

    return palin_lis

lis = []

for i in range(int(input())):

    lis.append(input())

for _ in check_palindrome(lis):

    print(_)

Q3. Create a function count_words() which takes a string as input and creates a dictionary with a word in the string as a key and its value as the number of times the word is repeated in the string. It should return the dictionary.

eg: "hello hi hello world hello" 

          dict={'hello':3,'hi':1,'word':1}

Create another function max_accurance_word() which takes a string as input and returns the word which is occurring a maximum number of times in the string. Use the count_words function inside this function.

Sample input:

"hello hi hello world hello"

Sample output:

'hello'

Code:

def count_words(string):

    l=string.split()

    s=set(l)

    d={}

    for i in s:

        x=l.count(i)

        d[i]=x

    return d

def max_occurance(string):

    d=count_words(string)

    l1=[]

    for i in d.values():

        l1.append(i)

    max1=max(l1)

    for i in d.keys():

        if d[i]==max1:

            return i

string=input() 

print(max_occurance(string))

Q4. Write a Python program to create a class called mobile which contains a method called display which displays the name of the mobile owner, mobile brand, colour and camera pixel.

Input Format:

String => name

String => brand name

String => color

Integer => pixel

Output Format:

Output is a String

Sample Input:

Dinesh

Lenovo vibe K5 note

gold

13

Sample Output:

Dinesh's own Lenovo vibe K5 note gold colour smartphone has a 13 MP camera

Case 1

Case 2

Input (stdin)

Dinesh

Lenovo vibe K5 note

gold

13

Output (stdout)

Dinesh's own Lenovo vibe K5 note gold colour smartphone has a 13 MP camera

Input (stdin)

Manoj

Vivo v11

white

21

Output (stdout)

Manoj own Vivo v11 white colour smartphone having a 21 MP camera

Code:

class mobile:

  def __init__(self,owner,brand,color,camera):

    self.owner = owner

    self.brand = brand 

    self.color = color 

    self.camera = camera

  def display(self):

    print("{owner} own {brand} {color} color smartphone having {camera} MP camera".format(owner = self.owner,brand = self.brand,color = self.color,camera = self.camera))

a= input()

b= input()

c= input()

d= input()

obj = mobile(a,b,c,d)

obj.display()

Read out everything Python and beyond here: https://www.guvi.in/blog/?s=Python

Q5. Write a Python program to calculate the salary of the temporary staff using Multilevel Inheritance.

Description:

Create a class Person which contains a constructor __init__() and a method display(self). The method displays the name of the person

Create another class Staff which inherits Person. It contains a constructor __init__() and a method display(self). The method displays Id.

Create another class Temporarystaff which inherits Staff, it also contains a constructor __init__() and two method displays (self), and Salary(self).

The method Salary(self) returns the total salary earned. The method display(self) displays a number of days, hours worked and total salary earned.

salary earned = total hours worked *150

Input Format:

String => name

Integer => Id

Integer => number of days

Integer => hoursworked

Output Format:

All outputs contain strings and integers.

Sample Input:

Tilak

157934

20

8

Sample Output:

Name of Person = Tilak

Staff Id is = 157934

No. of Days = 20

No. of Hours Worked = 8

Total Salary = 24000

Case 1

Case 2

Input (stdin)

Tilak

157934

20

8

Output (stdout)

Name of Person = Tilak

Staff Id is  = 157934

No. of Days = 20

No. of Hours Worked = 8

Total Salary = 24000

Input (stdin)

Praveen

124563

26

6

Output (stdout)

Name of Person = Praveen

Staff Id is  = 124563

No. of Days = 26

No. of Hours Worked = 6

Total Salary = 23400

Code:

class employee:

  def __init__(self,name,id,days,hours):

    self.name = name

    self.id = id

    self.days = days

    self.hours = hours

  def display(self):

    print("Name of Person = {name}\nStaff Id is  = {id}\nNo. of Days = {days}\nNo. of Hours Worked = {hours}\nTotal Salary = {salary}".format(name=self.name,id=self.id,days=self.days,hours=self.hours,salary=self.days*self.hours*150))

a = input()

b = int(input())

c = int(input())

d = int(input())

obj = employee(a,b,c,d)

obj.display()

Q6. Write a Python program to check the quantity of petrol in the bike using exception handling.

If there is no petrol i.e. null in the bike it should raise an exception. That exception is handled by using except block and it should print "There is no fuel in the bike". Otherwise, it should the show quantity of petrol on the bike. 

Input Format:

The input consists of a string which denotes a fuel.

Output Format:

Output is a String

Sample Input:

40

Sample Output:

Petrol Quantity = 40

Case 1

Case 2

Input (stdin)

40

Output (stdout)

Petrol Quantity =  40

Input (stdin)

NulL

Output (stdout)

There is no fuel in the Bike

Code:

a=input()

try:

  if(a.lower()!='null'):

    print("Petrol Quantity = ",a)

  else:

    raise ValueError

except(ValueError) :

  print("There is no fuel in the Bike")

Some more TCS Xplore Python coding questions & answers:

Q7.Write a Python program to display the Passport details of the Person using composition.

Description:

Create a class Passport and class Person. Compose the class Passport in the class Person.

Class Passport contains constructor __init__() which sets the name, address and passport no.

Display the name of the person, Address and passport number of the person. 

Input Format:

Name => String

Address => String

passport number => String

Output Format:

Three outputs. All are String

Sample Input:

RamKumar

Kollam

J7546891

Sample Output:

Name: RamKumar

Address: Kollam

Passport Number: J7546891

Case 1

Case 2

Input (stdin)

RamKumar

Kollam

J7546891

Output (stdout)

Name: RamKumar

Address: Kollam

Passport Number: J7546891

Input (stdin)

Purushothaman

Mumbai

J1535231

Output (stdout)

Name: Purushothaman

Address: Mumbai

Passport Number: J1535231

Code:

#Type your code here...

class passport:

  def __init__(self,name,address,pa):

    self.name=name

    self.address=address

    self.pa=pa

  def display(self):

    print("Name :",self.name)

    print("Address :",self.address)

    print("Passport Number :",self.pa)

class person(passport):

  def __init__(self,name,address,pa):

    super().__init__(name,address,pa)

    super().display()

a=input()

b=input()

c=input()

e1=person(a,b,c)

Prepare to crack the TCS Xplore Python Coding questions with GUVI: Python with IIT Certification

Q8. Longest Increasing Subsequence

Given an integer array 'A'. Find the length of its Longest Increasing Subsequence of a sub-array from the given integer array. The elements are sorted in monotonic increasing order. You need to create a function that takes two inputs - integer 'n' and an integer array containing 'n' integers. To return the length of its LIS.

Format:

Input:

The integer input is 'n'. And Integer array 'A' input, contains 'n' integers.

Output:

Return the length of its LIS.

Constraint:

1 <= input1 <= 1000

Example:

Input:

3

1, 3, 2

Output:

2

Case 1

Case 2

Input (stdin)

3

1 3 2

Output (stdout)

2

Input (stdin)

9

10 22 9 33 21 50 41 60 80

Output (stdout)

6

Code:

def lis(arr,n):

    lis = [1]*n

    for i in range (1, n):

        for j in range(0 , i):

            if arr[i] > arr[j] and lis[i]< lis[j] + 1 :

                lis[i] = lis[j]+1

    maximum = 0

    for i in range(n):

        maximum = max(maximum , lis[i])

    return maximum

n=int(input())

arr = []

arr=list(map(int, input().split(' ')[:n]))

print(lis(arr,n))

The next coding question from the list of TCS Xplore Python coding questions is here:

Q9. Consider a row of n coins. We play a game against an opponent by alternative turns. In each turn, a player selects either the first or last coin from the row. Now remove it from the row permanently and take the value of a coin. Find the maximum possible amount of money.

Example:

Input:

4

5 3 7 10

Output:

15

Case 1

Case 2

Case 3

Case 6

Case 7

Case 8

Case 9

Case 10

Input (stdin)

4

5 3 7 10

Output (stdout)

15

Input (stdin)

7

8 15 3 7 10 22 5

Output (stdout)

26

Input (stdin)

8

10 3 8 2 6 7 15 1

Output (stdout)

39

Input (stdin)

5

1 2 3 4 5

Output (stdout)

9

Input (stdin)

7

11 22 33 44 55 66 88

Output (stdout)

187

Code:

def optimalStrategyOfGame(arr, n):

    table = [[0 for i in range(n)]

                for i in range(n)]

    for gap in range(n):

        for j in range(gap, n):

            i = j - gap

            x = 0

            if((i + 2) <= j):

                x = table[i + 2][j]

            y = 0

            if((i + 1) <= (j - 1)):

                y = table[i + 1][j - 1]

            z = 0

            if(i <= (j - 2)):

                z = table[i][j - 2]

            table[i][j] = max(arr[i] + min(x, y),

                              arr[j] + min(y, z))

    return table[0][n - 1]

n=int(input())

arr1 = [int(i) for i in input().split()] 

print(optimalStrategyOfGame(arr1, n))

MDN

Wrapping Up

So, these are the top TCS Xplore Python Coding Questions & Answers for you. If you have any questions that you think are important and not included here, kindly share them below in the comment section. This place for knowledge sharing is trending as the next best destination for learning Technology.

TCS XPLORE, GUVI

Articles similar to TCS Xplore Python Coding Questions & Answers:

Top Postman Interview Questions

CRED Interview Questions

Interview Questions

If you are aspiring to explore Python through a self-paced course, try GUVI’s Python self-paced certification course with IIT Certification.

Career transition

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Share logo Whatsapp logo X logo LinkedIn logo Facebook logo Copy link
Free Webinar
Free Webinar Icon
Free Webinar
Get the latest notifications! 🔔
close
Table of contents Table of contents
Table of contents Articles
Close button

  1. The TCS Xplore program is coupled with a plethora of engagements which include:
  2. Top TCS Xplore Python coding questions & answers:
    • Q1. Write a Python code to print the position or index of a given string (taken as input from a user) from a given list of strings.
    • Q2. Make a function check_palindrome() that takes a list of strings as an argument. It returns the string which is a palindrome.
    • Q3. Create a function count_words() which takes a string as input and creates a dictionary with a word in the string as a key and its value as the number of times the word is repeated in the string. It should return the dictionary.
    • eg: "hello hi hello world hello"
    • dict={'hello':3,'hi':1,'word':1}
    • Q4. Write a Python program to create a class called mobile which contains a method called display which displays the name of the mobile owner, mobile brand, colour and camera pixel.
    • Q5. Write a Python program to calculate the salary of the temporary staff using Multilevel Inheritance.
    • Q6. Write a Python program to check the quantity of petrol in the bike using exception handling.
    • Q7.Write a Python program to display the Passport details of the Person using composition.
    • Q8. Longest Increasing Subsequence
    • Q9. Consider a row of n coins. We play a game against an opponent by alternative turns. In each turn, a player selects either the first or last coin from the row. Now remove it from the row permanently and take the value of a coin. Find the maximum possible amount of money.
  3. Wrapping Up