C++ Questions and Answers Part-14

1. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
enum cat
{
temp = 7
};
int main()
{
int age = 14;
age /= temp;
cout << "If you were cat, you would be " << age << endl;
return 0;
}
a) If you were cat, you would be 5
b) If you were cat, you would be 2
c) If you were cat, you would be 7
d) If you were cat, you would be 9

Answer: b
Explanation: The age will be divided by using compound assignment operator and so it will return the age of the cat according to your age.

2. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
enum test
{
A = 32, B, C
};
int main()
{
cout << A << B<< C;
return 0;
}
a) 323334
b) 323232
c) 323130
d) 323134

Answer: a
Explanation: If we not assigned any value to enum variable means, then the next number to initialized number will be allocated to the variable.
Output:
323334

3. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
enum colour
{
green, red, blue, white, yellow, pink
};
int main()
{
cout << green<< red<< blue<< white<< yellow<< pink;
return 0;
}
a) 012345
b) 123456
c) compile time error
d) runtime error

Answer: a
Explanation: The enumerator values start from zero if it is unassigned.
Output:
012345

4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
enum channel {star, sony, zee};
enum symbol {hash, star};
int i = 0;
for (i = star; i <= zee; i++)
{
printf("%d ", i);
}
return 0;
}
a) 012
b) 123
c) compile time error
d) runtime error

Answer: c
Explanation: Enumeration variable ‘star’ appears two times in main() which causes the error. An enumaration constant must be unique within the scope.

5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int i;
enum month
{
JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
};
for (i = MAR; i <= NOV; i++)
cout << i;
return 0;
}
a) 01234567891011
b) 123456789101112
c) 34567891011
d) 123456789

Answer: c
Explanation: We are getting the values from march to november and printing its concern number.

6. What does the following statement mean?
int (*fp)(char*)
a) pointer to a pointer
b) pointer to an array of chars
c) pointer to function taking a char* argument and returns an int
d) function taking a char* argument and returning a pointer to int

Answer: c
Explanation: The (*fn) represents a pointer to a function and char* as arguments and returning int from the function. So according to that, the above syntax represents a pointer to a function taking a char* as an argument and returning int.

7. The operator used for dereferencing or indirection is ____
a) *
b) &
c) ->
d) ->>

Answer: a
Explanation: * is used as dereferencing operator, used to read value stored at the pointed address.

8. Choose the right option.
string* x, y;
a) x is a pointer to a string, y is a string
b) y is a pointer to a string, x is a string
c) both x and y are pointers to string types
d) y is a pointer to a string

Answer: a
Explanation: * is to be grouped with the variables, not the data types.

9. Which one of the following is not a possible state for a pointer.
a) hold the address of the specific object
b) point one past the end of an object
c) zero
d) point to a type

Answer: d
Explanation: A pointer can be in only 3 states a, b and c.

10. Which of the following is illegal?
a) int *ip;
b) string s, *sp = 0
c) int i; double* dp = & i;
d) int *pi = 0;

Answer: c
Explanation: dp is initialized int value of i.