Pascal - 2 (string)

Question:

Whether the output from this program?

var s : string[12];
begin
s := 'setia';
writeln('#', s, '#');
s := s + ' sampai akhir ';
writeln('#', s, '#');
end.

a. #setia# (and the runtime error occurred)
b. #setia         #
    #setia sampai#
c. #setia#
    #setia sampai akhir#
d. #setia#
    #setia sampai#
e. #setia#
    #sampai akhir#

Discussion:

If our analysis on the program, s bertipe data is variable length character string with a maximum of 12. S initial value is the string ’setia’ ((length of line 5). Means that there is still free space for the rest of the string s as much as 7 units. Remember ... an empty space in here not contain spaces, but the null character. So when printing writeln ('#', s,'#'); still appear ‘#setia#’ and not ‘#setia       #’.

Then on the next line profile with a long string ‘ sampai akhir ‘ (14 characters including spaces). Can the string plus the string? Runtime error? he ... 3x here the meaning of plus signs (+) does not mean the addition, but the string unification. Operator + can be given in the string. But do not try to use fraudulent operators -, * or / in the string operation. Computer can error. 

So that the string s is the new, ’setia’ + ‘ sampai akhir ‘. Continue ... what is the result? whether the ’setia sampai akhir’? Eit .. whoa, how much capacity the number of characters that can be loaded on the s? Oops ... there are only 12 characters only. Thus the result is ’setia sampai’ (12 characters).

Next printing writeln ('#', s,'#'); and the result is ‘#setia sampai#‘. Thus the result is a D.

Source : Rosihan Ari’s Blog at blog.rosihanari.net (edited)

0 comments:

Post a Comment

Visitors


counter

Translate it

Followers

User


counter