OS Test 2

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Give three concrete examples drawn from programming languages with which you are familiar in which a variable is live but not in scope

1. When a subroutine is called with a local variable with the same name 2. variables declared in a code block (for/while loops, conditionals, simple code blocks with {}) with identical names as other variables 3. local variables with the same name as global variables

Here are some type and variable declarations in C syntax: typedef char* String1; typedef char* String2: String1 a; String2 b, c; String2 d; State which variables are type equivalent under(a) structural equivalence, (b) strict name equivalence, and (c) the actual C equivalence algorithm.

1. a = b = c = d since they all have the same "structure" 2. b = c = d since b c and d have the same name 3. a = b = c = d since c uses structural equivalence

Many storage-management algorithms maintain a list of free blocks. Here are two different algorithms, first fit and best fit, used to search this list and return a block of the appropriate size. Which strategy, if either, will result in lower external fragmentation? What does external fragmentation depend on?

Best fit will result in less external fragmentation. External fragmentation happens when here is enough total memory space to satisfy a request but the available spaces are not contiguous. Thus by storing memory where it best fits, the unavoidable spaces between data will be smaller.

Binding time for the variable declaration that corresponds to a particular variable reference (use)

Binding time is bound at compile time in C since C used static scope.

Binding time for the maximum length allowed for a constant (literal) character string

Binding time is determined at the language implementation time

Binding time for the number of built in functions

Binding time is determined when the language is designed

What is a static semantic?

Checks that are performed at compile time.

Describe the difference between deep and shallow binding of referencing environments

Deep

Binding time for the address of a particular library routine

Runtime

Explain the distinction between decisions that are bound statically and those that are bound dynamically.

Static binding = compile time Dynamic binding = run time Type information is being used by static binding, while object is being used to conclude the dynamic binding. In static binding, we see that overloaded methods are bonded in it. In dynamic bonding, we see that overridden methods are bonded.

Discuss the differences among the denotational, structural, and abstraction based types of view

The denotational point of view considers that a type is a set of values. A value has a given type if it belongs to the set, an object has a given type if its value is guaranteed to be in the set. Constructive point of view considers that a type is either one of a small collection of built-in types or a composite type create by applying a type constructor to one or more simpler types. Abstraction-based views of types considers that a type is defined by the operations that may be performed by it and the effects of these operations.

Explain the distinction between the lifetime of a name to object binding and its visibility

The difference between the lifetime of a name-to-object binding and its visibility is that its lifetime encapsulates all the time between the binding's creation and estruction, while its visibility describes whether or not that certain binding will be used. For instance, if a global and local variable share the same name, within the local variable's scope, the global variable's binding is invisible and cannot be accessed.

Consider the following declaration in C: double(*foo(double (*)(double, double[]), double)) (double, ...); Describe in English the type of foo.

The foo is a function which is taking the following parameters. A pointer to a function and a double. This pointer to a function takes parameters of a double and an array or doubles, and then returns a double. This foo after taking these parameters, it returns a pointer to a function. This function takes arguments, a double and a varied argument list.

Why might it be useful to distinguish between the header and the body of a module?

The header of a module specifies the properties and the name of the module. The type is declared, and it is used to provide information about the program. The body of a module contains statements and the objects that are hidden from the users and other subroutines. It provides the actual content.

What is a just in time compiler?

developed to provide faster implementations. These compilers translate the byte code into machine language before every execution.

Explain the distinction between interpretation and compilation. What are the comparative advantages and disadvantages of the two approaches?

1) As interpretation scans the program line by line, it provides better error detection mechanism than compilation. 2) Interpretation does not convert the input program into any intermediate form, so it provides better and early debugging facility. 3) Some languages are impossible to be implemented without the interpretation because they utilize the "Late Binding" feature, or the ability to assign the values to the variables late during the execution phase. 4) Compliation is faster than interpretation. Complication saves the result in cache, so it saves the time of interpretation, which scans the program every time a variable is requested. 5) Compliation is more efficient than interpretation.

List 6 kinds of tools that commonly support the work of a compiler within a larger programming environment

1) Assemblers are used to convert the mnemonics into machine language 2) Debuggers are used to detect errors in the program 3) Preprocessors are tools which read the program before the compliation. It removes the comments and white spaces in the program 4) Linkers are used to combine library routines to various parts of a program 5) Style Checkers - these tools are used to provide the syntactic or semantic constraints, which compiler doesn't provide 6) Configuration Management Tools - Configuration management tools keep intact the various compiled components.

What is mix-in inheritance? What problem does it solve?

1. Mix-in inheritance is a limited version of true inheritance. It allows programmers to define the interface the class must provide so it can be used in specific context. When classes have only one true parent, we can still mix in multiple different interfaces,

Consider the following code snippet in C-like syntax: int A[4]; int runit(){ i = 0; A[0] = 2; A[1] = 1; A[2] = 0; A[3] = 2; mys(i,a[i]); print(i, a[0], a[1], a[2],a[3]); mys(a[1],a[i]); print(i, a[0], a[1], a[2],a[3]); return 0; } void mys(int x, int y){ x = x + y;y = x - y;x = x - y; } By reference.

2. Pass by reference 1. print(i, a[0], a[1], a[2],a[3]); 1. mys() is void, but we passed by reference so i and a[i] swapped. We get 2,0,1,0,2 2. Print (i, a[0], a[1], a[2], a[3]); 1. mys(a[1], a[2]) is invoked We get(2, 0, 0, 1, 2);

Consider the following function/macro definition in C:#define SQUARE(x) x*x int squareF(int x) { return x * x; } Assume a = 3. What is the computed result of SQUARE(a)? (Macro expansion.)

3*3

Consider the following code snippet in C-like syntax: int A[4]; int runit(){ i = 0; A[0] = 2; A[1] = 1; A[2] = 0; A[3] = 2; mys(i,a[i]); print(i, a[0], a[1], a[2],a[3]); mys(a[1],a[i]); print(i, a[0], a[1], a[2],a[3]); return 0; } void mys(int x, int y){ x = x + y;y = x - y;x = x - y; } By value-result.

3. Pass by value result - same as reference 1. print(i, a[0], a[1], a[2],a[3]); 1. mys() is void, but we passed by reference so i and a[i] swapped. We get 2,0,1,0,2 2. Print (i, a[0], a[1], a[2], a[3]); 1. mys(a[1], a[2]) is invoked. We get(2, 0, 0, 1, 2);

Consider the following code snippet in C-like syntax: int A[4]; int runit(){ i = 0; A[0] = 2; A[1] = 1; A[2] = 0; A[3] = 2; mys(i,a[i]); print(i, a[0], a[1], a[2],a[3]); mys(a[1],a[i]); print(i, a[0], a[1], a[2],a[3]); return 0; } void mys(int x, int y){ x = x + y;y = x - y;x = x - y; } By macro expansion.

5. Pass by name 1. I+a[i] // 0 + a[0] ; i=2 2. a[2]=2+a[2]// a[2]=2 3. I-a[i] // 2-a[2] ; i=0 1. print(i, a[0], a[1], a[2],a[3]) => gives us 0, 0, 1, 2, 2 4. i=i+a[i] // 0 +a[0] ; i=0 5. a[0]=0-a[0] // a[0]=0 6. i=0+a[0] // i=0 1. print(i, a[0], a[1], a[2],a[3]) => gives us 0, 0, 1, 2, 2

Consider the following code snippet in C-like syntax: int A[4]; int runit(){ i = 0; A[0] = 2; A[1] = 1; A[2] = 0; A[3] = 2; mys(i,a[i]); print(i, a[0], a[1], a[2],a[3]); mys(a[1],a[i]); print(i, a[0], a[1], a[2],a[3]); return 0; } void mys(int x, int y){ x = x + y;y = x - y;x = x - y; } By name.

5. Pass by name 1. I+a[i] // 0 + a[0] ; i=2 2. a[2]=2+a[2]// a[2]=2 3. I-a[i] // 2-a[2] ; i=0 1. print(i, a[0], a[1], a[2],a[3]) => gives us 0, 0, 1, 2, 2 4. i=i+a[i] // 0 +a[0] ; i=0 5. a[0]=0-a[0] // a[0]=0 6. i=0+a[0] // i=0 1. print(i, a[0], a[1], a[2],a[3]) => gives us 0, 0, 1, 2, 2

Consider the following function/macro definition in C: #define SQUARE(x) x*x int squareF(int x) { return x * x; } Assume a = 3. What is the result of the invocation of squareF(a) ? (Assume pass by value semantics.)

9

Explain the difference between a declaration and a definition. Why is the distinction important?

A declaration is using an object or using a function without being concerned about the way of implementation A definition is implementing a function and describing how the operations are being performed.

What are two principal tradeoff between reference counts and tracing strategy used in garbage collecting?

A principle tradeoff between reference counts and tracing strategy is the computational time it takes to perform. It is expensive to increment or decrement a variable when it is lost or copied. The reference count model does not grow with dynamic memory. The tracing strategy is less expensive per iteration but grows asymptotically.

What does it mean for a language to be statically typed?

A statically typed language is so if it performs type checking at compile time instead of runtime. In a statically typed language, the error will be thrown at compile time not at run time. According to statically typed language, the compiler can statically assign a correct type to every expression.

Explain the difference between true iterators and iterator objects.

A true iterator enumerates items, they produce (by calculating) loop index values. Iterator objects on the other hand, which provides methods of initializations. The iterator is in the object's data members, they maintain all intermediate state in explicit data structures, not a program counter.

Explain the importance of information hiding

Also called encapsulation. Involves hiding critical concepts or decisions of the program code. Main motive is to hide those interfaces of he objects whose knowledge is not required for the implementation of the objects.

What are first class subroutines? What languages support them?

An object which is being passed as a parameter and then it is being returned from the subroutine and finally it is assigned to a variable.

Run the following program in Perl: sub foo { my $lex = $_[0]; sub bar { print "$lex\n"; } bar(); } foo(2); foo(3); You may be surprised by the output. Perl 5 allows named subroutines to nest, but does not create closures for them properly. Rewrite the code above to create a reference to an an anonymous local subroutine and verify that it does create closures correctly. Add the line use diagnostics; to the beginning of the original version and run it again. Based on the explanation this will give you, speculate as to how nested named subroutines are implemented in Perl 5.

Based on the code, nested named subroutines have state, and that state can be changed. On the first call to foo, the variable lex is present in the subroutine foo and bar. Foo gets the value for the variable lex from the subroutine call. Then after it prints the argument of lex, it calls the subroutine bar. Bar does not have the parameters so it adopts the value of lex from the outside subroutine into its own value of lex. After that, the value of lex is assigned to 5, and the first call to foo is finished. During the second call to foo, the same steps occur, this time the subroutine bar already has a value for the variable lex. So when foo(3) calls bar, bar will not need to and will not retreive a value for lex from te outside subroutine and will use its value for lex it got from the previous foo call.

What prevents, say C, from being strongly typed?

C is not strongly typed because it does not perform any check at run time and strongly typed languages perform checks at run time.

What is a dynamic semantic?

Checks that are performed at runtime.

What is the difference between a compiler and a preprocessor?

Compiler translates the source program in high level language to assembly or machine code, whereas preprocessor only modifies the source program by performing few transformation. The transformations to be applied are determined by simple pattern recongnition techniques.

Which way does the stack grow?

Downward

What is the advantage of binding things as early as possible? What is the advantage of delaying bindings?

Early Binding - It is also known as static binding. It is the time at which the compiler allocates memory to the objects perform other operations before the execution of the program. Early binding helps in reducing the number of run time erros as all the errors are reported at compile time itself. Delay Binding - It is also known as dynamic binding. Delay binding method is slower than early binding method. The compiler need not refer to the libraries that contain the object at compile time, it prevents static checking. We can also change the target method at run time.

What is an l value?

Expressions that represent locations where the value is to be stored.

Explain the distinction between limited and unlimited extent of objects in a local scope.

For limited extent objects, these are local objects whose lifetime or scope is limited. Objects belonging to this type get destroyed at the end of each execution. Memory space is reclaimed at regular intervals. Mainly used in many imperative languages. Space allocation or allotment for such objects is generally done on the stack. Suffer from the dangling reference problem. Unlimited extent objects are objects whose lifetime or scope is indefinite. Not destroyed at the end of each scopes execution. Used in many scripting languages. Heap is used for the local objects, dangling reference problem is not an issue.

What are forward references? Why are they prohibited or restricted in many programming languages?

Forward declaration is using an identifier before its declaration. It allows us to use the type by pointer by we cannot declare a variable.

In Section 6.1.2 ("Orthogonality"), we noted that C uses = for assignment and == for equality testing. The language designers state: "Since assignment is about twice as frequent as equality testing in typical C programs, it's appropriate that the operator be half as long" [KR88, p. 17]. What do you think of this rationale?

I think this rationale is sound. Consider this, comparison requires two values, which often happens to be variables. These variables are given a value prior to the comparison via assignment. However, assignment does not accompany comparison nearly as much as comparison follows assignment. This means, a variety of operations can follow an assignment, not just a comparison, but comparison almost always requires some sort of value to be assigned first before it is compared.

Explain how an integrated development environment (IDE) differs from a collection of command line tools

IDE's not only maintain the source code, but creates a syntax tree. Command Line tools have no user interface through which user can make changes

What is a dangling reference?

If a heap variable is destroyed any pointer variable still pointing to that specific heap is called a dangling reference, it is a reference to an object that no longer exists.

If foo is an abstract class in a C++ program, why is it acceptable to declare variables of type foo*, but not of type foo?

If foo is a type for a variable then it means we are creating the object of the abstract class which is not possible for abstract classes. Foo f is not valid, foo* f = new foo() is not valid, but foo* f is.

Consider the following function/macro definition in C:#define SQUARE(x) x*x int squareF(int x) { return x * x; } Assume a = 3. Give an example where squareF(x) and SQUARE(x) compute different values.

If we passed 2+1, for the macro we would get 2+1*2+1 => 5, if we use the function we get 9.

What happens to the implementation of a class if we redefine a data member? For example, suppose we have class foo{ public: int a; char *b; }; ... class bar : public foo { public: float c; int b; }; Does the representation of a bar object contain one b field or two? If two, are both accessible, or only one? Under what circumstances?

If we redefine a data member there is no error. Two b fields can be present, but to access the b field of class A, the scope resolution operator is used.

Compare Java final methods with C++ non virtual methods. How are they the same? How are they different?

In Java, final is a keyword that can be applied to a method, class, or to a variable. After making a reference final, this it cannot be changed. When a method is declared as final, it cannot be overridden in its subclass. In C++, a non-virtual method acts as a final method in java. Non-virtual methods in C++ are resolved at compile time and in Java, final methods are bound at compile time. In C++ all methods are non-virtual by default but in Java, all methods are virtual by default. To make a method non-virtual, the final keyword is used. The final method in Java cannot be overridden in a derived class.

What is the difference between a value model of variables and a reference model of variables? Why is the distinction important?

In a value model of variables, a variable is a named container for a value. In a reference model, a variable is a named reference to a value.

Consider the following pseudo code: x : integer := 1 y : integer := 20 procedure add() x := x + y procedure second(P : procedure) x : integer := 2 P() procedure first() y : integer := 3 second(add) first() write integer(x) Explain why 4 is printed out if the language uses dynamic scoping with deep binding.

In dynamic scoping with deep binding, the environment is bound to the function when it is passed as an argument. X = 1 and Y = 3 when add() is passed to second(), so 4 is the final value.

Consider the following pseudo code: x : integer := 1 y : integer := 20 procedure add() x := x + y procedure second(P : procedure) x : integer := 2 P() procedure first() y : integer := 3 second(add) first() write integer(x) Explain why 1 is printed out if the language uses dynamic scoping with shallow binding.

In dynamic scoping with shallow binding, function calls take the environment of the previous function call.

Describe the difference between static and dynamic scoping

In static scoping, the object is searched for in the local function and then in the function in which that function was defined and so on. Dynamic scoping searches for the object in the local function and then in the function that called the function and so on.

What is the purpose of the compilers symbol table?

It is a data structure, which is maintained by the semantic analyzer. It contains the information about the identifiers in the program such as data type, size of the element, scope of the identifier within the program.

What is a frame pointer? What is it used for?

It is a memory location that can be accessed through registers which presents in stack frame. It points to the beginning of the storage space allocated for any one function, and does not change during the execution of that function.

What is an opaque export?

It means that the declaration and definition of the object cannot be changed but it can be passed as arguments to other subroutines.

Is Java compiled or interpreted (or both)? How do you know?

Java can be considered as compiled and interpreted. Java programs are first compiled in intermediate form known as byte code. Byte code is known as standard form which provides the facility of platform independence. Earlier versions of Java used byte code interpreter to execute the programs. Later versions started using Just in Time compiler which is used to provide faster execution of programs by translating the byte code into machine language immediately before every execution.

Name two languages in which a program can write new pieces of itself "on the fly."

Just in time compilers are used. Lisp and Prolog.

Explain the distinction between the lifetime of a name to object binding and it's visibility.

Lifetime is used for developing and the distortion of the name to object binding. It is used to refer the extent of the object. It can be used to publicly fetch the input. Visibility is that an object may or may not be visible at a particular point in the program. It refers to the scope of an object which is being used.

What are macros? What was the motivation for including them in C? What problems may they cause?

Macros are most frequently used to define names for constants. Motivation for implementation is it ignores the requirement of supporting the named constant in the language itselff. More efficient than any other C function. Avoids the overhead of a subroutine call. Problems caused can be that is implemented by textual substitution, preprocessor will replace all the macros in the program. Makes the compiler slow. Provides binding mechanism which is different from the other programming languages. Some operations are unpredictable.

Is &(&i) ever valid in C? Explain.

No, '&' takes an L value and returns a R value, this statement would be making the outer '&' to take an R value so it would never be valid.

What distinguishes operators from other sorts of functions?

Operators can be defined as built in functions that use some special symbols and have a simpler syntax in comparison to other general functions. Arguments are known as operands.

What is P-Code?

P code is referred to as intermediate output which is produced as output in the bootstrapping process of a Pascal compiler.

Explain the difference between prefix, infix, and postfix notation.

Prefix - function name or operator appears before its arguments in a function call. Infix - function name or operator appears among its arguments in a function call. Postfix the function name appears after its arguments in a function call.

What is elaboration?

Process of storage or stack allocation and creation of bindings. Responsible for entailing the creating of stacks and initialization of stack dynamic storage bindings.

Binding time for the referencing environment for a subroutine that is passed as a parameter

Runtime

Binding time for the total amount of space occupied by program code and data

Runtime

What are the phases of compilation?

Scanner/Lexical Analysis, Parser/Syntax Analysis, Semantic Analysis, Intermediate Code Generation, Code Optimization, Code Generation

Name 8 major categories of control-flow mechanisms.

Sequencing, Selection, Iteration, Procedural abstraction, Recursion, Concurrency, Exception Handling, Non-Determinancy

Consider the following C declaration, compiled on a 64-bit x86 machine: struct{ int n; char c; } A[10][10]; If the address of A[0][0] is 1000 (decimal), what is the address of A[3][7]?

Since structs are 8 bytes we can get the index by doing basic arithmetic (start + row + col). 1000 + 3(10)(8) + 7(8) which gets us 1296.

What is a calling sequence?

Specified some predefined set of instructions to prepare and then it calls some subroutine of it. When any data is needed then it provides its availability. At last it confirms the computer that where it has to return after the execution of the subroutine.

What does it mean for a language to be strongly typed?

Strongly typed languages are the languages that requires each object to hae type. In a strongly typed language, at runtime, it must be verified for an operation to be allowed on an object. It is not possible to work around the type system. In a strongly typed language, an error will occur if we are trying to apply an operation to an object which does not support that operation.

Briefly describe three "unconventional" compilers - compilers whose purpose is not to prepare a high level program for execution on a general purpose processor.

TEX and TROFF are text formatter compilers which convert the high level programs into instructions for laser printers. Query language processors also constitute this type of compiler, which translate the SQL queries into file operations which are used to provide the result of desired query.

Given the following C++ declaration, double* dp = new double(3); void* vp; int* ip; which of the following assignments does the compiler complain about? vp = dp; dp = vp; ip = dp; dp = ip; ip = vp; ip = (int*) vp; ip = static_cast<int*>(vp); ip = static_cast<int*>(dp); ip = reinterpret_cast<int*>(dp); Briefly explain the behavior of the compiler. Will *ip ever have the value 3 after one of the assignments to ip ?

The compiler will complain about: dp = vp, ip = dp, dp = ip, ip = vp, ip = static_cast<int*>(dp) *ip wont have the value 3 since the compiler will complain as soon as it tries to do dp = vp. The compiler compares sizes, in this example, where vp is being assigned to dp, dp and vp have different types of information. Compiler also complains on the line where dp is being assigned to ip, this is due to the pointers containing different information about the type of what they point to.

What is an object closure? How is it related to a subroutine closure?

The concept of object closure or a function object comes into the existence when an object works as a function along with its referencing environment. By using the object closure, it is also feasible to incorporate or instantiate any subroutine matching the particular argument or parameter list.

Suppose this C++ code: const int k = ???; // you need to figure out the value of ??? int * i = 0; struct A { int x[k]; }; A* a = 0; printf( "%d %d %d %d %d %d ", i+1, i+k, k, a+k, &(a[9]), &(a[9]) - (a+1) ); outputs the values for the first two expressions: 4 12 What are its remaining outputs? Briefly explain how the output is obtained. (Hint: pay attention to coercion.)

The first element is 4, which is represented by i + 1. We know that i begins at 0, and is an int pointer. We know that the size of an int is 4 bytes, so for the first element we are moving forward 4 starting from 0 to get to 4. To get the second output of 12, we jump over i + k places which is 3 places. We assume that k must equal 3 to give us the starting answers of 4 and 12. The third element is just 3 since k = 3. For the 4th value, we move the pointer 12 for the size of the struct (3 ints) and multiply by k which is 4, and get 36. For the 5th element, we simply multiply a by 9. Since we know that a = 12, 12 * 9 = 108. Finally, doing pointer subtraction with an array a which has 9 elements, minus some element 1 will get us the 8th element of the array and therefore 8 gets printed.

What is the complexity of using first fit algorithm to allocate blocks? Briefly explain an algorithm to speed up the allocation. What is the complexity of this modification?

The first fit algorithm is worst case linear. This occurs when the block will not fit, or only fits into the last found space. Heuristically, the first fit algorithm will execute faster than best fit. We can do this by keeping a list of free blocks in our memory space. When one is allocated to, we remove it from the free list. It will be returned when deallocate the space. We will keep the list sorted in decreasing order of size. If we keep the largest free block first, we are likely to get a hit on the first block. Thus the look up time for allocation would be constant.

Consider the following pseudo code: x : integer := 1 y : integer := 20 procedure add() x := x + y procedure second(P : procedure) x : integer := 2 P() procedure first() y : integer := 3 second(add) first() write integer(x) What does this program print if the language uses static scoping?

The program will print out 21. The add function is the only function where x was not redefined. Static scoping would mean that once x is redefined in the procedure, it loses access to the global x.

What do we mean by the scope of a name to object binding?

The scope of a name-to-object binding is the area within which the binding is active. Can be local or global.

What do we mean by the scope of a name-to-object binding?

The scope of a name-to-object binding refers to the section of code in which that binding is visible.

What is a referencing environment?

The statement which is used to show the provided names as a collection is treated as the referencing environment of a statement

C++ and Java loops are often written with empty bodies by placing all side effects into the tests, such as in the following two examples: i = 0; while (a[i++] != 0); for (i = 0; a[i] != 0; i++); Are these loops equivalent? Explain.

They are not equivalent since the while loop will point to the next element after the first zero element is found i++ occurs first. For the for loop, it will be the index of the first zero element since i++ occurs only if the second clause (a[i] != 0) occurs.

We noted in Section 6.1.1 that most binary arithmetic operators are left associative in most programming languages. In Section 6.1.4, however, we also noted that most compilers are free to evaluate the operands of a binary operator in either order. Are these statements contradictory? Why or why not?

This is not contradictory. Saying that compilers are free to evaluate in any order does not mean that they would evaluate them as left associative half the time. Compilers tend to evaluate arithmetic operators the same way language language designers intended it to be.

What does true multiple inheritance make possible that mix-in inheritance does not?

True inheritance on the other hand, allows the programmer to specify the functionality for an interface that has to be provided by the inheriting classes if it is to be used in that context- which is not seen in mix-in inheritance.

What is a static chain? What is it used for?

Used to point the ARI of parent static by ARI for subprogram. Used the ARI's connects its static ancestors Used for lexical scoping. Connects the static links.

The original designer of C++ once described templates as "a clever kind of macro that obeys the scope, naming, and type rules of C++". How close is the similarity? What can templates do that macros can't? What can macros do that templates can't?

Using templates, a user can pass any data type as a parameter in order to avoid writing the same code for a different type. Templates are type safe in comparison to macros. Macros are also a type name whenever a name is given the compiler will replace that name with the given code. Macros are not type safe, and will only work with one data type. Macros and templates can both be used to simplify the code, and both can be used for the replacement of code. Templates can be used for more than one expansion while macros cannot. Macros, however, will take less time to compile.

Why are binding rules particularly important for languages with dynamic scoping?

When we execute a program their bindings rely on their current processing state, the procedures being followed are called binding rules.

def foo(x) y = 2 bar = Proc.new { print x, "\n" y = 99 } bar.call() print y, "\n" end foo(3) Run the following script and explain it's output. Now comment out the second line (y = 99) and run the script again. Explain what happens. Restate our claim about scoping more carefully and precisely.

When we run the program, it gives us the answers 3 and 99. When we run the program again, we get 3 and a runtime error. The reason why this happens is because the statement y = 99 is not within scope of the print y statement. Y loses scope when the program leaves the code block it is in. The y = 2 extended the scope of y to allow it to be printed using the print y statement, so when its value is changed, its state is changed, and allows the value to be printed.

Give an example in C in which an in-line subroutine may be significantly faster than a functionally equivalent macro. Give another example in which the macro is likely to be faster. (Hint: Think about applicative vs normal order evaluation of arguments)

a) inline int square(int n){ Return n*n;} main(){ Int s, n = 10, i; for(int i = 1; i <= 10; i++){ S = square(i) printf("%d\t", s); } b) #define square(i) i*i main(){ Int i = 10; printf("%d", square(i)); }

What does it mean for a scope to be closed?

allows the definition of one module to be used in other modules. It facilitates abstraction that is used to show the data. When we use type of any particular classes with closed scope then we see that instances are being dispatched by type of the runtime.

What is a binding?

an association between two things that is association of a component of the program with its property

Explain the meaning of the following C declarations: double *a[n]; double (*b)[n]; double (*c[n])(); double (*d())[n];

double *a[n]; - > an array of n pointers of double data type double (*b)[n]; -> A pointer to an array of n pointers of double data type double (*c[n])(); -> an array of n pointers to functions which return doubles. double (*d())[n]; -> A function is declared which returns a pointer to an array of n doubles.

What does it mean for an expression to be referentially transparent?

if the expression gives the same results whenever the expression is evaluated. the value does not depend on the time at which the expression is evaluated.

What is garbage collection?

mechanism in which management of memory allocation and memory release to various objects is performed.

What is internal fragmentation?

occurs when a storage management algorithm allocates a block that is larger than required to hold a given object.

What is external fragmentation?

occurs when the blocks that have been assigned to actie objects are scattered through the heap in such a way that the remaining unused space is composed of multiple blocks. There may be alot of free space, but none may be large enough to satisfy next request.

Consider the following code snippet in C-like syntax: int A[4]; int runit(){ i = 0; A[0] = 2; A[1] = 1; A[2] = 0; A[3] = 2; mys(i,a[i]); print(i, a[0], a[1], a[2],a[3]); mys(a[1],a[i]); print(i, a[0], a[1], a[2],a[3]); return 0; } void mys(int x, int y){ x = x + y;y = x - y;x = x - y; } By value

print(i, a[0], a[1], a[2],a[3]); 1. mys() is void, we passed by value , nothing changes, we get 0,2, 1, 0,2 print(i, a[0], a[1], a[2],a[3]); 1. Again nothing changes. 0,2,1,0,2

What is bootstrapping?

process of compilation in the same language in which it was developed. This technique is used to implement the self hosting feature of compilers.

What was the intermediate form employed by the original AT&T C++ compiler?

the source code was translated into C code, which again was compiled through the C compiler to produce the final assembly language code.

scope resolution operator

used to define the member functions of a class outside the class. (::)

Explain the closest nested scope rule.

when a variable is being declared in the current subroutine and any nested subroutines which are defined internally then at that time must need to get a local same name definition.


Kaugnay na mga set ng pag-aaral

Boardvitals (Intra-Operative Procedures)

View Set

Lesson 2 - Thermal Energy Transfers

View Set

Human Resource Management Test 2 - Quinn Texas State

View Set

(A) C. 1: Scientific Thinking in Psychology:

View Set

OS Chapter 5 Process Synchronization

View Set

Computer Science Fundamental Principles Vocabulary - Unit #8

View Set