CS508 Final Paper Solution by adeel109

VIEWS: 130 PAGES: 16

									                                                                                                         Page # 1




                    aper CS508 solved by BC020200307 (Final term Fall 2005)

Question No: 1                                                                            Marks: 25

Consider the following Code written in C.

       int a = 0;

       int f(int x, int y)
       {
                return (a = x + 1);
       }

       int g(int *x)
       {
               return (*x = a + 2);
       }

       void main()
       {
             double w;
             w = double(a) + f(a, g(&a))/g(&a);
             printf (“w is %f”, w);
       }

On some different important commercial compilers, the values found for the variable w were 5.6, 4.5, and 7.5.
Furthermore, this last value changed to 2.5 when the expression to compute w was changed to:

       w = f(a, g(&a))/g(&a) + double(a);


None of this is an error by the compiler writer!

   (a) Give a justification for each one of these values.




                                                                                                  !
             "#          $#%
                                                                                                                                     Page # 2


    " &                                                     %



    $   &                                                       %
                     '


(                                                          )*%
        '                +           ,

        -        .           .       /       0

                                                           )*
             .       -                   0                                                    /   0

        1            .                                                          /       0 /
        -            +                                          /
                                                                                                               2 !        # $ 3
         ,                                         $                                                                       $ '
                                                                    "   /                             4                  4

        '!                                   0                                                                                   $
                                         )

        .        /           0                                          /   0       4    )            #*                     .

        5,                   -               ,                                                             -   )#

        -                .       -               #* ) # ) *

(                                                          6)%
        -                        +           ,

        -        .           .       /       0

                                                           6)
             .       -                   0                                                    /   0

        3                            7                                                                                   ,
                                                                                                  8                  /
                                                                                                                     $
                                                 2 !                                       $ -        ,                          4
                                 /       4             0                                2 !                              7
                                                                    0                   $                                            $
                                                                                                                                 Page # 3

     9                /       0       4       $               ")                            .

     5,               -                   ,                                                         -   4#

     -            .       -               ")          4 # 6)

(                                                      : )%
     -                    +           ,

     -        .       .           /   0

                                                      :)                                                            '
                          0                                                $                    $               $

     8                                                        /
                                                  1                                                         '
          $                   ,                                                6                        6                         2 !
              6                                        -           ,                    )                               )

     5            /           0       )       $                                    $)                       .

     5,               -                   ,                                                         -   )#

     -            -       .               )# $)               :)

5,


(                                                      $ )%
     -                    +           ,

     -        .       -           /   0

                                                       $)                                                           '
                          . 5,                        .               ,                                                 .   ##

                      -                                           /        0%
     ;                                                                                                                      0
                              $                                        $           $

     8                                                        /
                                                  1                                                         '
          $                   ,                                                6                        6                         2 !
              6                                        -           ,                    )                               )

     5            /           0       )       $                                    $)                       -
                                                                                          Page # 4

     -            -   .       $)   ## $ )


  (b) Would Java have the same kind of problem? Why or why not. Justify.



     8    (                                                                (
                                              5
              ;
              <                                   +                                  7
                                                      ,

                          <                                 #=
                                                           "#




Question No: 2                                                                 Marks:15
                                                                                                              Page # 5

Compare C# and Java and highlight at least five differences. Then critically compare these features and argue why
one design is better than the other.



;                   .              (         -               />;           %
       /                                         ?                                   ?
        ,
       @                /
       9
       8
       /                                                 2       < !                            2    !+
                                                         (
       ;
       '
       8
       -                                                     ?                   +
            AA                                                             ?B %%
       2    !           2              ! !           !           +
       -                                             7
       C        .             ,                                            2 !
            .       +
       .        /D            (                                                          % /D    E        '
       F                    E;'F             (                   (
       '                                                                                        < ? ?
                                   G                                   G
       ' /D
                                                                                                 Page # 6


0               .                (       -        />;       %
        E                                          (
        ;
        ;                2       !>                     <
        C
        G           G?       ?                                               ?
        H
        ;                    G                G

.                                                                   /    '                       /D
            (                                 ,         +
                                             /                      /D                       <
    +                                ;                                                                /
H                                      /D                       +
                                     /D                                          (

Question No: 3                                                                   Marks: 20
                                                                                                           Page # 7
    (a) Write a Prolog description of your family tree (based only on facts), going back to your grandparents and
        including all descendents (males and females). The facts should include the gender of a person and
        relationship of every person with every other person.



1    %


      +


          I




          +   7




                       I




                            I
                            Page # 8

        +




                +




    +
    +
    +




        +



                    +   7
+       7


                    I




    +       7
    +       7       I
    +       7
    +       7
    +       7
    +       7
    +       7
                    Page # 9

+
+           I
+
+
+
+
+


        I




        I




                I




    I
    I
    I
    I
    I
        Page # 10




    I




I




    I
                                                                                                         Page # 11

(b) Write a set of rules for family relationships, including all relationships from grandparents through two
    generations. Now add these to the facts of part (a), and eliminate all the facts except the gender and parent-
    child relationship. You should still be able to establish the relationship of any person with any other person in
    your family.




  +


      I




      +    7




          +




               +




                   I
                                                                      Page # 12

      +    7
      +    7     I
      +    7
      +    7
      +    7
      +    7
      +    7

      E J %?           E J             E
     1 J %?          1 J          1

           @ J %?          1 J          @ 1
            @ J %?          1 J           @ 1

       3 & %?       3 J                  & J
    & 3 %?       3 J                  & J

     ; J %?          ;       9 ;           9 J JK    ;
       . J %?        .       9 .            9 J JK    .

      ; J %?         9 J              9 ; JK    ;

    - J %?       9 J          - 9
    L J %?        9 J           L 9

Question No: 4                                            Marks: 15
                                                                                                       Page # 13

Compare weak and strong typing. Argue why it is OK to have weak typing in scripting languages but not in other
general purpose languages such as Java and C++.



& +              %
& +
   ,                     '                                                              !
                                         5,                  +               (   ;   939

;                    %
;
                                                                                               ,
    I                        +                                                                5,
                              /   (

'       CM                        +
                                                                     ;
                                                                                               ,
                                                   ,             ,
                 ?

'                                                                (           /
                                                         C               <               +
             '               +                               ?                                     +
 ,

'                                                                                                         +


Question No: 5                                                                           Marks: 25
                                                                              Page # 14

Consider the following 5 binding times:

(1) language design time
(2) program writing time
(3) compile time
(4) load time
(5) run time

Give at least one example of each of the above for the following languages:

   (i)       C++



         F              0            %
         H

         9          &            %
         ;7


         /                  %

         3    2 !

         F              %
         &                                     2 !

         H          %
               N                "#
                                                                              Page # 15

(ii)         Java



       F                   0                  %
       H

       9               &                   %
       ;7                                                                4$


       /                           %

       3         2 !

       F                   %
       4$?                                             2 !

       H               %
             ;                         ;          O3     PQ



(iii)        Javascript



       F                   0                  %
       H                               +

       9               &                   %
                 7

       /                           %
       8 -

       F                   %
                               )

       H               %
       -          ?                                                  ,
                      $"               RP??                   ??PB

       , O3            PQ              RP?? ,                 ??PB
                                                                  Page # 16

(iv)         PHP



       F                    0            %
       H                             +

       9                &                %
                  7

       /                         %
       8 -

       F                %
             S         $"            S

       H                %
       -          ?                                      ,
       S     $           #
                       !" )!         S             $
                                                  " )

       S     O3             PQ       S


(v)          C#



       F                    0            %
       H

       9                &                %
       ;7                                                    4$


       /                         %

       3         2 !

       F                    %
       4$?                                        2 !

       H                %
             ;                       ;       O3     PQ

								
To top