C++ Programming Questions and Answers – Character Types

This section on C++ interview questions and answers focuses on “Character Types”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ questions on “Character Types” along with answers, explanations and/or solutions:

1. How many characters are specified in the ASCII scheme?
a) 64
b) 128
c) 256
d) 24
View Answer

Answer: b
Explanation: There are 128 characters defined in the C++ ASCII list.

2. Given the variables p, q are of char type and r, s, t are of int type. Select the right statement?

    1. t = (r * s) / (r + s);
    2. t = (p * q) / (r + s);

a) 1 is true but 2 is false
b) 1 is false and 2 is true
c) both 1 and 2 are true
d) both 1 and 2 are false
View Answer

Answer: c
Explanation: Every character constant has an integer value. Also char belongs to the integral type hence arithmetic and logical operations can be performed on them.
advertisement
advertisement

3. Which of the following belongs to the set of character types?
a) char
b) wchar_t
c) only a
d) both wchar_t and char
View Answer

Answer: d
Explanation: wchar_t and char are used to represent wide character and character.

4. What will be the output of the following C++ code?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         char c = 74;
  6.         cout << c;
  7.         return 0;
  8.     }

a) A
b) N
c) J
d) I
View Answer

Answer: c
Explanation: The literal value for 74 is J. So it will be printing J.
advertisement

5. How do we represent a wide character of the form wchar_t?
a) L’a’
b) l’a’
c) L[a]
d) la
View Answer

Answer: a
Explanation: A wide character is always indicated by immediately preceding the character literal by an L.

6. What will be the output of the following C++ code?

advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char a = '\012';
  5.  
  6.         printf("%d", a);
  7.         return 0;
  8.     }

a) Compiler error
b) 12
c) 10
d) Empty
View Answer

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

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

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.

8. 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
View Answer

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

9. 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
View Answer

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

10. 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
View Answer

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.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.