C++ Programming Questions and Answers – Character Classification

C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Character Classification”.

Q 1. Which function is used to check whether a character is an alphabet?
A. isblank()
B. isalnum()
C. isdigit()
D. isalpha()

Show Answer Answer:-D. isalpha()
Explanation Character classification provides isalpha() function to check whether a character in C++ is an alphabet or not.

Q 2. Which function is used to check whether a character is an alphabet or number?
A. isalpha()
B. isdigit()
C. isalnum()
D. isblank()

Show Answer Answer:-C. isalnum()
Explanation Character classification provides isalnum() function to check whether a character in C++ is alphabet or number.

Q 3. Which function is used to check whether a character is a number?
A. isalpha()
B. isalnum()
C. isdigit()
D. isblank()

Show Answer Answer:-C. isdigit()
Explanation Character classification provides isdigit() function to check whether a character in C++ is number or not.

Q 4. Which function is used to check whether a character is a tab or space?
A. isalpha()
B. isalnum()
C. isdigit()
D. isblank()

Show Answer Answer:-D. isblank()
Explanation Character classification provides isblank() function to check whether a character in C++ is space or tab.

Q 5. Which function is used to check whether a character is tab or space or whitespace control code(\n,\r,etc.)?
A. isspace()
B. isalnum()
C. iscntrl()
D. isblank()

Show Answer Answer:-A. isspace()
Explanation Character classification provides isspace() function to check whether a character in C++ is tab or space or whitespace control code(\n, \r, etc.).

Q 6. Which function is used to check whether a character is tab or a control code?
A. isspace()
B. isalnum()
C. iscntrl()
D. isblank()

Show Answer Answer:-C. iscntrl()
Explanation Character classification provides iscntrl() function to check whether a character in C++ is tab or a control code.

Q 7. Which function is used to check whether a character is printable on console?
A. isprint()
B. isxdigit()
C. iscntrl()
D. ispunct()

Show Answer Answer:-A. isprint()
Explanation Character classification provides isprint() function to check whether a character in C++ is printable on console.

Q 8. Which function is used to check whether a character is hexadecimal?
A. isxdigit()
B. isprint()
C. iscntrl()
D. ispunct()

Show Answer Answer:-A. isxdigit()
Explanation Character classification provides isxdigit() function to check whether a character in C++ is hexadecimal.

Q 9. Which function is used to check whether a character is punctuation mark?
A. isxdigit()
B. isprint()
C. iscntrl()
D. ispunct()

Show Answer Answer:-D. ispunct()
Explanation Character classification provides ispunct() function to check whether a character in C++ is punctuation mark.

Q 10. 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[12] = "Hello World";
	for(int i=0;i<12;i++)
        {
		cout<<(bool)isalpha(arr[i]);
	}
}

A. 111110111110
B. 111111111110
C. 111000111110
D. 111110000000

Show Answer Answer:-A.111110111110
Explanation In this program we are checking whether a character is an alphabet or not so in “Hello World” except space everything is alphabet, therefore, we have 11111011111 but it is followed by a 0 because every string is followed by a null character which is not alphabet, therefore, we have 0 at the of the binary string.

Q 11. 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[12] = "H3ll0 W0r1d";
	for(int i=0;i<12;i++)
        {
		cout<<(bool)isalpha(arr[i]);
	}
	cout<<endl;
	for(int i=0;i<12;i++)
        {
		cout<<(bool)isdigit(arr[i]);
	}
}

A. 000000000000
010010010100

B. 101100100010
010010010111

D. 111111101010
010010000000

D. 101100101010
010010010100

Show Answer Answer:-D. 101100101010 010010010100
Explanation In this program, we are first checking the alphabets in the string then digits in the string so accordingly one can find the answer.

Q 12. 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[12] = "H3ll0\tW0r1d";
	for(int i=0;i<12;i++)
        {
		cout<<(bool)isprint(arr[i]);
	}
}

A. 111000111110
B. 111111111110
C. 111110111110
D. 111110000000

Show Answer Answer:-C. 111110111110
Explanation In this program we are checking the presence of alphabets and digits in the string so accordingly one can find the answer.

Q 13. 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[12] = "H3ll0\tW0r1d";
	for(int i=0;i<12;i++)
        {
		cout<<(bool)iscntrl(arr[i]);
	}
}

A. 111111111110
B. 111000111110
C. 000001000001
D. 111110000000

Show Answer Answer:-C. 000001000001
Explanation In this program we are checking the presence of control codes i.e. \n, \r, \r\n, \t, etc. in the string so accordingly one can find the answer.

Q 14. 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[20] = "\'H3ll0\'";
	for(int i=0;i<8;i++)
        {
		cout<<(bool)ispunct(arr[i]);
	}
}

A. 10000010
B. 111111111110
C. 111000111110
D. 111110000000

Show Answer Answer:-A. 10000010
Explanation In this program we are checking the presence of punctuation characters like quotes(‘, “, etc.) in the string, so ispunct() returns 1 for single quote positions and returns 0 otherwise.

Q 15. 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

Show Answer Answer:-C. 111111000000000000000000000
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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page