C++ Questions and Answers Part-22

1. What will be the output of the following C++ code?
#include <iostream>
#include <cctype>
using namespace std;
int main(int argc, char const *argv[])
{
char arr[27] = "abcdefghijklmnopqrstuvwxyz";
for(int i=0;i< 27;i++)
{
cout<<(bool)isxdigit(arr[i]);
}
}
a) 111001100011110000000111100
b) 101010101010101001010101010
c) 111111000000000000000000000
d) 111111111000001111011110111

Answer: c
Explanation: In this program, we are checking the presence of hexadecimal characters in the string and as only a, b, c, d, e and f are used as hexadecimal characters therefore only first bits are 1 and others are 0.

2. Which operator is having the right to left associativity in the following?
a) Array subscripting
b) Function call
c) Addition and subtraction
d) Type cast

Answer: d
Explanation: There are many rights to left associativity operators in C++, which means they are evaluation is done from right to left. Type Cast is one of them.

3. Which operator is having the highest precedence?
a) postfix
b) unary
c) shift
d) equality

Answer: a
Explanation: The operator which is having the highest precedence is postfix and lowest is equality.

4. What is this operator called ?:?
a) conditional
b) relational
c) casting operator
d) unrelational

Answer: a
Explanation: In this operator, if the condition is true means, it will return the first operator, otherwise second operator.

5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a;
a = 5 + 3 * 5;
cout << a;
return 0;
}
a) 35
b) 20
c) 25
d) 30

Answer: b
Explanation: Because the * operator is having highest precedence, So it is executed first and then the + operator will be executed.
Output:
20

6. What is the use of dynamic_cast operator?
a) it converts virtual base class to derived class
b) it converts the virtual base object to derived objects
c) it will convert the operator based on precedence
d) it converts the virtual base object to derived class

Answer: a
Explanation: Because the dynamic_cast operator is used to convert from base class to derived class.

7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 6, c, d;
c = a, b;
d = (a, b);
cout << c << ' ' << d;
return 0;
}
a) 5 6
b) 6 5
c) 6 7
d) 6 8

Answer: a
Explanation: It is a separator here. In C, the value a is stored in c and in d the value b is stored in d because of the bracket.
Output:
5 6

8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int i, j;
j = 10;
i = (j++, j + 100, 999 + j);
cout << i;
return 0;
}
a) 1000
b) 11
c) 1010
d) 1001

Answer: c
Explanation: j starts with the value 10. j is then incremented to 11. Next, j is added to 100. Finally, j (still containing 11) is added to 999 which yields the result 1010.
Output:
1010

9. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ()
{
int x, y;
x = 5;
y = ++x * ++x;
cout << x << y;
x = 5;
y = x++ * ++x;
cout << x << y;
return 0;
}
a) 749735
b) 736749
c) 367497
d) 367597

Answer: a
Explanation: Because of the precedence the pre-increment and post increment operator, we got the output as 749736.
Output:
749735

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

Answer: a
Explanation: Here the condition is false on conditional operator, so the b value is assigned to c.
Output:
6