C++ Questions and Answers Part-21

1. 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()

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

2. Which function is used to check whether a character is tab or a control code?
a) isspace()
b) isalnum()
c) iscntrl()
d) isblank()

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

3. Which function is used to check whether a character is printable on console?
a) isxdigit()
b) isprint()
c) iscntrl()
d) ispunct()

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

4. Which function is used to check whether a character is hexadecimal?
a) isxdigit()
b) isprint()
c) iscntrl()
d) ispunct()

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

5. Which function is used to check whether a character is punctuation mark?
a) isxdigit()
b) isprint()
c) iscntrl()
d) ispunct()

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

6. 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

Answer: a
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.

7. 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
c) 111111101010
010010000000
d) 101100101010
010010010100

Answer: d
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.

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

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

9. 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) 000001000001
c) 111000111110
d) 111110000000

Answer: b
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.

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[20] = "\'H3ll0\'";
for(int i=0;i< 8;i++)
{
cout<<(bool)ispunct(arr[i]);
}
}
a) 10000010
b) 111111111110
c) 111000111110
d) 111110000000

Answer: a
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.