CS Programming & Software Development 2 — Questions and Answers
Question 1: A function calls itself with no condition that stops the calls. What is the most likely runtime result?
- A compile-time syntax error
- The function returns null automatically
- A stack overflow error (Correct answer)
- The loop counter resets to zero
Correct answer: A stack overflow error
Recursion without a base case keeps pushing stack frames until the call stack overflows.
Question 2: Which principle is violated when the same block of validation code is copied into five different functions?
- Encapsulation
- DRY (Don't Repeat Yourself) (Correct answer)
- Polymorphism
- Lazy evaluation
Correct answer: DRY (Don't Repeat Yourself)
DRY states that duplicated logic should be factored into a single reusable location.
Question 3: In a language with zero-based indexing, what does arr[3] refer to in the array [10, 20, 30, 40, 50]?
- 40 (Correct answer)
- 30
- 50
- An out-of-bounds error
Correct answer: 40
With zero-based indexing, index 3 is the fourth element, which is 40.
Question 4: A developer wants two versions of a feature tested on the same codebase without affecting the main release. Which version control practice fits best?
- Committing directly to main
- Deleting the repository history
- Tagging the latest release
- Creating separate branches (Correct answer)
Correct answer: Creating separate branches
Branches isolate parallel lines of development from the main codebase.
Question 5: Which of the following is an example of an infinite loop?
- for (i = 0; i < 10; i++) { print(i); }
- while (x > 0) { x = x - 1; } where x starts at 5
- while (x > 0) { print(x); } where x starts at 5 and never changes (Correct answer)
- do { x++; } while (x < 3); where x starts at 0
Correct answer: while (x > 0) { print(x); } where x starts at 5 and never changes
Since x is never modified inside the loop, the condition x > 0 stays true forever.
Question 6: What is the primary purpose of a unit test?
- To measure how many users the system supports
- To verify that an individual component works correctly in isolation (Correct answer)
- To check the visual design of the interface
- To deploy code to production servers
Correct answer: To verify that an individual component works correctly in isolation
Unit tests validate the smallest testable pieces of code independently of the rest of the system.
Question 7: A program compiles successfully but produces incorrect output when run. What type of error is this?
- Logic error (Correct answer)
- Syntax error
- Linker error
- Compilation error
Correct answer: Logic error
A logic error means the code is valid but the algorithm produces wrong results.
A function calls itself with no condition that stops the calls.
What is the most likely runtime result?