This section on C++ interview questions and answers on “Sizes”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.
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
Answer: b
Clarification: 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
Answer: d
Clarification: 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)
b) c) d)
Answer: b
Clarification: 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
250+ TOP MCQs on Class Hierarchies Introduction and Answers
Answer: a
Clarification: 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)
Answer: c
Clarification: sizeof(char) <= sizeof(wchar_t) <= sizeof(long).
6. What will be the output of the following C++ code?
-
#include
-
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
Answer: c
Clarification: The sum of three numbers are belongs to different number systems, so the result is type casted into integer.
Output:
7. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
int main ( )
-
{
-
static double i;
-
i = 20;
-
cout << sizeof(i);
-
return 0;
-
}
a) 4
b) 2
c) 8
d) garbage
Answer: c
Clarification: 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
-
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
Answer: b
Clarification: 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
-
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
Answer: d
Clarification: 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
-
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
Answer: a
Clarification: Character is 1 byte, integer 4 bytes and float 4 bytes.