C++ Data Types, Variables & Keywords Interview Questions

C++ Data Types, Variables & Keywords

What are the fundamental data types in C++?
C++ has several fundamental data types: int (integers), float (single-precision floating point), double (double-precision floating point), char (characters), bool (boolean), and void (typeless).
What is the difference between float and double in C++?
float is a single-precision floating point type (typically 4 bytes, 7 decimal digits precision). double is a double-precision floating point type (typically 8 bytes, 15-16 decimal digits precision). Double has higher precision and range.
What is the bool data type in C++?
bool is a boolean data type that can hold only two values: true (1) or false (0). It occupies 1 byte of memory.
What is the void data type in C++?
void is a special data type that represents the absence of type. It's used for functions that don't return a value, as a generic pointer type (void*), and in function parameters to indicate no parameters.
What are modifiers in C++ data types?
Modifiers alter the meaning of basic data types: signed, unsigned, short, long, long long. Example: unsigned int, long double.
What is the difference between signed and unsigned data types?
signed data types can represent both positive and negative values (default for most types). unsigned data types can represent only non-negative values (zero and positive), effectively doubling the positive range.
What are derived data types in C++?
Derived data types are created from fundamental types: arrays, pointers, references, functions, and user-defined types like structures, unions, classes, and enumerations.
What is the auto keyword in C++11 and later?
auto is a type specifier that automatically deduces the data type of a variable from its initializer. It simplifies code and is especially useful with complex types like iterators.
What is the difference between declaration and definition of a variable?
Declaration introduces a variable's name and type without allocating memory. Definition declares the variable AND allocates memory for it. A variable can be declared multiple times but defined only once.
What are variable scope rules in C++?
C++ has three variable scopes: local (within a function/block), global (outside all functions), and class (within a class). Local variables hide global variables with the same name.
What is the difference between static and automatic variables?
Automatic variables (local variables) are created when the function is called and destroyed when it returns. Static variables persist for the program's lifetime, retaining their value between function calls.
What are constants in C++ and how are they declared?
Constants are fixed values that cannot be modified. Declared using: const keyword (compile-time constant), constexpr (C++11, compile-time constant), or #define preprocessor macro.
What is the volatile keyword in C++?
volatile tells the compiler that a variable's value may change at any time without any action being taken by the code. It prevents compiler optimizations on that variable, useful for hardware registers and multithreading.
What are C++ keywords/reserved words?
Keywords are reserved words with special meaning to the compiler. Examples: int, float, if, else, while, for, class, public, private, virtual, template, namespace, auto.
What is the difference between typedef and using in C++?
typedef creates an alias for a type (C style). using (C++11) also creates type aliases but is more versatile and can be used for template aliases. Example: using IntPtr = int*;
What are reference variables in C++?
A reference is an alias for an existing variable. Once initialized, it cannot be changed to refer to another variable. Declared with & symbol. References are safer than pointers as they cannot be NULL.
What is type casting in C++?
Type casting converts one data type to another. C++ supports: implicit casting (automatic), explicit C-style casting (type)expression, and C++ style casts: static_cast, dynamic_cast, const_cast, reinterpret_cast.
What is the size of different data types in C++?
Sizes are implementation-dependent but typical sizes are: char (1 byte), int (4 bytes), float (4 bytes), double (8 bytes), bool (1 byte). Use sizeof() operator to check sizes on your system.
What is the difference between local and global variables?
Local variables are declared inside a function/block, have limited scope/lifetime, and are stored on stack. Global variables are declared outside all functions, have program-wide scope/lifetime, and are stored in data segment.
What are the rules for naming variables in C++?
Variable names can contain letters, digits, underscores. Must begin with letter or underscore. Cannot be a keyword. Case-sensitive. Should be meaningful and follow naming conventions (camelCase, snake_case, PascalCase).
Note: These questions cover fundamental concepts of C++ data types, variables, and keywords. Understanding these basics is essential for any C++ programming interview.
C++ Programming Fundamentals Next