In: Computer Science
C++ Questions:
1. True/False: an int uses 4 bytes in memory on all systems.
2. True/False: a double typically offers better precision than a float.
3. True/False: The following two statements are equivalent assuming isPair is a variable of type bool:
if (isPair) if (isPair == true)
4. Provide a value we could put in the blank so that the value of x will be 20 after the code snippet.
int x = _____; x *= 4;
5. Provide a value we could put in the blank so that the value of x will be 7 after the code snippet.
int x = _____; x = x % 10;
6. What is the error in the code below:
string slogan = 'Go Giants!';
Solution:
(1)
The answer will be "False"
Explanation:
=>If processor system is 16 bits then size of int = 2 bytes
=>If processor system is 32 bits then size of int = 4 bytes
=>So on the basis of above statements we can say that int can take 2 byte or 4 byte depending upon the processor system.
(2)
The answer will be "true"
Explanation:
=>Double uses more bits after decimal point as compared to float and it gives more precise computation.
(3)
The answer will be "true"
Explanation:
=>In if(isPair), isPair is a boolean variable if value of isPair = 1 then if condition will be true otherwise false.
=>In if(isPair == 1) isPair boolean variable's value will be compared with 1 if the value of isPair variable is 1 then condition isPair == 1 will return true otherwise false.
=>Hence on the basis of above statements we can say that given both expressions are equivalent.
(4)
The answer will be "5".
Explanation:
=>x *= 4; means x = x*4 so when x = 5 value of x = 5*4 means x = 20.
(5)
The answer will be "7"
Explanation:
=>x = x % 10 where % is modulus operator.
=>When x = 7, x = 7 % 10 will imply x = 7.
(6)
The answer will be "Instead of single quotes there should be double quotes"
Explanation:
=>As slogan is of string type so 'Go Giants!' is not correct because in single quote characters are represented not string so there should be double quotes.
I have explained each and every part with the help of statements attached to it.