pointers mcq sanfoundry

  1. What is a pointer in C++? A pointer in C++ is a variable that stores the memory address of another variable. It allows direct manipulation and access to the memory location of the variable it points to.

  2. How is a pointer declared in C++? A pointer is declared by using the asterisk () symbol before the variable name. For example, "int ptr;" declares a pointer variable named "ptr" that can store the memory address of an integer variable.

  3. What is the "address of" operator in C++? The "address of" operator in C++ is denoted by the ampersand (&) symbol. It is used to get the memory address of a variable. For example, "int x = 5; int* ptr = &x;" assigns the memory address of variable "x" to the pointer variable "ptr".

  4. How is a value assigned to a pointer in C++? A value can be assigned to a pointer by using the "address of" operator or by directly assigning another pointer of the same type. For example, "int* ptr = &x;" assigns the memory address of variable "x" to the pointer variable "ptr".

  5. How is the value pointed by a pointer accessed in C++? The value pointed by a pointer can be accessed by using the dereference operator () before the pointer variable. For example, "ptr" returns the value stored at the memory address pointed by the pointer variable "ptr".

  6. What is the null pointer in C++? The null pointer in C++ is a special value that is assigned to a pointer variable when it is not pointing to any valid memory address. It is represented by the constant value "nullptr". For example, "int* ptr = nullptr;" assigns the null value to the pointer variable "ptr".

  7. How is dynamic memory allocated in C++ using pointers? Dynamic memory allocation in C++ is done using the "new" operator. It allocates memory from the heap and returns the memory address, which can be stored in a pointer variable. For example, "int* ptr = new int;" allocates memory for an integer on the heap and assigns the memory address to the pointer variable "ptr".

  8. How is dynamic memory deallocated in C++ using pointers? Dynamic memory deallocation in C++ is done using the "delete" operator. It releases the memory allocated using the "new" operator. For example, "delete ptr;" deallocates the memory pointed by the pointer variable "ptr".

  9. What is a pointer arithmetic in C++? Pointer arithmetic in C++ allows performing arithmetic operations on pointers, such as incrementing or decrementing the memory address they point to. For example, "ptr++" increments the pointer variable "ptr" to point to the next memory location.

  10. How is a pointer to a function declared in C++? A pointer to a function in C++ is declared by specifying the function's return type, followed by an asterisk () and the pointer variable name. For example, "int (ptr)(int, int);" declares a pointer variable named "ptr" that can point to a function taking two integer parameters and returning an integer.

  11. How is a pointer to a class member declared in C++? A pointer to a class member in C++ is declared by using the class name, followed by the scope resolution operator (::), and the member name, enclosed in parentheses. For example, "int (MyClass::*ptr);" declares a pointer variable named "ptr" that can point to an integer member of the class "MyClass".