Here is a listing of C++ interview questions on “Sizes” along with answers, explanations and/or solutions:
1. The size of an object or a type can be determined using which operator?
a) malloc
b) sizeof
c) malloc
d) calloc
View Answer
Explanation: The sizeof operator gives the size of the object or type.
2. It is guaranteed that a ____ has at least 8 bits and a ____ has at least 16 bits.
a) int, float
b) char, int
c) bool, char
d) char, short
View Answer
Explanation: char types in C++ require atleast 8 bits and short requires atleast 16 bits, whereas for bool only 1 bit suffices and both int and float requires atleast 32 bits.
3. Implementation dependent aspects about an implementation can be found in ____
a) <implementation>
b) <limits>
c) <limit>
d) <numeric>
View Answer
Explanation: The limit header holds the details of the machine dependent details.
4. Size of C++ objects are expressed in terms of multiples of the size of a ____ and the size of a char is _______
a) char, 1
b) int, 1
c) float, 8
d) char, 4
View Answer
Explanation: Each object in C++ is expressed in terms of char type and size of char type is one byte.
5. Identify the incorrect option.
a) 1 <= sizeof(bool) <= sizeof(long)
b) sizeof(float) <= sizeof(double) <= sizeof(long double)
c) sizeof(char) <= sizeof(long) <=sizeof(wchar_t)
d) sizeof(N) = sizeof(signed N) = sizeof(unsigned N)
View Answer
Explanation: sizeof(char) <= sizeof(wchar_t) <= sizeof(long).
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int num = 0x20 + 020 + 20;
cout << sizeof(num)<<'\n';
return 0;
}
a) 2
b) 4
c) Depends on compiler
d) Garbage
View Answer
Explanation: The sum of three numbers are belongs to different number systems, so the result is type casted into integer.
Output:
$ g++ size.cpp $ a.out 4
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ( )
{
static double i;
i = 20;
cout << sizeof(i);
return 0;
}
a) 4
b) 2
c) 8
d) garbage
View Answer
Explanation: The size of the double data type is 8.
$ g++ size1.cpp $ a.out 8
8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int num1 = 10;
float num2 = 20;
cout << sizeof(num1 + num2);
return 0;
}
a) 2
b) 4
c) 8
d) garbage
View Answer
Explanation: In this program, integer is converted into float. Therefore the result of num1 and num2 is float. And it is returning the size of the float.
Output:
$ g++ size2.cpp $ a.out 4
9. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a = 5;
float b;
cout << sizeof(++a + b);
cout << a;
return 0;
}
a) 2 6
b) 4 6
c) 2 5
d) 4 5
View Answer
Explanation: The a as a integer will be converted to float while calculating the size. The value of any variable doesn’t modify inside sizeof operator. Hence value of variable a will remain 5.
Output:
$ g++ size3.cpp $ a.out 4 5
10. What will be the output of the following C++ code (in 32-bit systems)?
#include <iostream>
using namespace std;
int main()
{
cout << sizeof(char);
cout << sizeof(int);
cout << sizeof(float);
return 0;
}
a) 1 4 4
b) 1 4 8
c) 1 8 8
d) 1 8 2
View Answer
Explanation: Character is 1 byte, integer 4 bytes and float 4 bytes.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Apply for C++ Internship
- Check C++ Books
- Check Programming Books