C++ Programming Questions and Answers – Sizes

This section on 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), 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
View Answer

Answer: b
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

Answer: d
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

Answer: b
Explanation: The limit header holds the details of the machine dependent details.
advertisement
advertisement

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

Answer: a
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

Answer: c
Explanation: sizeof(char) <= sizeof(wchar_t) <= sizeof(long).
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int num = 0x20 + 020 + 20;
  6.         cout << sizeof(num)<<'\n';
  7.         return 0;
  8.     }

a) 2
b) 4
c) Depends on compiler
d) Garbage
View Answer

Answer: c
Explanation: The sum of three numbers are belongs to different number systems, so the result is type casted into integer.
Output:

advertisement
$ g++ size.cpp
$ a.out
4

7. What will be the output of the following C++ code?

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main ( )
  4.     {
  5.         static double i;
  6.         i = 20;
  7.         cout << sizeof(i);
  8.         return 0;
  9.     }

a) 4
b) 2
c) 8
d) garbage
View Answer

Answer: c
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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int num1 = 10;
  6.         float num2 = 20;
  7.         cout << sizeof(num1 + num2);
  8.         return 0;
  9.     }

a) 2
b) 4
c) 8
d) garbage
View Answer

Answer: b
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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int a = 5;
  6.         float b;
  7.         cout << sizeof(++a + b);
  8.         cout << a;
  9.         return 0;
  10.     }

a) 2 6
b) 4 6
c) 2 5
d) 4 5
View Answer

Answer: d
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)?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         cout << sizeof(char);
  6.         cout << sizeof(int);
  7.         cout << sizeof(float);
  8.         return 0;
  9.     }

a) 1 4 4
b) 1 4 8
c) 1 8 8
d) 1 8 2
View Answer

Answer: a
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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.