Unit II · Viva Questions
Revise the concepts orally. Open the study answer for a detailed explanation.
Explain the types of control statements in C.
- Does a block always need a decision?
- No. A block can contain statements that execute in sequence.
- Is return the same as break?
- No. return leaves a function; break leaves the nearest loop or switch.
Explain the simple if statement with syntax and an example.
- Can an if body contain several statements?
- Yes. Put the statements inside braces.
- What does if (0) do?
- It skips its body because zero is false.
Explain if-else with a C program to test whether an integer is even or odd.
- Does zero count as even?
- Yes. Zero has remainder zero when divided by 2.
- Can both branches run in one selection?
- No. A single if-else selects only one branch.
Explain nested if statements with an example.
- Which if receives an unmatched else?
- The nearest preceding unmatched if permitted by the syntax.
- Do equal largest values cause a problem here?
- No. The program needs the maximum value, which remains the same.
Explain the else-if ladder with a grade-classification example.
- Will later branches run after a true branch?
- No. The ladder finishes after its selected branch.
- Is a final else compulsory?
- No, but it is useful for remaining or invalid cases.
Explain switch with syntax and a menu-based example.
- Can switch compare strings directly?
- No. Standard C switch uses an integer controlling expression.
- What is fall-through?
- Execution continues into following case statements when no transfer stops it.
Compare an else-if ladder and switch.
- Can an else-if condition compare floating-point values?
- Yes, relational conditions can compare floating-point values, with the usual precision considerations.
- Does switch test every case as a separate if condition?
- No. It selects a matching label; the implementation strategy is chosen by the compiler.
Explain the while loop with syntax and an example.
- What happens if the first while test is false?
- The body is skipped.
- Does C automatically update a while counter?
- No. The program must include the required update.
Explain the for loop with syntax and its execution order.
- How often is for initialisation executed?
- Once when the loop is entered.
- Where does continue go in a for loop?
- To the update expression, followed by the next condition test.
Explain the do-while loop and show why it executes at least once.
- Is the final semicolon optional in do-while?
- No. It is required.
- Where does continue go in do-while?
- To the condition test at the end of the loop.
Compare while, for and do-while loops.
- Must for always use a counter?
- No. Its expressions can implement other valid repetition conditions.
- Can equivalent for and while loops have equal complexity?
- Yes. Their iteration counts and body work can be the same.
Explain nested loops with a rectangular output example.
- Which loop does break leave in a nested pair?
- The nearest enclosing loop containing that break.
- Why is the newline after the inner loop?
- To finish one complete row before the next outer iteration.
Explain the break statement with an example.
- Does break leave every nested loop?
- No. It leaves only the nearest enclosing loop or switch.
- Does a for update run after break?
- No. The loop is left immediately.
Explain the continue statement and its behaviour in different loops.
- Can continue be used outside a loop?
- No.
- Does continue in a while loop automatically increment a counter?
- No. Only updates actually reached by the program take place.
Explain goto with its advantages, limitations and an example.
- Can goto jump to a label in another function?
- No. The label must belong to the same function.
- Is a label a barrier to normal execution?
- No. Normal execution can reach a labelled statement too.
Write a C program to find the sum of the first n natural numbers using a loop.
- Why does a sum accumulator start at zero?
- Zero is the identity for addition.
- Does the formula version have the same operation count as the loop?
- No. The formula uses a fixed number of arithmetic operations; the loop performs n additions.
Write a C program to calculate factorial using a loop.
- What should this loop return for 1!?
- 1, because the initial product is unchanged.
- Can unsigned long long store every factorial?
- No. It has a finite range.
Write a C program to print a multiplication table for a given number of rows.
- Does a larger selected number require more table iterations?
- No. The row count determines the number of iterations.
- What is the purpose of 1LL in the product?
- It makes the multiplication use long long arithmetic.
Write a C program to find the sum of digits of a non-negative integer.
- Why does dividing by 10 eventually stop this loop?
- A positive integer loses its last decimal digit each time until it becomes zero.
- What is the digit sum of 1000?
- 1.
Write a C program to print a pattern whose row i contains i copies of i.
- Why is the inner limit row instead of n?
- Each successive row must contain one more value, rather than a fixed number of values.
- How many values are printed for five rows?
- 1 + 2 + 3 + 4 + 5 = 15.