Character Types(C++ Programming ) Questions and Answers

Question 1.

What constant defined in <climits> header returns the number of bits in a char?


  1.    CHAR_SIZE
  2.    SIZE_CHAR
  3.    BIT_CHAR
  4.    CHAR_BIT
Explanation:-
Answer: Option D. -> CHAR_BIT

None.



Question 2.

Suppose in a hypothetical machine, the size of char is 32 bits. What would sizeof(char) return?


  1.    4
  2.    1
  3.    Implementation dependent
  4.    Machine dependent
Explanation:-
Answer: Option B. -> 1

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



Question 3.

Is the size of character literals different in C and C++?


  1.    Implementation defined
  2.    Can't say
  3.    Yes, they are different
  4.    No, they are not different
Explanation:-
Answer: Option C. -> Yes, they are different

In C++, sizeof('a') == sizeof(char) == 1. In C however, sizeof('a') == sizeof(int).




Question 4.

In C++, what is the sign of character data type by default?


  1.    Signed
  2.    Unsigned
  3.    Implementation dependent
  4.    None of these
Explanation:-
Answer: Option C. -> Implementation dependent

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.



Question 5.


What is the output of this program?


1.
#include
2.
int main()
3.
{
4.
char a = '\012';
5.
6.
printf("%d", a);
7.
return 0;
8.
}
  1.    Compiler error
  2.    12
  3.    10
  4.    Empty
Explanation:-
Answer: Option C. -> 10

The value '`setminus`012' means the character with value 12 in octal, which is decimal 10.




Question 6.

Which of the following belongs to the set of character types?


  1.    char
  2.    wchar_t
  3.    only a
  4.    both a and b
Explanation:-
Answer: Option D. -> both a and b

wchar_t and char is used to represent wide character and character.



Question 7.

How do we represent a wide character of the form wchar_t?


  1.    L'a'
  2.    l'a'
  3.    L[a]
  4.    la
Explanation:-
Answer: Option A. -> L'a'

A wide character is always indicated by immediately preceding the character literal by an L.



Question 8.


What will be the output of this program?
1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
char c = 74;
6.
cout

  1.    A
  2.    N
  3.    J
  4.    I
Explanation:-
Answer: Option C. -> J

The literal value for 74 is J. So it will be printing J.



Question 9.

Select the right option.
Given the variables p, q are of char type and r, s, t are of int type
1. t = (r * s) / (r + s);
2. t = (p * q) / (r + s);


  1.    1 is true but 2 is false
  2.    1 is false and 2 is true
  3.    both 1 and 2 are true
  4.    both 1 and 2 are false
Explanation:-
Answer: Option C. -> both 1 and 2 are true

Every character constant has an integer value. Also char belongs to the integral type hence arithmetic 

and logical operations can be performed on them.



Question 10.

How many characters are specified in the ASCII scheme?


  1.    64
  2.    128
  3.    256
  4.    none of the mentioned
Explanation:-
Answer: Option B. -> 128

None.