Programming and Problem Solving Through Python Language Theory Question

Strings In Python- Set 2

  1. lstrip() method is used for ?
    1. delete all the trailing chracters
    2. delete all the leading chracters
    3. delete all the leading and trailing chracters
    4. delete upper case characters
    Answer : B
    Explain : strip() function removes any leading, and trailing whitespaces. Lstrip()- It just remove left side leading chracters. L'string' - a leading string to appear at the beginning of the character or numeric data column.
  2. What does "hello".startswith("he") return ?
    1. True
    2. False
    3. Error
    4. None
    Answer : A
    Explain : startswith() give 'true' if the string start with given value. Other wise give False if not.
  3. Which method is used to split a string into a list of substrings based on a delimiter ?
    1. spilt()
    2. divide()
    3. break()
    4. separate()
    Answer : A
    Explain : spilt() function is used to spilt the string into list.
  4. What is the output of print('2'+'3') ?
    1. 23
    2. '23'
    3. 5
    4. 2+3
    Answer : A
    Explain : In this 2 and 3 treated as string, so operation perfom on these string is concatenation which can join these both and give result 23.
  5. What does the following code print?
     
    x = 'mohan' 
    for i in range(len(x)): 
     x[i].upper()
    print (x) 
    
    1. mohan
    2. MOHAN
    3. Error
    4. None of these
    Answer : A
    Explain : x[i].upper does not store any variable so it print default x value.
  6. String can be write in ?
    1. '-'
    2. "-"
    3. '''-'''
    4. ALl of these
    Answer : D
    Explain : String can be written in single quote, double quote and triple quote.
  7. String is
    1. mmutable
    2. immutable
    3. can be change after declare
    4. None of these
    Answer : B
    Explain : String are immutable ( unchanged ) which can not be change and declare in quote('').
  8. What is the output of given code - print('work'*3) ?
    1. workworkwork
    2. work
    3. work3
    4. work*3
    Answer : A
    Explain : In this work is string and on string use replication (*), which return work - 3 times.
  9. What is the output of following code ?
    x = 'abcd' 
    for i in range(len(x)): 
     	i.upper() 
    print (x)
    
    1. 0 1 2 3
    2. a b c d
    3. error
    4. None of these
    Answer : C
    Explain : When we run this we see an error, 'int' object has no attribute 'upper' because i value goes to 0 1 2 3 (lenght of string) and string upper function does not convert interger value to upper-case.
  10. Which function is used to convert into string ?
    1. sting.variable
    2. tostr()
    3. str()
    4. sting()
    Answer : C
    Explain : The str() function takes in any python data type and converts it into a string.
  11. What is the output of following code ?
    str="This is string"
    print(str.istitle())
    
    1. True
    2. False
    3. Error
    4. Convert all chracter of string in upper case
    Answer : B
    Explain : istitle() function only checks the first letter and first letter after space is in capital or not. If capital then True else False.
  12. Which of following is output of given code :
    stri="string"
    print(len(stri))
    
    1. stri
    2. string
    3. 6
    4. 5
    Answer : C
    Explain : len () function is used to check lenght of string. So, string has 6 character so lenght is 6.
  13. What is the output of following code :
    stri="string"
    print(stri[4])
    1. ng
    2. stri
    3. n
    4. g
    Answer : C
    Explain : In this slicing is use, So stri[4] means the letter at 4 no. starting from 0.
  14. What is the output of following code ? a='hello'
    a='hi'
    print(a)
    1. hello
    2. hi
    3. hellohi
    4. hihello
    Answer : B
    Explain : It print the update value of variable.
  15. What is output ?
    a='hi\'how\'are you'
    print(a)-
    1. hi'how'are you
    2. hi\'how\'are you
    3. hi are you
    4. 'how'
    Answer : A
    Explain : It show 'how' in quote.
  16. What is the output of following code ?
    a='abcde'
    l=len(a)
    print(l)
    1. 4
    2. 5
    3. abcde
    4. Error
    Answer : B
    Explain : In in the value store is lenght of string a which is 5.
  17. What is the index value of o in string 'python' ?
    1. -2
    2. 4
    3. 5
    4. Both A and B
    Answer : D
    Explain : From left to right the index value is 4 and from right index value is -2.
  18. Which of following code :
    a="Hello World" print(a[::-1])
    1. Hello World
    2. d
    3. dlroW olleH
    4. dlroW Hello
    Answer : C
    Explain : It just reverse the string.
  19. What is the output of following code ?
    var = "Hello"
    print(type(var))
    var=10
    print(type(var))
    1. str and int
    2. Hello and 10
    3. Word and Number
    4. string
    Answer : A
    Explain : Python type() is a built-in function that is used to return the type of data stored in the objects or variables in the program.

Next Set

1