26. In Python, for a = [1, 2, 3, 4, 5], what is a[1:4]?
- [2, 3, 4]
- [1, 2, 3, 4]
- [2, 3, 4, 5]
- [1, 2, 3]
Answer: A) [2, 3, 4]
Explanation: Slicing excludes the stop index. Lesson 2.2 · Programming (C++ / Java / Python)
MCQs / Computer Science / NSCT: Programming (C++ / Java / Python)
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.
Answer: A) [2, 3, 4]
Explanation: Slicing excludes the stop index. Lesson 2.2 · Programming (C++ / Java / Python)
Answer: C) A branch
Explanation: Branches isolate work until merged. Lesson 2.5 · Programming (C++ / Java / Python)
Answer: C) The class and its subclasses
Explanation: Java also allows package access. Lesson 2.3 · Programming (C++ / Java / Python)
Answer: D) [2, 3, 4]
Explanation: Running the code prints [2, 3, 4]. Lesson 2.2 · Programming (C++ / Java / Python)
Answer: A) The type of a variable is decided at run time
Explanation: A variable can refer to an int and later a string. Lesson 2.1 · Programming (C++ / Java / Python)
Answer: A) Cannot be instantiated directly
Explanation: It is meant to be extended. Lesson 2.3 · Programming (C++ / Java / Python)
Answer: C) 2 3
Explanation: Running the code prints 2 3. Lesson 2.5 · Programming (C++ / Java / Python)
Answer: C) B
Explanation: Running the code prints B. Lesson 2.3 · Programming (C++ / Java / Python)
Answer: C) 1 3
Explanation: Running the code prints 1 3. Lesson 2.1 · Programming (C++ / Java / Python)
Answer: D) 15
Explanation: Running the code prints 15. Lesson 2.1 · Programming (C++ / Java / Python)
Answer: A) True
Explanation: Running the code prints True. Lesson 2.5 · Programming (C++ / Java / Python)
Answer: C) arr.length
Explanation: length is a field for arrays (length() is for Strings). Lesson 2.2 · Programming (C++ / Java / Python)
Answer: A) do-while
Explanation: The condition is checked after the body. Lesson 2.1 · Programming (C++ / Java / Python)
Answer: D) super
Explanation: super(...) must be the first statement in the child constructor. Lesson 2.3 · Programming (C++ / Java / Python)
Answer: D) The second part is not evaluated, so no error occurs
Explanation: && short-circuits when the left side is false. Lesson 2.1 · Programming (C++ / Java / Python)
Answer: D) -3
Explanation: Java integer division truncates toward zero. Lesson 2.1 · Programming (C++ / Java / Python)
Answer: B) [0, 3, 6, 9]
Explanation: Running the code prints [0, 3, 6, 9]. Lesson 2.2 · Programming (C++ / Java / Python)
Answer: B) [1, 9]
Explanation: Running the code prints [1, 9]. Lesson 2.2 · Programming (C++ / Java / Python)
Answer: B) [2, 4, 6]
Explanation: Running the code prints [2, 4, 6]. Lesson 2.5 · Programming (C++ / Java / Python)
Answer: B) s1.equals(s2)
Explanation: == compares references; equals() compares content. Lesson 2.1 · Programming (C++ / Java / Python)
Answer: A) x = 6, y = 5
Explanation: Post-increment gives the old value to y, then increases x. Lesson 2.1 · Programming (C++ / Java / Python)
Answer: A) 9
Explanation: Running the code prints 9. Lesson 2.3 · Programming (C++ / Java / Python)
Answer: A) virtual
Explanation: Virtual functions use dynamic dispatch. Lesson 2.3 · Programming (C++ / Java / Python)
Answer: A) Abstraction
Explanation: Abstract classes and interfaces provide abstraction. Lesson 2.3 · Programming (C++ / Java / Python)
Answer: D) By reference
Explanation: Changes to x affect the caller's variable. Lesson 2.2 · Programming (C++ / Java / Python)
Practise NSCT: Programming (C++ / Java / Python) MCQs