Software Development Exam  >  Software Development Notes  >  Basics of C++  >  Assignment: Pointers in C++

Assignment: Pointers in C++ | Basics of C++ - Software Development PDF Download

Multiple Choice Questions (MCQs)

Q.1. Which of the following statements is true regarding pointers in C++?
(a) Pointers are not used in C++
(b) Pointers can only store memory addresses of variables
(c) Pointers can be used to dynamically allocate memory
(d) Pointers can only be used with built-in data types

Ans. (c)

Q.2. What is the output of the following code snippet?
int x = 5;
int *ptr = &x;
cout << *ptr << endl;

(a) 0
(b) 5
(c) Memory address of x
(d) Compilation error

Ans. (b)

Q.3. Which operator is used to access the value pointed to by a pointer?
(a) &
(b) *
(c) %
(d) #

Ans. (b)

Q.4. What does the following code snippet do?
int x = 10;
int *ptr = &x;
*ptr = 20;

(a) Changes the value of x to 10
(b) Changes the value of x to 20
(c) Creates a new variable named ptr
(d) No effect on the value of x

Ans. (b)

Q.5. What is the purpose of the const keyword when declaring a pointer?
(a) It makes the pointer constant
(b) It makes the pointed-to value constant
(c) It prevents the pointer from being dereferenced
(d) It specifies a constant memory address

Ans. (b)

Higher Order Thinking Questions (HOTS)

Q.1. Write a C++ program to swap two numbers using pointers.

#include <iostream>

using namespace std;


void swapNumbers(int* a, int* b) {

    int temp = *a;

    *a = *b;

    *b = temp;

}


int main() {

    int x = 5, y = 10;

    cout << "Before swap: x = " << x << ", y = " << y << endl;

    swapNumbers(&x, &y);

    cout << "After swap: x = " << x << ", y = " << y << endl;


    return 0;

}

Q.2. Explain the concept of pointer arithmetic with an example.

Pointer arithmetic allows performing arithmetic operations on pointers, such as addition, subtraction, increment, and decrement. These operations are scaled by the size of the data type the pointer points to. For example:
int arr[] = {1, 2, 3, 4, 5};

int* ptr = arr;  // Pointer to the first element of the array

// Accessing array elements using pointer arithmetic

cout << *ptr << endl;        // Output: 1

cout << *(ptr + 1) << endl;  // Output: 2

cout << *(ptr + 2) << endl;  // Output: 3

Q.3. Discuss the differences between a pointer and a reference in C++.

Differences between a pointer and a reference in C++:

  • A pointer can be reassigned to point to different objects, whereas a reference cannot be reassigned once initialized.
  • Pointers can be uninitialized (pointing to garbage values) or set to NULL, whereas references must be initialized when declared.
  • Pointers can be NULL, meaning they don't point to any valid memory location, while references must always refer to valid objects.
  • Pointers require the use of the dereference operator (*) to access the value they point to, while references can be used directly as if accessing the original object.
  • Pointers can be used in pointer arithmetic, whereas references cannot.

Q.4. Write a C++ function that takes a pointer to an integer as a parameter and doubles the value pointed to by the pointer.

#include <iostream>

using namespace std;


void doubleValue(int* ptr) {

    *ptr *= 2;

}


int main() {

    int x = 5;

    int* ptr = &x;


    cout << "Before doubling: x = " << x << endl;

    doubleValue(ptr);

    cout << "After doubling: x = " << x << endl;


    return 0;

}

Q.5. What are the advantages of using pointers in C++? Provide examples to support your answer.

Advantages of using pointers in C++:

  • Dynamic memory allocation: Pointers allow dynamically allocating and deallocating memory, which is useful when the size of data is not known at compile time.
  • Efficient parameter passing: Passing pointers to functions is more efficient than passing large objects by value, as it avoids copying the entire object.
  • Data structures: Pointers are essential for implementing data structures like linked lists, trees, and graphs.
  • Direct memory access: Pointers allow direct access to memory, enabling low-level manipulation and efficient memory operations.
  • Function pointers: Pointers to functions allow implementing callbacks, event handling, and polymorphism.

Fill in the Blanks

Q.1. A pointer is a variable that stores the _______________ of another variable.

Ans. address

Q.2. The unary operator used to get the address of a variable is _______________.

Ans. &

Q.3. A void pointer is a special type of pointer that can point to _______________ type of data.

Ans. any

Q.4. The _______________ operator is used to access the value pointed to by a pointer.

Ans. •(asterisk)

Q.5. The null pointer in C++ is represented by the keyword _______________.

Ans. nullptr

True or False

Q.1. Pointers can only be used with arrays.

Ans. False

Q.2. It is not possible to have a pointer to a function.

Ans. False

Q.3. Pointers are always initialized to NULL by default.

Ans. False

Q.4. Pointers are used to allocate memory dynamically. 

Ans. True

Q.5. Pointers allow direct access to the physical memory.

Ans. True

Hands-On Questions

Q.1. Write a C++ program to find the sum of elements in an integer array using pointers.

#include <iostream>

using namespace std;


int sumArrayElements(int* arr, int size) {

    int sum = 0;

    for (int i = 0; i < size; i++) {

        sum += *arr;

        arr++;

    }

    return sum;

}


int main() {

    int arr[] = {1, 2, 3, 4, 5};

    int size = sizeof(arr) / sizeof(arr[0]);


    int sum = sumArrayElements(arr, size);

    cout << "Sum of elements: " << sum << endl;


    return 0;

}

Q.2. Write a C++ program to reverse a string using pointers.

#include <iostream>

using namespace std;


void reverseString(char* str) {

    char* start = str;

    char* end = str;


    while (*end != '\0') {

        end++;

    }

    end--;


    while (start < end) {

        char temp = *start;

        *start = *end;

        *end = temp;


        start++;

        end--;

    }

}


int main() {

    char str[] = "Hello";

    cout << "Before reverse: " << str << endl;

    reverseString(str);

    cout << "After reverse: " << str << endl;


    return 0;

}

Q.3. Write a C++ function that takes two integer pointers as parameters and swaps the values pointed to by them.

#include <iostream>

using namespace std;


void swapValues(int* a, int* b) {

    int temp = *a;

    *a = *b;

    *b = temp;

}


int main() {

    int x = 5, y = 10;

    cout << "Before swap: x = " << x << ", y = " << y << endl;

    swapValues(&x, &y);

    cout << "After swap: x = " << x << ", y = " << y << endl;


    return 0;

}

Q.4. Write a C++ program to dynamically allocate memory for an integer using the new keyword and then deallocate the memory using the delete keyword.

#include <iostream>

using namespace std;


int main() {

    int* ptr = new int;

    *ptr = 42;


    cout << "Value stored at ptr: " << *ptr << endl;

    delete ptr;  // Deallocating the memory


    return 0;

}

Q.5. Write a C++ program to find the maximum element in an integer array using pointers.

#include <iostream>

using namespace std;


int findMaxElement(int* arr, int size) {

    int maxElement = *arr;


    for (int i = 1; i < size; i++) {

        if (*(arr + i) > maxElement) {

            maxElement = *(arr + i);

        }

    }

    return maxElement;

}


int main() {

    int arr[] = {3, 8, 2, 6, 4};

    int size = sizeof(arr) / sizeof(arr[0]);


    int maxElement = findMaxElement(arr, size);

    cout << "Maximum element: " << maxElement << endl;


    return 0;

}

The document Assignment: Pointers in C++ | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
70 videos|45 docs|15 tests

Top Courses for Software Development

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

practice quizzes

,

Semester Notes

,

study material

,

Objective type Questions

,

Exam

,

MCQs

,

Assignment: Pointers in C++ | Basics of C++ - Software Development

,

shortcuts and tricks

,

pdf

,

Free

,

Previous Year Questions with Solutions

,

Assignment: Pointers in C++ | Basics of C++ - Software Development

,

Assignment: Pointers in C++ | Basics of C++ - Software Development

,

Sample Paper

,

Important questions

,

video lectures

,

mock tests for examination

,

Extra Questions

,

Viva Questions

,

ppt

,

Summary

,

past year papers

;