eXercise #2
Scalar Data in
Delphi
BYTE
1. What are the
values of each of the following operations? Write the resultant output
in your exercise books:
- +37
- 17 shl
2
- sqr(7)
- 71 div
3
- succ (73)
- 71 mod
3
- ord(19)
- 21 *3
- abs(-123)
- 16/4
- sqrt(4)
|
- pred(1)
- not 56
- 16 and
32
- 11 xor
3)
- 23 shr
2
|
2. Given the following
declararions:
Var a,
b, c : byte;
Determine the values
that will be output for each of the variables and then explain it.
- begin
a:=3 ;
b:=17;
c:=b-a;
showmessage('a= ' + inttostr(a) + ' b= ' + inttostr(b) + ' c=' + inttostr(c))
end
- begin
a:=8 ;
b:=13;
b:=9;
c:=b+a;
showmessage('a= ' + inttostr(a) + ' b= ' + inttostr(b) + ' c=' + inttostr(c))
end
- begin
a:=17;
b:=1;
c:=16 shr 1;
b:=3;
showmessage('a= ' + inttostr(a) + ' b= ' + inttostr(b) + ' c=' + inttostr(c))
end
- begin
a:=6 ;
b:=sqr(a);
c:=b mod a;
showmessage('a= ' + inttostr(a) + ' b= ' + inttostr(b) + ' c=' + inttostr(c))
end
- begin
a:=3 ;
b:= 9;
c:=5;
c:= c+sqr(a);
showmessage('a= ' + inttostr(a) + ' b= ' + inttostr(b) + ' c=' + inttostr(c))
end
CHAR
1. Determine the
value of each of the following expressions. You may need to have a copy
of the ASCII collating sequence handy.
- chr(77)
- upcase('*')
- ^j
- integer('E')
- ord('S')
- succ('Z')
- byte('A')
- pred('A')
|
- upcase('m')
- boolean(^A)
- 'x'<>'X'
- ord(#32)
- 'd'=#100
- ord(^L)
- #25<=^Y
|
2. Given the declarations
var
a,b: byte;
c: char;
determine the values
outputted for each of these variables after the following sequences
of assignments.
- begin
a:= 70;
b:= 9;
c:= chr(a+b);
showmessage('a= ' + inttostr(a) + ' b= ' + inttostr(b) + ' c=' + c)
end.
- begin
b:= 3;
a:= 69;
c:='M';
c:= chr(a-b);
showmessage('a= ' + inttostr(a) + ' b= ' + inttostr(b) + ' c=' + c)
end.
- begin
c:= 'h';
b:= 15;
a:= ord(c);
showmessage('a= ' + inttostr(a) + ' b= ' + inttostr(b) + ' c=' + c)
end.
- begin
c:= '1';
a:= ord(c);
c:= succ(c);
b:= ord(c);
showmessage('a= ' + inttostr(a) + ' b= ' + inttostr(b) + ' c=' + c)
end.
- begin c:= '3';
b:= ord(c);
c:= chr(b);
a:= 7+b;
showmessage('a= ' + inttostr(a) + ' b= ' + inttostr(b) + ' c=' + c)
end.
BOOLEAN
1. What is the value
of each of the following operations?
- true and
true
- pred (true)
- true or
false
- ord (true)
- false xor
false
- false <
= true
- false and
true
- true >
= false
|
- true xor
false
- false <=
false
- false or
false
- true <>
false
- ord (false)
- false <
true
- succ (false)
- false =
false
|
INTEGER
1. What is the value
of
- $ABAB shr
3
- ord (9155)
- lo ($FACE)
- $60FF xor
$7905
- 385 div
71
- swap ($ABCD)
- 193 and
2076
- 385 mod
71
|
- lo (300)
- maxint
or 5000
- not maxint
- hi (745)
- abs (-42
1)
- hi ($3000)
- swap (1
2 3)
|
2. Despite the fact
that, i has been properly declared as an integer variable,
each of the following statements will cause problems. Describe what
type of error has been made in each, decide whether the compiler
will spot it or whether it will be an error at runtime.
- sqr(i)
- i := sqrt(3)
- i : = 3.5
- i := 250*300
- i := 307 div
0
- i+l := 41*3
- sqr(i) := 16
REAL
1. Determine the
values of each of the following:
- abs (-1.93)
- In (42.8)
- int (32.84)
- arctan
(1.0)
- sqr (-1.4)
- exp (1.0)
- sqrt (36.0)
- sin (1.0)
- frac (18.83)
|
- 7.8*-6.1
- trunc (231.4)
- 3.7-5.2
- round (-82.7)
- 6.4/8.0
- trunc (-5.5)
- 3.1 >
9.2
- cos (1.22)
- 21.5 <=
-21.5
|
2. Assuming that
both r and s are legally declared
real variables, that i is of type integer and that
p is a boolean variable, what is wrong with each of
the following assignment sequences?
- begin
r:= 3.0;
s:=r-r;
r:= r/s
end.
- begin
r :=-3.1;
i:= int(r);
s:= -r
end.
- begin
s:= 12.0;
r:= frac(s);
s:= s/r
end.
- begin r:= 3.0;
s:= sqrt(9.0); p:= r=s end.
TYPE CONVERSION
1. Express the following
using conversions.
- the character
with ASCII number 98.
- the ASCII number
of the character 'k', as a byte.
- the real number
which corresponds to 71.
- the byte corresponding
to the Integer 23.
- the integer which
corresponds to the ASCII number of -L.
- the integer which
is as close as possible to 123.98.
- the character
which corresponds to the integer 50.
- the integer which
is equivalent to the byte value 18.
2. Assuming b
is byte, c is char, p is boolean,
i is integer and r is real, what is
wrong with each of the following assignment statements?
- i:= sqrt(3)
- 9:= b
- p:= odd(true)
- i:= frac(9.0)
- b:= trunc(maxint)
- i:= odd(97)
- 72:= trunc(72.9)
- 7<3:= true
- p:= not 21
- r:= real(10)
- p:= sqr(2)=sqrt(16)
EVALUATING EXPRESSIONS
1. What is the value
of each of the following expressions?
- 10-5+3
- 10-5*3
- 10-5-3
- 29 div 5 mod
3
- 29 mod 5 div
3
- 25 div 9 mod
3 * 2
- abs (17 mod 13)
- sqr (17 div 3)
- abs (sqr(l1 mod
3) - sqr(l1 div 3))
- pred (ord (succ
(7))
- pred (pred (pred
('M'))
- ord (succ (chr
(pred(86))))
- abs(43) abs(27.4)
- sqr(7.0) sqrt(81.0)
- succ (sqr (8))
- pred (trunc (int
(-11.83)))
- sqr (ord (pred
('B'))
- odd (succ (ord
('R'))
- trunc (abs (-1.0*13.7))
- 6+3=2+7
- (6>5) and (4>3)
- (6>5) or (3>4)
- (6>5) and (3>4)
- '6' < 'D'
- not (6 > 3)
- (2=1) or not
(3=4)
- not (8<9) and
('v'<'V')
- not ((8<9) and
('v'<'V'))
- (7 < 6) and true
- false or not
false
- false and not
false
- not (not (2 >
3) and not false)
2. Given the declared
store
Var i : integer;
c : char;
p,q : boolean;
r : real;
Begin
i:= 10;
c := 'S';
p := true;
q := false;
r := 4.5;
determine the value of the following boolean expressions:
- i >= 10
- p and not q
- i > trunc(r)
- not p and q
- ord(c) < ord('T')
- not p and not q
- i*r > 100.0
- not (p and q)
- p and q
- not p or not q
- not (p or q)
- not (i>5) and not (p and (c='S'))
3. Find an expression which will convert:
- any uppercase letter into the corresponding lowercase letter.
- any lowerease letter into the corresponding uppercase letter.
- any uppercase letter into the corresponding control character.
4. The printing characters (commencing with the space) start at character
number 32 and end with character number 126. The center letter in this
sequence of characters has number 79. To save you looking this up, it
is the capital letter '0'. To encode a message, you take each letter
or character of the message and replace it with the character which
is as far from 'O' as the character but on the other side. In other
words, you draw a line through 'O' and mirror each character in this
line.
- Create an expression which converts any printing character into
its coded character.
- To complete this secret message business, you will also need a decoder.
Write the decoding expression.