Our Features

Our Features
.

Friday, 17 June 2016

Python Questions and Answers – Strings – 6

This set of Python Quiz focuses on “Strings”.
1. What is the output of the following?
print("xyyzxyzxzxyy".count('yy'))
a) 2
b) 0
c) error
d) none of the mentioned
View Answer
Answer: a
Explanation: Counts the number of times the substring ‘yy’ is present in the given string.
2. What is the output of the following?
print("xyyzxyzxzxyy".count('yy', 1))
a) 2
b) 0
c) 1
d) none of the mentioned
View Answer
Answer: a
Explanation: Counts the number of times the substring ‘yy’ is present in the given string, starting from position 1.
3. What is the output of the following?
print("xyyzxyzxzxyy".count('yy', 2))
a) 2
b) 0
c) 1
d) none of the mentioned
View Answer
Answer: c
Explanation: Counts the number of times the substring ‘yy’ is present in the given string, starting from position 2.
advertisements
4. What is the output of the following?
print("xyyzxyzxzxyy".count('xyy', 0, 100))
a) 2
b) 0
c) 1
d) error
View Answer
Answer: a
Explanation: An error will not occur if the end value is greater than the length of the string itself.
5. What is the output of the following?
print("xyyzxyzxzxyy".count('xyy', 2, 11))
a) 2
b) 0
c) 1
d) error
View Answer
Answer: b
Explanation: Counts the number of times the substring ‘xyy’ is present in the given string, starting from position 2 and ending at position 11.
6. What is the output of the following?
print("xyyzxyzxzxyy".count('xyy', -10, -1))
a) 2
b) 0
c) 1
d) error
View Answer
Answer: b
Explanation: Counts the number of times the substring ‘xyy’ is present in the given string, starting from position 2 and ending at position 11.
7. What is the output of the following?
print('abc'.encode())
advertisements
a) abc
b) ‘abc’
c) b’abc’
d) h’abc’
View Answer
Answer: c
Explanation: A bytes object is returned by encode.
8. What is the default value of encoding in encode()?
a) ascii
b) qwerty
c) utf-8
d) utf-16
View Answer
Answer: c
Explanation: The default value of encoding is utf-8.
9. What is the output of the following?
print("xyyzxyzxzxyy".endswith("xyy"))
a) 1
b) True
c) 3
d) 2
View Answer
Answer: b
Explanation: The function returns True if the given string ends with the specified substring.
10. What is the output of the following?
print("xyyzxyzxzxyy".endswith("xyy", 0, 2))
a) 0
b) 1
c) True
d) False
View Answer
Answer: d
Explanation: The function returns False if the given string does not end with the specified substring.

Python Questions and Answers – Strings – 5

This set of Basic Python Questions & Answers focuses on “Strings”.
1. What is the output of the following?
print("abc DEF".capitalize())
a) abc def
b) ABC DEF
c) Abc def
d) Abc Def
View Answer
2. What is the output of the following?
print("abc. DEF".capitalize())
a) abc def
b) ABC DEF
c) Abc def
d) Abc Def
View Answer
3. What is the output of the following?
print("abcdef".center())
a) cd
b) abcdef
c) error
d) none of the mentioned
View Answer
4. What is the output of the following?
print("abcdef".center(0))
advertisements
a) cd
b) abcdef
c) error
d) none of the mentioned
View Answer
5. What is the output of the following?
print('*', "abcdef".center(7), '*')
a) * abcdef *
b) * abcdef *
c) *abcdef *
d) * abcdef*
View Answer
6. What is the output of the following?
print('*', "abcdef".center(7), '*', sep='')
a) * abcdef *
b) * abcdef *
c) *abcdef *
d) * abcdef*
View Answer
7. What is the output of the following?
print('*', "abcde".center(6), '*', sep='')
a) * abcde *
b) * abcde *
c) *abcde *
d) * abcde*
View Answer
Answer: c
Explanation: Padding is done towards the right-hand-side first when the final string is of even length.
advertisements
8. What is the output of the following?
print("abcdef".center(7, 1))
a) 1abcdef
b) abcdef1
c) abcdef
d) error
View Answer
Answer: d
Explanation: TypeError, the fill character must be a character, not an int.
9. What is the output of the following?
print("abcdef".center(7, '1'))
a) 1abcdef
b) abcdef1
c) abcdef
d) error
View Answer
Answer: a
Explanation: The character ‘1’ is used for padding instead of a space.
10. What is the output of the following?
print("abcdef".center(10, '12'))
a) 12abcdef12
b) abcdef1212
c) 1212abcdef
d) error
View Answer
Answer: d
Explanation: The fill character must be exactly one character long

Python Questions and Answers – Strings – 4

This set of Python Coding Questions & Answers focuses on “Strings”.
1. What is “Hello”.replace(“l”, “e”)
a) Heeeo
b) Heelo
c) Heleo
d) None
View Answer
Answer:a
Explanation:Execute in shell to verify.
2. To retrieve the character at index 3 from string s=”Hello” what command do we execute (multiple answers allowed) ?
a) s[3] b) s.getitem(3)
c) s.__getitem__(3)
d) s.getItem(3)
View Answer
Answer:a, c
Explanation:__getitem(..) can be used to get character at index specified as parameter.
3. To return the length of string s what command do we execute (multiple answers allowed) ?
a) s.__len__()
b) len(s)
c) size(s)
d) s.size()
View Answer
Answer:a,b
Explanation:Execute in shell to verify.
4. If a class defines the __str__(self) method, for an object obj for the class, you can use which command to invoke the __str__ method.(multiple answers allowed)
a) obj.__str__()
b) str(obj)
c) print obj
d) __str__(obj)
View Answer
Answer:a,b,c
Explanation:Execute in shell to verify.
5. To check whether string s1 contains s2, use
a) s1.__contains__(s2)
b) s1 in s2
c) s1.contains(s2)
d) si.in(s2)
View Answer
Answer:a,b
Explanation:s1 in s2 works in the same way as calling the special function __contains__ .
advertisements
6. Suppose i is 5 and j is 4, i + j is same as
a) i.__add(j)
b) i.__add__(j)
c) i.__Add(j)
d) i.__ADD(j)
View Answer
Answer:b
Explanation:Execute in shell to verify.
7. What is the output of the following code ?
  1. class Count:
  2.     def __init__(self, count = 0):
  3.        self.__count = count
  4.  
  5. c1 = Count(2)
  6. c2 = Count(2)
  7. print(id(c1) == id(c2), end = " ")
  8.  
  9. s1 = "Good"
  10. s2 = "Good"
  11. print(id(s1) == id(s2))
a) True False
b) True True
c) False True
d) False False
View Answer
Answer:c
Explanation:Execute in the shell objects cannot have same id, however in the case of strings its different.
8. What is the output of the following code ?
  1. class Name:
  2.     def __init__(self, firstName, mi, lastName):
  3.         self.firstName = firstName
  4.         self.mi = mi
  5.         self.lastName = lastName
  6.  
  7. firstName = "John"
  8. name = Name(firstName, 'F', "Smith")
  9. firstName = "Peter"
  10. name.lastName = "Pan"
  11. print(name.firstName, name.lastName)
advertisements
a) Peter Pan.
b) John Pan.
c) Peter Smith.
d) John Smith.
View Answer
9. What function do you use to read a string?
a) input(“Enter a string”)
b) eval(input(“Enter a string”))
c) enter(“Enter a string”)
d) eval(enter(“Enter a string”))
View Answer
Answer:a
Explanation:Execute in shell to verify.
10. Suppose x is 345.3546, what is format(x, “10.3f”) (_ indicates space)
a) __345.355
b) ___345.355
c) ____345.355
d) _____345.354
View Answer
Answer:b
Explanation:Execute in the shell to verify.

Python Questions and Answers – Strings – 3

This set of Python Technical Interview Questions & Answers focuses on “Strings”.
1. What is the output when following statement is executed ?
  1. >>>chr(ord('A'))
a) A
b) B
c) a
d) Error
View Answer
2. What is the output when following statement is executed ?
  1. >>>print(chr(ord('b')+1))
a) a
b) b
c) c
d) A
View Answer
advertisements
3. Which of the following statement prints hello\example\test.txt ?
a) print(“hello\example\test.txt”)
b) print(“hello\\example\\test.txt”)
c) print(“hello\”example\”test.txt”)
d) print(“hello”\example”\test.txt”)
View Answer
4. Suppose s is “\t\tWorld\n”, what is s.strip() ?
a) \t\tWorld\n
b) \t\tWorld\n
c) \t\tWORLD\n
d) World
View Answer
5. The format function returns :
a) Error
b) int
c) bool
d) str
View Answer
6. What is the output of “hello”+1+2+3 ?
a) hello123
b) hello
c) Error
d) hello6
View Answer
7. What is the output when following code is executed ?
  1. >>>print("D", end = ' ')
  2. >>>print("C", end = ' ')
  3. >>>print("B", end = ' ')
  4. >>>print("A", end = ' ')
a) DCBA
b) A, B, C, D
c) D C B A
d) A, B, C, D will be displayed on four lines
View Answer
advertisements
8. What is the output when following statement is executed ?(python 3.xx)
  1. >>>print(format("Welcome", "10s"), end = '#')
  2. >>>print(format(111, "4d"), end = '#')
  3. >>>print(format(924.656, "3.2f"))
a)    Welcome# 111#924.66
b) Welcome#111#924.66
c) Welcome#111#.66
d) Welcome   # 111#924.66
View Answer
9. What will be displayed by print(ord(‘b’) – ord(‘a’)) ?
a) 0
b) 1
c) -1
d) 2
View Answer
10. Say s=”hello” what will be the return value of type(s) ?
a) int
b) bool
c) str
d) String
View Answer
Answer:c
Explanation:str is used to represent strings in python.

Python Questions and Answers – Strings – 2

This set of Advanced Python Interview Questions & Answers focuses on “Strings”.
1. What is the output of the following code ?
  1. class father:
  2.     def __init__(self, param):
  3.         self.o1 = param
  4.  
  5. class child(father):
  6.     def __init__(self, param):
  7.         self.o2 = param
  8.  
  9. >>>obj = child(22)
  10. >>>print "%d %d" % (obj.o1, obj.o2)
a) None None
b) None 22
c) 22 None
d) Error is generated
View Answer
Answer:d
Explanation:self.o1 was never created.
2. What is the output of the following code ?
  1. class tester:
  2.     def __init__(self, id):
  3.         self.id = str(id)
  4.         id="224"
  5.  
  6. >>>temp = tester(12)
  7. >>>print temp.id
a) 224
b) Error
c) 12
d) None
View Answer
Answer:c
Explanation:id in this case will be the attribute of the class.
3. What is the output of the following code ?
  1. >>>example = "snow world"
  2. >>>print "%s" % example[4:7]
a) wo
b) world
c) sn
d) rl
View Answer
Answer:a
Explanation:Execute in the shell and verify.
advertisements
4. What is the output of the following code ?
  1. >>>example = "snow world"
  2. >>>example[3] = 's'
  3. >>>print example
a) snow
b) snow world
c) Error
d) snos world
View Answer
Answer:c
Explanation:Strings cannot be modified.
5. What is the output of the following code ?
  1. >>>max("what are you")
a) Error
b) u
c) t
d) y
View Answer
Answer:d
Explanation:Max returns the character with the highest ascii value.
6. Given a string example=”hello” what is the output of example.count(l)
a) 2
b) 1
c) None
d) 0
View Answer
Answer:a
Explanation:l occurs twice in hello.
7. What is the output of the following code ?
  1. >>>example = "helle"
  2. >>>example.find("e")
advertisements
a) Error
b) -1
c) 1
d) 0
View Answer
Answer:c
Explanation:returns lowest index .
8. What is the output of the following code ?
  1. >>>example = "helle"
  2. >>>example.rfind("e")
a) -1
b) 4
c) 3
d) 1
View Answer
Answer:b
Explanation:returns highest index.
9. What is the output of the following code ?
  1. >>>example="helloworld"
  2. >>>example[::-1].startswith("d")
a) dlrowolleh
b) True
c) -1
d) None
View Answer
Answer:b
Explanation:Starts with checks if the given string starts with the parameter that is passed.
10. To concatenate two strings to a third what statements are applicable (multiple answers are allowed) ?
a) s3 = s1 + s2
b) s3 = s1.add(s2)
c) s3 = s1.__add__(s2)
d) s3 = s1 * s2
View Answer
Answer:a,c
Explanation:__add__ is another method that can be used for concatenation.