πŸ“Š Problem Solving and Python Programming
Q. To open a file c:\scores.txt for reading, we use
  • (A) infile = open(“c:\\scores.txt”, “r”)
  • (B) infile = open(“c:\\scores.txt”, “r”)
  • (C) infile = open(file = “c:\\scores.txt”, “r”)
  • (D) infile = open(file = “c:\\scores.txt”, “r”)
πŸ’¬ Discuss
βœ… Correct Answer: (B) infile = open(“c:\\scores.txt”, “r”)
πŸ“Š Problem Solving and Python Programming
Q. What is the current syntax of remove() a file?
  • (A) remove(file_name)
  • (B) remove(new_file_name, current_file_name,)
  • (C) remove(() , file_name))
  • (D) none of the mentioned
πŸ’¬ Discuss
βœ… Correct Answer: (A) remove(file_name)
πŸ“Š Problem Solving and Python Programming
Q. Write a list comprehension for producing a list of numbers between 1 and 1000 that are divisible by 3.
  • (A) [x in range(1, 1000) if x%3==0]
  • (B) [x for x in range(1000) if x%3==0]
  • (C) [x%3 for x in range(1, 1000)]
  • (D) [x%3=0 for x in range(1, 1000)]
πŸ’¬ Discuss
βœ… Correct Answer: (B) [x for x in range(1000) if x%3==0]

Explanation: the list comprehension [x for x in range(1000) if x%3==0] produces a list of numbers between 1 and 1000 that are divisible by 3.

πŸ“Š Problem Solving and Python Programming
Q. Write a list comprehension to produce the list: [1, 2, 4, 8, 16……212].
  • (A) [(2**x) for x in range(0, 13)]
  • (B) [(x**2) for x in range(1, 13)]
  • (C) [(2**x) for x in range(1, 13)]
  • (D) [(x**2) for x in range(0, 13)]
πŸ’¬ Discuss
βœ… Correct Answer: (A) [(2**x) for x in range(0, 13)]

Explanation: the required list comprehension will print the numbers from 1 to 12, each raised to 2. the required answer is thus, [(2**x) for x in range(0, 13)].

πŸ“Š Problem Solving and Python Programming
Q. , x is even} (including zero)
  • (A) [x for x in range(1, 20) if (x%2==0)]
  • (B) [x for x in range(0, 20) if (x//2==0)]
  • (C) [x for x in range(1, 20) if (x//2==0)]
  • (D) [x for x in range(0, 20) if (x%2==0)]
πŸ’¬ Discuss
βœ… Correct Answer: (D) [x for x in range(0, 20) if (x%2==0)]

Explanation: the required list comprehension will print a whole number, less than 20, provided that the number is even. since the output list should contain zero as well, the answer to this question is: [x for x in range(0, 20) if (x%2==0)].

πŸ“Š Problem Solving and Python Programming
Q. , i)]
  • (A) a list of prime numbers up to 50
  • (B) a list of numbers divisible by 2, up to 50
  • (C) a list of non prime numbers, up to 50
  • (D) error
πŸ’¬ Discuss
βœ… Correct Answer: (C) a list of non prime numbers, up to 50

Explanation: the list comprehension shown above returns a list of non-prime numbers up to 50. the logic behind this is that the square root of 50 is almost equal to 7. hence all the multiples of 2-7 are not prime in this range.

πŸ“Š Problem Solving and Python Programming
Q. ] for row in (0, 1, 2)]
  • (A) [7, 8, 9]
  • (B) [4, 5, 6]
  • (C) [2, 5, 8]
  • (D) [1, 4, 7]
πŸ’¬ Discuss
βœ… Correct Answer: (C) [2, 5, 8]

Explanation: to get a particular column as output, we can simple iterate across the rows and pull out the desired column, or iterate through positions in rows and index as we go. hence the output of the code shown above is: [2, 5, 8].

πŸ“Š Problem Solving and Python Programming
Q. for col in row] for row in A]
  • (A) [[11, 12, 13], [14, 15, 16], [17, 18, 19]]
  • (B) error
  • (C) [11, 12, 13], [14, 15, 16], [17, 18, 19]
  • (D) [11, 12, 13, 14, 15, 16, 17, 18, 19]
πŸ’¬ Discuss
βœ… Correct Answer: (A) [[11, 12, 13], [14, 15, 16], [17, 18, 19]]

Explanation: the code shown above shows a list comprehension which adds 10 to each element of the matrix a and prints it row- wise. hence the output of the code is: [[11, 12, 13], [14, 15, 16], [17, 18, 19]]

Jump to