CPA Programming Fundamentals & Logic 3 — Questions and Answers
Question 1: Which type of error occurs when a program compiles successfully but produces incorrect results at runtime?
- Syntax error
- Compile-time error
- Logic error (Correct answer)
- Type error
Correct answer: Logic error
A logic error means the program runs without crashing but produces wrong output due to a flaw in the algorithm.
Question 2: An algorithm that divides a problem into smaller subproblems of the same type and solves them recursively is using which strategy?
- Greedy algorithm
- Divide and conquer (Correct answer)
- Dynamic programming
- Brute force
Correct answer: Divide and conquer
Divide and conquer breaks a problem into smaller instances of itself, solves each recursively, and combines the results.
Question 3: What value does an uninitialized boolean variable typically hold by default in strongly-typed languages?
- TRUE
- NULL
- FALSE (Correct answer)
- 0
Correct answer: FALSE
In most strongly-typed languages, boolean variables default to FALSE when declared but not explicitly initialized.
Question 4: Which control structure is most appropriate when you need to execute a block of code at least once before checking a condition?
- for loop
- while loop
- do-while loop (Correct answer)
- if-else statement
Correct answer: do-while loop
A do-while loop executes its body first, then checks the condition, guaranteeing at least one execution.
Question 5: What is the purpose of a flowchart in programming?
- To compile code faster
- To visually represent the steps and logic of an algorithm (Correct answer)
- To allocate memory for variables
- To define class hierarchies
Correct answer: To visually represent the steps and logic of an algorithm
A flowchart uses standardized symbols to graphically depict the flow of logic and decision-making steps in an algorithm.
Question 6: If a function calls itself within its own definition, it is known as a:
- Overloaded function
- Recursive function (Correct answer)
- Void function
- Lambda function
Correct answer: Recursive function
A recursive function is one that calls itself as part of its own execution to solve a problem.
Question 7: Which of the following is an example of a relational operator?
- &&
- ++
- >= (Correct answer)
- =
Correct answer: >=
The >= (greater than or equal to) operator is a relational operator used to compare two values.
Which type of error occurs when a program compiles successfully but produces incorrect results at runtime?