C++ Questions and Answers Part-17

1. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int arr[] = {4, 5, 6, 7};
int *p = (arr + 1);
cout << arr;
return 0;
}
a) 4
b) 5
c) address of arr
d) 7

Answer: c
Explanation: As we counted to print only arr, it will print the address of the array.
Output:
0xbfb1cff

2. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ()
{
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p + 4) = 50;
for (int n = 0; n < 5; n++)
cout << numbers[n] << ",";
return 0;
}
a) 10,20,30,40,50,
b) 1020304050
c) compile error
d) runtime error

Answer: a
Explanation: In this program, we are just assigning a value to the array and printing it and immediately dereferencing it.
Output:
10,20,30,40,50,

3. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int arr[] = {4, 5, 6, 7};
int *p = (arr + 1);
cout << *arr + 9;
return 0;
}
a) 12
b) 5
c) 13
d) error

Answer: c
Explanation: In this program, we are adding the value 9 to the initial value of the array, So it’s printing as 13.
Output:
13

4. Which value can we not assign to reference?
a) integer
b) floating
c) unsigned
d) null

Answer: d
Explanation: If it can be assigned with a null value means, it is a copy of the pointer.

5. Identify the incorrect statement.
a) Reference is the alternate name of the object
b) A reference value once defined can be reassigned
c) A reference value once defined cannot be reassigned
d) Reference is the alternate name of the variable

Answer: b
Explanation: Reference is a thing which points to the valid memory address, so it can’t be redesigned.

6. Which reference modifier is used to define the reference variable?
a) &
b) $
c) #
d) @

Answer: a
Explanation: & aka ‘ampersand’ used to define a reference variable.

7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void swap(int &a, int &b);
int main()
{
int a = 5, b = 10;
swap(a, b);
cout << "In main " << a << b;
return 0;
}
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
cout << "In swap " << a << b;
}
a) In swap 105 In main 105
b) In swap 105 In main 510
c) In swap 510 In main 105
d) In swap 510 In main 510

Answer: a
Explanation: As the function is called by reference i.e. all the changes are done directly into the memories of a and b. Therefore changes made to a and b in swap function is reflected back to main function. Hence the values of a and b in swap as well as in main function is changed.
Output:
In swap 105 In main 105

8. What does a reference provide?
a) Alternate name for the class
b) Alternate name for the variable
c) Alternate name for the pointer
d) Alternate name for the object

Answer: b
Explanation: Because we are pointing memory address using the temp variable.

9. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a = 9;
int & aref = a;
a++;
cout << "The value of a is " << aref;
return 0;
}
a) 9
b) 10
c) error
d) 11

Answer: b
Explanation: The value is declared and it isincrementedrement, so it’s value is 10.

10. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void print (char * a)
{
cout << a << endl;
}
int main ()
{
const char * a = "Hello world";
print(const_cast (a) );
return 0;
}
a) Hello world
b) Hello
c) world
d) compile time error

Answer: a
Explanation: In this program we used the concept of constant casting to cast the variable and printing it.
Output:
Hello world