Question 12

Given the following assignment of a variable LS = [1, 9, 2, 8, 3, 7, 5, 4]

Given the following assignment of a variable LS = [1, 9, 2, 8, 3, 7, 5, 4] What will be the output of the following? print (LS [::2]+ LS [::-2])
  1. A[5, 3, 2, 1, 9, 8, 7, 4]
  2. B[1, 2, 3, 5, 4, 7, 8, 9]
  3. C[4, 7, 8, 9, 1, 2, 3, 5]
  4. D[9, 8, 7, 4, 5, 3, 2, 1]
Show answerHide answer

Correct answer: (B) [1, 2, 3, 5, 4, 7, 8, 9]

Explanation

LS[::2] takes indexes 0, 2, 4, 6, giving [1, 2, 3, 5]. LS[::-2] starts from the last element and goes backwards in steps of 2, giving [4, 7, 8, 9]. Joined, they give [1, 2, 3, 5, 4, 7, 8, 9].

From DSSSB TGT Computer Science Practice Set 1

This is question 12 of DSSSB TGT Computer Science Practice Set 1. See all questions of this paper with answers, or attempt the paper as a timed online test to get your score and all-India rank.