C++ Memory Management and Types Fundamentals Quiz Quiz

Explore essential concepts of C++ memory management and variable types with this introductory quiz. Solidify your understanding of dynamic memory, data types, pointers, and related syntax to write safer and more efficient C++ programs.

  1. Dynamic Memory Allocation

    Which C++ operator is used to allocate memory for a single integer dynamically on the heap?

    1. malloc
    2. add
    3. new
    4. alloc

    Explanation: The 'new' operator is used in C++ to allocate memory dynamically for a single object or variable on the heap. 'malloc' is a C-style function and not an operator, which is less preferable in C++. The options 'add' and 'alloc' are not valid operators for memory allocation in C++. Choosing 'new' ensures type-safety and constructor calls where appropriate.

  2. Primitive Data Types

    Which of the following is a floating-point data type in C++ typically used for decimal values?

    1. bool
    2. float
    3. int
    4. char

    Explanation: 'float' is designed to store decimal values with single precision, which is useful for representing numbers with fractional parts. 'int' only stores integers, 'char' represents characters, and 'bool' is for boolean values. Only 'float' among the options is meant for floating-point arithmetic.

  3. Pointer Declaration

    Which line correctly declares a pointer to an integer in C++?

    1. pointer int ptr;
    2. ptr int*;
    3. int* ptr;
    4. int ptr*;

    Explanation: In C++, 'int* ptr;' is the proper syntax for declaring a pointer to an integer. 'pointer int ptr;' and 'ptr int*;' are not valid syntax. The statement 'int ptr*;' mistakenly places the asterisk after the variable name, which is not standard. The asterisk should be placed before the variable name to indicate a pointer.

  4. Releasing Dynamic Memory

    What statement should be used to deallocate memory allocated with 'new' for a single object in C++?

    1. remove
    2. delete
    3. release
    4. free

    Explanation: 'delete' is the correct statement for deallocating memory allocated with 'new' in C++. 'free' is used in C, not C++, and should not be mixed with 'new'. 'remove' and 'release' are not valid memory-management statements in C++. Using 'delete' ensures proper cleanup and avoids memory leaks.

  5. Const Qualifier Usage

    Which keyword makes a variable’s value unchangeable after its initialization in C++?

    1. immutable
    2. final
    3. const
    4. static

    Explanation: The 'const' keyword specifies that a variable's value cannot be modified after initialization in C++. 'static' relates to storage duration, not immutability. 'final' is relevant in inheritance to prevent overriding, and 'immutable' is not a C++ keyword. Only 'const' provides the immutability guarantee in C++.

  6. Array Declaration

    Which declaration correctly creates an array of 5 integers in C++?

    1. int arr(5);
    2. int[5] arr;
    3. int arr[5];
    4. arr int[5];

    Explanation: 'int arr[5];' is the standard way to declare an array of five integers in C++. 'int arr(5);' creates a single integer, not an array. 'int[5] arr;' and 'arr int[5];' are incorrect syntax in C++. Using the correct placement of brackets is essential for array declarations.

  7. Sizeof Operator

    What does the 'sizeof' operator return when applied to a data type like 'double' in C++?

    1. The signedness of the type
    2. The number of elements in an array
    3. The number of bytes used by the type
    4. The memory address of a variable

    Explanation: 'sizeof' returns the size in bytes that a data type or variable occupies in memory. It does not provide array element counts, indicate type signedness, or reveal memory addresses. Only the first option matches the actual function of 'sizeof' in C++.

  8. Null Pointer Representation

    Which keyword represents a null pointer constant in modern C++?

    1. null
    2. NULLPTR
    3. nullptr
    4. ZERO

    Explanation: 'nullptr' is the standard C++ keyword for representing a null pointer constant, introduced for type safety. 'null' is not a valid keyword in C++, and 'NULLPTR' is not recognized by the language. 'ZERO' is not related to pointer nullification, making 'nullptr' the only correct option.

  9. Default Initialization

    What is the initial value of an uninitialized local 'int' variable in standard C++?

    1. One
    2. Zero
    3. Null
    4. Undefined (garbage value)

    Explanation: Local variables of type 'int' that are not explicitly initialized remain with an undefined or garbage value. 'Zero' could be correct for some global or static variables, but not for local ones. 'One' and 'Null' are not default values for uninitialized integers in C++. This can lead to unexpected behavior if the variable is used before assignment.

  10. Reference Types

    Which line correctly declares a reference to an existing integer variable named 'num' in C++?

    1. ref int = u0026num;
    2. int refu0026 = num;
    3. int *ref = num;
    4. intu0026 ref = num;

    Explanation: 'intu0026 ref = num;' is the correct syntax to declare 'ref' as a reference to the integer variable 'num'. 'int *ref = num;' is for pointers, not references. 'ref int = u0026num;' attempts to assign a pointer to an int, which is invalid, while 'int refu0026 = num;' is an incorrect placement of the 'u0026'. Only the first option properly defines a reference in C++.