C++ interview questions and answers focuses 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)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.
C++ interview questions on “Sizes” along with answers, explanations and/or solutions:
Q 1. The size of an object or a type can be determined using which operator?
A. malloc
B. sizeof
C. malloc
D. calloc
Show Answer
Answer:-B. sizeofExplanation
The sizeof operator gives the size of the object or type.Q 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
Show Answer
Answer:-D. char, shortExplanation
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.Q 3. Implementation dependent aspects about an implementation can be found in ____
A. <implementation>
B. <limits>
C. <limit>
D. <numeric>
Show Answer
Answer:-B.Explanation
The limit header holds the details of the machine dependent details.Q 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
Show Answer
Answer:-A. char, 1Explanation
Each object in C++ is expressed in terms of char type and size of char type is one byte.Q 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)
Show Answer
Answer:-C. sizeof(char) <= sizeof(long) <=sizeof(wchar_t)Explanation
sizeof(char) <= sizeof(wchar_t) <= sizeof(long).Q 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. Garbage
D. Depends on compiler
Show Answer
Answer:-D. Depends on compilerExplanation
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 4Q 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
Show Answer
Answer:-C. 8Explanation
The size of the double data type is 8. $ g++ size1.cpp $ a.out 8Q 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
Show Answer
Answer:-B. 4Explanation
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 4Q 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. 2 6
C. 4 5
D. 4 6
Show Answer
Answer:- D. 4 5Explanation
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 5Q 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
Leave a Reply