Consider the program given below, in a block-structured pseudo-language with lexical scoping and nesting of procedures permitted.
Program main; Var ... Procedure A1; Var ... Call A2; End A1 Procedure A2; Var ... Procedure A21; Var ... Call A1; End A21 Call A21; End A21 Call A1; End main.Consider the calling chain : Main->A1->A2->A21->A1 The correct set of activation records along with their access links is given by :
Question 1 Explanation:
Access link is defined as link to activation record of closest lexically enclosing block in program text, so the closest enclosing blocks respectively for A1 ,A2 and A21 are main , main and A2 Since, Activation records are created at procedure entry time and destroyed at procedure exit time. Solution: Link to activation record of closest lexically enclosing block in program text. It depends on the static program text. Here Calling sequence is given as, Main->A1->A2->A21->A1 Now A1,A2 are defined under Main...So A1,A2 Access link are pointed to main A21 is defined under A2 hence its Access link will point to A2 . Reference: http://www.cs.nyu.edu/courses/spring08/G22.2130-001/lectures/lecture-13.html
This solution is contributed by Nitika Bansal
This solution is contributed by Nitika Bansal
Which languages necessarily need heap allocation in the runtime environment?
Those that support recursion
Those that use dynamic scoping
Those that allow dynamic data structures
Those that use global variables
Question 2 Explanation:
Heap allocation is needed for dynamic data structures like tree, linked list, etc.
Which of the following are true?
I. A programming language which does not permit global variables of any kind and has no nesting of procedures/functions, but permits recursion can be implemented with static storage allocation II. Multi-level access link (or display) arrangement is needed to arrange activation records only if the programming language being implemented has nesting of procedures/functions III. Recursion in programming languages cannot be implemented with dynamic storage allocation IV. Nesting of procedures/functions and recursion require a dynamic heap allocation scheme and cannot be implemented with a stack-based allocation scheme for activation records V. Programming languages which permit a function to return a function as its result cannot be implemented with a stack-based storage allocation scheme for activation records
II and V only
I, III and IV only
I, II and V only
II, III and V only
Question 3 Explanation:
I. Recursion cannot be implemented with Static Storage Allocation. Static allocation means, compiler has to decide size for function calls. In case of recursion, it is not possible for compiler to decide as depth of recursion depends on recursion parameter which may be an input from user also. II. Is CORRECT. Programming languages that support nested subroutines also have a field in the call frame that points to the stack frame of the latest activation of the procedure that most closely encapsulates the callee, i.e. the immediate scope of the callee. This is called an access link or static link (as it keeps track of static nesting during dynamic and recursive calls) and provides the routine (as well as any other routines it may invoke) access to the local data of its encapsulating routines at every nesting level. Some architectures, compilers, or optimization cases store one link for each enclosing level (not just the immediately enclosing), so that deeply nested routines that access shallow data do not have to traverse several links; this strategy is often called a "display" [Source: https://en.wikipedia.org/wiki/Call_stack ] III. Recursion CAN be implemented with any kind of Dynamic Storage Allocation scheme. IV. Nesting features are always implemented in a language using STACK and NOT Heap. (See above point II for details) V. Is CORRECT. In stack based allocation scheme, once a function has returned, it is removed from function call stack. Therefore returning a function from a function doesn't look possible.
Which one of the following is NOT performed during compilation?
Dynamic memory allocation
Symbol table management
Question 4 Explanation:
Dynamic memory allocation happens at run time only. Compiler only compiles instructions for dynamic memory allocation like malloc(), calloc().
Which of the following statements are CORRECT?
1) Static allocation of all data areas by a compiler makes it impossible to implement recursion. 2) Automatic garbage collection is essential to implement recursion. 3) Dynamic allocation of activation records is essential to implement recursion. 4) Both heap and stack are essential to implement recursion.
1 and 2 only
2 and 3 only
3 and 4 only
1 and 3 only
Question 5 Explanation:
1) Static allocation of all data areas by a compiler makes it impossible to implement recursion. True, dynamic allocate of memory is required for function call stack as number of calls is not known advance for recursive functions. 2) Automatic garbage collection is essential to implement recursion. False, Automatic garbage collection is not essential. 3) Dynamic allocation of activation records is essential to implement recursion. True, as number of calls or number of activation records is not known advance for recursive functions. 4) Both heap and stack are essential to implement recursion. Heap is not needed for function calls. It is generally used for dynamic memory allocation by user (or programmer). See Memory Layout of C Programs for details.
Consider the following code written in a pass-by-reference language like FORTRAN and these statements about the code.
subroutine swap(ix,iy) it = ix L1 : ix = iy L2 : iy = it end ia = 3 ib = 8 call swap (ia, 1b+5) print *, ia, ib endS1: The compiler will generate code to allocate a temporary nameless cell, initialize it to 13, and pass the address of the cell swap S2: On execution the code will generate a runtime error on line L1 S3: On execution the code will generate a runtime error on line L2 S4: The program will print 13 and 8 S5: The program will print 13 and -2 Exactly the following set of statement(s) is correct:
S1 and S2
S1 and S4
S1 and S5
Question 6 Explanation:
Pass-by-reference nature of FORTRAN makes it pass pointers:
- to bound cells to its subroutines, if they are already present
- to unnamed temporary cells later to be bound, if they are not there
(S1) Because the second argument is an expression, which could be evaluated easily by compiler SDT rules to a value 13, compiler itself will generate code to define and declare an unnamed temporary cell with value 13 and pass it to swap subroutine. [CORRECT] (S2) 'ix' and 'iy' are variables bound to valid mutable cells, thus there is no reason to get a run time error on line L1. [INCORRECT] (S3) Incorrect due to same reason as of S2 [INCORRECT] (S4) Due to the pass-by-reference nature of the language, the cell bound to variable 'ia' will get value 13 and the temporary unnamed cell which was allocated and passed to the swap subroutine will get value 3. Seemingly, cell bound to variable 'ib' is unchanged, thus printing 13 and 8 at the end of this routine. [CORRECT] (S5) Incorrect due to same reason as of S4 [INCORRECT]Hence, answer should be (B) S1 and S4 This explanation has been contributed by Vineet Purswani.
A common property of logic programming languages and functional languages is:
both are procedural languages
both are based on λ-calculus
both are declarative
both use Horn-clauses
The goal of structured programming is to:
have well indented programs
be able to infer the flow of control from the compiled code
be able to infer the flow of control from the program text
avoid the use of GOTO statements
Question 8 Explanation:
The main goal of structured programming is to get an understanding about the flow of control in the given program text. In structure programming various control structures such as switch-case, if-then-else, while, etc. allows a programmer to decode the flow of the program easily.
Choose the best matching between the programming styles in Group 1 and their characteristics in Group 2.
|Q.||Logic||2.||Imperative, abstract data type|
|R.||Object-oriented||3.||Side-effect free, declarative, expression evaluation|
|S.||Imperative||4.||Declarative, clausal representation, theorem proving|
P-2, Q-3, R-4, S-1
P-4, Q-3, R-2, S-1
P-3, Q-4, R-1, S-2
P-3, Q-4, R-2, S-1
Question 9 Explanation:
P: Functional Programming is declarative in nature, involves expression evaluation, & side effect free. Q: Logic is also declarative but involves theorem proving. R: Object oriented is imperative statement based & have abstract (general) data types. S: Imperative: The programs are made giving commands & follows definite procedure & sequence
Which of the following statements is FALSE ?
In statically typed language, each variable in a program has a fixed type
In un-typed languages, values do not have any types
In dynamically typed languages, variables have no types
In all statically typed languages, each variable in a program is associated with values of only a single type during the execution of the program
Question 10 Explanation:
This is an ambiguous question. "Untyped language" has no standard well-formulated definition, thus answering this question would be a bit difficult. (A) Statically typed languages have one type associated to a variable, which is fixed once it has been deduced. Though, types could either be specified while code editing by the coder (Eg. C, Java), or it can be inferred at compile time (Eg. C++11, Haskell). Hence, after compile-time, every variable is bound to one fixed type, making this statement [TRUE] (B) According to one definition, un-type languages store values in form of bits, thus neither variables nor values have any types associated to them. Hence this statement becomes [TRUE] (C) Dynamically typed languages deduce types of values and bind them to the variables storing those values. Hence, values sure have fixed types, but variables don't have fixed types bound to them. Although we can say that binding a type to a variable according to values makes them typeful, but they don't have one fixed type. This statement has some ambiguity. [TRUE/FALSE] (D) Same reason as of (A) [TRUE] Hence, correct answer should be (C) This solution is contributed by Vineet Purswani.
There are 35 questions to complete.
My Personal Notes arrow_drop_up