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

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

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.

76. What is the output of this Python code? python a = [3, 1, 2] b = a.copy() b.append(4) print(len(a))

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

Answer: A) 3

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

Discuss this question

77. In C++, after int x = 5; int y = ++x; what are x and y?

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

Answer: D) x = 6, y = 6

Explanation: Pre-increment increases x first, then assigns. Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

78. In Java, a class can achieve multiple inheritance of type by:

  1. Using friend functions
  2. Implementing multiple interfaces
  3. Extending multiple classes
  4. Using pointers

Answer: B) Implementing multiple interfaces

Explanation: A class may implement any number of interfaces. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

79. "Write once, run anywhere" is a feature of:

  1. HTML
  2. Java
  3. Assembly
  4. C

Answer: B) Java

Explanation: The JVM runs the same bytecode on any platform. Lesson 2.5 · Programming (C++ / Java / Python)

Discuss this question

80. What is the output of this Python code? python a = 5 b = 2 print(a // b, a % b)

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

Answer: D) 2 1

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

Discuss this question

81. Given Animal a = new Dog(); a.speak(); where Dog overrides speak(), which method runs?

  1. Compile error
  2. Dog's speak()
  3. Animal's speak()
  4. Both

Answer: B) Dog's speak()

Explanation: Dynamic dispatch uses the actual object type. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

82. In Python 3, what is the result of 7 / 2?

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

Answer: A) 3.5

Explanation: The / operator always gives a float in Python 3. Lesson 2.1 · Programming (C++ / Java / Python)

Discuss this question

83. What is the output of this Python code? python def power(b, e=2): return b e print(power(3), power(2, 5))

  1. 3 25
  2. 9 32
  3. 9 10
  4. 6 32

Answer: B) 9 32

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

Discuss this question

84. In Python, "hello"[::-1] gives:

  1. "olleh"
  2. An error
  3. "h"
  4. "hello"

Answer: A) "olleh"

Explanation: A step of −1 reverses the string. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

85. Accessing an element outside an array's bounds in C++:

  1. Is undefined behaviour
  2. Returns 0
  3. Always throws an exception
  4. Causes a compile error

Answer: A) Is undefined behaviour

Explanation: C++ does not check bounds for raw arrays. Lesson 2.4 · Programming (C++ / Java / Python)

Discuss this question

86. What is printed? java try { System.out.print("A"); int x = 5 / 0; System.out.print("B"); } catch (ArithmeticException e) { System.out.print("C"); } finally { System.out.print("D"); }

  1. AD
  2. ACD
  3. ACBD
  4. ABCD

Answer: B) ACD

Explanation: B is skipped when the exception is thrown; catch and finally run. Lesson 2.4 · Programming (C++ / Java / Python)

Discuss this question

87. What is the output of this Python code? python print(10 - 2 3 + 4)

  1. 28
  2. 8
  3. 12
  4. 0

Answer: B) 8

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

Discuss this question

88. The principle "Don't Repeat Yourself" (DRY) encourages:

  1. Copying code for speed
  2. Putting repeated logic in one reusable function
  3. Writing longer comments
  4. Avoiding functions

Answer: B) Putting repeated logic in one reusable function

Explanation: Duplication makes maintenance harder. Lesson 2.5 · Programming (C++ / Java / Python)

Discuss this question

89. Strings in Java are immutable. This means:

  1. Strings cannot be printed
  2. Strings cannot be compared
  3. A String object's characters cannot be changed after creation
  4. Strings are stored on the stack only

Answer: C) A String object's characters cannot be changed after creation

Explanation: Methods like toUpperCase() return a new String. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

90. Java does not allow a class to extend two classes mainly to avoid:

  1. Slow compilation
  2. The diamond problem
  3. Encapsulation
  4. Memory leaks

Answer: B) The diamond problem

Explanation: Ambiguity arises when two parents have the same method. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

91. What is the output of this Python code? python nums = [4, 1, 3] nums.sort() print(nums, sorted(nums, reverse=True))

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

Answer: D) [1, 3, 4] [4, 3, 1]

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

Discuss this question

92. A constructor is:

  1. A static variable
  2. A method that deletes objects
  3. A type of loop
  4. A special method called when an object is created

Answer: D) A special method called when an object is created

Explanation: It initialises the new object. Lesson 2.3 · Programming (C++ / Java / Python)

Discuss this question

93. In Java, unused objects are removed automatically by the:

  1. Compiler
  2. Destructor
  3. Garbage collector
  4. Operating system only

Answer: C) Garbage collector

Explanation: The JVM's garbage collector frees heap memory. Lesson 2.4 · Programming (C++ / Java / Python)

Discuss this question

94. Restructuring code to improve readability without changing behaviour is:

  1. Deploying
  2. Compiling
  3. Debugging
  4. Refactoring

Answer: D) Refactoring

Explanation: Refactoring keeps functionality the same. Lesson 2.5 · Programming (C++ / Java / Python)

Discuss this question

95. What is the output of this Python code? python def f(n): if n == 0: return 0 return n + f(n - 1) print(f(4))

  1. 6
  2. 10
  3. 24
  4. 4

Answer: B) 10

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

Discuss this question

96. What is the output of this Python code? python class A: pass class B(A): pass print(isinstance(B(), A), issubclass(A, B))

  1. True False
  2. True True
  3. False True
  4. False False

Answer: A) True False

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

Discuss this question

97. What is the output of this Python code? python s = 'data science' print(s.title())

  1. data Science
  2. Data science
  3. DATA SCIENCE
  4. Data Science

Answer: D) Data Science

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

Discuss this question

98. What is the output of this Python code? python print(bool(0), bool('0'))

  1. True True
  2. False False
  3. True False
  4. False True

Answer: D) False True

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

Discuss this question

99. Two Java methods with the same name but different parameter lists show:

  1. Encapsulation
  2. Method overriding
  3. Method overloading
  4. Inheritance

Answer: C) Method overloading

Explanation: Overloading is resolved at compile time. Lesson 2.2 · Programming (C++ / Java / Python)

Discuss this question

100. What is the output of this Python code? python t = (1, 2, 3) print(t 2)

  1. (2, 4, 6)
  2. (1, 2, 3, 1, 2, 3)
  3. Error
  4. [1, 2, 3, 1, 2, 3]

Answer: B) (1, 2, 3, 1, 2, 3)

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

Discuss this question

Previous Page 4 of 6 Next

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

More Computer Science chapters