Uploaded by sh3mom _z

278 assignment

advertisement
Q1) Consider the following BNF:
<ID> →<ID> <Letter>
| <ID> <Digit>
| <Letter>
<Letter>→ A|B|….|Z|a|b|….|z
<Digit>→ 0|1|…….|9
a. What are the terminals and non-terminals?
Terminals :A-Z,a-z/0-9
Non-terminals:<ID>,<Letter>,<Digit>
b. Convert this BNF to Extended BNF.
<ID>→ A|B|….|Z|a|b|….|z {( A|B|….|Z|a|b|….|z/0|1|…….|9)}
c. Give the parse tree and left most derivation for the following input: Sal4.
Q2) Consider the following C code:
int w;
void main()
{
int x;
fun();
}
void fun()
{
static int y;
int z;
}
Which variables are under life time in function main and fun?
w
x
y
z
Main
yes
yes
yes
No
fun
Yes
Yes
yes
yes
Q3) What is the address of the element [3][5] in the following array:
rec array[20][10];
Where the rec is union as follows:
union rec{
int x;
float y;
double z;
}
Note: the offset is 100, int size is 2 bytes, the float is 4 bytes and the double is 8 bytes.
Q4) What is the output of the following code:
void main()
{
int a=2, b=3, *c=&a, *d=&b;
int z= *c;
*c= *d;
*d= z;
cout<<a<<" "<<b;}
solution:3 2 (because we do swapping by addresses and pointers. )
Q5) Consider the array A and slice B:
35365
A
50887
63728
56787
B
088
372
678
a. Write the R and Python statements which use the array A and produce the slice B.
R statement
Python statement
b. What is the content of B in the following statement by assuming R and Python languages?
B= A [1:3 , 2:4]
R language
Q6) What is the output of the following python program:
a=6
b=2
def fun1():
b=3
a=9
def fun2():
global a
Python Language
nonlocal b
b=a/b
a=a-b
fun2()
print(a,b)
fun1()
print(a,b)
solution:
9 2.0
4.0 2
Q7) Consider the following JavaScript program:
function sub1() {
var c, d, z;
function sub2() {
var b, a, x;
...
}
...
}
function sub3() {
var a, b, d;
function sub4() {
var z, a, c;
...
}
...
}
List all the variables; along with the program units where they are declared assuming static
scoping is used.
sub1
sub2
sub3
sub4
Q8) What is the output of the following Java code?
public class test {
public static void main(String[] args) {
try{
int a[]= new int [3];
int x=0,y=4;
a[4]=0;
y=y/x;
}catch(IndexOutOfBoundsException e){
System.out.println("Message 1…");
}
catch(ArithmeticException e){
System.out.println("Message 2…");
}
finally{
System.out.println("Message 3…");
}
System.out.println("Message 4…");
}}
Solution:
Message 1…(because of the error that happened when we want to access the index 4 and it’s not
in a array.)
Message 3…(this shown because we are in the end block of try-catch called finally and it’s
execute when try and catch finished .)
Message 4…(normal command that been executed in the main fun after the try-catch block )
Q9) Assume the following JavaScript program was interpreted:
a. Under Static-scoping rules, what value of x and y are displayed in function sub1?
b. Under dynamic-scoping rules, what value of x and y are displayed in function sub1?
marks]
var x,y;
function sub1() {
document.write(x);
document.write(y);
}
function sub2() {
var x;
x = 3;
sub1();
[2
}
x = 1;
y=2;
sub2();
Q10) Consider the following JavaScript program:
[5
marks]
var a, b, c;
function sub1() {
var c, d, z;
function sub2() {
var b, a, x;
...
}
...
}
function sub3() {
var a, b, d;
function sub4() {
var z, a, c;
...
}
...
}
List all the variables; along with the program units where they are declared assuming dynamic
scoping is used.
main call sub1 , sub1 call sub2, sub2 call sub3 and sub3 call sub4
Download