Vous êtes sur la page 1sur 9

Model 4

C++, Python and PHP


1. Consider the following C function:
int f(int n)
{
static int i = 1;
if(n >= 5) return n;
n = n+i;
i++;
return f(n);
}
The value returned by f(1) is
(A) 5 (B) 6 (C) 7 (D) 8

Answer: (C)

Explanation: Since i is static, first line of f() is executed only once.


Execution of f(1)
i=1
n=2
i=2
Call f(2)
i=2
n=4
i=3
Call f(4)
i=3
n=7
i=4
Call f(7)
Explanation:
since n >= 5 return n(7) with return value = 7
Option (C) is correct.

2 What is the return value of f(p, p) if the value of p is initialized to 5 before


the call? Note that the first parameter is passed by reference, whereas the
second parameter is passed by value.
int f(int &x, int c) {
c = c - 1;
if (c == 0) return 1;
x = x + 1;
return f(x, c) * x;
}
(A) 3024
(B) 6561
(C) 55440
(D) 161051
Answer (B)
Explanation:
Since c is passed by value and x is passed by reference, all functions will have
same copy of x, but different copies of c.
f(5, 5) = f(x, 4)*x = f(x, 3)*x*x = f(x, 2)*x*x*x = f(x, 1)*x*x*x*x = 1*x*x*x*x =
x^4
Since x is incremented in every function call, it becomes 9 after f(x, 2) call. So the
value of expression x^4 becomes 9^4 which is 6561.

3. If class A is friend of class B and if class B is friend of class C, which of the


following is true?
A. Class C is friend of Class A
B. Class A is friend of class C
C. Class A and Class C do not have a friend relationships
D. None of the above

Answer:B

4. Which of the following type of class allows only one object of it to be created?
A. Virtual class B. Abstract class
C. Singleton class D. Friend class

Answer:c

5. Why reference is not same as a pointer?


A. A reference can never be null.
B. A reference once established cannot be changed.
C. Reference doesn't need an explicit dereferencing mechanism.
D. All of the above.
Answer:D

6. How Late binding is implemented in C++?


A. Using C++ tables
B. Using Virtual tables
C. Using Indexed virtual tables
D. Using polymorphic tables

Answer:B

7. Which of the following cannot be used with the keyword virtual?


A.class
B. member functions

C. constructor D. destructor
Answer:c

8. When does the void pointer can be dereferenced?


A. when it doesn't point to any value
B. when it cast to another type of object
C. using delete keyword
D. none of the mentioned

Answer : B

9. Which of the following is true when we apply &(addressof) operator to a


reference variable?
A. The address of the object pointed by the reference is returned.
B. The address of the reference is returned
C. Compiler issues an error when we try to get the address of a reference variable.
D. Compiler issues a warning when we try to get the address of a reference variable.

Answer.A

10. Which of the following member is not recommended in a header file?


A. Type definitions(typedefs) B. Class definitions
C. Function definitions D. Template definitions

Answer.C

11. What is the output of the following program?


from math import *
a = 2.13
b = 3.7777
c = -3.12Q
print(int(a), floor(b), ceil(c), fabs(c))
a) 2 3 -4 3
b) 2 3 -3 3.12
c) 2 4 -3 3
d) 2 3 -4 3.12

Ans. (b)

Explanation:
i. int() returns the integer value of a number, int(2.13) = 2.
ii. floor() returns the largest integer lesser or equal to the number, floor(3.777) = 3.
Iii.ceil() returns smallest integer greater or equal to the number, ceil(-3.12) = -3.
Iv.fabs() return the modulus of the number, thus fabs(-3.12) = 3.12.

12. What is the output of the following program?


def REVERSE(L):
L.reverse()
return(L)
def YKNJS(L):
List = list()
List.extend(REVERSE(L))
print(List)

L = [1, 3.1, 5.31, 7.531]


YKNJS(L)
a) [1, 3.1, 5.31, 7.531]
b) [7.531, 5.31, 3.1, 1]
c) IndexError
d) AttributeError: ‘NoneType’ object has no attribute ‘REVERSE’
Ans. (b)
Explanation:
i.REVERSE() reverses the list and returns it.
ii.YKNJS() adds reverse of a list L to the empty list List. L = [1, 3.1, 5.31, 7.531],
gets reversed and becomes [7.531, 5.31, 3.1, 1].

13. What is the output of the following program?


data = [2, 3, 9]
temp = [[x for x in[data]] for x in range(3)]
print (temp)
a) [[[2, 3, 9]], [[2, 3, 9]], [[2, 3, 9]]]
b) [[2, 3, 9], [2, 3, 9], [2, 3, 9]]
c) [[[2, 3, 9]], [[2, 3, 9]]]
d) None of these
Ans. (a)
Explanation: [x for x in[data] returns a new list copying the values in the list data
and the outer for statement prints the newly created list 3 times.

14. What is the output of the following program?


my_tuple = (1, 2, 3, 4)
my_tuple.append( (5, 6, 7) )
print len(my_tuple)

Answer: Error !
A.1 B.3 C.5 D.Error
Explanation:
Tuples are immutable and don’t have an append method as in case of Lists.Hence
an error is thrown in this case.
15. What is the value of L at the end of execution of the following program?
L = [2e-04, 'a', False, 87]
T = (6.22, 'boy', True, 554)
for i in range(len(L)):
if L[i]:
L[i] = L[i] + T[i]
else:
T[i] = L[i] + T[i]
break
a) [6.222e-04, ‘aboy’, True, 641]
b) [6.2202, ‘aboy’, 1, 641]
c) [6.2202, ‘aboy’, True, 87]
d) [6.2202, ‘aboy’, False, 87]

Ans. (d)

Explanation:
i.The for loop will run for i = 0 to i = 3, i.e. 4 times(len(L) = 4). 2e-04 is same as
0.0002, thus L[i] = 6.22 + 0.0002 = 6.2202.
ii.String addition will result in concatenation, ‘a’ + ‘boy’ = ‘aboy’. False + True is
True, it’ll return the integer value of 1.
iii.As tuples are immutable, the code will end with TypeError, but elements of L
will be updated.

16. <?php

$s= "Hello Welcome!";


echo $s. "\n";
echo chop($s, "e!");

?>

Output:
A. Hello Welcome!
Hello Welcom

B. Hello Welcome!
Hello Welcome

C. Hello Welcome!
Hello Welcome!

D.None
Answer.A

17.What is the output?


<?php

echo (abs(-6.4);

?>

A.6.4 B.-6.4 C.6 D.-6

Answer A. 6.4

18. <?php
class GeeksforGeeks
{
public $Geek_name = "GeeksforGeeks";

// Constructor is being implemented.


public function __construct($Geek_name)
{
$this->Geek_name = $Geek_name;
}
}

$Geek = new GeeksforGeeks("GeeksforGeeks");


echo $Geek->Geek_name;
?>

A. GeeksforGeeks B.Geeks C.forGeeks D.None

Answer.A

19. <?php

$arr = array (10, 30, 50,);


foreach ($arr as $val) {
echo "$val \n";
}
$arr = array ("Ram", "Laxman", "Sita");
foreach ($arr as $val) {
echo "$val \n";
}

?>

Output:
A.10
30
50
Ram
Laxman
Sita

B.10
30
50
Ram
Sita
Laxman

C.10
50
30
Ram
Laxman
Sita

D.None

Answer:A

20.
<?php
$var1 = 15;
$var2 =
30;
$sum = $var1
+
$var2;

// "\n" for new line


echo $sum, "\n";

$sum1 = $var1 + $var2;


echo $sum1;
?>
Output:
A.45
45

B.45
50
C.50
45
D.50
50

Answer:A

Vous aimerez peut-être aussi