Test Series - cpp

Test Number 10/102

Q: Is bool a fundamental data type in C++?
A. Yes
B. No, it is a typedef of unsigned char
C. No, it is an enum of {false, true}
D. No, it is expanded from macros
Solution: C++ has bool as a fundamental data type.
Q: Find the odd one out.
A. std::vector
B. std::vector
C. std::vector
D. std::vector
Solution: std::vector is a specialized version of vector, which is used for elements of type bool and optimizes for space. It behaves like the unspecialized version of vector and the storage is not necessarily an array of bool values, but the library implementation may optimize storage so that each value is stored in a single bit.
Q: What is the value of the bool?

bool is_int(789.54)
A. True
B. False
C. 1
D. 2
Solution: The given number is a double not an integer, so the function returns 0 which is boolean false.
Q: What happens when a null pointer is converted into bool?
A. an error is flagged
B. bool value evaluates to true
C. bool value evaluates to false
D. the statement is ignored
Solution: A pointer can be implicitly converted to a bool. A nonzero pointer converts to true and zero valued pointer converts to false.
Q: Which of the following statements are false?
A. bool can have two values and can be used to express logical expressions
B. bool cannot be used as the type of the result of the function
C. bool can be converted into integers implicitly
D. a bool value can be used in arithmetic expressions
Solution: Boolean can be used as a return value of a function.
Q: For what values of the expression is an if-statement block not executed?
A. 0 and all negative values
B. 0 and -1
C. 0
D. 0, all negative values, all positive values except 1
Solution: The if-statement block is only not executed when the expression evaluates to 0. its just syntactic sugar for a branch-if-zero instruction.
Q: Which of the two operators ++ and — work for the bool data type in C++?
A. None
B. ++
C. 
D. ++ & —
Solution: Due to the history of using integer values as booleans, if an integer is used as a boolean, then incrementing will mean that whatever its truth value before the operation, it will have a truth-value of true after it. However, it is not possible to predict the result of — given knowledge only of the truth value of x, as it could result in false.
Q: Evaluate the following.

   (false && true) || false || true
A. 0
B. 1
C. 2
D. false
Solution: The given expression is equivalent to
[( false AND True) OR false OR true] This is OR or three values so if any of them will be true then the whole exp will be true and as we have last value as true so the answer of expression will be TRUE.
Q: How many characters are specified in the ASCII scheme?
A. 64
B. 128
C. 256
D. 24
Solution: There are 128 characters defined in the C++ ASCII list.
Q: 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
Solution: Every character constant has an integer value. Also char belongs to the integral type hence arithmetic and logical operations can be performed on them.

You Have Score    /10