0% ont trouvé ce document utile (0 vote)
21 vues34 pages

CH 2

Le document contient des questions pratiques sur les chaînes, les listes, les tuples et les dictionnaires en Python, avec des affirmations à évaluer comme vraies ou fausses. Il couvre également des questions à choix multiples (QCM) sur les opérations de chaîne et les méthodes associées. Les questions portent sur des concepts tels que l'immuabilité des chaînes, l'indexation et les méthodes de manipulation de chaînes.

Transféré par

sujaysrinivasan.2632
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
21 vues34 pages

CH 2

Le document contient des questions pratiques sur les chaînes, les listes, les tuples et les dictionnaires en Python, avec des affirmations à évaluer comme vraies ou fausses. Il couvre également des questions à choix multiples (QCM) sur les opérations de chaîne et les méthodes associées. Les questions portent sur des concepts tels que l'immuabilité des chaînes, l'indexation et les méthodes de manipulation de chaînes.

Transféré par

sujaysrinivasan.2632
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF ou lisez en ligne sur Scribd
2825, 11:25 AM CS-CHAPTER 2-08 - Google Docs [STRINGS, LISTS, TUPLES, DICTIONARY] PRACTICE QUESTIONS STATE TRUE OR FALSE _| String is mutable data type. 2. | Python considered the charactor enclosed in triple quotes as String 3._| String traversal can be done using for" loop only. 4__| Strings have both positive and negative indexes. 5._| The find() and index() are similar functions, 6._| indexing of string in python starts from 1. 7. | The Python strip) method remaves any spaces or specified characters at the start and end of a string B. | The startswith() string method checks whether a sting starts with a particular substring, "The characters of string will have two-way Indexing” ‘ASSERTION & REASONING “A: Strings are Immutable. R¢ Individual elements cannot be changed in string in place. “Ar Siring is Sequence datatype. R: A python sequence is an ordered collections of items where each item is indexed by an integer. ‘The value of c will be 5 R: +" operator adds integers but concatenates strings print{c) ‘This will lead to output: "He" R: Python string does not support - operator x str1="Hello" and str1="World” then print{str1*3) will give ervor: R:* replicates the string hence correct output will be HellotielloHello hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 1134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs [Link]/1k_EOBXOxGR2NZBYex634id7e8m@XGjLANNFEWLWieledt7tap=t0 2138 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs Teli then print{,wo" not in stz) will print false R : not in returns true if a particular substring is not present in the specified string ‘A: The following command >>>"USA12¥ wupper() will return False. R: All cased characters in the string are uppercase and requires that there be at least one cased character ‘A: Function isalnum() is used to check whether characters in the string are alphabets or numbers. R: Function isalnum() returns false if it either contains alphabets or numbers ‘A: The indexes of a string in python Is both forward and backward, R: Forward index is from 0 to (length-1) and backward index is from-1 to length) 10. ‘A: The tile() function in python is used to convert the first character li each word to Uppercase and remaining characters to Lowercase in the string and returns an updated string, R: Strings are immutable in python, ‘OBJECTIVE TYPE QUESTIONS (MCQ) What is the output of hello''1+2+3? Ghello123 —_(b)hellos—_(c)Error_(@) Hello+6. Ifn="Hello” and user wants to assign n[0]="F" what will be the result? (a) Itwill replace the first character (b It’s not allowed in Python to assign a value to an individual character using index (©) Iewill replace the entire word Hello into F (@) Itwill remove H and keep rest of the characters Im listslicing, the start and stop can be given beyond limits. itis then: (a) Raise exception Indextrror (b) Raise exception Valuefrror (c)Return elements falling between specified start and stop values (@) Return the entire list hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 3134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs Select the correct output of he code Swr="Computer” ‘Sur=Str[-4:] prin(S*2) iter [Link] cuteruter d. None ofthese Given the Python declaration s1="Hello”. Which of the following statements will give an error? G) print(s1[4)) (6) s2=s1 (6) sl=s1[4] (@ si[4]="Y" Predict the output of the following code: R S="important” for ins: RS=i#RS print(RS) ‘What will the following code print? >>> print(max("zo0 145 com") (145 (H)122 (x _(d) 200 Ms. Hetvee is working on a string program, She wants to display last four characters of a string object named s, Which of the following is statement is true? as[4] bs{4]_csf-#]_dsl4) ‘What will be the output of following code snippet: msg ="Hello Friends" msg [22-1] ajHello _b) Hello Friend c)'sdnei¥ ollett" d) Friend 70, ‘Suppose stri= ,welcome", All the following expression produce the same result except one. Which one? (a) st 1-6] (B) stef ::-1][ :-6] (©) ste ::6] (@) ste0] + stxf- 1] it. Consider the string state = "Jharkhand". Identify the appropriate statement that will display the lastfive characters of the string state? (a) state [-5:] (b) state [4:] (c) state [4] (d) state [4] 12, ‘Which of the following statement(s) would give an error during ‘execution? # Statement 1 # Statement 2 S[0)='@ # Statement 3 S=S+"Thank you" # Statement (a) Statement 1 (b) Statement 2 (©) Statement 3 (@) Statement 4 hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 4134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs 73. Select the correct output of the following string operations mystring = ‘native" stringList = ["abe","native" xy2"] print(stringList{1] == mystring) print{stringList{1] is mystring) a)True bj False c) False. ©) Ture False True Flase Ture 14 Select the correct output of the code n= "Post on Twitter is trending” q = nsplit() print( q{0}+q(2}+aL3]+al4]11:] ) (@)Pos on witter is rending (b)Pos on Twitter is ending (6) Poser witteris ending (4) Poser is ending 15. ‘What will be the result of the following command: >>>"i love python’.capitalizeQ) (a) Love Python’ (b) ‘Love python’ (©) LOVE PYTHON’ (@) None of the above 16 The number of element return from the partition function is: >>>"T love to study Java’ partition("study") @i_)3 4 (a5 17. Predict the output of the following: L-Spartition('N’) G=L[0] + +L[1] +2 + Lf] print(G) (INDIAN — () EDIA-—(@) FDIAN (4) L-N-DIAN DIAN" 18. ‘What will be the output of the following command: >>>string= A quick fox jump over a lazy fox" >>>[Link]("fox",9,34) (j28 29 "8 7 19. Select the correct option for ouput X="abedet™ ‘while in X print(, end="") (Emr (Ja (infinite loop —_(@) No output 20. ‘What is the output of the following code? Si="Computer2023 s2="2023" print([Link]®), S2isdigit()) a)False True ») False False ©) True False 4) True True hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 5134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs 2. ‘What will be the output of the following statement given: sample paper 2022" print([Link]()) 4) CBSE. sample paper 2022 b) CBSE. SAMPLE SAPER 2022 ) cbse. sample paper 2022 d) Cbse, Sample Paper 2022 22. Select the correct output of the code: a= "Year 2022 atall the best” a=asplit(’) [0] + °F + afd] ++ a3] print (b) a) Year -0- at All the best by Yerr 2022 I the best ) Year 022- at All the best 4) Year ~ 0 atall the best 23. Select the correct output of the code: mystr= = Python programming is fun!” mystr ‘mystrpartition(‘pro’) mystr='$'join{mystr) @) Python $programming is fun! (b) Python $proSgramming is funt (c) Python pro$ gramming is fun'S (@) Python SpSroSgramming is $fun! 2 Identify the output of the following Python statement: s="Good Morning" print([Link]()..tite(), end=""") (a) GOOD MORNING!Good Morning (b) Good Morin! Good Morning {6 Good morning! Good Morning! {@) Good moming Good Morning! 25. What isthe output of the following code? Sr World” ‘StrLreplace(‘o' [Link](’o {@)Iiello World (b) elit Weld (c) Hello W*rld (d) Error jello 26. ‘State whether the statement is True or False? [No matter the underlying data type, if values are equal returns true. 27 Predict the output of the following: S="Hello World!" print([Link]("Hello")) @o 3 O37 M37 28. Which of the following is not a valid Python String ‘operation? (@) Welcome'+'10" (b)'Welcome’*10 (©) 'Welcome"*10.0 (a) '10'"Welcome’ [Link] OBxIDxGR2NZ8ex834iG7e8m9XG)LANHFEWL/Wfledt7tab=0 6134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs 25. ‘Which of the following statement(s) would give an error after exocutin the following code? jlue Tick will cost $8” # Statement 1 M) 4# Statement 2 jothing costs more than Truth” # Statement 3 # Statement 4 EM [-1: -3:-1] += ME[: 7] ‘# Statement 5 (a) Statement’S_(b) Statement 4 (c) Statement 5 (d) Statement 4 and 5 30. Select the correct output of the following code s="1HN#F#O#REM#ANT#IECHS” Lelist(s-split("#")) 31. print(L) (a) [JHNHEHOMRAMHAWTHLACHS] aR aM AY Tals INFORMATIC (@) [INFORMATICS] Whined Line? Sin range{0,8) Lines print(STRING[S]) Lines print(S STRING) #Lines ‘Which statement is wrong in above code: a) Line3 and Lines ») Linet and Lines, €) Line? and Lined 4) Line’ and Lines, 32. Consider the following code that inputs a string and removes all special characters from it after converting it to lowercase. = input{"Enter a string") slower() for cin’. 20N' print(s) For eg: ifthe input is TAM , WHAT I AM", it should print iam ‘what i am Choose the correct option to complete the code a.s = [Link](c, ces =[Link](c, y} bs =[Link](e, "\O') fone) d.s=[Link](c) 33. Which of the following statement(s) would give an error after executing the following code? str= "Python Programming” st = #82 print(str*2) 483 print(str*x) 454 @si_ ss 08 Ws hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 7138 2825, 11:25 AM CS-CHAPTER 2-08 - Google Docs Z=MARKS / 3- MARKS 1._| 1FS“"python language” is a Python string, which method will display the following output 'P’ in uppercase and remaining in lower case? Rewrite the following code: for Name in [Aakash, Sathya, Tarushi] IF Namef0]='S" print(Name) 3. | Consider the given Python string declaration: >>> mystring = "Programming is Fun’ >>> printmystring{-50:10:2].swapcaseQ) Rewrite the following: STRING=""WELCOM! for S in range(0,8): if STRINGS}: print(STRING(S)) Else: print "NO" 3. | Given is a Python string declaration: >ppWish = "iit Wishing All Happy Diwali @#S” Write the output of >>>Wishl -6::-6] 6. | Predict the output of the following: for x in range (lenName)):if Name{x]. supper () R=R+Name[x] lower() elif Name[x].islower() R=R+ Name[x}.upper() elif Name[x].isdigit0: R=R+Name[xl] se: R=R+"#" print(R) 7. | Predict the output of the following: >>> ‘he likes tea very much'.rstrip(‘crunch’) >5> "There is a mango and an apple on the table’ rstrip(‘c) hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 2825, 11:25 AM CS-CHAPTER 2-08 - Google Docs Find the output Msgi="[Link]” Msg2="GUeSTS" Msg3=" for I in range(0,len(Msg2)+1) If Msgi[I]>="A" and Msgl[I]<="M' ‘Msg3=Msg3+Msg][I] elif Msgl [I]>="N" and Msgi|[I]<="2" Msg3=Msg3+Msg2{I] else: Msg3-Msg3+"*" print(Msg3) Predict the output of the following: s= & Four” n=len(s) m= for iin range(0, n} if (S[i] >='8 and sli] =m +s[i).upper() elif (s[i] >='3' and s[i] <='2’):m =m4si-l] If (Li) isdigit() m=m +5{i).lower() else: print{m) Zz): m Name= Pythons.” R= for x in range(len(Name)): if Name{x].isupper() R=R+Namelx].lower() elif Name{x}.islower() R=R+Name{x]-upper() elif Name[x!.isdigitQ ReR+Name[el] print(R) hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 2825, 11:25 AM CS-CHAPTER 2-08 - Google Docs To. Find output generated by the following code: string="aabbcc" count: while True: if string[0]=="a' string=string[2:] elif stringl-1 string=string(:2] alse count+=1 break print(string) print{count) it. Write the output of the code snippet, txt = “Time, will it come back?” x = txt find(’ y= txtfind(?") print(txt{x+2:y)) 12. Predict the output ofthe following: S='Python Programming! L=Ssplitg S='),join(L) print(S) 13. Predict the output of the following: S="Good Morning Madam” I=S split) for Win if Wlower() print(W) -W[:-1].Jower() hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 10134 2825, 11:25 AM CS-CHAPTER 2-08 - Google Docs TOPIC LIST STATE TRUE OR FALSE Tist immutable data type. Listhold key and values. Lists once created cannot be changed. The pop() and remove() are similar functions. The append() can add an element in the middle of the list Del statement deletes sub list from alist Sort() and sorted() both are similar function. T1=[5,7,9/4,12) 5 natin Id List can be extend ‘Mutable means only we can insert elements Index of last element in list is n-1, where n is total number of elements. We can concatenate only two listat one ume, Lpop() method will return deleted element immediately Linsert() method insert a new element at the beginning of the list only, 15. If index is not found in insert{) Le. Linsert(102,89) then it will add 89 at the end of the list 16. Lreverse() will accept the parameters to reverse the clements of the list. a7. Lsort{reverse=1) will reverse the listin descending order 18. L-pop() used to delete the element based on the element of the ist. 19. [Link]() used to clear the elements of thelist but memory will exists after clear 20. Lappend() append more than one element at the end of the lise. 2. L=[10,20,30) Leextend{[90,100]) print{L) output of the above code is: [10,20,30,[90,100]] 22. “After sorting using [Link]() method, the index number of the list element will change. 23. L-remove() used to delete the element ofthe list based on its value, if the specified value is not there it will return Error. hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 36 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs ASSERTION & REASONING A listis mutable data Type R: We can insert and delete as many elements from list ‘A: Elements can be added oF removed from a list, R: List is an immutable datatype. ‘A: List can be changed after creation R: List is mutable datatype, ‘A: Following code will result into output: True a J print( ais b) "is" is used for checking whether or not bath the operands is using same memory location 0,20] ‘A list) Is used to convert sting in to ist element R: string will be passed into list() it will be converted into list elements A reverse() is used to reverse the list elements Reason: it can reverse immutable data type elements also ‘A: pop() can delete elements from the last index of list R: this function can remove elements from the last index only. A Ten() displays the listTength R itis applicable for both mutable and immutable data type, “A: Usha wants to sort the list having name of students of her class in descending order using sorted() function R: sorted() function will sort the list in ascending order 10. ‘A: Raju Is working on a list of numbers. He wants to print the list values using traversing method, He insists that list can be traversed in forward direction and backward direction i beginning to last and last to beginning. R: List can be traversed from first element to last element only. from t ‘A: Anu wants to delete the elements from the list using pop) function. IR: Her friend Pari suggested her to use del () function to delete more than one elements from the list. 12. ‘A: Students in the Lab practical were asked to store the elements in a List data type in which we can perform all the ‘operations like addition , deletion , deletion , traversal R: List is mutable data type hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 12134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs OBJECTIVE TYPE QUESTIONS(MCQ) Which point can be considered as difference between string and list? (@) Length (b) Indexing and Slicing (c) Mutabitity (a) Accessing individual elements Whats the output when we execute list( hello")? (a) Lh’, 6° .1°, 1", ,0°L (6) [shello"] _(c) [.llo"] (@) [oleh] Given a List L=[7,18,9,6,1], what will be the output of L[-2::-1]? [6.9187] WIA] [59.18] (2) [6] fa List L=[Hello World! Identify the correct output of the following, Python command: >>> print(" (@) Hello World! (b) Hello, World! (o)'Hello'Wortd!” (@) 'Hello'World!" Consider the list aList=[ "SIPO°, [1,3,5,7]]. What would the following, code print? @s3_ st (L3_@it Suppose list1 is [1, 3,2], What is list * 2? a) [2, 6,4] (b) [1, 3, 2,1, 3] ([.3.2.13,2) (@) [[Link],2,1) ‘Suppose list = [0.5 x for x in range(0,4)], list is a) [0, 1, 2, 3] b) [0, 1,2, 3, 4) © 10.0,05, 10,15] __d) [0.0, 0.5, 10, 1.5, 2.0] SL 37 o> print(L1[3]= "100" (@) [6.4,2,9,7,100] () [6,4,2,100] (©) [64,2,1,0,0] (@) [64 Which statement is not correct )The statement x = x + 10 is a valid statement D)List slice is alist itself Lists are immutable while strings are mutable. d)Lists and strings in pythons support two way indexing 10, Which one is the correct output for the following code? a=[1,2,3,4] b-[sum(a[0x+1]) for x in range(0,len(a)): print (b) 10 H)[13S7] (4 [13,610] i, What will be the output for the following python code: L=[10,20,30,40,50] T-Las print(L) (a) [10,20,30,40,50,5] —_(b) [15,25,35,45.55] (©) [5.10,20,30,40,50]___(d) Error hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 19186 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs 12. Select the correct output of the coder L1, L2 = [10, 23, 36, 45, 78], [] for i inrange :L2inser(, [Link]()) print( L1, L2, sep="@" (@) [78 45, 36,23, 10) @[] (b) [10, 3, 36, 45, 78] @ [78, 45, 36, 23, 10] (6) [10, 23, 36,45, 78] @ [10, 23, 36, 45, 78] (d) [ ] @ [78, 45, 36, 23, 10] 3 ‘The append() method adds an clement at (@ First (b) Last (c) Specified index (4) Atany location 14. ‘The return type of x in the below code is bt could eat bananas all d x= twtpartition("bananas") (a)string —_(b) List (©) Tuple __(d) Dictionary 15. ‘Sl: Operator + concatenates one list to the end of another list ‘S2: Operator * is to multiply the elements inside the list. ‘Which option is correct? (a) St is correct, $2 is incorrect (b) S1 is incorrect, $2 is incorrect (c) SLis incorrect, $2 is incorrect, (@ SLis incorrect, 82 is correct 16. ‘Which of the following statement is tue for extend() list method? (a) Adds element at Last (b) Adds multiple elements at last (c) Adds element at specified index (@) Adds elements at random index 77. What is the output of the fo 92112345) iA Bnge(t,s): ali-t}=ali} (51234 (923451 (23455 for iin range(0,5} print(afi],end="") ia. ‘What is the output of the functions shown below? ‘min(max(False,-3,-4),2.7) @)3 )-+ | (9 Tre (a) False hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 1434 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs 79. Ifwe have 2 Python Lists as follows: L1=[10,20,30], L2=[40,50,60] If we want to generate a list L3 as: then best Python command out ofthe following is: (a) L3=LisL2 (b) L3=[Link](L2) 20. Identify the correct output of the following Python code 1=13,3,2,2,1,1] Lappend(.pop(L-popQ))) print(L) (@) (3.2,2.1.3] (0)[3.3.2,2,3] (0[1.3,2.2,1] (€)[1.1,2,2,3,3] 2. Predict the output of the following: LI=[1,4,3.2] LoL Lisort() print(L.2) @l1432] (L234) (1432 1234 22. TIFPL is Python list as follows: PL=[Basic,/C/ +] 23. Given listl = [34,66 12,89,28, 99] Statement 1: [Link]() Statement 2: list1[:-1] ‘Which statement modifies the contents of original list. (a) Statement 1 (b) Statement 2 (©) Both Statement 1 and 2. (q) None of the mentioned 2 Which command we use can use To remove string “hello” from listi, (a) listLremove("hello") __(b) list1-pop([Link](‘hello’)) (©) Both A and B {(@) None of these 25. Tow would you create aloop to iterate over the contents ofthe list tiven as Days = [31, 28, 31, 30, 31, 30, 31, 31,30] and print out each element? @ for days in. range(Days) print(days) (6) for days in Days print(days) (© fordaysin range(Len(Days)} print(days) (4) for days in Days print{monthDays! hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 19134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs [Link]/1k_EOBXOxGR2NZBYex634id7e8m@XGjLANNFEWLWieledt7tap=t0 16134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs 2. Predict the output of the following: ‘S=[[AYB', ‘MW print(S{1}{1)) @r orc @D 26. Predict the output ofthe following: LI=1L2.3] La=f4,5] 13-167] Livextend(L2) Liappend(L3) print(La) (@) [1234567] ()11.2.3,45),6,7) (11234516711 (0112314511671 27. ‘State True or False "There is no conceptual Timit to the size of alist” 28. ‘The statement del [13] do which of the following task? a deletes elements 2 to 4 elements from the list deletes 2nd and 3rd element from the list, , deletes ist and 3rd element from the list deletes Ist, 2nd and 3rd element from the list 29. ‘Which of the following will give output as: [23, 2, 9, 75]? IF list1=[6,23,3,2,0,9,8,75] (@ prine(listi[1:7:2]) (b) print(list1[0-7:2)) (6) printflisti[1:8:2}) _(c) print(list1[0:8:21) 30, Find the output values = [[3, 4,5, 1], (33, 6,1, 2]] for row in values: rowsart() for element in row: print{element, end="") print() @). The program prints on row 345 133612 (b). The program prints two rows 3451 followed by 33 6 12 (©). The program prints two rows 3451 followed by 33 6 12 (@). The program prints two rows 1345 followed by 12.633 hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 Ti38 2825, 11:25 AM CS-CHAPTER 2-08 - Google Docs Z MARKS / 3 MARKS (a) =51=[1, Computer”2; Science” 10, PRE" 30; BOARD'] >>oprint(L[3:)) (b) Identify the output ofthe following Python statements. poox = [[10.0, 11.0, 12.0],[13.0, 140, 15.0] >eey = x{1][2] >>>print(y) Write the output of following code: Ist1=[10,15,20,25,30] IstLinsert(3,4) IstLinsert(2.3) printflis{-5)) Write output for the following code: list1=[x+2 for x in range(S)] print{list1) What will be the output of the following code? a=[1,2,3,4] s-0 for af-1] ina: print(a[-1}) st=al-l print{,sum="'s) 5 oO Predict the output of the following: l Li=[30.40] 12-[50,60] Lappend(Lt) Lestend(L2) print(L) 20) Predict the output of the following: M=[11, 22, 33, 44] Q=M [2] +=22 Lelen(M) for i in range (L): print(“Now@", Q{L--1}, "#", MUil) Prodict the output of the Python code given below: I=[ ] for i in range(4) Lappend(2*i+1) print(i(:-1)) hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 18134 2825, 11:25 AM CS-CHAPTER 2-08 - Google Docs Predict the output of the following: TXT = [°10°'20"30"/5"] cnt =3 TOTAL=0 for Cin (7,5.4,6):T = TXT[CNT] TOTAL = float (1) +¢ print (TOTAL) oN Write the output ofthe following: x= ((1,2,3)18,5, 6117.8, 9]) result = [] for items in for item in items ifitem % 2 == 0: [Link](item) print(result) ‘Write the output of the following python coder Numbers =[9,18,27,36] for num in Numbers for N in range(1,num%8): print(N'S" end=") print) 10. Predict the output of the following: Listt = list("Bxamination’) List2 Listl[1=1] new_list = [] for tin List2: [Link](i) if i%62==0. [Link](i) print(Listl) ii. Predict the output of the following: fruitist [Apple,,'Berry’, ‘Cherry’, Papaya] fruit list2 = fruit list fruit list3 = fruitlisti[] fruit list2{0] = 'Guaval fruit ist3[1] ="Kiwi’ sum = ° for Is in (fruit list, fruit ist2, fruit list3}: if |s{0] == "Guava! sum += 1 If ls[1] == 'Kiwi' sum += 20 print(sum) hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 19134 2825, 11:25 AM CS-CHAPTER 2-08 - Google Docs 72. Predict the output of the following: LAL2=[10,15,20,25). | for i in range(len(Lt)) L2insert(i,L1.pop0) print(L1,12,sep="&’) 14 Predict the output of the following: L1=[10,20,30,20,10], L2=[] fori in L1: If} not in L2: [Link](i) print(L1,L2,sep='$') Predict the output of the following: num=[10,20,30,40,50,60] for x in range(0,len(num),2}: ‘num|x}, num[x+1]}=num[x+1], num[x) print(num) 16. Predict the output of the following: 1=[1,2,3,45] Lst=[] for in range(len(L)): if iM 221: (Li LUi]"*2), Lstappend(t) print{Lst) 17. Predict the output of the following: sub=["CS", "PHY", “CH r del sub[2] [Link]("ENG") sub,pop(1) print(sub) Ta. Predict the output of the following: Predict the output: list =['Red, ‘Green’, ‘Blue’, ‘Cyan'Magenta''Yellow’,] print(liset([-4:0:-1)) 79. Predict the output of the following: L=[10,20,40,50,60,67] 1[0:3]="100" print(L) hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 20134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs [Link]/1k_EOBXOxGR2NZBYex634id7e8m@XGjLANNFEWLWieledt7tap=t0 21134 2825, 11:25 AM CS-CHAPTER 2-08 - Google Docs 20. Predict the output ofthe folowing: Ist= [40,15, 20, 25, 30] Istsor) Istinsert(3, 4) Istappend(23) print(st) 2 Predict the output of the following: L=[10,2,5,1,90,23,45] Lsort(reverse=True) S=L-pop(2) Linsert(4,89) Lsort() Lremove(90) print(L) 22. Predict the output ofthe following: L=(10,2,5,-1,90,23,45] Lsort(reverse=True) G=Lappend([Link](L-pop0))) print(G) 23. Predict the output of the following: L=['30'/40''20''50] count=3 Sun i fori inf6.45,8} T=L{count} Sum-float(T) +1 Pappend|(Sum) print) print(P[30:-1)) for i in(0,1,2,3] ePLi}}+10 print(P) a Predict the output ofthe following: L=['FORTRAN'C+#"Java’‘Python’] Linsert(2,"HTML") del 14) Lremove('Java’) Lpop() Linsert(17PHP") LsortQ Lpop() print(L) hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 22134 2825, 11:25 AM CS-CHAPTER 2-08 - Google Docs TOPIC: TUPLES STATE TRUE OR FALSE Tuple data structure is mutable tuples allowed to have mixed lengths: Tuple have one element for Ex: S=(10) We Can concatenate tuples. The elements of the of a tuple can be deleted ‘Atuple storing other tuples is called as nested tuple The + Operator adds one tuple to the end of another tuple 2] fo] en} a] | 9] 5} ‘A tuple can only have positive indexing, ‘ASSERTION & REASONING ‘A Tuples are ordered R: Items in a tuple has an defined order that will not be changed. ‘A: Atuple can be concatenated to a list, buta list cannot be concatenated to a tuple, R: Lists are mutable and tuples are immutable in Python ‘A: Tuple is an in inbuilt data structure R: Tuple data structure is created by the programmers and not by the creator of the python a=(1,2,3) a[0]=4 A: The above code will result in error R: Tuples are immutable, So we cant change them. ‘A: A tuple cannot be sorted inline in python and therefore sorted () returns a sorted list after sorting a tuple. R: Tuples are non-mutable data type >>> tuplel = (10,20,30,40,50) >> tuplel index(90) ‘A: Above statements gives Valuelrror in python R: Valuerror: [Link](x): x not in tuple T1= (10, 20, ( y), 30) print([Link](x}) A: Above program code displays the index ofthe element x" R: Returns the index ofthe first occurrence of the element in the given tuple >>stuplel = (10,20,30,40,50,60,70,80) >>etuplel[2:] Output: (30, 40, 50, 60, 70, 80) A: Output given is incorrect R: Slicing in above case ends at last index hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 23184 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs % t=(10,20,30) t{1]=45 the above mentioned question will throws an error R: Tuple are used to store sequential data in a program 10. ‘A: Forming a tuple from Individual values is called packing IR: Many individual data can be packed by assigning them to one single variable, where data is separated by ‘OBJECTIVE TYPE QUESTIONS (MCQ) Which of the following statement creates a tuple? a}t=[1,2,34] ——b) t=(1,2,3,4} c) t<1,2,3,4> d) 1,2,3,4) Which of the following operation is supported in python with respect to tuple? at b)tappend(33)__c)tetet__—_d) tsumO) Which of the following is not a supported operation in Python? (a) "xy2"+"abe" ——(b) (2)+(3.)(e) 243 (4) [2.4] 411.2] Predict the output: T=(1) print(T*3) 3) (111) | 111 @) (3) Identify the errors in the following code: MyTuplei=(1, 2, 3) ‘#Statement1 MyTuple2=(4) ‘#Statement2 [Link](4) #Statement3 print(MyTuple1, MyTuple2) #Statement# (@) Statement 1 (b) Statement 2 (c) Statement 3 {(@) Statement 2 &3 Suppose a tuple Tup is declared as Tup = (12, 15, 3, 80) which ofthe followingis incorrect? (a) print(Tup[1]) {b) Tup[2] = 90 (6) prine(ain(Tup)) (4) prinelen(Tup)) Predict the output of the following code: 2,3,4,5,6) print([Link](4)) @4 5 6 Ww hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 2434 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs Given tp = (1,2,3,4.5,6). Which of the following two statements will give the same output? 1. print(tpl:-1)) # Statement 1 2. print(sp[0%5]) # Statement 2 3. print{sp[0:4]) # Statement 3 4. print(tp[-4:]) # Statement 4 (a) Statement 1 and 2 (b) Statement 2 and 4 (©) Statement 1 and 4 (4) Statement 1 and 3 What will be the output for the following Python statement? T=(10,20,[30,40,50],60,70) T[2][1]=100 print{T) (a) (10,20,100,60,70) (b) 10,20,{30,100,50],60,70) (©) (10,20,[100,40,50),60,70) (4) None of these 10. Which of the following is an unordered collection of elements (a) List (b) Tuple’—(¢) Dictionary (4) String it. Consider the following statements in Python and the question that follows: i. Tupleis an ordered list ii, Tuple is a mutable data type From the above statements we can say that (@) Only (i) is correct (b) Only (ii) is correct (c) Both (i) and (ii) are correct (@) None of (i) and (ii) are correct 12. Which of the following is nota tuple in Python? (a) (4,2,3) (6) COne'"Two"”Three") (c) (10,) (4) COne") 13. Suppose a tuple T is declared as T=(10,20,30) and a list L=["mon", tue’, “wed ‘which of the following is incorrect ? a) min(L) b) L{2] = 40 ¢) T[3] ="thurs” d) print{ min¢t)) 14 ‘Suppose a Python tuple T is declared as: T- ) Which ofthe following command is invalid? ()T=CD)—()T=(D)(E)T#=(D')—_ (a) TH=('D') is. Predict the output: tupt = (2.41621,3) tupi(2]{1]=7 print(tupl) (ror 0) 2416213) (2416713) 2467.3) hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 25134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs 16. ] Consider a tuple t=(2.4.5), which of the following will give error? (@) list(Q)[-1]=2 (b) print(t-1]) (c)list().append(6) (4) t{-1]=7 17. | Suppose a tuple TI is declared as TI = (10, 20, 30, 40, 50) which of the| following is incorrect? a)print{I{1]) b) T[2] =-29 ) print{max(1)) d) print(len(T)) 1. | What will be the output of the following Python code ? T1=() T2sT1*2 print(len(T2)) @o 2 ji (@) For 19. | Predict the output ‘Tup-(3,1,2,4) sorted(Tup) print(Tup) @B1249 6) 1.23.4) (0 B1241 @) 1.234) 20. | Predict the output ofthe following: $=((1,2) [3-41.[5,6]) s[j[)=8 print(T) (a) ((1.2),184),[5.6)) (b) ((2.2],[3,4),[8,6)) (©) ({1.2}.13.4115.8)) (4) Will generate an Error as a Tuple is immutable hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 26134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs Z MARKS / 3 MARKS Predict the output of the following: test list = (5,6, 7] [Link] = (9, 10) res = tuple(list(test_tup) + test list) print{str(res)) tuplel = (5, 12, 7,4,9,6) listt =list(tuplet) list insert(2,8) listL_popO tuple1=tuple(listt) print(tuplet) Predict the output of the folowing code 17.61, (44), [5,9], 34), [55] (62), [84)) list{ tuple1) rl ist{) for elem in listy tot=0 for-valuein elem: tot ifelem count(value) wo 2: new listappend{ value) else: print( tuple( sw_list) ) Write the output of the code given below: ab,ed= (1.23,4) ‘mytuple = (ab,c.d)*2+(5**2,) print{len(mytuple)+2) hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 27134 2825, 11:25 AM CS-CHAPTER 2-08 - Google Docs "TOPIC - DICTIONARY STATE TRUE OR FALSE Dictionary values are immutable List can be converted in Dictionary Itis always not necessary to enclose dictionary in{} = ||] Dictionary keys are unique and always of string type [Link]() is used to delete the last items of the dictionary. [Link]() method is used to insert new key-value pair inside of the dictionary. ‘ASSERTION & REASONING ._| As Dictionaries are mutable, R: Individual elements can be changed in dictionary in place. 2. | Az Dictionary in Python holds data items In key-value pairs R: Immutable means they cannot be changed after creation ‘A: Key of dictionary can’ tbe changed R: Dictionary Key's are immutable | A: Key of dictionary must be single element, R: Key of dictionary is always string type. 5. | A: Dictionary and set both are same because both enclosed in { R: Dictionary is used to store the data in a key-value pair format. 6. | As The popQ) method accepts the key as an argument and remove the associated value R: pop() only work with list because itis mutable. ‘A: The items of the dictionary can be deleted by using the del keyword. R: del is predefined keyword in python, hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 28134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs ‘OBJECTIVE TYPE QUESTIONS (MOQ) Which statement is correct for dictionary? (a) A dictionary is a ordered set of key: value pair (b) each of the keys within a dictionary must be unique (©) each of the values in the dictionary must be unique (a) values in the dictionary are immutable ‘Which of the following is the correct statement for checking the presence ofa key in the dictionary? a) in b) not in 6) found in a} exists in ‘What will be the output for the following Python statements? D= {’Amit":90, "Reshma’96, "Subail":92, “John”:95} print(“John’ in D, 90 in D, sep=“#") (a) True#False (b)True#True (c) False#True (d) False#False Choose the most correct statement among the following = (a) a dictionary is a sequential set of elements (b) a dictionary is a set of key-value pairs (©) adictionary is a sequential collection of elements key- value pairs (@) a dictionary is a nom-sequential collection of elements ‘What will be output of the following code: d1=(1:2,3:4,5:6) d2=[Link]() print(d2) G2} G6 (902) 66) ‘Which of the following statements create a dictionary? ad=() 1b) d= {"john":40, "peter"s45) 6) d= {40: john’, 45: "peter" 4) All of the mentioned above ‘What will be output of following: = {1 "SUM", 2: "DIFF", 3: "PROD"} for iin print () Predict the output of the following: D=(1:"One",2:"Two",3:"Three"} L=[ ] for KV in Daitems(): if V(O]=="T": Lappend(k) print(L) (a) (1.2.3), (@) (2.3) hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 20134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs “Given the following diovonary employee-(salary’'10000;age'22/name’/Mahesh’) [Link](age') print{employee) ‘What is output? a. (age'22) b. (Salary 10000, name':"Mahesh') «. (salary’:10000/age'22,nameMahesh’) 4. None of the above 10. va, Bz, C3) ‘Then, which ofthe following command will remove the entire dictionary from the memory? (@)del(D) (b) Ddel() _(c) Declear() (a) Dremovet) T. Predict the output of the following: D1: 1C:9,D'10) D2=('8'5,D':10} D1update(D2) print(D1) (@ (W5'B'5,/c:9;D'10) &) (Ws! (©) {8'5/C:9/D's10) 4 12. Predict the output of the Following: D1=(1:2,23,3-4) D2=Diget(1.2) print(D2) @2 3B 0223) 13. ‘Which of the following operation(s) supported in Dictionary? (a) Comparison(only = and !=) (b) Membership (©) Concatenation (@) Replication 1 Predict the output of the following: D1={1:10,4:56,100:45} D2=(1:10,4:56,45:100} print(D1==D2) (a) True (b) False (©) Cannot Compare the dictionaries (@) Eror 15. Predict the output ofthe following: D1=(1:10,4:56,10045) D2=(1:10,4:56,45:100} print(D1<=D2) (@) True (0) False {c) Cannot Compare the dictionaries (d) Error hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 30134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs 16. ‘Write a python command to create list of Keys from a dictionary, dict=(aiA, 'b''BIyeC, 'dD'} (a) dictkeysQ) —(b) Keys()_(c) [Link]() (4) None of these 17. ‘What will the following code do? dict=("Phy’:94,"Che":70,"Bio':82,"Eng":95) dictupdate({"Che":72,’Bio":80}) () Itwill create new dictionary as dict=("Che":72,"Bio":80) and old dict will be deleted, (b) [twill throw an error as dictionary cannot be updated. (6) twill simply update the dictionary as dict=("Phy":94,"Che":72,"Bio":80, "Eng’95) (4) Iwill not throw any error but it will not do any changes in diet 18. Consider the coding given below and fil inthe Blanks Dict, d=(,BookName" Python’, Author"y"Arundhati Roy") Dict [Link]() # Statement 1 List L=["java"’SQL""Python”] List_Lpop() # Statement 2 (i) inthe above snippet both statement 1 and 2 deletes the last clement from the object Dict d and 79: List___(True/False) (i) Statement (1/2) alone deletes the last element in its object Tdentfy the output of following code d={''"Delhi'sb'’Mumba’/c'"Kol kata} for iin a ifiin diy en(ali) print(x) @e 0 5 a7 20. What willbe the output of the following code? DI=(I “One"2: "Two", 3:°C") D2=(4:"Four’,5: "Five" Diupdate(b2) print (D1) 2) (4: "Four" 5: Five") b) Method update) doesn’t exist for dictionary et ) ) (10ne' 2: "Twot,3:"C*4:"Four" "Five" hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 3134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs 2. “Given the following dictionaries Dict1=("Tuple': immutable, ‘List’ ‘mutable, ‘Dictionary’: mutable, ‘String’ "immutable’} Which of the following will delete key:value ey=,Dictionary" in the above mentioned dictionary? (2) del Dicti{ Dictinary'] (©) Dicti[,Dictionary"].detete{) (6) delete (Diet1."Dictionary")) (@) del(Dictt [,Dictionary"}) 2 MARKS 7 3 -MARKS Write a statement in Python to declare a dictionary whose Keys are Sub1, Sub2, Sub3 and values are Physics, Chemistry, Math respectively. Debug the following code and underline the correction made: dedict{ } n-input{"enter number of terms") for i in range(n}: a=input{(enter a character’) d[i}=a Write the output of the code given below: >epsquares = (I: 1, 2:4, 3:9, 4: 16, 5:25) >p>print(squares,pop(4)) Waite the output of the code given below Emp = ( “SMITH":45000 , "CLARK":20000 } Emp["HAPI 23000 Emp["SMITH"] = 14000 print( [Link]()) ill be the output ofthe following Python code? strl=strLestr(a1fiJ)+"" trl :-1] print(str2[::-1)) Predict the output of the Python Code given below: ‘de{"Rohan’:67,"Ahasham’:78,"naman":89,"pranav":79} print(d) ssum=0 for jing: sum+=d{i] print(sum) print(’sum of values of dictionaries"sum) hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 2134 ‘18/25, 11:28 AM CS-CHAPTER 2-08 - Google Docs Predict the output of d-(2:b'4ie'} dl={1!a)) dupdate(di) afi]="" print(list(d valuesQ)) Mydict={} Mydict{(1,2,3)]=12 Mydict{(45,6)]=20 Mydiet{(1.2)]=25 toal-0 fori in Mydict total=total+Mydict{i] print{otal) print(Mydict) Write the output of the code given below: dll = {name's ‘Aman’, "age": 26} 2 = (27:age!‘age'28) dLupdate(d2) print{[Link]() 10. ‘Write the output of the following code given below: Marks = {'Sidhi':65, ‘Prakul':62, ‘Suchitras64,‘Prakash':50} newMarks =| {Suchitral:66/Arun':55; Prkash'49) [Link]) for key,value in [Link](): print{key;scored’valuemarks in Pre Board’,end="') if(value<55) print(,and needs imporovement’en print) i. ‘Write the output of the following Python program code: my_dict=() my_dict{(1,2,4)] =8 my_dict{(4,2.1)] = 10 smy-diet|(1,2)] = 12 fork in my_dict: sum += my-dietik] print (sum) print{my_dict) hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 33184 2825, 11:25 AM CS-CHAPTER 2-08 - Google Docs 72. Predict the output of the following: Lisa’) for i in range(1,5) D{t}=Thi-1] print(D) 3B. Predict the output of the following: L=[10,20,30] Del) Nalen(s) fori in range(N) DIL[I}]=SEi) for KV in Ditems() print([Link] Tt What will be the output, D1={'A":12, "BY: 6, °C" :50, print (50 in D1 70) sn by the following piece of code: 15. Predict the output ofthe folowing: Pais0o D={KTM'45;Royalfnfeld"75,Jawa:3/TVS°08, "Yamata‘111) for} ind: if(len(j)>5); [Link]] print() Xe(‘Suzukt:1000;Bajaj:21) [Link](X) prin(x) print(D) Drint(D get Jawa’ KTM) hitps does. google.comidocumentik_EOBx/OxGRZNZBYex834is7e&m9XGjLANHFEWLWieledt?in0=t0 sar34

Vous aimerez peut-être aussi