Assuming that the snippet below has been executed successfully, which of the following expressions evaluate to True? (Choose two.) string = 'SKY'[::-1] string = string[-1]
Assuming that the snippet below has been executed successfully, which of the following expressions evaluate to True? (Choose two.) string = 'SKY'[::-1] string = string[-1]
After reversing the string 'SKY', the result is 'YKS'. Then assigning string = string[-1] sets string to 'S'. Thus, string[0] refers to 'S', and since string has only one character, string[-1] also refers to 'S', making string[0] == string[-1] true. The length of the string 'S' is 1, making len(string) == 1 true.
B and D are correct
string = 'SKY'[::-1] print (string) string = string[-1] print (string) print (string[0] == 'Y') print (string[0] == string[-1]) print (string is None) print (len(string) == 1)
True: B. string[0] == string [-1] D. len(string) == 1
string after reverse and just take -1 = string = S string[0] == string [-1] len(string) == 1
Ans A, False # string[0] == 'S' =>So string[0] !== 'Y' return false Ans B, False # string[0] == 'Y' => but string[-1] = S, return false Ans C, False # string is None => but string = 'SKY', so return false Ans D, True # len(string) = 1 => so len(string) == 1, so return true Correct answer should be B and D
B and D are correct answer