Week 2
Week 2
Week 2
Total Marks : 25
Question 1
Consider the following program. [MCQ, Marks 2]
#include <iostream>
using namespace std;
int add(int n1 = 0) { return n1; }
int add(int n1 = 0, int n2 = 0) { return n1 + n2 - 1;}
int add(int n1 = 0, char c1 = ’a’){ return n1 + c1 - 1; }
int add(char c1 = ’z’, int n1 = 0) { return n1 + c1 ; }
int main() {
int c = add(10, 5);
cout << c << endl;
return 0;
}
a) 15
b) 14
c) 62
Answer: b)
Explanation:
The call add(10, 5) invokes the function int add(int, int) which returns the value 14.
Hence, the correct answer is 14.
1
Question 2
Consider the following program. [MCQ, Mark 2]
#include <iostream>
using namespace std;
#define DOUBLE1(x) x + x
#define DOUBLE2(x) 2 * x
int main() {
int a=2;
cout << DOUBLE1(a++) << " " << DOUBLE2(a++) << endl;
return 0;
}
What will be the output (Consider right to left execution of the cout statement)?
a) 7 4
b) 8 4
c) 4 4
d) 7 6
Answer: a)
Explanation:
In a macro call, the arguments get substituted blindly, and then evaluated. So, the evaluation
of the cout expression will be done as follows:
cout << DOUBLE1(a++) << " " << DOUBLE2(a++) << endl;
cout << a++ + a++ << " " << 2 * a++ << endl;
cout << a++ + a++ << " " << 2 * 2 << endl; //(a becomes 3 after substitution)
cout << 4 + 3 << " " << 4 << endl; //(a becomes 5 after substitution)
cout << 7 << " " << 4 << endl;
2
Question 3
Consider the following code segment. [MCQ, Marks 2]
#include <iostream>
using namespace std;
int& func(int& x) { //LINE-1
return x = x*2;
}
int main() {
int a = 3, b = 4;
int& c = func(a);
cout << a << " " << c << " ";
func(a) = b;
cout << a << " " << c;
return 0;
}
a) 6 6 2 2
b) 6 8 4 2
c) 6 6 4 4
d) 8 6 4 4
Answer: c)
Explanation:
The change in the formal parameter x is reflected on the actual variable a because it is passed
as a reference. However, not as a constant reference, since the formal parameter is modified
within the function. So, first cout statement will print 6 6. The statement int& c = func(a);
requires the return type to be a reference type. This statement will modify the value of a and
c with the value of b. Hence, c) is the correct option.
3
Question 4
Consider the following code segment. [MCQ, Marks 2]
#include <iostream>
using namespace std;
void compute(int x1, int x2, ___________, __________){ //LINE-1
x3 = x1 + x2;
*x4 = x1 * x3;
}
int main(){
int a = 10, b = 20, c = 1, d = 1;
compute(a, b, c, &d); //LINE-2
cout << c << ", ";
cout << d;
return 0;
}
Fill in the blank at LINE-1 so that the program will print 1, 300.
Answer: d)
Explanation:
Since the changes made in x3, *x4 in function compute() need to be reflected in the variables
c, d in main(), these are either pass-by-reference or pass-by-address.
From call at LINE-1, it can be observed that c is passed-by-value and d is passed-by-address.
Thus, the header of compute() must be:
void compute(int x1, int x2, int x3, int* x4)
4
Question 5
Consider the following code segment. [MSQ, Marks 2]
#include <iostream>
using namespace std;
int main(){
const int &x; //LINE-1
x = 10; //LINE-2
cout << x;
return 0;
}
a) 0
b) 10
Answer: c), d)
Explanation:
When a reference variable is declared as const, that should be initialized at the time of
declaration. At LINE-1, x is declared as constant but is not initialized at the time of declaration.
Hence, this line will give compilation error.
Constant variable cannot be reassigned after declaration. Since we try to change the value of
x at LINE-2 (after declaration) which gives another error.
Hence, correct options are c) and d).
5
Question 6
Consider the following code segment. [MSQ, Marks 2]
#include <iostream>
using namespace std;
int main(){
int x = 2, y = 7;
______ int &r = ++x + ++y; //LINE-1
cout << r;
return 0;
}
Fill in the blank at LINE-1 with appropriate option/s such that the output is: 11
a) const
b) volatile
c) static
d) inline
Answer: a)
Explanation:
The result of expression ++x + ++y is assigned to the reference variable r. The result is stored
in a temporary address which is referred by the variable r. Hence, r should be declared as
constant.
Intentionally made as MSQ
6
Question 7
Consider the following code segment. [MSQ, Marks 2]
#include <iostream>
#include<stdlib.h>
using namespace std;
int main(){
char *x = ______________; //LINE-1
cout << *x;
free(x);
return 0;
}
a) (char*)malloc(10*sizeof(char))
b) new char(’a’)
c) new char(97)
d) new char[97]
Answer: b), c)
Explanation:
The pointer variable x should be assigned with character type memory and should be initialized
with ’a’ or ASCII value of ’a’ (i.e. 97) to get the desired output. Hence, the correct option
are b) and c).
7
Question 8
Consider the following code segment. [MSQ, Marks 2]
#include <iostream>
using namespace std;
int main(){
int n = 97;
const char *p = &n; //LINE-1
*p = ’x’; //LINE-2
cout << n;
return 0;
}
a) 97
b) 120
Answer: c), d)
Explanation:
The pointer variable p is declared as pointer to constant char, which restricts any modification
of value at the location where p points to. Since at LINE-1, we attempt to change the value
of the pointee or *p using integer variable reference, it gives a compilation error. Moreover, at
LINE-2, we attempt to change the pointee again with a constant character, which also gives
compilation error.
8
Question 9
Consider the following function prototypes of overloaded function func(). [MCQ, Marks 2]
a) 2, 3, 5
b) 2, 3
c) 2
d) 1, 2
Answer: a)
Explanation:
Prototype-1: since the second argument 3.7f of float type does not matches with char*. Thus,
it does not match with the call.
Prototype-2: since the arguments type float upcasted to type double, it matches with the call.
Prototype-3: since the third formal argument is optional, it is a exact match with the call.
Prototype-4: since the number of arguments for the caller and callee are not the same, it does
not match with the call.
Prototype-5: since the third formal argument is optional and the actual arguments can be
implicitly casted to the formal argument types, it matches with the call.
9
Programming Questions
Question 1
Consider the program below.
#include <iostream>
using namespace std;
#define THRICE(X) ________________ //LINE-1
int main(){
int n;
cin >> n;
cout << THRICE(n+1);
return 0;
}
Public 1
Input: 3
Output: 16
Public 2
Input: 5
Output: 36
Private
Input: 2
Output: 9
Answer:
LINE-1: (X) * X * X
Explanation:
If we observe the test cases, the definition of macro must be
#define THRICE(X) (X) * X * X
THRICE(3+1) = (3 + 1) * 3 + 1 * 3 + 1 = 4*3 + 1*3 + 1 = 16
THRICE(5+1) = (5 + 1) * 5 + 1 * 5 + 1 = 6*5 + 1*5 + 1 = 36
10
Question 2
Consider the following program.
The program must satisfy the sample input and output. Marks: 3
#include <iostream>
using namespace std;
int sqr(_______________) { // LINE-1
return ________; // LINE-2
}
int main() {
int x, y;
cin >> x >> y;
cout << sqr(x + y);
return 0;
}
Public 1
Input: 2 5
Output: 49
Public 2
Input: -8 5
Output: 9
Private
Input: -4 -2
Output: 36
Answer:
LINE-1: const int& x
LINE-2: x * x
Explanation:
The function call is made with an argument which is a constant expression. As the function
is called with a call-by-reference strategy, and actually an expression (x+y) is passed, the
argument should be constant in nature. So, LINE-1 should be filled as const int &x. The
function gives a square value as output. So LINE-2 should be filled as x*x.
11
Question 3
Consider the following program.
The program must satisfy the sample input and output. Marks: 3
#include <iostream>
#include <string>
using namespace std;
________________________________ { // LINE-1
int r = b + 10 * a;
cout << r;
}
int main() {
int p, q;
cin >> p >> q;
print(p, q);
return 0;
}
Public 1
Input: 4 1
Output: 41
Public 2
Input: 5
Output: 50
Private
Input: 7 4
Output: 74
Answer:
LINE-1: void print(int a, int b = 0)
Explanation:
The function header should have two parameters. The second parameter should have the
default value 0. So, LINE-1 can be filled with
void print(int a, int b = 0)
12