CSci 3102 Extension Winter 1994 Exercises - 1 ------------- 1. Which of the following are legal Pascal identifiers? a) NigHt b) 3Mxyz c) X-Ray d) p1234 e) count/4 f) 32.76 g) to day h) begin i) read j) File_Manager 2. Classify each of the following as an integer constant, real constant or neither : a) +1 b) -(-1) c) five d) 1.2x10 e) 24E0 f) 1,024 g) .3E2 h) 5E0.5 i) 5E-5 j) 3E 10 3. Which of the following are legal string constants? a) '$19.25' b) 'Don't' c) 'Doesn''t' d) 'too late' e) '12 + 34' f) '(2e10)' g) ''hello'' h) '''' 4. Find the value of the following expressions, or explain why it is not a valid expression : a) (2+3)(4+5) b) 2*-3+4 c) 2 DIV 3 + 3 / 5 d) 9 DIV (3 + 1) * 4 e) 7 + 7 MOD 3 f) 9 / 2 DIV 3 g) 7 MOD (5 MOD 3) h) 25 * 1 DIV 2 i) trunc (8 / 5) + round (8 /5) 5. Write a CONSTANT section to associate each of the following identifers with its respective constant value. Identifier Constant Value ---------- -------------- month july fica 123-45-6789 price $95.00 gross 2500.00 partno 48874 bound 0.0039 6. Write a declaration that will associate each of the following identifiers with its respective data type. Identifier Data Type ---------- --------- period char terminal boolean status char index integer row integer 7. Assuming that Number is of integer type, xValue and yValue are of real type, Grade is of character type, and Found is of boolean type, determine which of the following are valid Pascal statements. a) xValue = 2.738 b) xValue := 2.0E3 c) Grade := 'B+' d) yValue := 1 e) xValue := '1.0' f) Number + 1 := Number g) xValue := Number h) Found := 'true' i) Grade := A j) Number := yValue k) xValue : = 1.03 8. Write Pascal assignment statements for the following : a) X is incremented by DeltaX. b) Add the rightmost digit of Number to Number. c) Assign the last three digits of Stock with a decimal point before the last two digits to Price (e.g if Stock = 1758316 then Price is assigned 3.16) 9. For each of the following tell whether the two expressions are equal : a , b, and c are integers. a) a * (b / c) and a * b / c b) a * (b DIV c) and a * b DIV c c) a DIV b and a * (1 / b) d) (a + b) DIV c and a DIV c + b DIV c 10. Assuming A,B, and C are integer variables and X,Y, and Z are real variables, tell what value, if any, will be assigned to each of the variables, or expalin why an error occurs. a) readln(A, B, C); Input : 1 2 readln(X, Y, Z); 3 4 5 6 b) read(A , X) Input : 1 2.2 read(B , Y) 3 4.4 read(C , Z) 5 6.6 c) readln(A); Input : 1 2 3 read(B , C); 4 5 6.6 readln(X , Y); 7.9 8.9 read(Z); 11. Use truth tables to display the values of the following boolean expressions for all possible values of a, b, and c. a) NOT (a AND b) b) (NOT a) OR (NOT b) c) a AND (b OR c) d) (a AND b) OR (a AND c) 12. Write boolean expressions to express the following. a) y is strictly between 2 and 5. b) Alpha and Beta have the same sign. c) x is less than 3 or y is less than 4, but not both d) The boolean expression is TRUE if and only if x is TRUE and exactly one of y and z is TRUE. 13. Write a sequence of Pascal statements to 'swap' (interchange) the values of two variables say x and y.