MCQs / Computer Science / NSCT: Programming (C++ / Java / Python)

NSCT: Programming (C++ / Java / Python) MCQs — Computer Science

138 multiple-choice questions on NSCT: Programming (C++ / Java / Python) from Computer Science, each with the correct answer and, where available, a teacher's explanation. Read through them to revise the chapter, then practise the same questions one by one to test yourself.

1. What is the output of this Python code? python s = 'Programming' print(s[3], s[-1], len(s))

  1. g g 10
  2. g g 11
  3. g n 12
  4. r g 11

Answer: B) g g 11

Explanation: Running the code prints g g 11. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

2. In Python, the constructor of a class is named:

  1. constructor
  2. init
  3. self
  4. new only

Answer: B) init

Explanation: init initialises a new instance. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

3. What is the output of this Python code? python a = [1, 2, 3, 4, 5, 6] print(a[::2])

  1. [1, 4]
  2. [1, 2]
  3. [1, 3, 5]
  4. [2, 4, 6]

Answer: C) [1, 3, 5]

Explanation: Running the code prints [1, 3, 5]. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

4. Every recursive function must have:

  1. A global variable
  2. Two return statements
  3. A loop
  4. A base case

Answer: D) A base case

Explanation: The base case stops the recursion. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

5. What is the output of this Python code? python print(list(map(lambda v: v 2, [1, 2, 3])))

  1. [2, 4]
  2. [3, 4, 5]
  3. [2, 4, 6]
  4. [1, 2, 3, 1, 2, 3]

Answer: C) [2, 4, 6]

Explanation: Running the code prints [2, 4, 6]. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

6. What is the output of this Python code? python s = {1, 2, 2, 3, 3, 3} print(len(s))

  1. 2
  2. 3
  3. 6
  4. 1

Answer: B) 3

Explanation: Running the code prints 3. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

7. Hiding the internal data of a class and allowing access only through methods is:

  1. Encapsulation
  2. Overloading
  3. Polymorphism
  4. Inheritance

Answer: A) Encapsulation

Explanation: private fields with getters/setters. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

8. Local variables of a function are stored in the:

  1. Stack
  2. Cache only
  3. Disk
  4. Heap

Answer: A) Stack

Explanation: Each call has a stack frame. Lesson 2.4 · Programming (C++ / Java / Python)

Discuss this question

9. What is the output of this Python code? python x = 10 def g(): x = 20 return x print(g(), x)

  1. 20 20
  2. 20 10
  3. 10 20
  4. 10 10

Answer: B) 20 10

Explanation: Running the code prints 20 10. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

10. Writing a small test that checks one function in isolation is:

  1. Acceptance testing
  2. Integration testing
  3. System testing
  4. Unit testing

Answer: D) Unit testing

Explanation: Unit tests target the smallest testable part. Lesson 2.5 · Programming (C++ / Java / Python)

Discuss this question

11. The continue statement in a loop:

  1. Skips the rest of the current iteration
  2. Restarts the program
  3. Exits the loop
  4. Stops the program

Answer: A) Skips the rest of the current iteration

Explanation: Control jumps to the next iteration. Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

12. A static variable in a class:

  1. Exists only inside a method
  2. Cannot be changed
  3. Is created for each object
  4. Is shared by all objects of the class

Answer: D) Is shared by all objects of the class

Explanation: Static members belong to the class itself. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

13. In Java, when an object is passed to a method, the method receives:

  1. A copy of the reference to the object
  2. The variable itself by reference
  3. A full copy of the object
  4. Nothing – objects cannot be passed

Answer: A) A copy of the reference to the object

Explanation: Java is pass-by-value, and the value is a reference. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

14. What is the output of this Python code? python n = 0 for ch in 'mississippi': if ch == 's': n += 1 print(n)

  1. 4
  2. 2
  3. 3
  4. 11

Answer: A) 4

Explanation: Running the code prints 4. Lesson 2.5 · Programming (C++ / Java / Python)

Discuss this question

15. What is the output of this Python code? python print('a,b,,c'.split(','))

  1. ['a', 'b', ',', 'c']
  2. ['a', 'b', '', 'c']
  3. ['a,b,,c']
  4. ['a', 'b', 'c']

Answer: B) ['a', 'b', '', 'c']

Explanation: Running the code prints ['a', 'b', '', 'c']. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

16. Which Java class is best for building a string in a loop efficiently?

  1. Scanner
  2. StringBuilder
  3. String
  4. Character

Answer: B) StringBuilder

Explanation: StringBuilder is mutable, avoiding many temporary objects. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

17. What is the output of this Python code? python total = 0 for i in range(1, 11): if i % 2 == 0: total += i print(total)

  1. 20
  2. 55
  3. 30
  4. 25

Answer: C) 30

Explanation: Running the code prints 30. Lesson 2.5 · Programming (C++ / Java / Python)

Discuss this question

18. What is the output of this Python code? python d = {'x': 1} print(d.get('y', 0), 'x' in d)

  1. 0 False
  2. Error
  3. None True
  4. 0 True

Answer: D) 0 True

Explanation: Running the code prints 0 True. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

19. A class Car that extends class Vehicle is an example of:

  1. Inheritance
  2. Encapsulation
  3. Abstraction
  4. Composition

Answer: A) Inheritance

Explanation: Car "is a" Vehicle. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

20. What is the output of this Python code? python print(int('12') + int(3.9))

  1. 123
  2. 15.9
  3. 15
  4. 16

Answer: C) 15

Explanation: Running the code prints 15. Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

21. What is the output of this Python code? python print(list(zip([1, 2, 3], 'ab')))

  1. [(1, 'a'), (2, 'b')]
  2. [(1, 'a'), (2, 'b'), (3, None)]
  3. [(1, 2, 3), ('a', 'b')]
  4. [1, 'a', 2, 'b']

Answer: A) [(1, 'a'), (2, 'b')]

Explanation: Running the code prints [(1, 'a'), (2, 'b')]. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

22. What is the output of this Python code? python def f(n): if n <= 1: return n return f(n - 1) + f(n - 2) print(f(7))

  1. 21
  2. 13
  3. 7
  4. 8

Answer: B) 13

Explanation: Running the code prints 13. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

23. In Python, what does "-".join(["a", "b", "c"]) return?

  1. "a-b-c"
  2. "abc-"
  3. "-a-b-c-"
  4. ["a-b-c"]

Answer: A) "a-b-c"

Explanation: join inserts the separator between elements. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

24. What is the value of "10" + 20 + 30 in Java?

  1. 60
  2. "102030"
  3. "1050"
  4. "3030"

Answer: B) "102030"

Explanation: Once a String appears first, + concatenates the rest. Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

25. In C++, a function declared as void f(int &x) receives x:

  1. As a constant
  2. As a pointer to pointer
  3. By value
  4. By reference

Answer: D) By reference

Explanation: Changes to x affect the caller's variable. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

Page 1 of 6 Next

Practise NSCT: Programming (C++ / Java / Python) MCQs

More Computer Science chapters