0% found this document useful (0 votes)
7K views24 pages

TCS PRA-IRA-CPA Questions

cdscuy uewc ou oceoc ewc ui cp

Uploaded by

Gagan Nir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
7K views24 pages

TCS PRA-IRA-CPA Questions

cdscuy uewc ou oceoc ewc ui cp

Uploaded by

Gagan Nir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 24
Python Question and Answers Question | ? QUeStION Haare Solution Mesa . Question HH Solution Ml... Question IV, 10 Solution IV... Question V: Solution V.. Question IV: Solution ‘Question vill Solution Vill TCS Confidential (Not to be circulated) Question | Create a function with the name check_palindrome which takes alist of strings as input. ‘The function checks each string of the list whether it is a palindrome or not and returns another list containing the palindrome from the input string. Note: i. Appalindrome is a a word, phrase, or sequence that reads the same backwards as forwards, e.g. madam ii, The check for the string to be palindrome should be case-insensitive , e.g. madam and. Madam both should be considered as palindrome. You can use the below sample input and output to verify your solution. Sample Input: 3 Hello Malayalam Radar Output: Malayalam Radar ‘The first line in the sample input is the count of strings to be included in the list as the 1st input. ‘The strings are then provided one by one as the next lines of input. For more details, please refer to the below main section of code You can use the below sample main section to test your code. Sample Main Method: if __name. cou inp_st for iin range(count): inp_str.append{input()) output=check_palindrome(inp_str) if len(output) for iin output print(i) else: print('No palindrome found’) for strin str_list: if ste{:-1] lower) res.append{str) return res if_name_=="_1 count=int(input()) inp_str=(] for | in range(count Inp_str-append{input()) output=check palindrome(inp_str) iflen(output)= for ‘in output: print() else: print('No palindrome found’) TCS Confidential (Not to be circulated) Question II Create a class Employee with the below attributes: emp_id of type Number emp_name of type String emp_role of type String emp_salary of type Number Create the __init__ method which takes all parameters in the above sequence. The method should set the value of attributes to parameter values inside the method. Create a method inside the class with the name increase_salary.. This method takes a Number value as arguments which is the percentage by which the salary should be incremented and increases the salary of the employee by the given percentage amount. If the employee salary is 110000 and the given percentage is 10, then the updated salary of the employee should be 11000. Create another class Organisation with the below attributes: Create another class Organis ion with the below attributes: ‘org_name of type String ‘emp_list of type List having Employee objects Create the __init__ method which takes all parameters in the above sequence. The method should set the value of attributes to parameter values inside the method. Create another method inside the class with the name calculate_increment. This method takes a String value and a Number value as argument which is the employee role and the increment percentage respectively. The method checks all the employees in the employee list of the organisation whose role is same as the given role and increase their salary by the given increment percentage. The method returns a list of employees containing all the employees ‘whose salary was incremented, Note: use the method increase_salary defined in the Employee class to calculate the incremented salary of the employees whose role is same as the given role. You can use the below given sample input and output to verify your solution. The 1st input taken in the main section is the number of Employee objects to be added to the list of Employees. ‘The next set of inputs are the employee id, employee name, employee role and employee salary for each employee taken one by one. ‘Sample Input: 4 100 Rajesh Developer 40000 101 ‘Ayesha Developer 41000 102 Hari Analyst 45000 103 ‘Aman Manager 60000 Developer 5 Output: Rajesh 4200.0 ‘Ayesha 43050.0, For more clarity, please refer to the below main section of code You can use this section to test your code ‘Sample main section: if__name__=="_main_': TCS Confidential (Not to be circulated) emp_iist count=int(input()) for iin range{count): eid=int(input()) name=input() role=input() salary=int(input()) emp_list.append|Employee(eid,name,role,salary)) (0=Organisation("XYZ Corp",emp_list) inp_role=input() inp_percent=int(input()) result=o.calculate_increment{inp_role,inp_percent) if len(resutt) for emp in result: print{emp.emp_name,\t’emp.emp_salary) else: print('No employee found with the given role’) Solution I class Employee: def _init__(self,eld,ename,role,salary): selfemp_id=eid self.emp_name=ename self.emp_role=role self.emp_salary=salary def increase_salary(self,percent): self.emp_salary+=self-emp_salary*percent*0.01 class Organisation def _init_(seifname,cist) self.org_name=name self.emp_lst-alist def calculate_increment(self,role,percent) emp_res=(] for emp in self.emp_list: ifemp.emp_role: ‘emp.increase_salary(percent) ‘emp_res.append(emp) return emp_res if__name__=="_main_': cemp_list=[] count=int(input()) for iin range(count): eid=int(input() name=input() role=input() salary=int(input()) emp_list append|Employee(eid,name,role,salary)) (0=Organisation("XYZ Corp"emp_list) inp_role=input() inp_percent=int(input()) result=o.calculate_increment(inp_role,inp_percent) if len(result)!=0: for emp in result: print(emp.emp_name,'\t'emp.emp_salary) else: Print('No employee found with the given role’) TCS Confidential (Not to be circulated) Question III Create a class Account with the below attribute: ‘account_no of type Number account_name of type String account_balance of type Number Create the __init__method which takes all parameters in the above sequence and sets the value of the attributes inside the method. Create amethod depositAmnt inside the Account class which an takes @ number value as input parameter. The number value represents the amount to be deposited into the Account.The method Updates the balance of the Account ie. adds the given amount to the existing balance of the Account object: Create another method withdrawAmnt inside the Account class which takes which an takes a number value as input parameter. The number value represents the amount to be withdrawn from the ‘Account.The method deducts the amount from the existing balance of teh Account object However, the ‘minimum balance of the Account obejct is to be maintained as 1000. i.e withdrawal of the given amount will be possible only if the balance of the Account object is greter than or equal to 1000 after the ‘amount is withdrawn. The method return 1 if the withdrawal is possible; else returns 0 if the withdrawal is not possible. To test the code against your customized input through console, the input data needs to be entered in, the below order( as shawn below in the sample input) The first three lines in the below sample input represents the input for three variables of account object ite account no.(account_no},account name (account_name) and account balance (account_balance}, ‘with which the Account object will be created. ‘The fourth line in the sample input is the input for the amount to be deposited in the account object ‘and fifth line is the input for the amount to be withdrawn from the account object ‘Sample input: 120 Rajesh 1500 1200 2000 Output: 2700 Insufficient balance for withdrawal For more details, please refer the below main section of the code. Note: The below main section is already added in your console, You are requested not to modify anything in that section or it might result in failure of the test cases configured. Sample main section: if__name__=='_main_! acn int(input()) acname=raw_input() acntbal=int(input()) depamnt=int(input()) withamnt: (input) acnt=Account(acno,acname,acntbal) acntdemoobj=AccountDemo() print(acntdemoobj.depositAmnt(acnt,depamnt)) print(acntdemoobj.withdrawAmnt(acnt,withamnt)) Solution III Class Account: def__init_(self,acno,acntname,acentbal): self.account_no=acno self.account_name=aentname self.account_balance=acentbal def depositAmnt{self,amount) self-account_balance+=amount def withdrawAmnt(self,amount): lf(self.account_balance-amount<1000): return 0 else: self.account_balance--amount return 1 if__name__=="_main_: acno=int(input()) acnamesinput() acntbal=int(input()) withamnt=int(input()) obj=Account(acno,acname,acntbal) obj depositAmnt(depamnt) TCS Confidential (Not to be circulated) print("Balance after deposit :",0bj.account_balance) res=obj.withdrawAmnt(witharmnt) if res == 1: print("Balance after withdrawal :",obj.account_balance) else: print("Insufficient balance for withdrawal") Question IV Create a class Employee wih the below atibutes ‘empno of numeri ype ‘empname of sting ype leaves of dictionary ype ‘The leaves dictionary will have the leave fype(EL .SL,CL ) as key and no of balance leaves (for the respective leave type) 8s values ofthe key Create the __init__ method which takes all parameters in the above sequence and set them as the values of the attibutes indie the method, Create another class Company with below atributes: name of sting type ‘empe of iat type having employee objects Create the __init__ method which takes all parameters in the above sequence and set them as the values ofthe altibutes inside the method, Create a method display_leave in the Company class, which takes the employee number and a leave type as input parameters and dispiay the corresponding leave balance of the given leave type forthe employee with the gven ‘employee number. Create another method leave application in the Company ciass, which takes employee number, leave type and number of leaves applied by the employee in the given leave type. In this method.for the given ‘employee, check whether the leave balance in the given leave type is more than or equal to the number of eaves ‘applied, to grant the leave, Retum *Granted” if all the conditions are matched. Otherwise retum “Rejected” Totes! he code against your customized Input thraugh console “The input data needs to be entered inthe below order{ as shown below in the sample input). Example 1. Lets assume that you want to add 3 Employee objects. Input he data is as shown inthe sample input ». The first tine (9) n the below sample input indicates the number of employee objects to be created. Next five lines from second to 6th line , 8 1 Rajesh 6 10 20 In the below sample input represents employee number.employee name leave balance for “CL leave balance for “EL” and leave balance for of one employee abject 6. Sinilaly seventh tine to twelfth line i, from the below sample input represents employee number ,employee name leave balance for “CL", leave balance {for “EL" and leave balance for “SL'of second employee object Likewise, next five lines represents the input data fo third employee abject. dd. The last lines in the below sample input represents the employee number, leave type and number of leaves requested < Note: “The employee number and the leave ype value provided will act as a input for display_leave method and as well as, {for leave_application method . The number of leaves provided will act as input for leave_application method. ‘Sample Input: 3 1 Rojesh 10 Rajesh 10 Venkat 20 TCS Confidential (Not to be circulated) u Brahma 10 10 10 3 EL 10 ‘Sample output 10 Granted here 10 isthe number of leaves availabe with the employee of type EL and "Granted" isthe status of the teave_application. For more details, please refer to the below main section of the code Note: “The below main section is already added in your console, You are requested not to mocify anything in that section or it might result in fallure ofthe test cases configures ts ‘main: nsinfinput) €=Compenyt"ABC",emps={) for iin ange(n leaves ‘eno=intinput)) ‘ename=input) leavest'CL"Fininput()) leaves EL'"Fintingut0) leavest'SL'"Finlingut0) ‘e-Employeo(eno,ename leaves) ‘comps.eppendie) fempno=intinput)) Iype=input) nofeint(input()) print dsplay_leavelempno.type)) Print(e.leave_application(empno,type.nal) Solution IV class Employee: def _init_(self,eno,ename,leaves): self.empno=eno self.empname=ename self.leaves=leaves class Company: def __init_{self,cname,emps}: self.cname=ename self.emps=emps def leave_application(self,eno,Itype,nol): chkeFalse for ein solf.emps: if e.empno=-eno: for Itynl in e.leaves.items(): if t==Itype and nl>=nol: chk=True if chk: return "Granted" else: return "Rejected" defalisplay_leave(selt,eno, type} fore inself.emps: ife.empno=zeno: returne.leavestltype] c=Company("ABC",emps=[]) foriin range(n): int{inout() ‘ename=input() leaves("CL'J=int(input() leaves{"EL"-int(input0) leaves{*SL"int(input()) e=£mployee(eno,ename,leaves) c.emps.append{e} cempno=int(input)) TCS Confidential (Not to be circulated) 3 Itype=inputi) nol=int(input() print(c.display_leave(empno,itype)) print(-leave_application(empno,ltype,nol)) Question V: Create a function with the name count_words that take a string as input and return the count of occurrence of each word in the string. The word and count of occurrence of the word should be returned from the function as a dictionary. Consider the words to be separated by space in the string, Create another function with the name max_occurence_word that will take a string as input and return the word with maximum occurrence in the string. Note : Use the function count_words inside this function to get the count of occurrence of each word. To test the code against your customized Input through console, please refer to the below instructions The input string needs to be entered in the console. Consider the following sample input and expected output for reference. Sample Input : Hello Hi Bye TCS Hello Welcome Hello Bye Expected Output : Hello Please use the below main program code to implement and to testrun your code and submit the complete code along with this main code. ‘_main_': if_name_ input_string=raw_input() dict=count_words(input_string) st=max_occurence_word(input_string) print(st) Solution V def count_words(str): words=strsplit() res=(} for in words: resfil-words.count() return res def max_occurence_word(str): d=count_words(str) v= listd.values() ke lst. keys0) retum k{v.indeximax(v))] if__name__=='_main_' input_stringsinput() dict=count_words(input_string) print(ict) st vax_occurence_word(input_string) print(st) Question IV: Create a class Book with following attributes: book id of type Number book_name of type String Greate the iL__method to take the above attributes as input in the given sequence. Create another class Library with following attributes: lbrary_id of type Number ‘address of type String book list of tye List (Create a method in the Library class with the name count_books which takes a character as input and retums the count of books in the list of books that belongs to the library whose name starts with the given character. Create another method in the same class with the name remove_books which takes a list of book names as input and removes the book from the book lst of the Library ifthe name of the book matches with the name given in the input Ist of book names. To test the code against your customized Input through console: ‘The input data needs to be entered in the below order( as in the below sample input). Example’ ‘a, The frst input isthe count of book objects to be added. 18 TCS Confidential (Not to be circulated) b. The second and the third lines in the sample input represents book id and book name ofthe a book ‘object respectively. The next two lies are the book id and the book name of the next book object. and so ‘on for the other values ofthe book objects. «. The next line represents the character that isto be searched. 4. The next ne isthe count of book names tobe provided as the input st of Book names. «@. The subsequent lines are the names ofthe books provided to be included in the lst ‘Sample Input, inline to the above description: 3 100 (C++ Programming 200 Introduction to SQL 300 Core Java © 3 nyssey Core Java Hello World ‘Output for the above sample input 2 C+ Programming Introduction to SQL Note: For more detalls on a. Input data entered from standard input b. How this data will be processed c. ‘The order of the input data * , please refer the main program Please use the below main program code to implement and to tesUrun your code and submit the complete code along with this main code. count input) for iin range(count): bia=intinput)) bname=raw_input() }00K{bid,brame) books append(b) IsLibrary(123 ’Mumbai’,books) ‘char=raw_input() ‘ecount=int(input()) names=1) for iin range(ccount): names append(raw_input()) print(Lcount_books(char)) Leemove_books(names) for jin Lbook ist print(.book_name) Solution VI class Book: def _ init__{self,bid,bnamey: self.book_id=bid self.book_name=bname class ibrany: ef _init_(solfjid address bist): self.library, selfiaddress=address self.book_lst def count_books(self,char): count=0 for iin selfbook lst: If .book_name.startswith(char] counte=1 return count def remove_books(setf list): TCS Confidential (Not to be circulated) uv for book in bist: for b in self.book_list: If b.book_name==book: self.book_list.remove(b) if__name__ i books=[] count=int(input()) for in range(count) bid=int(input()) bname=input() b=Book{bid,bname) bbooks.append(b) {stibrary(123,'Mumbat’.books) char=input() count=int{input()) ‘names-(] for iin range(ecount): names append(input()) print(.count_books(char}) remove_books(names) for iin Lbook lst: print(i.book_name} Question VII: Create a function with the name check_prime which takes a number as input and checks if the given number is prime or composite. ‘The function returns 1 if the number is prime,0 if the number is composite and 0 otherwise. Create another function with the name prime_composite_list which takes a list of numbers and checks whether the numbers in the list are prime or composite. Include all the prime numbers in one list and all the composite numbers in another list. Create a 3rd list and include the list of prime numbers and the list of composite numbers in the 3rd list and retumn the 3rd list from this function, Note : use the function check_prime to find whether the number is prime or composite. To test the code against your customized Input through console, please refer to the below instructions: The first line in the sample input is the count of numbers to be provided in the input list ‘The next few lines are the numbers to be included in the list provided one by one Sample Input 4 " 7 90 44 Expected Output : [[11, 7), (90, 44]} Please use the below main program code to implement and to testrun your code and submit the complete code along with this main code. if_name_+ inp=0 count=int(input()) '_main for i in range(count): inp.append(int(input())) print(check_prime(inp[1])) 19 TCS Confidential (Not to be circulated) result=prime_composite_list(inp) print(result) Solution Vil: def check_prime(num): flag=-1 ifnum>1: for iin range(2, num//2): if (rum 9% flag=0 break else: fag=1 return flag def prime_composite_list{li) prime=[] composite=[] for inl if check_prime(i} prime.appendti) elif check_prime( composite.append{i) resel] res.append(prime) +es.append|composite) return res if_name_+ ‘np count=int(input()) for iin range(count): inp.append(int(input()) print{check_prime(inp{1))) esult=prime_composite_list(inp) print(result) Question VIII Create a class Item with following attributes: item_id of type Number item_name of type String item_price of type Number quantity_available of type Number Create the __init__method to take the above attributes as input in the given sequence ‘create a method calculate_price which will take a number as input which represnts the qunatty of the item to be checked. the method checks if the iter» has available quantity greater than or equal to the given quantity. If itis available, calculate the total price of the item by considering the item price and the quantity(Total price=Item price * Quantity). Return the total price from the method if the qunatoty Is available or else return 0. Create another class Store with following attributes: item_list of type List Create the __init__method to take the above attributes as input in the given sequence Create a method in the Store class with the name generate_bill which takes a dictionary as input. The dictionary includes the item name as the key and the quantity required as the value. This method calculates the total price of each item whose item_name is provided as key of the dictionary and returns the bill amount. Note : use the calculate_price method of the Item class to calculate total price of each item To test the code against your customized Input through console: ‘The input data needs to be entered in the below arder( as in the below sample input). 2 TCS Confidential (Not to be circulated) Example; 2 The first input is the count of Item objects to be added. b. The second,third,fourth and fifth line is the item id, item name, item price and the quantity available fot the 1st item object. The subsequent lines have the same fr the other item objects. . The next line is the count of values to be included in the input dictionary . The next line is name of the 1st required item and the subsequent line isthe equired quantity and so ‘on for the other dictionary values. For more details, refer to the sample input an its output with reference to the given main section below ‘Sample input: 3 1 Bread 20 5 2 Milk 50 10 3 Cookies 100 2 3 Bread Butter Milk ‘Output for the above sample input: 60 160 Note: For more details on ‘a Input data entered from standard input b. How this data will be processed . The order of the input data please refer the main program Please use the below main program code to implement and to test/run your code and submit the ‘complete code along with this main code. for iin range(icount) tlinput()) name=input|) input) quant=int(input() fem id,name,price,quant) list-append() s=Storetllst) inp=0 inp_count=int(inputi)) for iin rangelinp_count) name=input() quantity=int(input()) inp{name]=quantity print{ilst{0].calculate_price(2)) print(s.generate_billfinp)) Solution Vill: class Item: def __init__(self,id,name,price,quant): self.item_id=id self.item_nam: ‘ame self.item_price: self.quantity_available=quant def calculate_price(self,quantit if self.quanti ilable >= quanti self.quantity_available--quantity return self.tem_price*quantity else: return 0 TCS Confidential (Not to be circulated) 23 class Store: def _init_(selfjlist): selfitem_list=ilist def generate_billisef,req): bill=o for iin req: for jin self.item_List: If == j.item_name: bills=j.calculate_price(req{i)) return bill (input) inrangeticount}: input) name=input() price-intinput()) (input) isItem(id,name,price,quant) quan Lappend(i) s-store(ilst) inp=0) inp_countzint(inputQ) fori in range(inp_count): namerinput() quantity=int(input()) inp[name]=quantity print(ilist[0].calculate_price(2)) print(s.generate_billfinp))

You might also like