MCQS
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
How do you declare a pointer in C++?
- A.
int *ptr;
- B.
pointer ptr;
- C.
ptr = &variable;
- D.
point var;
- A.
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
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
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
Which operator is used to allocate memory for a pointer?
- A.
alloc
- B.
new
- C.
malloc
- D.
allocate
- A.
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
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
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
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
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
Which keyword is used to deallocate memory allocated with
new
in C++?- A.
delete
- B.
free
- C.
release
- D.
dealloc
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
What does the expression
ptr++
do, assumingptr
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
- A. Increments the value pointed by
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
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;
- Practice Programs:
- Simple:
- Question 1: Array Manipulation
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.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 usingdelete
.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.
Question 7:
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.