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

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

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.

101. What is the output of this Python code? python print('5' 3)

  1. 5 5 5
  2. 15
  3. 555
  4. Error

Answer: C) 555

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

Discuss this question

102. In Python, for a = [10, 20, 30, 40], what is a[-1]?

  1. 30
  2. 10
  3. 40
  4. Error

Answer: C) 40

Explanation: Negative indices count from the end. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

103. What is the output of this Python code? python def f(x): x = x + 1 return x x = 5 f(x) print(x)

  1. Error
  2. None
  3. 6
  4. 5

Answer: D) 5

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

Discuss this question

104. In Python, what is -7 // 2?

  1. 3
  2. -3
  3. -3.5
  4. -4

Answer: D) -4

Explanation: Floor division rounds down toward negative infinity. Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

105. What is the output of this Python code? python print([x x for x in range(4)])

  1. [0, 1, 2, 3]
  2. [1, 4, 9, 16]
  3. [0, 2, 4, 6]
  4. [0, 1, 4, 9]

Answer: D) [0, 1, 4, 9]

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

Discuss this question

106. Which statement about interfaces in Java (Java 8+) is correct?

  1. They can have constructors
  2. They can extend classes
  3. They can be instantiated with new
  4. They can contain abstract and default methods

Answer: D) They can contain abstract and default methods

Explanation: Default methods have a body; interfaces cannot be instantiated. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

107. In Java, a char occupies:

  1. 4 bytes
  2. 2 bytes (Unicode)
  3. 8 bytes
  4. 1 byte

Answer: B) 2 bytes (Unicode)

Explanation: Java chars are 16-bit UTF-16 code units. Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

108. In Python, which keyword is used to raise an exception?

  1. throw
  2. except
  3. raise
  4. throws

Answer: C) raise

Explanation: Java uses throw; Python uses raise. Lesson 2.4 · Programming (C++ / Java / Python)

Discuss this question

109. What is the output of this Python code? python words = ['apple', 'kiwi', 'banana'] print(max(words, key=len))

  1. 6
  2. apple
  3. kiwi
  4. banana

Answer: D) banana

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

Discuss this question

110. Variables declared inside a function are called:

  1. Global variables
  2. Static fields
  3. Constants
  4. Local variables

Answer: D) Local variables

Explanation: They exist only during the function call. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

111. What is the output of this Python code? python try: print(int('abc')) except ValueError: print('bad') else: print('ok')

  1. ok
  2. abc
  3. bad ok
  4. bad

Answer: D) bad

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

Discuss this question

112. When a child class provides its own version of a method already defined in its parent, it is called:

  1. Method overloading
  2. Method overriding
  3. Constructor chaining
  4. Encapsulation

Answer: B) Method overriding

Explanation: Overriding gives run-time polymorphism. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

113. What is the output of this Python code? python count = 0 for i in range(3): for j in range(4): count += 1 print(count)

  1. 12
  2. 7
  3. 3
  4. 4

Answer: A) 12

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

Discuss this question

114. What is the output of this Python code? python print(2 3 2)

  1. 512
  2. 18
  3. 64
  4. 36

Answer: A) 512

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

Discuss this question

115. What is the output of this Python code? python d = {'a': 1, 'b': 2} d['c'] = 3 d['a'] = 10 print(sum(d.values()))

  1. 15
  2. 3
  3. 6
  4. 13

Answer: A) 15

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

Discuss this question

116. In Java, what is the result of 7 / 2?

  1. 3
  2. 3.0
  3. 3.5
  4. 4

Answer: A) 3

Explanation: Both operands are int, so integer division truncates. Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

117. Which data type is best for storing a true/false value in Java?

  1. int
  2. boolean
  3. char
  4. String

Answer: B) boolean

Explanation: Java uses boolean with values true and false. Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

118. In a C++/Java switch statement, a missing break causes:

  1. Fall-through to the next case
  2. A compile error
  3. The loop to stop
  4. The default case to be skipped always

Answer: A) Fall-through to the next case

Explanation: Execution continues into the following case. Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

119. A memory leak in C++ happens when:

  1. Memory is freed twice
  2. A pointer is set to nullptr
  3. Allocated memory is never freed
  4. A variable is declared const

Answer: C) Allocated memory is never freed

Explanation: Memory stays reserved but unusable. Lesson 2.4 · Programming (C++ / Java / Python)

Discuss this question

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

  1. 60
  2. "3030"
  3. "102030"
  4. "1020 30"

Answer: B) "3030"

Explanation: Evaluation is left to right: 10 + 20 = 30, then 30 + "30" = "3030". Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

121. In C++, int p = &x; then p gives:

  1. The address of x
  2. The address of p
  3. The value of x
  4. A compile error

Answer: C) The value of x

Explanation: dereferences the pointer. Lesson 2.4 · Programming (C++ / Java / Python)

Discuss this question

122. What is the output of this Python code? python class P: def init(self, name): self.name = name def str(self): return 'Hi ' + self.name print(P('Ali'))

  1. Hi
  2. Ali
  3. <P object
  4. Hi Ali

Answer: D) Hi Ali

Explanation: Running the code prints Hi Ali. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

123. Run-time polymorphism in Java is achieved through:

  1. Constructors
  2. Method overriding
  3. Static methods
  4. Method overloading

Answer: B) Method overriding

Explanation: The JVM picks the method based on the actual object type. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

124. What is the output of this Python code? python print(max(3, 9, 2), min('b', 'a', 'c'))

  1. 9 c
  2. 9 a
  3. 3 a
  4. 9 b

Answer: B) 9 a

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

Discuss this question

125. Which access modifier gives the most restricted access?

  1. default (package)
  2. public
  3. private
  4. protected

Answer: C) private

Explanation: private members are visible only inside the class. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

Previous Page 5 of 6 Next

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

More Computer Science chapters