Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

SHORT QUESTIONS

1. What are the design issues for names?


Ans: The following must be considered design concerns for names:
• Is it true that names are case-sensitive?
• Are the language's specific terms reserved words or keywords?

2. In what way are reserved words better than keywords?


Ans: Because the ability to redefine keywords can be confusing, reserved words
are preferred over keywords as a language design choice. There is one
possible issue with reserved words: if the language has a significant amount
of reserved words, the person may have trouble coming up with non-
reserved names.

3. What is the l-value of a variable? What is the r-value?


Ans: L-value:
The assignment operator must have a modifiable operand on the left side,
which is normally a vector.
R-value:
Fetches the value form parameters or constants and must have a modifiable
operand in the right side of an assignment.

4. Define binding and binding time.


Ans: The act of associating properties with names is known as binding. The
moment in the program's life cycle that this relation happens is known as
binding time.

5. After language design and implementation, what are the four times
bindings can take place in a program?
Ans: Compile time, load time, link time, run time.

6. Define static binding and dynamic binding.


Ans: If a static binding happens before run time and stays constant during
program execution, it is called static binding. If a dynamic binding appears
for the first-time during execution or can alter during execution, the
program is said to be dynamic.
7. What are the advantages and disadvantages of implicit declarations?
Ans: Advantages:
The naming conventions are straightforward. The compiler or interpreter
in this case assigns a type to a variable dependent on the variable's syntactic
structure.
Disadvantages:
Implicit declarations can be harmful to reliability because they preclude the
compilation process from finding any typographical and programmer
errors, despite the fact that they are a minor convenience for programmers.

8. What are the advantages and disadvantages of dynamic type binding?


Ans: Advantages:
There are no declared categories and there is a lot of flexibility.
Disadvantages:
It raises the cost of execution, and detecting type errors by the compiler is
difficult.

9. Define static, stack-dynamic, explicit heap-dynamic, and implicit heap


dynamic variables. What are their advantages and disadvantages?
Ans:
(i) Static:
Static variables are those that are attached to memory cells until program
execution begins and remain bound to those memory cells until the
completion of the program.
Advantages:
efficiency in terms of time, subprograms that are time-sensitive
Disadvantages:
No recursion

(ii) Stack-dynamic:
Which its storage bindings are created when their declaration statements
are expanded but whose types are statically related.
Advantages:
maintains memories, recurrence.

Disadvantages:
In comparison to static, stack-dynamic is less effective in terms of running
time. There are no subprograms that are adaptive to the user's background.

(iii) Explicit heap-dynamic:


The programmer's specific run-time instructions assign and deallocate
unnamed (abstract) memory cells.
Advantages:
Storage management that is flexible.
Disadvantages:
less effective, pairing new/delete statements is susceptible to mistakes.

(iv) Implicit heap dynamic variables:


Implicit heap-dynamic parameters are only linked to heap storage as they
are assigned values.
Advantages:
highest competence.
Disadvantages:
cost of maintaining all complex attributes at runtime, Error identification
by the compiler is improved.

10. Define lifetime, scope, static scope, and dynamic scope.


Ans: Lifetime:
when a variable's memory is allotted (when in existence).
Scope:
Variables in the software may be referenced or set in sections.
Static scope:
based on the text of the source program (static ancestors).
Dynamic scope:
The scope is determined by the sequence of calls made during runtime.

11. What is a static ancestor of a subprogram? What is a dynamic ancestor


of a subprogram?
Ans: The static ancestors are those that comprise the subprogram in question
exclusively. And the ancestors that are named to meet the subprogram in
question are known as dynamic ancestors.
12. What is the referencing environment of a statement
Ans: The collection of all variables in the statement that can be seen is called
referencing environment of a statement.

13. What are the advantages and disadvantages of dynamic scoping?


Ans: Advantages:
Only writability is a benefit of dynamic scoping. It's more practical and
flexible than static scoping.
Disadvantages:
• When using dynamic scoping, accessing nonlocal variables takes even
longer than accessing nonlocal variables when using static scoping.
• Since local variables of subprograms are all accessible to all other
executing subprograms, regardless of textual proximity, there is no way to
prevent local variables from being reached by subprograms.
• reluctance to search for nonlocal vector references statically.
PROGRAMMING QUESTIONS AND
NUMERICALS

Q1. Write a C99 function that includes the following sequence of


statements:

x = 21;
int x;
x = 42;

Run the program and explain the results. Rewrite the same code in
C++ and Java and compare the results
Ans: main(void)
{
x = 21;
int x;
x = 42;
}

Result and explanation:


test.c: In function 'main':
test.c:2: error: 'x' undeclared (first use in this
function)
test.c:2: error: (Each undeclared identifier is
reported only Once

C++:
main(void)
{
x = 21;
int x;
x = 42;
}

Compiler error message:


test.cpp: In function 'int main()':
test.cpp:4: error: 'x' was not declared in this scope
Java:
public class test {
public static void main(String[] args){
x = 21;
int x;
x = 42;
}
}

Compiler error message


test.java:3: cannot find symbol
symbol: variable x
location: class PE5
x = 21;
^
1 error

test.c is unable to be compiled by the compiler since the C programming language


employs static type binding through explicit declaration. An object in the program
that lists the variable name and determines its type must exist before it can be
referenced.
While using the C++ and java compilers to compile a program using the same
objects in C++ and Java, similar effects have been found. The explanation for
these compilers' failure to compile the code is the same as for C failure to compile
test.c

Q2: Write three functions in C or C++: one that declares a large array
statically, one that declares the same large array on the stack, and one
that creates the same large array from the heap. Call each of the
subprograms a large number of times (at least 100,000) and output the
time required by each. Explain the results.
Ans:
#include <iostream>
#include <time.h>

void static_array(void)
{
static int my_array1[200000];
}
void stack_array(void)
{
int my_array2[200000];
}

void heap_array(void)
{
int *my_arr3 = (int *) malloc(200000 * sizeof(int));
}

int main (void)


{
int a, b, c;
clock_t start = clock();

for(a=0; a<150000; ++a)


{
static_array();
}

clock_t end = clock();


double time_req_1 = ((double) (end - start)) / CLOCKS_PER_SEC;

printf("static array took %f seconds to execute \n",


time_req_1);

start = clock();

for(a=0; a<150000; ++a)


{
stack_array();
}

end = clock();
double time_req_2 = ((double) (end - start)) / CLOCKS_PER_SEC;

printf("stack array took %f seconds to execute \n", time_req_2);

start = clock();

for(a=0; a<150000; ++a)


{
heap_array();
}
end = clock();
double time_req_3 = ((double) (end - start)) / CLOCKS_PER_SEC;

printf("heap array took %f seconds to execute \n", time_req_3);

Q3: Given the following program of a language called crypt. What is the
output of the program if dynamic scoping is used? What is the output
if static scoping is used? Main is the entry point in the program:
Program Output dynamic Output Static
scoping scoping
procedure main
{ In sub1 X is 31 In sub1 X is 21
integer X=10;
procedure sub1 In sub2 X is 32 In sub2 X is 32
{
X = X + 1; In sub1 X is 21 In sub1 X is 21
print (“In sub1 X is “, X);
X = 5; In main X is 20 In main X is 20
}
procedure sub2
{
integer X = 30;
Call sub1;
X=X+2;
print (“In sub2 X is “, X);
}
X = 20;
Call sub2
Call sub1
print (“In main X is “, X);
}
Q4: Consider the following grammar and remove the direct/indirect left
recursion (if any) with all intermediate steps:

Ans:

A → B | a | CBD
B→ C|b
C → bEF | aEF | cEF
E → BDE | epsilon
F → EF | epsilon
D→ d

You might also like