C++ Questions and Answers Part-12

1. What will be the output of the following C++ code?
#include <stdio.h>
int main()
{
char a = '\012';
printf("%d", a);
return 0;
}
a) Compiler error
b) 12
c) 10
d) empty

Answer: c
Explanation: The value ‘\012’ means the character with value 12 in octal, which is decimal 10.

2. In C++, what is the sign of character data type by default?
a) Signed
b) Unsigned
c) Implementation dependent
d) Unsigned Implementation

Answer: c
Explanation: The standard does not specify if plain char is signed or unsigned. There are three distinct character types according to the standard: char, signed char and unsigned char.

3. Is the size of character literals different in C and C++?
a) Implementation defined
b) Can’t say
c) Yes, they are different
d) No, they are not different

Answer: c
Explanation: In C++, sizeof(‘a’) == sizeof(char) == 1. In C however, sizeof(‘a’) == sizeof(int).

4. Suppose in a hypothetical machine, the size of char is 32 bits. What would sizeof(char) return?
a) 4
b) 1
c) Implementation dependent
d) Machine dependent

Answer: b
Explanation: The standard does NOT require a char to be 8-bits, but does require that sizeof(char) return 1.

5. What constant defined in <climits> header returns the number of bits in a char?
a) CHAR_SIZE
b) SIZE_CHAR
c) BIT_CHAR
d) CHAR_BIT

Answer: d
Explanation: CHAR_BIT is a macro constant defined in <climits> header file which expresses the number of bits in a character object in bytes.

6. Which of the following is not one of the sizes of the floating point types?
a) short float
b) float
c) long double
d) double

Answer: a
Explanation: Floating point types occur in only three sizes-float, long double and double.

7. Which of the following is a valid floating-point literal?
a) f287.333
b) F287.333
c) 287.e2
d) 287.3.e2

Answer: c
Explanation: To make a floating point literal, we should attach a suffix of ‘f’ or ‘F’ and there should not be any blank space.

8. What is the range of the floating point numbers?
a) -3.4E+38 to +3.4E+38
b) -3.4E+38 to +3.4E+34
c) -3.4E+38 to +3.4E+36
d) -3.4E+38 to +3.4E+32

Answer: a
Explanation: This is the defined range of floating type number sin C++. Also range for +ve and -ve side should be same so the answer is -3.4E+38 to +3.4E+38.

9. Which of three sizes of floating point types should be used when extended precision is required?
a) float
b) double
c) long double
d) extended float

Answer: c
Explanation: Float for single precision, double for double precision and long double for extended precision.

10. Which is correct with respect to the size of the data types?
a) char > int < float
b) int < char > float
c) char < int < float
d) char < int < double

Answer: d
Explanation: The char has less bytes than int and int has less bytes than double whereas int and float can potentially have same sizes.