πŸ“Š Problem Solving and Python Programming
Q. To shuffle the list(say list1) what function do we use?
  • (A) list1.shuffle()
  • (B) shuffle(list1)
  • (C) random.shuffle(list1)
  • (D) random.shufflelist(list1)
πŸ’¬ Discuss
βœ… Correct Answer: (C) random.shuffle(list1)

Explanation: execute in the shell to verify.

πŸ“Š Problem Solving and Python Programming
Q. Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following is correct syntax for slicing operation?
  • (A) print(list1[0])
  • (B) print(list1[:2])
  • (C) print(list1[:-2])
  • (D) all of the mentioned
πŸ’¬ Discuss
βœ… Correct Answer: (D) all of the mentioned

Explanation: slicing is allowed in lists just as in the case of strings.

πŸ“Š Problem Solving and Python Programming
Q. Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1]?
  • (A) [2, 33, 222, 14]
  • (B) error
  • (C) 25
  • (D) [25, 14, 222, 33, 2]
πŸ’¬ Discuss
βœ… Correct Answer: (A) [2, 33, 222, 14]

Explanation: execute in the shell to verify.

πŸ“Š Problem Solving and Python Programming
Q. print sum
  • (A) 11
  • (B) 12
  • (C) 21
  • (D) 22
πŸ’¬ Discuss
βœ… Correct Answer: (B) 12

Explanation: when assigning names1 to names2, we create a second reference to the same list. changes to names2 affect names1. when assigning the slice of all elements in names1 to names3, we are creating a full copy of names1 which can be modified independently.

πŸ“Š Problem Solving and Python Programming
Q. Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is:
  • (A) [0, 1, 2, 3]
  • (B) [0, 1, 2, 3, 4]
  • (C) [0.0, 0.5, 1.0, 1.5]
  • (D) [0.0, 0.5, 1.0, 1.5, 2.0]
πŸ’¬ Discuss
βœ… Correct Answer: (C) [0.0, 0.5, 1.0, 1.5]

Explanation: execute in the shell to verify.

πŸ“Š Problem Solving and Python Programming
Q. To add a new element to a list we use which command?
  • (A) list1.add(5)
  • (B) list1.append(5)
  • (C) list1.addlast(5)
  • (D) list1.addend(5)
πŸ’¬ Discuss
βœ… Correct Answer: (B) list1.append(5)

Explanation: we use the function append to add an element to the list.

Jump to