UGC NET JULY 2018 (Computer Science And Applications Paper-II) Question 5-6
Q5. Given below are three implementations of the swap() function in C++.
Which of the following actually swap the contents of the two integer variables p and q ?
(1). (a) only (2). (b) only (3). (c) only (4). (b) and (c) only
Answer : (2). (b) only
Reason : If we execute option (a) code than that code will only pass the value of p and q to the function swap (i.e. called pass by value) which will not exchange the values of p and q also there is no use of address operator.
If we execute option (b) code than that code will pass address locations of p and q to swap function instead of actual values. Now the temp stores the value of a (i.e. address location of p, e.g. 1000) when temp = a; after that we have a=b; which replaces the value of a with new value of b i.e. address of q, e.g. 1002 and b=temp; again replaces the actual value of b with value temp (i.e. address location of p). When the swap function finishes its execution part with exchange the content of variable p and q because it replaces the address locations.
If we try to execute option (c) than the code will work fine but it will not replace the content of p and q. Here inside the swap function there is only exchange of addresses for pointer variable a and b not for p and q, So there is exchange of contents for p and q.
Q6. In Java which of the following statement is/are true ?
S1 : The 'final' keyword applied to a class definition prevents the class from being extended through derivation.
S2 : A class can only inherit one class but can implement multiple interfaces.
S3 : Java permits a class to replace the implementation of a method that it has inherited. It is called method overloading.
Code :
(1). S1 and S2 only (2). S1 and S3 only
(3). S2 and S3 only (4). All of S1,S2 and S3
Answer : (1). S1 and S2 only
Reason : The statement S1 is true, we can not extends a class which having final keyword with class declaration.
The Second statement S2 is also true that Java don't allow us to extend multiple classes at one time but Java allows us to implement multiple interfaces by using implements keyword.
The third statement S3 is false because the ability of a subclass to override a method allows a class to inherit from a superclass and it is called overriding. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides. An overriding method can also return a subtype of the type returned by the overridden method.
Q5. Given below are three implementations of the swap() function in C++.
(a) | (b) | (c) |
void swap (int a,int b) { int temp; temp = a; a = b; b = temp; } int main() { int p = 0, q =1; swap(p,q); } | void swap (int &a,int &b) { int temp; temp = a; a = b; b = temp; } int main() { int p = 0, q =1; swap(p,q); } | void swap (int *a,int *b) { int *temp; temp = a; a = b; b = temp; } int main() { int p = 0, q =1; swap(&p,&q); } |
Which of the following actually swap the contents of the two integer variables p and q ?
(1). (a) only (2). (b) only (3). (c) only (4). (b) and (c) only
Answer : (2). (b) only
Reason : If we execute option (a) code than that code will only pass the value of p and q to the function swap (i.e. called pass by value) which will not exchange the values of p and q also there is no use of address operator.
If we execute option (b) code than that code will pass address locations of p and q to swap function instead of actual values. Now the temp stores the value of a (i.e. address location of p, e.g. 1000) when temp = a; after that we have a=b; which replaces the value of a with new value of b i.e. address of q, e.g. 1002 and b=temp; again replaces the actual value of b with value temp (i.e. address location of p). When the swap function finishes its execution part with exchange the content of variable p and q because it replaces the address locations.
If we try to execute option (c) than the code will work fine but it will not replace the content of p and q. Here inside the swap function there is only exchange of addresses for pointer variable a and b not for p and q, So there is exchange of contents for p and q.
Q6. In Java which of the following statement is/are true ?
S1 : The 'final' keyword applied to a class definition prevents the class from being extended through derivation.
S2 : A class can only inherit one class but can implement multiple interfaces.
S3 : Java permits a class to replace the implementation of a method that it has inherited. It is called method overloading.
Code :
(1). S1 and S2 only (2). S1 and S3 only
(3). S2 and S3 only (4). All of S1,S2 and S3
Answer : (1). S1 and S2 only
Reason : The statement S1 is true, we can not extends a class which having final keyword with class declaration.
The Second statement S2 is also true that Java don't allow us to extend multiple classes at one time but Java allows us to implement multiple interfaces by using implements keyword.
The third statement S3 is false because the ability of a subclass to override a method allows a class to inherit from a superclass and it is called overriding. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides. An overriding method can also return a subtype of the type returned by the overridden method.
0 comments:
Post a Comment