0% found this document useful (0 votes)
5 views

MCQs (Ch - 6 - Python Programming)

tyd6ude8

Uploaded by

Himanshu Mohanta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
5 views

MCQs (Ch - 6 - Python Programming)

tyd6ude8

Uploaded by

Himanshu Mohanta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
pyTHON PROGRAMMING. + upper()— converts all lowercase characters in a string to uppercase. Syntax: string upper() + tile) -returns a copy of the string in ttle case. Syntax: string title), + Istrip()— returns a string with all the leading whitespaces(default) removed. Can also be used to remove any other leading character. Syntax: string Istrip((chars}) + sstripQ ~ returns a string with all the trailing whitespaces(default) removed. Can also be used to remove any other trailing character. Syntax: string rstrip((chars}) + strip - returns a string with all the leading and trailing whitespaces(default) removed. Can also be used to remove any other leading and trailing character. Syntax: string strip((chars}) + count()~returns the number of occurrences of a substring in the given string, counting from the start position till the end position if specified. Syntax: string.count(substr, start =0 , end-len(string)) + find() - returns the index ifa string occurs in the given string, counting from the start position til the end. position if specified. Ifno occurrence is found it returns -1. Syntax: string find(substr, start=0, end =len(tring)) + index() is same as find() except that it raises an error ifthe string isnot found. Syntax: string index(substr, start=0, end =len(string)) + partition) - searches for a string in the given string and returns a tuple containing three elements : 1. The part of the string before the string to be searched. 2. The search string, 3. Part of the string after the search string. Syntax: string partition(value) + endswith() determines if string or a substring endswith suffix; returns true, ifso and false otherwise ‘Syntax: string endswith(suffix, start, end) + startswith() determines if a string or a substring of string starts with substring; returns true, if so and false otherwise. Syntax: string startswith(str, beg-0, end=len(string)) + joind returns a string in which the element of sequence have been joined by string separator. Syntax: string join{iterable) STAND ALONE MCQs Gncn eet) .1. Python allows repetition ofa set of statements using construct. (A) Looping, (8) Decision (© Condition oom (©) Sequence Q.3.sensssnn i a null operation. Ans. Option (A) is correct. (A) break > (B) continue : also known as eee mee oot seaman is unknown as looping. (©) jump .2-By default, the range of values in for loop sta!5 Any. Option () a Frm ain every eration. Option (0) acamect (ay 0 @1 () 10 Ans. Option (B) is correct. Q.4.Inif statement, when does the indented statement get executed? (A) Condition is true (8) Condition is false (©) Does not depend on condition (D) None of these ‘Ans. Option (A) is correct. 2 Syntax of if: if: statement 1 > statement 2 ‘Now if the tests to be true the block of statements (statements 1) will be executed. (Q.5. Which of these statements allows exactly two set of statements as options? (A) if (O) ifelifelse Ans. Option (B) is correct. (8) itelse (D) allof these else: statement set 2 Her two sets of statements are available as options, Q.6. The else of a loop executes when (A) The loop doesn't execute even once. (B) The loop is exited due to break. (© The loop’s condition becomes false (D) There is no else statement used with loops. Ans. Option (C) is correct. ae | Q.7.n operator (A) Check whether a value is contained inside a sequence (8) Check whether a variable exists (©) Checks whether flow of control is within a loop. (©) Does all the three above. ‘Ans. Option (A) is correct. | Explanation :in'isa membership operator. — | Q.8. Output of range (15,2, -2) will be (A) 15,13,11,9,7,5,3,1 (B) 15,13,11,9,7,5,3, (©) 2.3,4,5,6.0015 — (D)errorbecause of -2 ‘Ans, Option (B) is correct. ‘Explanation : function values Esererec eran lh Q.14. The slice operator is represented by swasl CaS MCQS Question Bark Chopterate Term, ROMPUTER ‘SCIENEE) Class - x Q.9. Consider the following block of code Al = per > 85 ‘Az = per < 85 debe per > 75 BI = per <75 &be per > 65 B2 = per < 65 dee per > 55 C= per < 55 dee per > 40 D=per<40 will (A) Ge {B) Store the value of per in respective variables. (C) Store the conditions with name to be used later {D) None of these ‘Ans, Option (C) is correct. jlanation : In Python named conditions can | snerate an error bbe used to store some respetective or complex conditions. Q.10. In the statement if Al : print (‘Excellent’) (A) There is a syntax error (B) Alisa named condition (C) Al needs to be defined earlier (D) Both (B) & (C) ‘Ans. Option (D) is correct. Explanation : Alisa named condition which is defined before using. (Q. 11. Whats displayed when we print an empty string? (a) 0 By (©) Name of the string (D) Blank space Ans. Option (D) is correct. Explanation : an empty string contains null which is a non printable character So we get a blank screen. Q.12 Right index starts from (ayo 1 oa (D) None of these ‘Ans. Option (C) is correct. | Setanta St the forward direction and ~ 1, ~ 2... in the backward direction, 0.18. nn Operators used to replicate string, (A) + (B) - — (D)/ ‘Ans. Option (C) is correct | Explanation : Mt on and when ; replicates the a, ): (i (D) Ans. Option (B) is correct. Bo yr prTHON PROGRAMMING Explanation : for stris i ae ea "| Q.15.Select two ways that can be used to extract a substring from a string. @ slice (i extended indexing (i) partition Q (iv) stip 0 (A) i&ii (B) ii Siti (0 iti&iv Dig ii ‘Ans. Option (A) is correct. peste indexing, Explanation : Two ways to slice a string are } [nmstep] ® PSS) PN D1 Wre yO) ye emg eee) Directions : In the following questions a statement of Assertion (A) is followed by a statement of reason, (®). Mark the correct choice as : (A) Both Assertion (A) and reason (R) are true and reason (R) is the correct explanation of Assertion A (B) Both Assertion (A) and reason (R) are true but reason (R) is not the correct explanation of Assertion (A). (©) Assertion (A) is true but reason (R) is false. (D) Assertion (A) is false but réason (R) is true. Q.1. Assertion (A): a= 0 whilea <=5: print (a*a) if( break azatl else: print ("Hello") Inabove code “Hello is never printed. Reason (R) : The else clause of a python loop executes when the loop terminates normaly, not When the loop is terminating because of a break statement. ‘Ans. Option (A) is correct. Explanation : In the code, since the value of a is being incremented by 1 in every iteration, therefore while loop will terminate by break statement when the value of a will be 2, So ‘Hello’ will never be displayed. Q.2 Assertion (A) : In the above code statement 2 and statement 3 will only be executed when in ‘if statement is false. Q. 16. The tuple that is returned by the partition() method contains (i) part of the string before the string to be searched (ii) the search string (ii) the sorted string (iv) part of the string after the search string iv (©) iii ‘Ans. Option (A) is correct. Explanation : The string partition () method! returns a tuple with three elements : 1, String before the search string, 2. Search string 3, String after the search string. Reason (R) : Statement 2 and statement 3 are parts, of else of if statement. ‘Ans. Option (C) is correct. Explanation : Reason for Assertion (A) is that the continue statement skips the rest of the ‘statements and causes the next iteration ‘of the loop to take place. Q.3.Assertion (A) : An if-else has less number of condition-check in comparison to two successive if statements, Reason (R) : The condition will be tested once in contrast to two comparisons of the same code implemented with two successive ifs. ‘Ans. Option (A) is correct. Explanation: When we use two ifs, two ‘conditions are specified and both of them will be tested but with if else only one a condition is specified and ifitis false else partis executed. Q.4,Assertion (A) : Indentation in python is very important when you are using a compound statement. Reason (R) : All statements in one block are indented at the same level. ‘Ans, Option (B) is correct. 2.5. Assertion (A) : If isa block statement whereas elif and else are not. Reason (R) ; Anested if isan if that has another if in ts ifs body or in eli’ body or in its else's body. ‘Ans. Option (D) is correct. ae and else all are block or compound statements, Q.6. Assertion (A) : Named conditions store output of some logical expressions, Reason (R) : Named conditions are used in ‘if statements to enhance readability and reusability of conditions. ‘Ans. Option (D) is correct. Explanation : Named conditions store logical’ | | Explanation : Assertion (A) is not true as if, elif | relational expressions and not their results, So Assertion (A) is false but Reason (R) is true. Q.7. Assertion (A) : “ABC” > “abc' will give true. Reason (R) : Internally Python compares strings using Unicode values. ‘Ans. Option (D) is correct. Explanation : “ABC: > "abc" will give false because unicode of ‘A’ (65) is less than unicode of a! (97). Q.8. Name = "ABHAY" print (Name [::~ 1)) Assertion (A) : Above code will print YAHBA. Reason (R) : String [ ‘Ans. Option (A) is correct. =I] will reverse a string, CASE-BASED MCQs ‘Attempt any four sub-parts from each question. Each sub-part carries 1 mark. I. Read the following text and answer the questions on the basis of the same : Python strings are characters enclosed in quotes of any type : single, double or triple. An empty string * isa string that has 0 characters. Python strings are immutable. Strings are sequence of characters, where each character has a unique position : id/ index. The indexes of a string begin from 0 to (length 1) in forward direction and ~ 1,~2, length in backward direction. Q.1.lerating through the elements of a string one character ata time is called (A) Looping (B) Iteration (©) Traversing (D) Searching ‘Ans. Option (C) is correct. Explanation ; A. string character by character. Q.24&*are (A) Arithmetic operators (B) Relational operators (©) String operators (D) Both (A) & (C) ‘Ans. Option (D) is correct. ee ee ae | concatenates the ™" When used with one string and a number produces replication ofthe string. COswasl CASE MICOS Gueston Bark Choptensie Term, COMPUTER SCIEN Cass— Explanation : String [m :m: step] is used to slice 4 fring, When we dont speciy stator stop default value of 0 & len(string) is taken. ~ 1 move reverse direction, Q.9. Assertion (A) : print (true’capitalizeQ) will print true, Reason (R) output ‘Ans, Option (C) is correct. Explanation : Assertion (A) is true but Reason (B) is not. Capitalize) function returns the copy of the string with first letter capital. Here the string is ‘true’ 60 output is True. Q.10. Assertion (A) : Python strings are stored in memory by strong individual characters in contiguous memory locations. Reason (R) : The index is the numbered position of a letter in the string ‘Ans. Option (B) is correct. Explanation : Both Assertion (A) and Reason (B) are true but Reason (R) is not reason of assertion (A). As string is an array of characters 50 its individual characters are stored in contiguous memory location. Capitalize () returns true/false as Q.3. Select the invalid statements (a) C=243 (A) of&h Oecat ‘Ans. Option (B) is correct. (D) allare valid Explanation : +" can be used either with both numbers or both strings. Similarly "can be used either with both numbers or with one string and one number. Both operands of "* can not be strings. Q.4.'a' <'A' will (A) give error (C) give false ‘Ans. Option (C) is correct. Explanation : When comparing two strings, the ‘unicode value of each is compared. is peter an hao upper ne eer (5 0 is ‘upper case to Sof ths statement will return false. Q.5. Consider the following code name = "ANUBHAV" print name [:3] + name [3:] (B) give true (D) not possible i | | sant PYTHON PROGRAMMING What will be the output. (A) ANUBHAV (8) ANU (©) BHAVANU (D) ANUUBHAV ‘Ans. Option (A) is correct. Explanation : Name {: 3] will slice the string from beginning till the character at index 3 (exclude) it will give ANU. name [3 : will start from character at index 3 till the end of string will give BHAV, '+' operator will concatenate the results and output will be ANUBHAV IL Read the following text and answer the questions on the basis of the same : ‘The selection construct means the execution of statement(s) depending upon a condition-test. If a condition evaluate to true, a course-of-action (a set of statements) is followed otherwise another course of action is followed. This construct is also called decision construct. Qf statementisa (A) Simple statement (8) Empty statement (©) Compound statement (D) Iterative statement ‘Ans. Option (C) is correct. Explanation : A compound statement rep: sc erany Q.2 Toimplement selection construct in python, we use (A) if then else (8) ifstatement © while (©) for ‘Ans. Option (B) is correct. Explanation : tional statements | Q3.Thecodeif a>5: print (‘Hello’) ifa<5: print (‘Sorry’) Can also be re-written in a better way as (Ayif ads print (Hello’) ifas print (Hello”) ele ifac5: print (Sorry") a>s: print (Hello”) else: print Sorry") (D) This code can not be written in any other way (8) if (© if ‘Ans. Option (C) is correct. Explanation : Writing the code using else instead of two if statements involves. less condition testing and hence is faster. Q.4. Rectify the following code if@a=5) yrat6 clseifa=7 y=ato else (A) if @=5) yrat6 else ifa = 7: y=ato else: it y=a+6 else ifa = =7 yeato else: y=10 Ans. Option (D) is correct. Q.5.Study the following code predict what it is used form X = int (input ("Enter first number") (input ("Enter second number.) (input (‘Enter Third number:")) min = mid = max = None. ifx statement 1 if + break statement 2 statement 3 statement 4 statement 5 Choose the correct order of statement execution considering the in ‘if of while statement to be true. (A) 1. while (B) 1. statement 4 2. statement 1 2. statement 5 3.if 4. break 5. statement 2 6. statement 3 7. statement 4 8. statement 5 (©) Lwhile (0) while 2. statement 1 2. statement 1 Bf if 4. break 4. break 5. statement 2 5. statement 4 6.statement3 = 6 statement 5 7. while | ‘Ans. Option (D) is correct.

You might also like