Programming and data structure miscellaneous
- Which one of the following are essential features of an object-oriented programming language?
1. Abstraction and encapsulation
2. Strictly - typedness
3. Type-safe property coupled with sub-type rule
4. Polymorphism in the presence of inheritance
-
View Hint View Answer Discuss in Forum
Object Oriented Programming (OPP) is a programming paradigm. The language is object oriented as it use objects. Objects are the data structures that contain data fields and methods together with their interactions. The main features of the Programming techniques are
1. data abstraction
2. encapsulation
3. modularity
4. polymorphism
5. inheritance
Therefore, the essential features are given by statements (i) and (iv).Correct Option: B
Object Oriented Programming (OPP) is a programming paradigm. The language is object oriented as it use objects. Objects are the data structures that contain data fields and methods together with their interactions. The main features of the Programming techniques are
1. data abstraction
2. encapsulation
3. modularity
4. polymorphism
5. inheritance
Therefore, the essential features are given by statements (i) and (iv).
- Consider the following code written in a pass-byreference 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, ib + 5)
print *, ia, ib
end
S1 : 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 run time error on line L1
S3 : On execution the code will generate a run time error on line L2
S4 : The program will print 13 and 8
S5 : The program will print 13 and –2
Exactly which of the following sets of statements is/ are correct ?
-
View Hint View Answer Discuss in Forum
S1 : Yes the compiler will generate a temporary nameless cell & initialize it to 13 and pass to swap.
S2 : No error
S3 : No error
S4 : Program will print 13 and 8
S5 : False. Hence (b) is correct option.Correct Option: B
S1 : Yes the compiler will generate a temporary nameless cell & initialize it to 13 and pass to swap.
S2 : No error
S3 : No error
S4 : Program will print 13 and 8
S5 : False. Hence (b) is correct option.
- Consider these two functions and two statements S1 and S2 about them.
S1 : The transformation form work1 to work2 is valid, i.e., for any program state and input arguments, work2 will compute the same output and have the same effect on program state as work 1.
S2 : All the transformations applied to work1 to get work2 will always improve the performance (i.e., reduce CPU time) of work2 compared to work1
-
View Hint View Answer Discuss in Forum
Both functions work1 & work 2 performs the same task, therefore S1 is true.
In S2 it is asking about improvement in performance i.e. reduction in CPU time. When compared work2 will reduce the CPU time, because in work1 a[i+2] is computed twice but in work2 a[i+2] is computed once and stored in t2, and then t2 is used. When we consider the performance in terms of reduction in CPU time, S2 is correct.Correct Option: D
Both functions work1 & work 2 performs the same task, therefore S1 is true.
In S2 it is asking about improvement in performance i.e. reduction in CPU time. When compared work2 will reduce the CPU time, because in work1 a[i+2] is computed twice but in work2 a[i+2] is computed once and stored in t2, and then t2 is used. When we consider the performance in terms of reduction in CPU time, S2 is correct.
- Consider this code to swap integers and these five statements:
The code
S1 : will generate a compilation error
S2 : may generate a segmentation fault by runtime depending on the arguments passed
S3 : correctly implements the swap procedure for all input pointers referring to integers stored in memory locations accessible to the process
S4 : implements the swap procedure correctly for some but not all valid input pointers
S5 : may add or subtract integers and pointers
-
View Hint View Answer Discuss in Forum
S2 : May generate segmentation fault if value at pointer Px or Py is constant or Px or Py point to a memory location that is invalid. And S4 : May not work for all inputs as arithmetic overflow can occur.
Correct Option: C
S2 : May generate segmentation fault if value at pointer Px or Py is constant or Px or Py point to a memory location that is invalid. And S4 : May not work for all inputs as arithmetic overflow can occur.
- Consider the following C function :
int f (int n)
{static int r = 0;
if (n < = 0) return 1;
if (n > 3)
{r = n;
return f (n – 2) + 2;
}
return f(n – 1) + r;
}
What is the value of f(5)?
-
View Hint View Answer Discuss in Forum
We follow, the following steps to obtain the value of f(5)
f(5)
r = 5f(3) + 2 = 18 ↑ f(2) + 5 = 16 ↑ f(1) + 5 = 11 ↑ f(0) + 5 = 6 ↑ 1
So, f (5) = 1 + 5 + 5 + 5 + 2 = 18Correct Option: D
We follow, the following steps to obtain the value of f(5)
f(5)
r = 5f(3) + 2 = 18 ↑ f(2) + 5 = 16 ↑ f(1) + 5 = 11 ↑ f(0) + 5 = 6 ↑ 1
So, f (5) = 1 + 5 + 5 + 5 + 2 = 18