Test 2 Review Chapters 6 - 8 Chapter 6 Exam Preparation Execises pg 295 7, 8 Chapter 7 Exam Preparation Execises pg 345 3, 4, 7, 8, 11, 12, 13,14 1. Given this program and the following data: PROGRAM Exercise(INPUT, OUTPUT); VAR A, B, C, Sum : INTEGER; PROCEDURE Add(VAR X, A, Z, Sum : INTEGER); BEGIN {Add} READ(X, A, Z); Sum := X + A + z END; {Add} BEGIN {Exercise} Add(A, B, C, Sum); WRITELN(A, B, C, Sum); END. {Exercise} Data: 2 4 6 8 What is your output? 2. Using a VAR parameter, a procedure can obtain the initial value of an actual parameter as well as change the value of the actual parameter in the calling program. A) True B) False 3. Using a value parameter, the value of a variable can be passed to a procedure and used for computation there, without any modification to the value of the variable in the main program. A) True B) False 4. Use the following procedure heading and call to answer the questions below. PROCEDURE Parameters(X : INTEGER; VAR Y : INTEGER); . . . Parameters(A, B); a. When the procedure is called, a storage location is created for X, then initialized to the value of A. A) True B) False b. Because Y is a VAR parameter, a storage location is created, then initialized to the address of B. Y stands for the variable B itself, and any change in Y chages B. A) True B) False c. X is initialized to the value of A because their positions correspond in the parameter list. A) True B) False d. X and Y can both be used to receive values from the main program, but only X, the value parameter, can be used to return a value to the main program. A) True B) False 5. Use the following block structure to answer the questions below: PROGRAM ScopeRules(INPUT, OUTPUT); VAR A, B : INTEGER; PROCEDURE Block1; VAR A1, B1 : INTEGER; PROCEDURE Block2; VAR A, A2, B2 : INTEGER; BEGIN {Block2} . . . END; {Block2} BEGIN {Block1} . . . END; {Block1} PROCEDURE Block3; VAR A3, B3 : INTEGER; BEGIN {Block3} . . . END; {Block3} BEGIN {ScopeRules} . . . END. {ScopeRules} a. A and B are global variables, accessible to all parts of Program ScopeRules, including Procedure Block2. A) True B) False b. Because ScopeRules is the outer block, statements in its body can refeence all variables declared in inner blocks, including Procedure Block2. A) True B) False c. Becuase Procedure Block2 is the innermost block, its local variables can be accessed by all other blocks. A) True B) False d. Variable A1 is nonlocal to Procedure Block2. A) True B) False e. Variable B2 is local to Procedure Block1. A) True B) False f. The statement A1 := A would be legal in Procedure Block1. A) True B) False g. The statement A3 := A1 would be legal in Procedure Block3. A) True B) False h. Variables A2 and B2 are not defined in any of the outer blocks. A) True B) False i. The statement A := B2 in Procedure Block2 assigns the value of B2 to the local variable A; it does not affect the global variable A. A) True B) False j. Variables A1 and B1 are nonlocal to Procedure Block2, local to Procedure Block1, and not defined in the outer block. A) True B) False 6. Know the following terms: a. Procedure call b. Parameter list c. Positional parameters d. Formal parameter e. Actual parameter f. Variable parameter g. Local variable h. Value parameter i. Global variable j. Name precedence k. Side effects 7. Given the following program, answer the questions below. PROGRAM Sample(INPUT, OUTPUT); VAR A, B : INTEGER; PROCEDURE Change(X : INTEGER; VAR Y : INTEGER); VAR B : INTEGER; BEGIN {Change} B := X; Y := Y + B; X := Y END; {Change} BEGIN {Sample} A := 10; B := 7; Change(A, B); WRITELN(A, B) END. {Sample} a. What are the values of Sample variable A and B when Change is called? (Let U indicate an undefined value.) b. What are the values of Change variables X, Y, and B when Change is called? (Let U indicate an undefined value.) c. What are the values of Sample vaiables A and B after Change is executed? (Let U indicate an undefined value.) 8. Given the following program, answer the questions below. PROGRAM Dog(INPUT, OUTPUT); VAR A, B : INTEGER; PROCEDURE Cat(X : INTEGER; Y : INTEGER); BEGIN {Cat} B := X; Y := Y + B; X := Y END; {Cat} BEGIN {Dog} A := 7; B := 10; Cat(A, B); WRITELN(A, B) END. {Dog} a. What are the values of Dog variable A and B when Cat is called? (Let U indicate an undefined value.) b. What are the values of variables X, Y, and B when Cat is called? (Let U indicate an undefined value.) c. What are the values of Dog vaiables A and B after Cat is executed? (Let U indicate an undefined value.) 9. Because a procedure is a subprogram within a progra, it cannot contain input or output instructions or other subprograms. A) True B) False 10. The parameters listed in the procedure heading are called "formal parameters;" the parameters listed in the invoking statement are called "actual parameters." A) True B) False 11. Write a procedure named Increment, with one variable parameter of type INTEGER, which adds 15 to the value received in the parameter and returns the new value to the calling program. 12. When writing procedures we have to be aware of the declarations for all other blocks, because if we declare a local variable with the same name as a variable in an outer block, we may accidentally modify that variable. A) True B) False Chapter 8 13. Write a function, Minimum, that returns the minimum of its three INTEGER parameters. 14. Because a function returns a single value through its identifier, it cannot have value parameters that allow values to be passed into the function. A) True B) False 15. A function call is always a component of an expression, but a procedure call always a statement in itself. A) True B) False 16. Both procedures and functions must have a type. A) True B) False 17. A good rule of thumb is to always use VAR parameters in the formal parameter list of a function. A) True B) False 18. Convert the following into a CASE statement (Chapter 9) IF (Count = 1) OR (Count = 2) OR (Count = 3) THEN WRITELN('Low Count') ELSE IF (Count = 4) OR (Count = 5) THEN WRITELN('Pass') ELSE IF Count = 6 THEN WRITELN('Pass') ELSE IF Count = 7 THEN BEGIN WRITELN('High Count'); OK := OK + 1 END 19. Use the following program structure to answer the questions below. PROGRAM Modular(INPUT, OUTPUT); VAR A, B, C : INTEGER; Q : BOOLEAN; FUNCTION One(Q1 : BOOLEAN; A1 : INTEGER) : INTEGER; VAR B : INTEGER; BEGIN {One} . . . END; {One} PROCEDURE Buzz(VAR A2 : INTEGER; B2 : INTEGER); VAR Q2 : BOOLEAN; FUNCTION Two(B3 : INTEGER; Q3 : BOOLEAN) : BOOLEAN; BEGIN {Two} . . . END; {Two} BEGIN {Buzz} . . . END; {Buzz} BEGIN {Modular} . . . END. {Modular} a. The statement One(Q,A) in the body of Program Modular would invoke Function One, initializing Q1 and A1 with the values of Q and A. A) True B) False b. The statement b:= A1 in Function One would assign the value of A1 to local variable B of One and would not affect global variable B of Program Modular. A) True B) False c. The statement A2 := Two(B2, Q2) in the body of Procedure Buzz would invoke Function Two and assign the result to A2. A) True B) False d. The statement Q1 := Two(B, Q) in the body of Function One would invoke Two and assign a BOOLEAN value to Q1. A) True B) False e. The statement Buzz(A, B) in the body of Program Modular would invoke Procedure Buzz, would initialize A2 and B2 to the values of A and B, and would return any changes in VAR parameter A2 to the main program in A. A) True B) False