Table of Contents
TCS Xplore is a learner-centered program with a 120-hour progressive induction program. You can find the most important TCS Xplore Python coding questions here.
A technology career with Tata Consultancy Services (TCS), India’s IT pioneer, is among the top aspirations of most Indian techies. If you are still exploring TCS, let me tell you that TCS is the first listed company in India to reach a capitalization of USD 100 billion. So, it is rightly called the jewel of Indian IT.
Whether you are a seasoned IT professional or a newbie just looking to explore your identity in the lush green planet of IT, joining an Indian company like TCS opens up unlimited opportunities for you.
The TCS Xplore program is packed with interactive activities including:
AsCEnD: This program will help you develop your digital skills. You will also be certified and can then show off your skills.
Hackathons : After mastering digital skills, you can look forward to experimenting with the same. Next up is a great platform to test your skills in the form of hackathons. You can demonstrate your capabilities in the field of digital technology through hackathons.
Digital Connect Line : Get insights from executives on the organization’s growth strategy, current industry challenges, and customer expectations.
Business Unit Connect: A comprehensive corporate learning experience through quizzes, hackathons, Leader Connects, and more.
Internships: Opportunity to learn and grow at one of the world’s leading IT companies.
So now let’s quickly move on to coding questions and answers.
Top TCS Xplore Python Coding Questions and Answers
Q1. Write python code to print the position or index of a certain string (taken as input by the user) from a list of given strings.
Ans. The program will take user input as a string and pass that string as an argument to the function. The function will take strings as arguments and return the position (or index) of the list if the passed string is present in the list, otherwise it returns “String not found”. If the passed strings are present at multiple indexes, then in this case the function returns only the index that appears first.
Considering the above scenario, let’s build logic to print the position of string passed from given string list.
Refer to the instructions below and the Input Output example to better understand the request.
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)
Unlock Your Coding Potential with Our Python Programming Course – Enroll Today
Q2. 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))
Q3. 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(_)
Q4. 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()
Q5. 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”)
Q 6. 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()
Some more TCS Xplore Python coding questions & answers:
Q7. 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))
Q8. 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)
Unlock Your Coding Potential with Our Python Programming Course – Enroll Today
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))
Unlock Your Coding Potential with Our Python Programming Course – Enroll Today
Conclusion
1: Which of the following data types is immutable in Python?
So, these are the best TCS Xplore Python encryption questions and answers for you. If you have any questions that you think are important but are not covered here, please share them below in the comments section. This knowledge sharing place is quickly becoming the second best destination for tech learning.