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.
Which C++ operator is used to allocate memory for a single integer dynamically on the heap?
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.
Which of the following is a floating-point data type in C++ typically used for decimal values?
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.
Which line correctly declares a pointer to an integer in C++?
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.
What statement should be used to deallocate memory allocated with 'new' for a single object in C++?
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.
Which keyword makes a variable’s value unchangeable after its initialization in C++?
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++.
Which declaration correctly creates an array of 5 integers in C++?
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.
What does the 'sizeof' operator return when applied to a data type like 'double' in C++?
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++.
Which keyword represents a null pointer constant in modern C++?
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.
What is the initial value of an uninitialized local 'int' variable in standard C++?
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.
Which line correctly declares a reference to an existing integer variable named 'num' in C++?
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++.