Solution:
Yes, agree with the statement. Use of abstraction allows us to concentrate on general ideas rather than on specific manifestations of these ideas. In programming, abstraction refers to what a piece of code does rather that how it is implemented. For example, in C/C++ consider the difference between a .h file (prototype declaration-what the program does)and a .c file (how the program does it, i.e., its implementation).
Solution:
int a; // global variable a
void test() {
cout << 2*a;
}
...
int main() {
a = 2;
test();
a = 3;
test();
...
}
In general, a parameterless abstraction may not perform exactly
the same computation every time it is called if it relies on the
exterior environment for at least one of the values that it uses.
void multiply (int m, int n) {
m = m*n;
cout <<m<< "," <<n<< endl;
}
Suppose the function is called with actual parameters i,j where i=2,j=3. If we are using call-by-value show what is printed when called with:
(a) multiply (i,j) and (b) multiply (i,i).
Now suppose that the parameters could be replaced by call-by-value-result parameters. Repeat parts (a) and (b) and explain the different effect (if any). Assume that the instructions are not executed sequentially.
Solution:
Assuming the instructions are not executed sequentially: Using call-by-value: (a) "6,3" (b) "4,2" Using call-by-value-result: (a) "6,3" (b) "4,4" The different effect is that when calling using call-by-value- result, an alias is set up for the actual parameter, referred to via the formal parameter. So, in part b using call-by-value- result, two aliases are set up for the variable i (one referred to by m and one referred to by n), so when m is changed in the line "m=m*n" this effectively changes the value of i (because of the alias), which (because of the other alias) effectively changes n.
Storage Type Local Static Global ------------------------------------------------------------- Visibility function function file Lifetime function program program Initialized Value not initialized 0 0 Purpose variable used same as local variable used by a single local, but by several function retains value functions
Answer:
On the left hand side of the equals sign. Following is a simple
example.
#include<iostream.h
int x; // global variable
int& setx(); //function declaration
void main()
{ // set x to a value, using
setx()=92; // using function call on the left side
cout <<"\nx=" <<x; //display new value in x
}
int& setx()
{
return x; // returns the value to be modified
}
In this program the function setx() is declared with a reference type, int&, as the return type: int& setx(). The function returns the values x by : return x. Now though it looks strange, you can put a call to this function on the left side of the equals sign: setx()=92. The result is that the variable returned by the function is assigned the value on the right side of the equals sign. The output of the program is x=92.
(b) Suppose a function has the following declaration: void person(char* name, int age); Modify the declaration so that default value for name is unknown and age 20.
Answer: (a)To modify the original argument. (b)void person(char* name="unknown", int age=20);
Answer: Modularity was introduced to support construction of large programs. A module is a large program units that can be implemented as an independent entity. A well-designed module typically has a single purpose and represents a narrow interface to other modules. A module typically makes only a few components visible outside. Such components are said to be exported by the module. Other modules remain hidden inside the module and used for implementing the visible components. Modules are designed to be resuable.
fun sqr(n: int)= n*n;and function call sqr(p+q)with p=2 & q=5
Show the binding and evaluation of the function in case of eager, normal-order and lazy evaluation.
Answer:
In case of eager evaluation: first 'p+q' is evaluated to 7 and formal parameter n is bound to 7. Finally 'n*n' is evaluated as 7x7 yielding 49.
In case of normal-order evaluation: formal parameter n is bound to 'p+q' itself. Each time the value of n is required during the evaluation of 'n*n', the espression 'p+q' is reevaluated. Thus the computation is (2+5)x(2+5)=49.
In case of lazy evaluation: formal parameter n is bound to 'p+q' itself. During the evaluation of 'n*n', the espression 'p+q' is evaluated to 7 once and copied that value for the calculation second 'p+q'.
Answer:
class FahrToCel {
public static void main(String arg[]) {
int fahr, cel;
int lower, upper, step;
lower=0;
upper=300;
step=20;
fahr=lower;
while (fahr <=upper) {
cel=5*(fahr-32/9);
System.out.print(fahr);
System.out.print(" ");
System.out.println(cel);
fahr=fahr+step;
}
}
}