Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 13

Our Lady’s College

Pascal Programming---input and output


Name:_______________________ S.4____( )
1. Write down the output of the following statements.
a) Write(1,2,3)
123
b) Write(‘1’,’2’,’3’)
123
c) Write(‘o’,’k’)
ok
d) Write(‘Pascal’,’Programming’)
PascalProgramming
e) Write(‘Pascal’, ‘□□Programming’)
Pascal□□Programming
f) Write(58:5)
□□□58
g) Write(‘a’:3.’b’:3,’c’:3)
□□□a□□□b□□□c
h) Write(123.456:9)
□1.23E+02
i) Write(123.456:17)
□1.2345600000E+02
j) Write(123.456:0)
□1.2E+02
k) Write(123.4567:8:4)
123.4567
l) Write(123.4567:10:2)
□□□□123.46
m) Write(123.4567:10:2)
□□□□□□□123
n) Write(123.456:0:0)
123

2. For each of the following program segments, write down its screen output.
a) writeln(‘****’); ****
writeln(‘* *’); * *
writeln(‘****’); ****

b) write(‘****’);
write(‘* *’);
write(‘****’); ***** *****
c) Subject := ‘Pascal programming’; I like
Writeln(‘I like ‘); Pascal Programming
Writeln(Subject); very much
Writeln(‘very much’);

d) Subject := ‘Pascal programming’;


Write(‘I like ‘);
Write(Subject);
Writeln(‘very much’); I like Pascal Programming very much

3. Assuming that I and J are integer variables with i=5, j=8 and that K is a real variable with
K=345.678, write down the output for each of the following program segments:

a) writeln(‘I’,’J’, ‘K’); IJK


b) writeln(‘I’,’J’:5, ‘K’:5); IBBBBJBBBBK
c) writeln(I,J,K); 583.4567800000E+02
d) writeln(‘I=’,I); I=5
e) write(‘Total =‘); Total =13
write(I+J :5);
f) writeln(‘Sum of I and K is’, I +K); Sum of I and K is 3.5067800000E+02
g) write(‘Sum of ‘, I:3,’ and ‘); Sum of 5 and 345.68 is
write(‘K:1:2, ‘ is ‘);
h) writeln(I:I); BBBB5
writeln(J:J); BBBBBBB8
writeln(K:J:I); 345.67800

4.. The following is a program output.

□□□□□□Name□□□□□□□contactNo□□□□□□□□□□□□□Salary
----------------------------------------------------------------------------
□□□□ JohnWu□□□□□□□□24332343□□□□□□□□□□□ 16432.23
□□□□□Ti mLi□□□□□□□□25433443□□□□□8.98243000E+03

Fill in the blanks for the format of writeln.

begin
Writeln(‘Name’ :10, ‘contactNo’:19, ‘Salary’:19);
Writeln(‘------------------------------------------------------------------’);
Writeln(‘JohnWu’ :10, ‘24332343’:19, 16432.226:19:2);
Writeln(‘TimLai’ :10, ‘25433443’:19, 8982.3:19)
end.

5.With the given variables and their initial values, write a set of output statements that will produce
the following output.

Program OutputTest;
Var A: integer;
B: real;
C: char;
D: string;
Begin
A:=100;
B:= 999.0;
C:=’Z’;
D:=’Dummy’
end.

a) bb100bb999bbZbbDummy
write(A:5);
write(B:5:0);
write(C:3);
writeln(D:7);
b) 100bZb9.99E+02bDummy
write(A);
write(C:2);
write(B:9);
writeln(D:6);
c) DummyZ
999.000100
write(D);
writeln(C);
write(B:7:3);
writeln(A);
d) 1099.00ZZZZ
write(A +B :7:2);
writeln(C,C,C,C);
4. Refer to the following program declaration.
var
S1, S2, S3 : String;
C1, C2, C3 : char;
N1, N2 : integer;
R1, R2 : real;

Write down the output of each of the following program segments with the given input values
(a) readln (N1, N2) ;
readln (N3) ;
writeln (N1) ;
writeln (N2) ;
writeln (N3) ;

12 23 34
98 87 76
12
23
98

(b) readln (C1, C2, C3) ;


writeln (C1) ;
writeln (C2) ;
writeln (C3) ;

X Y Z
X
b
Y

(c) readln (C1, C2, C3, N1) ;


writeln (C1) ;
writeln (C2) ;
writeln (C3) ;
writeln (N1) ;

34.567 21
3
4
.
567

d)readln (S1, S2) ;


readln (S3) ;
writeln (S1) ;
writeln (S2) ;
writeln (S3) ;

This is string one This is string two


This is string three
This is string one This is string two

This is string three

e) readln (N1, C1, C2, N2) ;


writeln (N1) ;
writeln (N2) ;
writeln (C1) ;
writeln (C2) ;

567 1 2 234
567
2
1

f) readln (S1, C1) ;


readln (N1, N2) ;
readln (R1, R2) ;
writeln (S1) ;
writeln (C1) ;
writeln (N1) ;
writeln (N2) ;
writeln (R1) ;
writeln (R2) ;

John Lee M
123 1 2 3 4 5
123.45 789
John Lee M
123
1
1.2345000000E+02
7.8900000000E+02

5. The following table shows the types and the values of the variables M,N,R,C,D,E,S,T and U.
Variable Type Value
M integer 25
N integer 99
R real 234.5678
C char ‘X’
D char ‘+’
E char ‘=’
S string ‘Joey’
T string ‘apple’
U string ‘eats’

Use these variables to write output statements to produce the following output.
(Note: Any other strings and blanks are not allowed. The symbol ‘ ’ represents a space.)

(a) 25
write(M)
(b) 9925
Write(N,M)

(c) 99 + 25 = 124
Write(N,D,M,E,M+N)

(d) 99 x 25
Write(N, C, M)

(e) Joey eats apple


Write(S, U:5,T:6)

(f) apple
Write(T:15) OR write(‘ ’,T)

(g) 2.35E +02


Write(R:9)

(h) 2.345678E + 02
Write(R:13)

(i) 235
Write(R:3:0) OR write(round(R) )

(j) 234.57
Write(R:6:2)
(k) 2345.568
Write(R:10:3)

(l) 235234.57

Write(R:3:0, R:6:2)

6. A program MarkList reads the names of three students and their marks in the Chinese, English
and Mathematics tests and displays them in the format of a table.

Sample output
Enter name: Amy
Enter Chinese, English and Mathematics marks : 88 56 65
Enter name : Billy
Enter Chinese, English and Mathematics marks : 100 82 90
Enter name : Carol
Enter Chinese, English and Mathematics marks : 45 37 50
Name Chinese English Mathematics
Amy 88 56 65
Billy 100 82 90
Carol 45 37 50

Complete the program MarkList.


1 program MarkList ;
2 var
3 Name1, Name2, Name3 : ______string____ ;
4 Chinese1, Chinese2, Chinese3 : ____integer____;
5 English1,English2, English3: integer;
6 _ Math1, Math2, Math3 : integer;_____________________________________
7 begin
8 write (‘Enter name: ‘) ;
9.___readln(Name1);_______________________________________________________
10 write (‘Enter Chinese, English, Mathematics marks:’);
11 readln(Chinese1, English1, Math1);
12 _write(‘Enter name:’);
13 _readln(Name2);
14 write(‘Enter Chinese , English, Mathematics,marks:’);
15 readln(Chinese2, English2, Math2);
16 _write(‘Enter name:’);
17 _readln(Name3);
18 _write(‘Enter Chinese, English, Mathematics marks:’);
19 readln(Chinese3, English3, Math3);
20 writeln (‘Name’ :12__________________________________ ,
‘Chinese’ _:12_______________________________ ,
‘English’ ____:12____________________________ ,
‘Mathematics’ ____:12__________________________) ;

21 writeln (Name1 __:12__________________________________ ,


Chinese1 __:12________________________________ ,
English1 :12 , Math1:12 );
22 writeln(Name2:12, Chinese2:12, English2:12, Math2:12);
23 writeln(Name3:12, Chinese3:12, English3:12, Math3:12)
24 end.

7. A program ClassClub reads the names of the class committee and displays them in the format
shown.

Sample output
who is the Chairman? Amy
who are the two Vice Chairmen? Peter
Grace
who is the Treasurer? Sandy
who is the Secretary? Christopher

**** 4A Class Committee ****


Chairman : Amy
Vice Chairman : Peter
Grace
Treasurer : Sandy
Secretary : Christopher

Complete the program ClassClub.


(Note: Use output with formatted field width to display the leading blanks)

1 program ClassClub ;
2 var
3 Chairman, Vicel, Vice2, Treasurer, Secretary : string _______________________ ;
4 begin
5 write (‘who is the Chairman? ’) ;
6 readln ( _Chairman__________________) ;
7 write (__’Who are the two Vice Chairmen?’)
8 _readln(Vice1);
9 write(‘ ‘:31);
10 readln(Vice2);
11 write(‘Who is the Treasurer?’);
12 readln( Treasurer);_______________________________
13 write(‘Who is the Secreatary?’);_____________________________________
14 readln(Secreatary);_____________________________________________
15 writeln;_________________________________________________________
16 writeln (‘**** 4A Class Committee ****’) ;
17 writeln (‘Chairman : ‘ : 17, Chairman);______________________________
18 _writeln(‘Vice Chairman : ‘:17, Vice1);_______________________________
19 _writeln(‘ ‘:17, Vice2);____________________________________________
20 writeln(‘Treasurer: ‘:17, Treasurer);__________________________________
21 writeln(‘Secreatary: ‘:17, Secreastary)________________________________
22 end._______________________________________________________________

8. For each of the following Boolean expressions, add parentheses ( ) to show the order of
evaluation and construct the truth table.
(a) not p and not q
(not p) and (not q)

p q (not p) and (not q)


TRUE TRUE FALSE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE TRUE

(b) p and q or r
(p and q) or r

p q r (p and q) or r
TRUE TRUE TRUE TRUE
TRUE TRUE FALSE TRUE
TRUE FALSE TRUE TRUE
TRUE FALSE FALSE FALSE
FALSE TRUE TRUE TRUE
FALSE TRUE FALSE FALSE
FALSE FALSE TRUE TRUE
FALSE FALSE FALSE FALSE
(c) p and not q or not r
(p and (not q)) or (not r)

P q r (p and (not q)) or (not r)


TRUE TRUE TRUE FALSE
TRUE TRUE FALSE TRUE
TRUE FALSE TRUE TRUE
TRUE FALSE FALSE TRUE
FALSE TRUE TRUE FALSE
FALSE TRUE FALSE TRUE
FALSE FALSE TRUE FALSE
FALSE FALSE FALSE TRUE

9. Simplify each of the following compound Boolean expressions to a simple Boolean


expression.

(a) ( x > 0 ) or ( x < 0 )

x <> 0

(b) (c = ‘A’) or (c > ‘A’)

c >= ‘A’

(c) (y > 5) and (y <> 5 )

y>5

(d) (r > 5) and (r >= 10) and (r = 10)

r = 10

(e) (s < 10) or (s = 5) or ( s < 15)

s < 15

(f) (t < 100) and (t > 10) or (t > 0)

10<t<100
(g) (z > 1) and (z = 1)

FALSE
10. Decimal Notation Scientific Notation Floating Point Notation
23.78 2.378X10 2.3780000000E+01
0.0 0.0X10^0 0.0000000000E+00
-0.0008934 -8.934X10^-4 -8.9340000000E-04
-50000.0005 -5.0000005X10^3 -5.00000005E+03
9900000000 9.9X10^9 9.9000000000E+09
-100010000 -1.0001X10^8 -1.0001000000E+08
-0.00000788 -7.88X10^-5 -7.8800000000E-05
0.00123456 1.23456X10^-3 1.23456E-03
11. Write down the result of the following expressions.
a) 12*-3+ -5
b) –5*6-18+7.0
c) 43/5
d) –22 div 6
e) –22 div –6
f) 22 mod 6
g) –22 mod –6
h) 43 div 5 mod 4
1a)-41 b)-4.1 000 000 000E+01
c)8.6 000 000 000E+00
d)-3 e)3 f)4
g)-4 h)0

Write down the result of the following functions.


i) abs(4.5)
j) abs(-999)
k) sqrt(9)
l) sqrt(-4)
m) trunc(9.9)
n) round(-99.9)
o) random(1)
p) sqrt(abs(-49))
q) 49.9 – trunc (49.9) 9.0000000000E-01
r) trunc (12345/1000) * 1000 12000
s) 6* (2+3) / abs (-4+1) 1.0000000000E+01
t) 29 div 5 mod 3 2
u) 29 mod 5 div 3 1
2a)4. 5 000 000 000E+00 b)999 c)3.000 000 000 0E+00
d)Error e)9 f)-100 g)0 h)7.0000000000E+00

g)0 h)7 I)9.000 000 000 0E-01


j)12000

2. Assume the variables X, Y, Z hold 1,2,3 respectively. Draw a dry run table for the following
program statements.
A) Z:=X;
X:=Y;
Y:=Z;
B) X:=X+2;
Y:=Y*3;
Z:=X+Y;
C) X:= X+Y;
Y:=Y+Z;
Z:=Z+X;
D)X:=3;
1. Assume the variables X, Y, Z hold 1,2,3 respectively. Draw a dry run table for the following
program statements.
A) Z:=X;

X:=Y;
Y:=Z;
B) X:=X+2;

Y:=Y*3;
Z:=X+Y;
C) X:= X+Y;
Y:=Y+Z;
Z:=Z+X;
D)X:=3;
Y:=2;
Z:=1;

3)a)311 b)369 c)356 d)321

You might also like