Pointers

 MCQS

  1. What is a pointer in C++?

    • A. A variable that cannot be changed
    • B. A variable that stores the address of another variable
    • C. A variable that holds only integers
    • D. A constant value in the program
  2. How do you declare a pointer in C++?

    • A. int *ptr;
    • B. pointer ptr;
    • C. ptr = &variable;
    • D. point var;
  3. What does the "&" operator do when used with a pointer?

    • A. It performs a bitwise AND operation
    • B. It returns the value pointed to by the pointer
    • C. It retrieves the memory address of the pointer
    • D. It is not a valid operation with pointers
  4. What is the purpose of the "nullptr" keyword in C++?

    • A. It represents an empty pointer
    • B. It is used for mathematical operations on pointers
    • C. It is a null value for integers
    • D. It is a deprecated keyword
  5. What is dereferencing a pointer?

    • A. Accessing the memory address of the pointer
    • B. Changing the address stored in a pointer
    • C. Accessing the value stored at the memory address pointed by the pointer
    • D. Converting a pointer to an integer
  6. Which operator is used to allocate memory for a pointer?

    • A. alloc
    • B. new
    • C. malloc
    • D. allocate
  7. What does the "delete" keyword do in relation to pointers?

    • A. Deletes the pointer itself
    • B. Deletes the memory allocated to the pointer
    • C. Deletes the content of the pointer
    • D. Deletes the variable being pointed to
  8. What is a null pointer in C++?

    • A. A pointer that contains 0
    • B. A pointer that points to the first element of an array
    • C. A pointer that points to a constant value
    • D. A pointer that contains garbage value
  9. What is the size of a pointer in C++?

    • A. 4 bytes
    • B. 8 bytes
    • C. Depends on the type of data it points to
    • D. Always 2 bytes
  10. What is a dangling pointer?

    • A. A pointer with an undefined value
    • B. A pointer pointing to a deleted memory location
    • C. A pointer with a value of zero
    • D. A pointer pointing to the end of an array
  11. What is the purpose of the new operator in C++?

    • A. Declaring a new variable
    • B. Allocating memory dynamically
    • C. Creating a new function
    • D. Initializing a constant
  12. Which keyword is used to deallocate memory allocated with new in C++?

    • A. delete
    • B. free
    • C. release
    • D. dealloc

  13. What is a potential issue with manual dynamic memory allocation?

    • A. Memory leaks
    • B. Stack overflow
    • C. Syntax errors
    • D. Division by zero

    Which operator is used for dynamic memory allocation in an array?

    • A. malloc
    • B. new[]
    • C. alloc
    • D. allocate
    1. What does the expression ptr++ do, assuming ptr is a pointer?

      • A. Increments the value pointed by ptr
      • B. Moves the pointer to the next memory location
      • C. Decrements the value pointed by ptr
      • D. Assigns the pointer to NULL
    2. How is pointer arithmetic related to the size of the data type it points to?

      • A. It is unrelated to the data type size
      • B. It depends on the operating system
      • C. It adjusts the pointer by the size of the data type
      • D. It always increments by 1 byte
    3. What is the correct way to initialize a pointer to a character in C++?

      • A. char *ptr = 'A';
      • B. char *ptr = "A";
      • C. char *ptr = &A;
      • D. char *ptr = new char;

    1. Practice Programs:
    2. Simple:


    3. Q1)Write a program to declare a pointer and assign it the address of an integer variable. Then, print the value of the variable using the pointer.
      Q2)Create a program that demonstrates the use of the new operator to allocate memory for an integer. Initialize the allocated memory with a value and print it. Don't forget to free the allocated memory.
      Q3)Write a program that uses an array and a pointer to find the sum of elements in the array.
      Q4)Create a program that swaps the values of two integers using pointers.
      Q5)Write a program to find the length of a string using pointers.
      Q6)Develop a program that uses a pointer to a function to perform arithmetic operations (addition, subtraction, multiplication, division) on two numbers.
      Q7)Implement a program that dynamically allocates memory for an array of integers based on user input for the array size. Allow the user to input values into the array and then print them.
      Q8)Create a program that shows the difference between pointer and array notation when accessing array elements.
      Q9)Write a program that uses pointers to find the maximum and minimum values in an array.
      Q10)Develop a program that uses a pointer to count the number of vowels in a given string.
    4. Question 1: Array Manipulation

    Write a program that initializes an array of integers, then uses a pointer to iterate through the array, doubling the value of each element.

    Question 2: Dynamic Memory Allocation

    Create a program that dynamically allocates memory for an integer using new, takes user input for the integer, prints the value, and then frees the allocated memory using delete.

    Question 3: String Manipulation

    Write a program that declares a string, uses a pointer to iterate through the characters of the string, and converts each character to uppercase.

    Question 4: Function with Pointers

    Create a function that accepts two integer pointers as arguments and swaps the values they point to. Test the function by passing two integer variables to it.

    Question 5: Pointer to Function

    Declare a function that takes an integer parameter and returns the square of the parameter. Then declare a pointer to this function and use it to calculate the square of a user-input integer.

    Question 6: Pointer Arithmetic

    Write a program that declares an array of floats, calculates the average using pointers, and prints the result.

  14. Question 7:



  15. Write a C++ program that swaps values of two float-type variables A and B. For swapping you can use a third variable temp to store values temporarily. All accesses to the variables (A and B) should be made pass-by reference without pointers.