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

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

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.

126. Calling a method on a null reference in Java throws:

  1. NullPointerException
  2. ClassCastException
  3. StackOverflowError
  4. IOException

Answer: A) NullPointerException

Explanation: There is no object to call the method on. Lesson 2.4 · Programming (C++ / Java / Python)

Discuss this question

127. What is the output of this Python code? python i = 0 while i < 10: i += 3 print(i)

  1. 3
  2. 10
  3. 12
  4. 9

Answer: C) 12

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

Discuss this question

128. The index of the first element of an array in C++, Java and Python is:

  1. 1
  2. It depends on the size
  3. 0
  4. −1

Answer: C) 0

Explanation: All three are zero-indexed. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

129. What is the output of this Python code? python print(round(2.5), round(3.5))

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

Answer: C) 2 4

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

Discuss this question

130. Which is a checked exception in Java?

  1. ArrayIndexOutOfBoundsException
  2. IOException
  3. NullPointerException
  4. ArithmeticException

Answer: B) IOException

Explanation: Checked exceptions must be caught or declared. Lesson 2.4 · Programming (C++ / Java / Python)

Discuss this question

131. Python manages memory mainly through:

  1. Manual free() calls
  2. Reference counting plus a cyclic garbage collector
  3. The stack only
  4. Smart pointers

Answer: B) Reference counting plus a cyclic garbage collector

Explanation: Objects are freed when no references remain. Lesson 2.4 · Programming (C++ / Java / Python)

Discuss this question

132. The finally block in Java:

  1. Runs only when there is an exception
  2. Is mandatory with every try
  3. Runs only when there is no exception
  4. Always executes, whether or not an exception occurs

Answer: D) Always executes, whether or not an exception occurs

Explanation: Used to release resources. Lesson 2.4 · Programming (C++ / Java / Python)

Discuss this question

133. A blueprint from which objects are created is a:

  1. Method
  2. Variable
  3. Class
  4. Package

Answer: C) Class

Explanation: Objects are instances of a class. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

134. The 'this' keyword refers to:

  1. The main class
  2. The parent class
  3. A static method
  4. The current object

Answer: D) The current object

Explanation: It distinguishes fields from parameters with the same name. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

135. Which C++ feature frees memory automatically when an owner goes out of scope?

  1. Raw pointers
  2. malloc
  3. Smart pointers such as uniqueptr
  4. goto

Answer: C) Smart pointers such as uniqueptr

Explanation: Smart pointers follow RAII. Lesson 2.4 · Programming (C++ / Java / Python)

Discuss this question

136. Objects created with new in C++ are stored in the:

  1. Heap
  2. Register
  3. Code segment
  4. Stack

Answer: A) Heap

Explanation: Dynamic allocation uses the heap. Lesson 2.4 · Programming (C++ / Java / Python)

Discuss this question

137. Which is a valid way to declare a constant in Java?

  1. let MAX = 10;
  2. final int MAX = 10;
  3. static MAX = 10;
  4. const int MAX = 10;

Answer: B) final int MAX = 10;

Explanation: Java uses the final keyword. Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

138. What is the output of this Python code? python class Counter: total = 0 def init(self): Counter.total += 1 a = Counter() b = Counter() c = Counter() print(Counter.total)

  1. 0
  2. 2
  3. 1
  4. 3

Answer: D) 3

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

Discuss this question

Previous Page 6 of 6

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

More Computer Science chapters