Chapter 8 Quiz
Instructions: Select the best answer for each question. Answers are provided at the end.
Question 1. What type of scoping does Pascal use?
A) Dynamic scope B) Lexical (static) scope C) Function scope D) Module scope
Question 2. In the following code, what is the scope of variable Y?
program Q2;
var X: Integer;
procedure P;
var Y: Integer;
begin
{ ... }
end;
begin
{ ... }
end.
A) The entire program
B) Only inside procedure P
C) Inside procedure P and the main program
D) Nowhere — it is never used
Question 3. What does the following program print?
program Q3;
var N: Integer;
procedure SetN;
var N: Integer;
begin
N := 42;
end;
begin
N := 7;
SetN;
WriteLn(N);
end.
A) 42 B) 7 C) 0 D) Compile error
Question 4. Which of the following is stored in a stack frame when a procedure is called?
A) Return address, parameters, and local variables B) Only local variables C) Only the return address D) The entire program's global variables
Question 5. What is variable shadowing?
A) When a variable is used before it is declared B) When an inner scope declares a variable with the same name as one in an outer scope C) When a variable goes out of scope D) When a global variable is modified by a procedure
Question 6. What does the following code print?
program Q6;
procedure Change(X: Integer);
begin
X := X + 100;
end;
var A: Integer;
begin
A := 5;
Change(A);
WriteLn(A);
end.
A) 105 B) 5 C) 100 D) 0
Question 7. What does the following code print?
program Q7;
procedure Change(var X: Integer);
begin
X := X + 100;
end;
var A: Integer;
begin
A := 5;
Change(A);
WriteLn(A);
end.
A) 5 B) 105 C) 100 D) Compile error
Question 8. Which parameter mode should you use for a large array that a function needs to read but not modify?
A) Value parameter
B) var parameter
C) const parameter
D) No parameter — use a global variable
Question 9. Why does the following code cause a compile error?
procedure Inc(var X: Integer);
begin
X := X + 1;
end;
begin
Inc(5);
end.
A) 5 is not an integer
B) var parameters require a variable with an address, and literals do not have one
C) You cannot call a procedure from the main program
D) The procedure name Inc is reserved
Question 10. In Pascal's call stack, what discipline is used?
A) First-In, First-Out (FIFO) B) Last-In, First-Out (LIFO) C) Random access D) Priority-based
Question 11. Procedure A calls procedure B, which calls procedure C. When C finishes, where does execution resume?
A) At the beginning of A
B) At the beginning of B
C) At the statement in B immediately after the call to C
D) At the statement in A immediately after the call to B
Question 12. What happens to local variables when a procedure returns?
A) They are saved for the next call B) They become global C) Their stack frame is destroyed and the memory is reclaimed D) They are automatically returned to the caller
Question 13. Consider this nested structure. Can Inner access OuterVar?
procedure Outer;
var OuterVar: Integer;
procedure Inner;
begin
OuterVar := 10;
end;
begin
Inner;
end;
A) No — local variables are private
B) Yes — Inner is nested inside Outer, so OuterVar is in scope
C) Only if OuterVar is passed as a parameter
D) Only if OuterVar is declared as var
Question 14. What mechanism does the compiler use to allow a nested procedure to access variables from its enclosing scope at runtime?
A) Global variable table B) Static link (scope pointer) in the stack frame C) Copying all enclosing variables into the nested procedure's frame D) Dynamic dispatch
Question 15. Which of the following is NOT a reason to prefer local variables over global variables?
A) Local variables prevent accidental modification by other procedures B) Local variables make data flow explicit C) Local variables are automatically initialized to zero D) Local variables reduce coupling between subprograms
Question 16. What is the output of this program?
program Q16;
var G: Integer;
procedure Add(X: Integer);
begin
G := G + X;
end;
begin
G := 10;
Add(5);
Add(3);
WriteLn(G);
end.
A) 10 B) 15 C) 18 D) 8
Question 17. A const parameter in Pascal guarantees which of the following?
A) The parameter's value will not change during the subprogram's execution B) The argument must be a constant, not a variable C) The parameter is stored in read-only memory D) The parameter will be passed by value
Question 18. When designing parameter lists, which principle is MOST important?
A) Use the shortest possible parameter names
B) Always use var parameters for efficiency
C) Make data flow explicit — inputs and outputs should be visible in the signature
D) Always use global variables for values needed by multiple procedures
Question 19. A program has a recursive procedure that calls itself 100,000 times without reaching a base case. What happens?
A) The program runs indefinitely B) A stack overflow occurs because the call stack runs out of memory C) The operating system adds more stack memory automatically D) The program prints an error and continues
Question 20. In the following code, which variables can procedure Beta access?
program Q20;
var A: Integer;
procedure Alpha;
var B: Integer;
procedure Beta;
var C: Integer;
begin
{ What can Beta see? }
end;
begin
Beta;
end;
procedure Gamma;
var D: Integer;
begin
{ ... }
end;
begin
Alpha;
end.
A) A, B, C, D B) A, B, C C) C only D) A, C
Answer Key
-
B — Pascal uses lexical (static) scope, determined by source text structure.
-
B —
Yis declared inside procedureP, so it is visible only insideP(and any subprograms nested withinP). -
B —
SetNdeclares its own localNthat shadows the globalN. Setting the localNto 42 has no effect on the globalN, which remains 7. -
A — A stack frame contains the return address, parameters (copies or addresses), local variables, and saved register state.
-
B — Shadowing occurs when an inner scope declares a name identical to one in an enclosing scope, hiding the outer declaration.
-
B —
Xis a value parameter (copy). Modifying the copy does not affectA. Output: 5. -
B —
Xis avarparameter (reference). ModifyingXdirectly modifiesA. Output: 105. -
C —
constprovides read-only access and avoids the cost of copying a large array.varwould allow modification (wrong semantics). Value would copy the entire array (wasteful). -
B — A
varparameter requires a variable with an address. The literal5has no modifiable memory location, so the compiler rejects the call. -
B — The call stack uses Last-In, First-Out: the most recently called subprogram is on top and returns first.
-
C — When
Creturns, its frame is popped and execution resumes inBat the statement immediately following the call toC. -
C — Local variables live in the stack frame, which is destroyed when the procedure returns. The memory is reclaimed.
-
B —
Inneris nested insideOuter, soOuterVaris inInner's scope chain and can be accessed directly. -
B — The compiler places a static link (scope pointer) in the nested procedure's stack frame that points to the enclosing scope's frame.
-
C — Local variables in Pascal are NOT automatically initialized to zero. They contain whatever garbage was in that stack memory. This is not a valid reason to prefer them.
-
C —
Gstarts at 10.Add(5)makes it 15.Add(3)makes it 18. Output: 18. -
A —
constguarantees the subprogram will not modify the parameter. The argument can be a variable or literal. The implementation (by value or by reference) is up to the compiler. -
C — Making data flow explicit through parameters is the most important design principle. It makes subprograms self-documenting and reduces hidden dependencies.
-
B — Each recursive call adds a stack frame. With 100,000 calls and no base case, the stack runs out of memory and the program crashes with a stack overflow error.
-
B —
Betais nested insideAlpha, which is at program level.Betacan see its ownC, its parentAlpha'sB, and the globalA. It cannot seeGamma'sDbecauseGammais a sibling ofAlpha, not an enclosing scope.