CSCI 150, Spring 2003

Home | | Course Schedule | | Assignments | | Lecture Notes

Solutions to exam 2 review problems

Problem 1:
Given the function definition:
	function Mystery( someVal: real): real;
	begin
		if  someVal > 2.0 then
		begin
			Mystery :=  3.0 * someVal;
		end
		else
		begin
			Mystery :=  0.0;
		end;
	end;
What is the value is assigned to x after execution of the following statement?

x := Mystery(2.5);

Answer: x = 7.5


Problem 2:
Consider the procedure definition:

	procedure Demo( var number: integer; answer: real);
	begin
		number := number*2;
		answer := number + 3.5;
	end;

Suppose the caller has variables myNum and myResult whose values are 20 and 4.8 respectively. What are the values of myNum and myResult after return from the following procedure call?

Demo(myNum, myResult);

Answer: myNum = 40, myResult = 4.8


Problem 3:

a)Write a function, 
	function TenToThePower( n : integer) : integer;
that returns 10 raised to the integer power specified by n.

Answer:

function TenToThePower(n : integer):integer;
var
	count : integer;
begin
	TenToThePower := 1;
	for count := 1 to n do
	begin
		TenToThePower := TenToThePower * 10;
	end;
end;


b) Write a Pascal statement to compute 10 to the power of 3 and store the answer in the variable, tenCubed.

Answer:


Problem 4: Convert to binary
237 = 11101101
45 = 00101101
63 = 00111111


Problem 5: Convert to hexadecimal
237 = ED
45 = 2D
63 = 3F


Problem 6: Convert to signed magnitude, 1's complement, and 2's complement:

DecimalSigned magnitude1's complement2's complement
22000101100001011000010110
-31100111111110000011100001
-22100101101110100111101010
49001100010011000100110001


Problem 7: Add the following two binary numbers:

Binary Decimal
01110001 113
+ 00011100 + 28
= 10001101 = 141


Problem 8: draw a circuit that computes the truth table:

abc
000
011
101
110

There are several possible solutions. The simplest uses an exclusive or (XOR) gate.

Another possible solution uses AND and OR gates and Inverters.


Problem 9: Write an expression for the output of the given circuit. Draw the truth table: