Test Series - cpp

Test Number 11/102

Q: 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
Solution: wchar_t and char are used to represent wide character and character.
Q: How do we represent a wide character of the form wchar_t?
A. L’a’
B. l’a’
C. L[a]
D. la
Solution: A wide character is always indicated by immediately preceding the character literal by an L.
Q: In C++, what is the sign of character data type by default?
A. Signed
B. Unsigned
C. Implementation dependent
D. Unsigned Implementation
Solution: 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.
Q: 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
Solution: In C++, sizeof(‘a’) == sizeof(char) == 1. In C however, sizeof(‘a’) == sizeof(int).
Q: 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
Solution: The standard does NOT require a char to be 8-bits, but does require that sizeof(char) return 1.
Q: What constant defined in  header returns the number of bits in a char?
A. CHAR_SIZE
B. SIZE_CHAR
C. BIT_CHAR
D. CHAR_BIT
Solution: CHAR_BIT is a macro constant defined in  header file which expresses the number of bits in a character object in bytes.
Q: The size_t integer type in C++ is?
A. Unsigned integer of at least 64 bits
B. Signed integer of at least 16 bits
C. Unsigned integer of at least 16 bits
D. Signed integer of at least 64 bits
Solution: The size_t type is used to represent the size of an object. Hence, it’s always unsigned. According to the language specification, it is at least 16 bits.
Q: Which of these expressions will return true if the input integer v is a power of two?
A. (v | (v + 1)) == 0;
B. (~v & (v – 1)) == 0;
C. (v | (v – 1)) == 0;
D. (v & (v – 1)) == 0;
Solution: Power of two integers have a single set bit followed by unset bits.
Q: Which of these expressions will isolate the rightmost set bit?
A. x = x & (~x)
B. x = x ^ (~x)
C. x = x & (-x)
D. x = x ^ (-x)
Solution: Negative of a number is stores as 2;s complement in C++, so when you will take AND of x and (-x) the rightmost digit will be preserved.
Q: 0946, 786427373824, ‘x’ and 0X2f are _____ _____ ____ and _____ literals respectively.
A. decimal, character, octal, hexadecimal
B. octal, hexadecimal, character, decimal
C. hexadecimal, octal, decimal, character
D. octal, decimal, character, hexadecimal
Solution: Literal integer constants that begin with 0x or 0X are interpreted as hexadecimal and the ones that begin with 0 as octal. The character literal are written within ”.

You Have Score    /10