CMPS 350 HW 05: Names, Type Checking, Scope and Binding

You must 'check' your answer on each question or your score will not be recorded.
01. What are some advantages that dynamic typing offers over static typing?
    A. reliability    B. flexibility     C. efficiency    D. writability
A B C D
02. Under what condition is language B less strongly typed than language A?
    A. if B has more implicit type coercions than A
    B. if B has more explicit type conversions than A
    C. both statements are true 
A B C
03. The scripting languages JavaScript, Python, PHP, and Ruby are
    A. dynamically typed   B. typeless   C. statically typed    D. a combination
A B C
04. Which is always bound to a modifiable memory reference?
    A. an lvalue          B. an rvalue       C. both must be bound 
A B C D
05. What is true in the assignment statement 'A = 5 + 8'?
    A. there is no rvalue
    B. the result of 5 + 8 is an unmodifiable value
    C. the rvalue is not bound to a memory address
    D. the lvalue must be modifiable.
A B C D
06. Select the true statement.
    A. A binding is static if it first occurs before run time and remains 
       unchanged throughout program execution.
    B. A binding is dynamic if it first occurs during execution or can change 
       during execution of the program.
    C. both A & B.
A B C
07. What is true about this C code if it comprises one compilation unit?
      void funa( ) {
        extern x;
        x = x--; 
        printf("x = %d",x);
      }
   A. the linker will bind x to a memory address on the runtime stack
   B. the linker will bind x to a memory address in the data section
   C. the compiler will bind x to a memory address in the data section
A B C
08. What is true concerning C static variables, such as x in this C function?
     void fun () {
          static int x = 5;
     }
   A. the value of x can never change
   B. x is bound to the same memory address throughout program execution
   C. x is bound to a memory address by the compiler
   D. there can be only one instance of a variable named 'x' in the program. 
A B C D
09. What can occur in a dynamically scoped language that cannot occur in a
    statically scoped language?
    A. a reference to a non-local variable can be bound to a memory address at 
       runtime 
    B. the visibility of a non-local variable can change at runtime 
    C. the address of a global variable can be bound by the compiler 
    D. the scope and visibility of a variable can be determined by looking at
       the code
A B C D

 ,---------------------------------------------,
 |  -- Sample Ada PROG1                        |
 |    procedure Main is                        |
 |    X : Integer := 0;                        |
 |    procedure Sub3;  (forward reference)     |
 |    procedure Sub2;  (forward reference)     |
 |   ,---------------------------------,       |
 |   |  procedure Sub1 is              |       |
 |   |  X : Integer := 0;              |       |
 |   |  --------------------------     |       |
 |   |  |  procedure Sub2 is     |     |       |
 |   |  |  begin                 |     |       |
 |   |  |    X := X + 2;         |     |       |
 |   |  |  end;                  |     |       |
 |   |  --------------------------     |       |
 |   |  begin                          |       |
 |   |   Sub3;                         |       |
 |   |  end;                           |       |
 |   '---------------------------------'       |
 |                                             |
 |   ,---------------------------------,       |
 |   |  procedure Sub3 is              |       |
 |   |  Y : Integer;                   |       |
 |   |  begin                          |       |
 |   |    X := X + 3;                  |       |
 |   |  end;                           |       |
 |   '---------------------------------'       |
 |   begin                                     |
 |     -- Y := 4                               |
 |     Sub1;                                   |
 |     Sub2;                                   |
 |     Put(X);                                 |
 |   end;                                      |
 '---------------------------------------------'

10. Since Ada has static scoping like C, the reference to non-local X from 
    Sub3 is bound to the declaration of X in Sub1 in PROG1. 
T F
11. Since Ada has static scoping, the reference to X in Sub2 is bound to the 
    declaration of X in Main in PROG1.
T F
12. Under Ada's static scoping, what value for X is displayed on the screen 
    after executing Main in PROG1? 
    A. 2        B. 3       C. 0
A B C
13. In PROG1, if you uncommented the line which is a reference to Y from Main
    what would happen?
    A. Y will be bound to the the address of Y in Sub3      
    B. it will result in a compilation error   
A B
 
 ,-------------------------------------------,
 |   -- sample Ada program JUNK              |
 |   Procedure JUNK is                       |
 |   X:INTEGER:= 0;                          |
 |   procedure SUB2;  // forward reference   |
 |                                           |
 |  ,----------------------,                 |
 |  | Procedure SUB1 is    |                 |
 |  | X:INTEGER;           |                 |
 |  | BEGIN                |                 |
 |  |    X := 10;          |                 |
 |  |    SUB2;             |                 |
 |  | END;                 |                 |
 |  '----------------------'                 |
 |                                           |
 |  ,--------------------,                   |
 |  | Procedure SUB2 is  |                   |
 |  | BEGIN              |                   |
 |  |   X := X + 15;     |                   |
 |  | END;               |                   |
 |  '--------------------'                   |
 |   BEGIN                                   |
 |     SUB2;                                 |
 |     SUB1;                                 |
 |     Put(X);                               |
 |   END;                                    |
 '-------------------------------------------'
14. Under static scoping what value of X is printed in procedure JUNK? 
    A. 0       B. 5      C. 15      D. 30
A B C D
15. Under dynamic scoping, what value of X would be printed in procedure JUNK?
    A. 0       B. 10       C. 25       D. 30
A B C D

The next questions refer to these two C compilation units:

,-------------------------------,  ,---------------------,
| /* file1.c */                 |  | /* file2.c */       |
| static int x = 10;            |  | static int x = 20;  |
| void fun(char *);             |  |                     |
| int main( )                   |  | void fun(char *str) |
| {                             |  | {                   |
|    {                          |  |    x = 30;          |
|      static int x = 15; j     |  |    str[0] = 'a';    |
|    }                          |  | }                   |
|    fun("hello");              |  |                     |
|    printf("%d",x);            |  |                     | 
|    return 0;                  |  |                     |
| }                             |  |                     |
'-------------------------------'  '---------------------'
16. This program will not link since you have declared multiple static 
    variables named 'x'.
T F
17. The source file2.c will not compile because you can't change the value of 
    a static variable; i.e., you can set the value once when the variable is 
    declared and never change it.
T F
18. Consider the statement  str[0] = 'a' in file2.c. What is true?
    A. Since the lvalue is immutable this statement will not compile.
    B. The code will compile but will produce a segmentation fault at runtime. 
    C. There is nothing wrong with this statement.
    D. The code will not compile because a literal string does not match type
       char *. 
A B C D
 
19. What is true regarding this C++ variable declaration?
             const int y = 5;       // this is a named constant

    A. The statement will not compile because the lvalue is unmodifiable.
    B. The statement will compile but any subsequent assignment statment for y 
       will result in an unmodifiable lvalue error by the compiler.
A B
20. A static variable with class scope in C++ exists before any object of that
    class is created.  
T F
21. It is possible to dynamically bind the value of a variable and statically
    bind the address of that same variable.
T F
22. What type of coercion is present in the following C code?
      int X = 10;
      float Y = 10;
      X = Y;

    A. an int is coerced into a float
    B. a float coerced into an int
    C. both A & B
A B C
23. How would would describe the type binding in this Perl statement? Assume
    this is the first reference to $var.
            $var = 5;

    A. $var is dynamically bound to type integer 
    B. $var is statically bound to type scalar  
    C. both A & B 
A B C
24. Which of the following is not a category of variables defined by storage
    binding?
    A. stack dynamic
    B. explicit stack dynamic
    C. static
    D. implicit heap dynamic
A B C D
25. What does this C function achieve?
    void ptrs(int** ptr1, int** ptr2){
       int* tmp_ptr = *ptr1;
       *ptr1 = *ptr2;    
       *ptr2 = tmp_ptr; 
    }
    A. you are swapping integer values
    B. you are swapping addresses to integers 
    C. you are swapping addresses to pointers
A B C
The regular expressions (REs) that follow use this notation:

[ ] denotes a set of choices for a single character
( ) is a substring
 * is 0 or more
+ is 1 or more
{ } is optional
. (dot) is a wildcard for a single character 
^ anchors to the beginning of the line
$ anchors to the end of the line
\ is used to escape a metacharacter   
26. An Ada identifier must start with a letter and be followed by zero or more 
    letters, digits or underscores. An underscore cannot be the final symbol 
    or be followed by another underscore. Is this regular express correct?
  
          [a-zA-Z][a-zA-Z0-9_]([a-zA-Z0-9])*{_[a-zA-Z0-9]}
T F
27. What will this Perl statement achieve?  (s is the substitute command and g 
    will apply the command to all instances of the match in the line)
 
         $line =~ s/^[0-9]*\.//g;            

    A. remove all digits in the line.
    B. remove all the digits at the beginning of the line.  
    C. remove all the digits at the beginning of the line that are followed by .
A B C
28. The statement below is an acceptable initialization for a static named 
    constant of type char[].

         const char myName[30] =  "santa claus";
T F
29. What does the RE in this Perl statement match?

         next if /^$/;   

    A. any line     B. a blank line
A B
30. Downcasting a derived type to a base type in C++ is 
    A. explicit only        B. implicit       C. never allowed
A B C
31. What is this Ada statement?
 
       type AN_ARRAY is array(FIRST..LAST) of INTEGERS;           

   A. an abstract data type 
   B. an array variable of integers named AN_ARRAY 
   C. both A and B 
A B C
32. This Ada program will raise a range error exception at runtime.

    PROCEDURE main is
    subtype SUB_INT is INTEGER range 1..50;              
    X : SUB_INT;           
    BEGIN
       X := 60; 
    END
T F
33. The following Perl statements will execute without error. What can you thus
    conclude?

        $var = 5;             
        $var = "hello there";        

     A. strings are primitive scalar types
     B. strings are implicitly coerced into integers
     C. Perl is a dynamically-typed language
     D. memory for $var is dynamically allocated   
A B C D
34. Which of the following do named constants support?
    A. readability  B. parameterization   C. both A & B
A B C
35. Ada was designed by the Dept of Defense for 
    A. reliability
    B. large team projects
    C. embedded systems
    D. concurrent programming
A B C D

  Right: Wrong: Percent: (Must be 100% for credit)

Errors:

Name (first name only): Sleipnir Username: 3-digit ID: