C++ Interview Questions – Data Types
1. [Asked in Infosys] What are data types in C++?
Answer:
Data types in C++ define the type of data a variable can store. C++ supports primitive, derived, and user-defined data types.
Categories of Data Types:
- Primitive Data Types โ
int,float,double,char,bool - Derived Data Types โ
array,pointer,reference - User-Defined Data Types โ
struct,class,enum,union
Example:
int num = 10;
float price = 9.99;
char grade = 'A';
bool isAvailable = true;
2. [Asked in TCS] What is the difference between int, float, and double in C++?
Answer:
| Data Type | Size | Precision | Example |
|---|---|---|---|
int | 4 bytes | No decimal | int a = 10; |
float | 4 bytes | ~6 decimal places | float pi = 3.1415; |
double | 8 bytes | ~15 decimal places | double pi = 3.1415926535; |
Example:
int a = 5;
float b = 5.12f;
double c = 3.1415926535;
Note: Use double for high precision calculations.
3. [Asked in Cognizant] What is the difference between char and string in C++?
Answer:
| Feature | char | string |
|---|---|---|
| Stores | Single character | Sequence of characters |
| Header File | None | <string> |
| Example | char ch = 'A'; | string name = "Alice"; |
Example:
#include <iostream>
#include <string>
using namespace std;
int main() {
char letter = 'A';
string name = "Alice";
cout << "Letter: " << letter << ", Name: " << name;
return 0;
}
Output:
Letter: A, Name: Alice
4. [Asked in Wipro] What is the bool data type in C++?
Answer:bool is a boolean data type that stores true (1) or false (0).
Example:
bool isPassed = true;
bool isFailed = false;
cout << isPassed; // Output: 1
cout << isFailed; // Output: 0
Use Case: Boolean values are commonly used in conditional statements and loops.
5. [Asked in IBM] What are derived data types in C++?
Answer:
Derived data types are created using primitive types.
Types of Derived Data Types:
- Arrays โ Collection of elements (
int arr[5];) - Pointers โ Store memory addresses (
int *ptr;) - References โ Alias for a variable (
int &ref = num;)
Example of Derived Data Types:
int arr[3] = {1, 2, 3}; // Array
int *ptr = &arr[0]; // Pointer
int &ref = arr[1]; // Reference
6. [Asked in Deloitte] What is the difference between signed and unsigned integers in C++?
Answer:
| Type | Size | Value Range |
|---|---|---|
signed int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
unsigned int | 4 bytes | 0 to 4,294,967,295 |
Example:
signed int x = -10;
unsigned int y = 100;
Use unsigned integers when negative values are not required (e.g., age, count).
7. [Asked in Amazon] What is the difference between enum and const in C++?
Answer:
| Feature | enum (Enumeration) | const (Constant) |
|---|---|---|
| Purpose | Defines named constants | Declares constant values |
| Memory Usage | Uses integer representation | Direct value storage |
| Example | enum Color {Red, Blue, Green}; | const int PI = 3.14; |
Example Using enum:
enum Color {Red, Blue, Green};
Color c = Blue;
cout << c; // Output: 1
Example Using const:
const int pi = 3.14;
Use enum for grouping related constants, const for defining fixed values.
8. [Asked in Microsoft] What is the auto keyword in C++?
Answer:
The auto keyword automatically detects the variable’s data type at compile time.
Example:
auto num = 10; // int
auto pi = 3.14; // double
auto name = "Alice"; // const char*
Use auto to simplify variable declaration and improve readability.
9. [Asked in Google] What is nullptr in C++? How is it different from NULL?
Answer:
nullptr(C++11 and later) โ A type-safe null pointer.NULL(C-style) โ Typically defined as0and can cause ambiguity in function overloading.
Example:
int *ptr1 = NULL; // C-style
int *ptr2 = nullptr; // Modern C++
Use nullptr in modern C++ for better type safety.
10. [Asked in Flipkart] What is the size of different data types in C++?
Answer:
| Data Type | Size (in bytes) |
|---|---|
char | 1 |
int | 4 |
float | 4 |
double | 8 |
bool | 1 |
void | 0 |
Example:
cout << "Size of int: " << sizeof(int) << " bytes";
cout << "Size of double: " << sizeof(double) << " bytes";
Output (depends on system):
Size of int: 4 bytes
Size of double: 8 bytes
Use sizeof() to determine the size of a data type dynamically.
