COPA Module 15: Introduction to Python – all 50 MCQ questions with answers

  1. Which command displays numbers and text on the screen?

    Answer: print() – print() displays text and numbers on the screen, e.g. print("Hello", 5).

  2. Which is a valid Python if statement?

    Answer: if a>=2: – A Python if needs a condition followed by a colon: if a >= 2:

  3. What will be the output of the following Python code snippet if x=1?

    Answer: 4 – << 2 shifts bits left by 2, i.e. multiplies by 4: 1 << 2 = 4.

  4. What will be the output of the following Python code?

    Answer: 0 1 2 – It prints 0, 1, 2; at i == 3 break runs, so the while-else part is skipped.

  5. Which multimedia application was developed using Python?

    Answer: TimPlayer – TimPlayer is a multimedia (media player) application written in Python.

  6. What will be the output of the following Python code snippet?

    Answer: True – The in operator checks dictionary keys; "john" is a key, so the result is True.

  7. What is an array?

    Answer: Hold more than one value at a time – An array (list) holds many values at the same time in one variable.

  8. Which of the following is true for variable names in Python?

    Answer: Unlimited Length – Python variable names can be of any length; they are case-sensitive and cannot start with a space.

  9. What will be the output of the following given code?

    Answer: 12 – self.id is set to str(12) = "12" before the local id is changed, so it prints 12.

  10. What is the output of print('%x, %X' % (15, 15))?

    Answer: f F – %x gives lowercase hex and %X uppercase hex: 15 → f, F.

  11. Which of the following term is correct for the given code?

    Answer: Dictionary – {key: value, …} pairs in curly braces form a dictionary.

  12. Which platform develops the business application in Python?

    Answer: Tryton – Tryton is a business application (ERP) platform built in Python.

  13. Which is a core data type in Python programming?

    Answer: List – List is a built-in core data type (along with int, str, tuple, dict).

  14. Which has the highest precedence in the expression?

    Answer: Parentheses – Parentheses () are evaluated first, then ** power, then * / and then + −.

  15. In which language Python was implemented?

    Answer: C – The standard Python interpreter (CPython) is written in the C language.

  16. What will be the output of the following given code?

    Answer: True – foo changes the same list object (no new list), so its id stays the same → True.

  17. Which keyword adds an alternative condition to an if statement?

    Answer: elif – elif adds another condition after if (short for "else if").

  18. Which program code makes use of the given module?

    Answer: Client – Code that imports and uses a module is called the client of that module.

  19. What will be the output of the following Python code snippet?

    Answer: Counter({3: 1, 4: 1, 5: 1}) – a & b keeps the minimum count of common items: 3→1, 4→1, 5→1.

  20. Which character gives single-line comments in Python?

    Answer: # – # starts a single-line comment in Python.

  21. Which is a keyword in Python language?

    Answer: pass – pass is a keyword meaning "do nothing"; eval is a built-in function, not a keyword.

  22. What will be the output of the following?

    Answer: ['Python', 'Java', 'C++', 'SQL'] – insert(len(lst), x) inserts at the end, just like append, so 'SQL' comes last.

  23. What will be the output of given Python code?

    Answer: 3 – "hello" has 5 letters, 2 of them are "l", so 3 letters are counted.

  24. What is the data type of print (type(0xFF))?

    Answer: int – 0xFF is a hexadecimal literal of the integer 255, so type is int.

  25. In which year Python language was developed?

    Answer: 1991 – Guido van Rossum first released Python in 1991.

  26. How to declare an array in python?

    Answer: brackets[ ] – In Python an array/list is written with square brackets: a = [1, 2, 3].

  27. Which is used as loop in Python?

    Answer: for loop – Python has for and while loops; there is no do-while loop.

  28. Which of the following Python statements will result in the output: 6?

    Answer: A[1][2] – Indexing starts at 0: A[1] is [4, 5, 6] and its index 2 is 6.

  29. Which function accepts input from the user?

    Answer: input() – input() reads text typed by the user (raw_input was Python 2).

  30. Which is the correct operator for power(ab)?

    Answer: a**b – ** is the power operator: 2 ** 3 = 8 (^ is bitwise XOR).

  31. How are data items added to an array?

    Answer: Items are appended to the next free index – append() adds a new item at the next free position (end) of the list.

  32. Which is a valid String creation in Python?

    Answer: str1 = 'str1' str1 = "str1" str1 = '''str''' – Strings can be made with single '…', double "…" or triple '''…''' quotes.

  33. Which command is used to delete the entry for “john” in the given code snippet?

    Answer: del d[“john”] – del d["john"] removes that key and its value from the dictionary.

  34. Which statement is correct for the following code?

    Answer: Positive integers then a = a +1 – For a positive a, the condition a <= 0 is false, so the else part runs: a = a + 1.

  35. What will be the output of a given code?

    Answer: [1, 2] – list() of a dictionary gives only its keys: [1, 2].

  36. What does the following code print to console?

    Answer: 1001 – The condition True is always true, so the if block prints 1001.

  37. Why control structure is used in Python program?

    Answer: Directs the order of execution of the statements – Control structures (if, for, while) decide the order in which statements run.

  38. Which is a valid Python file extension?

    Answer: .py – Python source files are saved with the .py extension.

  39. What happens while trying to retrieve a value using the expression d[“susan”] in the following code?

    Answer: Python raises a KeyError exception – Accessing a key that does not exist ("susan") raises KeyError.

  40. Who developed the Python language?

    Answer: Guido van Rossum – Guido van Rossum created Python (named after Monty Python).

  41. Which defines a block of code in Python language?

    Answer: Indentation – Python uses indentation (spaces) instead of braces to mark code blocks.

  42. What will be the output of the following code?

    Answer: No output – 5 > 11 is false and there is no else, so nothing is printed.

  43. What is the function of list.pop()?

    Answer: Removes the last element from a list – list.pop() without an index removes and returns the last item.

  44. Which of the following declarations is incorrect in python language?

    Answer: x y z p = 5000 6000 7000 8000 – Variable names cannot contain spaces, so "x y z p = …" is a syntax error.

  45. What will be the output of the following code?

    Answer: Given condition matched – and runs before or: x > 12 is True, so True or (…) = True → "matched".

  46. What will be the output of the following Python code if the system date is 18th August, 2016?

    Answer: 8 – date.month gives the month as an integer, so August → 8.

  47. What will be the output of the given Python code?

    Answer: [3,2,5,3,6] – pop(3) removes the item at index 3 (value 7): [3, 2, 5, 3, 6].

  48. What will be the output of the following given code?

    Answer: Decorated Ordinary – mk returns mk1 (a decorator wrapper): it prints "Decorated" and then calls mk2 → "Ordinary".

  49. Which Python command is used to add a new element to a list?

    Answer: List1.append(5) – append() adds an element to the end of a list: List1.append(5).

  50. What will be the output of the following Python code snippet?

    Answer: 2 – Only two different keys ('Apple', 'Ball') are stored, so len(total) = 2.