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

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

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.

26. In Python, for a = [1, 2, 3, 4, 5], what is a[1:4]?

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

Answer: A) [2, 3, 4]

Explanation: Slicing excludes the stop index. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

27. Creating a separate line of development in Git to build a feature is done with:

  1. A revert
  2. A tag
  3. A branch
  4. A stash

Answer: C) A branch

Explanation: Branches isolate work until merged. Lesson 2.5 · Programming (C++ / Java / Python)

Discuss this question

28. A protected member of a class is accessible in:

  1. Everywhere
  2. Only the same class
  3. The class and its subclasses
  4. Only the main method

Answer: C) The class and its subclasses

Explanation: Java also allows package access. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

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

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

Answer: D) [2, 3, 4]

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

Discuss this question

30. Python is described as dynamically typed because:

  1. The type of a variable is decided at run time
  2. It has no types
  3. Types are checked by the compiler
  4. Variables cannot change

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)

Discuss this question

31. An abstract class:

  1. Cannot be instantiated directly
  2. Must be final
  3. Can only have static methods
  4. Cannot have any methods

Answer: A) Cannot be instantiated directly

Explanation: It is meant to be extended. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

32. What is the output of this Python code? python a, b = 1, 2 a, b = b, a + b print(a, b)

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

Answer: C) 2 3

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

Discuss this question

33. What is the output of this Python code? python class A: def show(self): return 'A' class B(A): def show(self): return 'B' obj = B() print(obj.show())

  1. A
  2. Error
  3. B
  4. AB

Answer: C) B

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

Discuss this question

34. What is the output of this Python code? python for i in range(5): if i % 2 == 0: continue print(i, end=' ')

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

Answer: C) 1 3

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

Discuss this question

35. What is the output of this Python code? python x = 3 x += 2 x = 3 print(x)

  1. 9
  2. 17
  3. 11
  4. 15

Answer: D) 15

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

Discuss this question

36. What is the output of this Python code? python s = 'level' print(s == s[::-1])

  1. True
  2. level
  3. False
  4. Error

Answer: A) True

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

Discuss this question

37. In Java, the number of elements in an array arr is given by:

  1. len(arr)
  2. arr.size()
  3. arr.length
  4. arr.length()

Answer: C) arr.length

Explanation: length is a field for arrays (length() is for Strings). Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

38. Which loop always executes its body at least once?

  1. do-while
  2. while
  3. for-each
  4. for

Answer: A) do-while

Explanation: The condition is checked after the body. Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

39. The keyword used in Java to call the parent class constructor is:

  1. this
  2. base
  3. parent
  4. super

Answer: D) super

Explanation: super(...) must be the first statement in the child constructor. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

40. In the expression (x != 0 && 10 / x 1), what happens when x = 0?

  1. The result is true
  2. A syntax error occurs
  3. A division-by-zero error occurs
  4. The second part is not evaluated, so no error occurs

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)

Discuss this question

41. In Java, what is -7 / 2?

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

Answer: D) -3

Explanation: Java integer division truncates toward zero. Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

42. What is the output of this Python code? python print([x for x in range(10) if x % 3 == 0])

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

Answer: B) [0, 3, 6, 9]

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

Discuss this question

43. What is the output of this Python code? python def change(lst): lst.append(9) nums = [1] change(nums) print(nums)

  1. [1]
  2. [1, 9]
  3. [9]
  4. None

Answer: B) [1, 9]

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

Discuss this question

44. What is the output of this Python code? python x = [1, 2, 3] for i in range(len(x)): x[i] = 2 print(x)

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

Answer: B) [2, 4, 6]

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

Discuss this question

45. In Java, which comparison checks whether two String objects contain the same characters?

  1. s1.compare(s2) == 1
  2. s1.equals(s2)
  3. s1 = s2
  4. s1 == s2

Answer: B) s1.equals(s2)

Explanation: == compares references; equals() compares content. Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

46. In Java, after int x = 5; int y = x++; what are x and y?

  1. x = 6, y = 5
  2. x = 5, y = 6
  3. x = 6, y = 6
  4. x = 5, y = 5

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)

Discuss this question

47. What is the output of this Python code? python class Shape: def area(self): return 0 class Sq(Shape): def init(self, s): self.s = s def area(self): return self.s self.s shapes = [Shape(), Sq(3)] print(sum(x.area() for x in shapes))

  1. 9
  2. 0
  3. 3
  4. 12

Answer: A) 9

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

Discuss this question

48. In C++, to allow run-time polymorphism the base-class method must be declared:

  1. virtual
  2. inline
  3. const
  4. static

Answer: A) virtual

Explanation: Virtual functions use dynamic dispatch. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

49. Showing only essential features and hiding implementation details is:

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

Answer: A) Abstraction

Explanation: Abstract classes and interfaces provide abstraction. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

50. 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

Previous Page 2 of 6 Next

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

More Computer Science chapters