Programming and Problem Solving Through Python Language Theory Question

Strings In Python

  1. What does the len() function do when applied to a string?
    1. Returns the number of characters in the string.
    2. Converts the string to lowercase.
    3. Finds the first occurrence of a substring.
    4. Reverses the characters in the string.
    Answer : A
    Explain : len() function is used to calculate the lenght of a string.
  2. What is slicing in string ?
    1. to spilt the string in half.
    2. to access all elements of string
    3. slicing allows you to extract a substring by specifying a range of indices.
    4. None of the above
    Answer : C
    Explain : Slicing is used to print a particular word by using a range. For slicing we use [] brackets.
  3. What will be the output of the following Python code snippet ?
    str = "6/2"
    print( "str")
    
    1. 6/2
    2. 3
    3. 3.0
    4. str
    Answer : D
    Explain : str is in double quoted. So it print str not assign value.
  4. What will be the output of the following Python code snippet ?
    str = "6/2"
    print( str)
    
    1. 6/2
    2. 3
    3. 3.0
    4. str
    Answer : A
    Explain : It does not solve 6/2 because it treated it as string value not as number to solve.
  5. Solve print('work'+'hard')
    1. work+hard
    2. workhard
    3. work hard
    4. 'work' 'hard'
    Answer : B
    Explain : It treat this as a string and operation perform on it is concatenation (use to join string).
  6. What will be the output of the following Python code ?
    x = 'abcd'
    for i in x:
     print(i.upper())	
    
    1. a b c d
    2. A b c d
    3. A B C D
    4. error
    Answer : C
    Explain : for loop access all element word by word and convert them in uppar case then print.
  7. What will be the output of the following Python function?
    len(("hello",2, 4, 6))
    1. Error
    2. 6
    3. 4
    4. 3
    Answer : C
    Explain : lenght (len) function is used to count lenght of any string. So in this lenght of tuple is 4.
  8. What will be the output of the following Python code snippet ?
    for i in ("1234")[::-1]:
    		 print (i)
    
    1. 4 3 2 1
    2. 1 2 3 4
    3. 0 2 4
    4. Error
    Answer : A
    Explain : Here ("1234") treated as string value and accessing all element word by word. Remeber these word are in new line every time program run.
  9. what will be the output of following code ?
    print("a"+bc)
    1. abc
    2. a bc
    3. Error
    4. None of above
    Answer : C
    Explain : Error beacause bc is not defined. We have to declare this as string so, this can perform concatenatiom.
  10. Which of the following is NOT a valid method for string manipulation in Python ?
    1. upper()
    2. split()
    3. length()
    4. replace()
    Answer : C
    Explain : lenght() is not valid in string it is len(). upper() function - convert all letter in uppercase. split()- split string in list or words. replace() - replace letters in string to new.
  11. Which method is used to convert a string to uppercase ?
    1. to_upper()
    2. uppercase()
    3. toUpperCase()
    4. upper()
    Answer : D
    Explain : upper() is used to convert string in uppercase.
  12. What is the result of "python".find("th")?
    1. -1
    2. 2
    3. 3
    4. 1
    Answer : B
    Explain : It gives to because indexing value of t is 2.
  13. What is the result of "python".find("a")?
    1. -1
    2. 0
    3. false
    4. Error
    Answer : A
    Explain : Find() function gives -1 if not found value.
  14. What will be the output of given code :
    x="abcd"
    for i in range(len(x)):
        i.upper()
    print(x)
    
    
    1. A B C D
    2. a b c d
    3. 1 2 3 4
    4. Error
    Answer : D
    Explain : i.upper() means integer.upper() ( i value in this is 1, 2, 3, 4), which give a raise to error.
  15. What will be the output of given code ?
    x='abcd'
    for i in range (x):
        print(x)
    
    
    1. a b c d
    2. 0 1 2 3
    3. Error
    4. None of the above
    Answer : C
    Explain : Because of range it thinks x as a range. But it is string so for i in x is right but for i in range(x) is not correct.
  16. What will be the output of given code ?
    y= 'I love Python'
    y[3] ='s'
    print(y)
    
    
    1. snow
    2. 'I lsve Python'
    3. Error
    4. None of the above
    Answer : C
    Explain : 'str' object does not support item assignment
  17. What will be the output of following code ?
    x= min(max(False,-3,-4),2,7)
    print(x)
    
    
    1. -4
    2. -3
    3. 2
    4. False
    Answer : D
    Explain : max(False, -3, -4): The max function compares the provided arguments and returns the maximum value.
    In this case, the arguments are False, -3, and -4. In numeric comparisons, False is treated as 0, and among 0, -3, and -4, the maximum value is 0.
    min(0, 2, 7): The min function compares the provided arguments and returns the minimum value. Here, the arguments are 0, 2, and 7. The minimum value among these is 0.
    Assign the result of min(0, 2, 7) (which is 0) to the variable x.
    print(x): Print the value of the variable x. (Note 0 is treated as False.)
  18. What is the output of following code ?
    t = (2,3,4,3.5,5,6)
    print(sum(t)+ t.count(2))
    
    
    1. 24
    2. 23.5
    3. 24.5
    4. 25.5
    Answer : C
    Explain : sum(t) - sums all the value in it and t.count(2) - count 2 which is 1. So 23.5+1=24.5
  19. What does the 'strip()'' method do for a string ?
    1. Removes all spaces from the beginning and end of the string.
    2. Splits the string into substrings based on a delimiter.
    3. Converts the string to lowercase.
    4. Reverses the characters in the string.
    Answer : A
    Explain : The strip() method removes any leading, and trailing whitespaces. Leading means at the beginning of the string, trailing means at the end. We can remove also letter with this in string.
  20. What will the below Python code will give ?
    list1=[5,4,3,2]
    str1="1"
    for i in list1:
    	str1=str1+i
    print(str1)
    
    
    1. 15432
    2. 0
    3. 15
    4. Error
    Answer : D
    Explain : Error, because the value in i is not string. It is impossible to use + operator, because one is string and second is integer. To use + operator we first make i string using str() function.

Next Set

2