Question: 1.Consider the following skeletal C++ program: void fun1(void) { int *pf = new int[10]; //pf is a reference to a dynamically allocated integer array .
1.Consider the following skeletal C++ program:
void fun1(void) { int *pf = new int[10]; //pf is a reference to a dynamically allocated integer array
. . .
}
void fun2(void) { int b;
. . .
}
void main() { int a;
double *pm = new double [5]; //pm is a reference to a dynamically allocated double array
fun1();
delete []pm; //delete the array referenced by pm
fun2();
}
(a) what is the lifetime of variable a, b, pm and pf respectively?
(b) what is the lifetime of the double array allocated in main(), and integer array allocated in fun1() respectively?
2.Assume the following JavaScript program was interpreted using static-scoping rules. What value of x is displayed in function sub1? Under dynamic-scoping rules, what value of x is displayed in function sub1?
var x;
function sub1() {
document.write("x = " + x + "");
}
function sub2() {
var x;
x = 10;
sub1();
}
x = 5;
sub2();
3.Given the following two C++ or Java like for loop,
| for (int j = 0; j < 10; j++) { //code 1 //codes omitted } //more codes here |
| int j; //code 2 for ( j= 0; j < 10; j++) { //codes omitted } //more codes here |
- What is the scope of j in each of above codes respectively?
- If Id display js value after (i.e. outside) the loop, will there be an error?
- Given a Python for loop and a Java/C++ loop below, could you guess what values will be printed out, respectively?
| #Python loop for j in range(10) : #range(10) will generate 0, 1, ,9 j=j+1 print(j) |
| //Java loop for (int j=0; j<10; j++) { j=j+1; System.out.println(j); } |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
