Software Development Exam  >  Software Development Tests  >  Basics of C++  >  Test: Pointers - 2 - Software Development MCQ Download as PDF

Test: Pointers - 2 - Software Development MCQ


Test Description

30 Questions MCQ Test Basics of C++ - Test: Pointers - 2

Test: Pointers - 2 for Software Development 2024 is part of Basics of C++ preparation. The Test: Pointers - 2 questions and answers have been prepared according to the Software Development exam syllabus.The Test: Pointers - 2 MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Pointers - 2 below.
Solutions of Test: Pointers - 2 questions in English are available as part of our Basics of C++ for Software Development & Test: Pointers - 2 solutions in Hindi for Basics of C++ course. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free. Attempt Test: Pointers - 2 | 30 questions in 60 minutes | Mock test for Software Development preparation | Free important questions MCQ to study Basics of C++ for Software Development Exam | Download free PDF with solutions
1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: Pointers - 2 - Question 1

What is a pointer in C++?

Detailed Solution for Test: Pointers - 2 - Question 1

Pointers in C++ are variables that store memory addresses. They can be used to indirectly access and manipulate the values of other variables.

Test: Pointers - 2 - Question 2

Which of the following is the correct way to declare a pointer variable in C++?

Detailed Solution for Test: Pointers - 2 - Question 2

The correct way to declare a pointer variable in C++ is by using the asterisk (*) after the type name. For example, 'int* ptr'; declares a pointer variable named 'ptr' that can hold the address of an integer.

Test: Pointers - 2 - Question 3

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

Detailed Solution for Test: Pointers - 2 - Question 3

The code snippet initializes an integer variable 'x' with the value 5. Then, a pointer variable 'ptr' is declared and assigned the address of 'x'. Dereferencing the pointer using the asterisk (*) operator (*'ptr') gives the value stored at the memory location pointed to by 'ptr', which is 5.

Test: Pointers - 2 - Question 4

What is the purpose of the 'nullptr' keyword in C++?

Detailed Solution for Test: Pointers - 2 - Question 4

The 'nullptr' keyword is used to indicate that a pointer is uninitialized or does not currently point to any valid memory location. It is a replacement for the older 'NULL' macro and provides a more type-safe way to represent null pointers.

Test: Pointers - 2 - Question 5

Which operator is used to access the value pointed to by a pointer in C++?

Detailed Solution for Test: Pointers - 2 - Question 5

The asterisk (*) operator is used to access the value pointed to by a pointer. For example, '*ptr' gives the value stored at the memory location pointed to by 'ptr'.

Test: Pointers - 2 - Question 6

What is a null pointer in C++?

Detailed Solution for Test: Pointers - 2 - Question 6

A null pointer in C++ is a pointer that does not point to any valid memory location. It is represented by the address 0. It is important to note that dereferencing a null pointer results in undefined behavior.

Test: Pointers - 2 - Question 7

What is the output of the following code snippet?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr;
cout << *(ptr + 2);

Detailed Solution for Test: Pointers - 2 - Question 7

The code snippet initializes an integer array 'arr' with values {1, 2, 3, 4, 5}. A pointer variable 'ptr' is declared and assigned the address of the first element of the array ('arr'). Adding 2 to the pointer ('ptr + 2') moves the pointer two positions ahead in the array. Dereferencing the resulting pointer gives the value at that position, which is 3.

Test: Pointers - 2 - Question 8

Which of the following is true about pointer arithmetic in C++?

Detailed Solution for Test: Pointers - 2 - Question 8

Pointer arithmetic is allowed in C++ for pointers of any type. When performing pointer arithmetic, the size of the data type being pointed to is taken into account. For example, adding 1 to an 'int*' pointer moves the pointer by the size of an integer.

Test: Pointers - 2 - Question 9

What is the size of a pointer in C++?

Detailed Solution for Test: Pointers - 2 - Question 9

The size of a pointer in C++ depends on the underlying data type it points to. For example, the size of an 'int*' pointer is typically 4 bytes on a 32-bit system and 8 bytes on a 64-bit system.

Test: Pointers - 2 - Question 10

What is the purpose of the 'const' keyword when used with a pointer in C++?

Detailed Solution for Test: Pointers - 2 - Question 10

When the 'const' keyword is used with a pointer in C++, it indicates that the pointed value is constant and cannot be modified through that pointer. The pointer itself can still be reassigned to point to a different memory location.

Test: Pointers - 2 - Question 11

What is the output of the following code snippet?
int x = 5;
int* ptr = &x;
*ptr = 10;
cout << x;

Detailed Solution for Test: Pointers - 2 - Question 11

The code snippet initializes an integer variable 'x' with the value 5. Then, a pointer variable 'ptr' is declared and assigned the address of 'x'. Dereferencing the pointer using the asterisk (*) operator and assigning a value ('*ptr = 10';) modifies the value at the memory location pointed to by 'ptr', which is 'x'. Therefore, the value of 'x' becomes 10. Printing 'x' using 'cout' gives the output as 10.

Test: Pointers - 2 - Question 12

What is the output of the following code snippet?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr;
cout << *ptr++;

Detailed Solution for Test: Pointers - 2 - Question 12

The code snippet initializes an integer array 'arr' with values {1, 2, 3, 4, 5}. A pointer variable 'ptr' is declared and assigned the address of the first element of the array ('arr'). The expression '*ptr++' increments the pointer ('ptr++') and then dereferences the original pointer ('*ptr'). Since the post-increment operator is used, the pointer is incremented after the dereference operation. Therefore, the value of '*ptr' is 2.

Test: Pointers - 2 - Question 13

What is the output of the following code snippet?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr + 2;
cout << *ptr;

Detailed Solution for Test: Pointers - 2 - Question 13

The code snippet initializes an integer array 'arr' with values {1, 2, 3, 4, 5}. A pointer variable 'ptr' is declared and assigned the address of the third element of the array ('arr + 2'). Dereferencing the pointer using the asterisk (*) operator ('*ptr') gives the value stored at the memory location pointed to by 'ptr', which is 3.

Test: Pointers - 2 - Question 14

What is the output of the following code snippet?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr;
cout << *(ptr + 3);

Detailed Solution for Test: Pointers - 2 - Question 14

The code snippet initializes an integer array 'arr' with values {1, 2, 3, 4, 5}. A pointer variable 'ptr' is declared and assigned the address of the first element of the array ('arr'). Adding 3 to the pointer ('ptr + 3') moves the pointer three positions ahead in the array. Dereferencing the resulting pointer gives the value at that position, which is 4.

Test: Pointers - 2 - Question 15

What is the output of the following code snippet?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr;
cout << ptr[2];

Detailed Solution for Test: Pointers - 2 - Question 15

The code snippet initializes an integer array 'arr' with values {1, 2, 3, 4, 5}. A pointer variable 'ptr' is declared and assigned the address of the first element of the array ('arr'). The expression 'ptr[2]' is equivalent to '*(ptr + 2)', which accesses the value at the memory location two positions ahead of 'ptr', resulting in the value 3.

Test: Pointers - 2 - Question 16

What is the output of the following code snippet?
int x = 5;
int* ptr1 = &x;
int* ptr2 = ptr1;
*ptr2 = 10;
cout << *ptr1;

Detailed Solution for Test: Pointers - 2 - Question 16

The code snippet initializes an integer variable 'x' with the value 5. Two pointer variables 'ptr1' and 'ptr2' are declared and assigned the address of 'x' ('&x'). Then, 'ptr2' is assigned the value of 'ptr1'. Dereferencing 'ptr2' and assigning a value ('*ptr2 = 10';) modifies the value at the memory location pointed to by 'ptr2', which is 'x'. Therefore, the value of 'x' becomes 10. Printing '*ptr1' gives the output as 10.

Test: Pointers - 2 - Question 17

What is the output of the following code snippet?
int x = 5;
int* ptr = &x;
int** ptr2 = &ptr;
cout << **ptr2;

Detailed Solution for Test: Pointers - 2 - Question 17

The code snippet initializes an integer variable 'x' with the value 5. A pointer variable 'ptr' is declared and assigned the address of 'x'. Then, a pointer to a pointer variable 'ptr2' is declared and assigned the address of 'ptr'. Dereferencing 'ptr2' twice using the asterisk (*) operator ('**ptr2') gives the value at the memory location pointed to by 'ptr', which is 'x'. Therefore, the output is 5.

Test: Pointers - 2 - Question 18

What is the output of the following code snippet?
int x = 5;
int* ptr1 = &x;
int* ptr2 = nullptr;
ptr2 = ptr1;
*ptr2 = 10;
cout << *ptr1;

Detailed Solution for Test: Pointers - 2 - Question 18

The code snippet initializes an integer variable 'x' with the value 5. Two pointer variables 'ptr1' and 'ptr2' are declared and assigned the address of 'x' ('&x'). Then, '*ptr2 = 10'; modifies the value at the memory location pointed to by 'ptr2', which is 'x'. Therefore, the value of 'x' becomes 10. Printing '*ptr1' gives the output as 10.

Test: Pointers - 2 - Question 19

What is the output of the following code snippet?
int arr[] = {1, 2, 3, 4, 5};
int* ptr1 = arr;
int* ptr2 = ptr1 + 3;
cout << ptr2 - ptr1;

Detailed Solution for Test: Pointers - 2 - Question 19

The code snippet declares a pointer variable 'ptr' of type 'int*' and initializes it with 'nullptr'. When a null pointer is dereferenced, it results in undefined behavior. In this case, dereferencing 'ptr' and assigning a value ('*ptr = 0';) would attempt to modify the value at a null memory location, which is invalid.

Test: Pointers - 2 - Question 20

What is the output of the following code snippet?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr + 4;
cout << *(ptr - 2);

Detailed Solution for Test: Pointers - 2 - Question 20

The code snippet declares a constant pointer variable 'ptr' of type 'int*' and initializes it with the address of an integer variable 'x'. Since 'ptr' is a constant pointer, its value cannot be changed. Therefore, the statement 'ptr = &y'; attempting to reassign 'ptr' with the address of another integer variable 'y' will result in a compilation error.

Test: Pointers - 2 - Question 21

What is the output of the following code snippet?
int* ptr = new int[5];
ptr[2] = 10;
cout << ptr[2];
delete[] ptr;

Detailed Solution for Test: Pointers - 2 - Question 21

The code snippet dynamically allocates an integer array 'ptr' of size 5 using the new operator. The 'ptr[2] = 10'; statement assigns the value 10 to the element at index 2 of the array. Finally, 'cout << ptr[2]'; prints the value at index 2 of the array, which is 10. It is important to note that the dynamically allocated array is deallocated using the 'delete[]' operator to avoid memory leaks.

Test: Pointers - 2 - Question 22

What is a dynamic array in C++?

Detailed Solution for Test: Pointers - 2 - Question 22

A dynamic array in C++ is an array that can change its size during runtime. It is allocated using the 'new' operator and deallocated using the 'delete[]' operator. This allows the array to have a flexible size based on program requirements.

Test: Pointers - 2 - Question 23

What is the output of the following code snippet?
int** ptr = new int*[3];
for (int i = 0; i < 3; i++) {
    ptr[i] = new int[2];
    for (int j = 0; j < 2; j++) {
        ptr[i][j] = i + j;
    }
}
cout << ptr[2][1];
for (int i = 0; i < 3; i++) {
    delete[] ptr[i];
}
delete[] ptr;

Detailed Solution for Test: Pointers - 2 - Question 23

The code snippet dynamically allocates a two-dimensional integer array 'ptr' of size 3x2 using the 'new' operator. The nested loops initialize the elements of the array with values based on their indices ('i + j'). Finally, 'cout << ptr[2][1]'; prints the value at row 2, column 1 of the array, which is 0. It is important to note that the dynamically allocated array is deallocated using the 'delete[]' operator to avoid memory leaks.

Test: Pointers - 2 - Question 24

What is a multidimensional dynamic array in C++?

Detailed Solution for Test: Pointers - 2 - Question 24

A multidimensional dynamic array in C++ is an array with multiple dimensions that are dynamically allocated. It allows for flexibility in the size of each dimension and can be resized using appropriate memory allocation and deallocation techniques.

Test: Pointers - 2 - Question 25

What is the output of the following code snippet?
void print(int* arr, int size) {
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int* ptr = arr;
    print(ptr, 5);
    return 0;
}

Detailed Solution for Test: Pointers - 2 - Question 25

The code snippet defines a function 'print' that takes an integer pointer 'arr' and an integer 'size' as parameters. Inside the function, a loop is used to print the elements of the array pointed to by 'arr'. In 'main()', an integer array 'arr' is defined with values {1, 2, 3, 4, 5}. A pointer variable 'ptr' is initialized with the address of the first element of the array. The 'print(ptr, 5'); statement calls the 'print' function and passes the pointer 'ptr' and the size of the array. The function 'print' iterates over the array using the pointer and prints its elements. Therefore, the output is 1 2 3 4 5.

Test: Pointers - 2 - Question 26

What are smart pointers in C++?

Detailed Solution for Test: Pointers - 2 - Question 26

The code snippet attempts to assign the address of a function 'fun' to a function pointer variable 'ptr'. However, the syntax used for function pointers is incorrect. The correct syntax for declaring and initializing a function pointer is 'return_type (*ptr)(parameter_types)';. Therefore, the code will result in a compilation error.

Test: Pointers - 2 - Question 27

What is the purpose of using smart pointers in C++?

Detailed Solution for Test: Pointers - 2 - Question 27

The code snippet defines a function 'add' that takes two integers as parameters and returns their sum. Inside 'main()', a function pointer 'ptr' is declared and initialized with the address of the 'add' function. The statement 'int result = (*ptr)(10, 20);' calls the function pointed to by 'ptr' with arguments 10 and 20. The function adds the two integers and returns their sum, which is then assigned to the variable 'result'. Therefore, the output is 30.

Test: Pointers - 2 - Question 28

What is the output of the following code snippet?
#include <memory>

int main() {
    std::unique_ptr<int> ptr(new int(5));
    std::cout << *ptr;
    return 0;
}

Detailed Solution for Test: Pointers - 2 - Question 28

Function pointers in C++ provide a way to pass functions as arguments to other functions. This allows for increased flexibility and modularity in program design by enabling the use of different functions based on runtime conditions or requirements.

Test: Pointers - 2 - Question 29

When should function pointers be used in C++?

Detailed Solution for Test: Pointers - 2 - Question 29

Function pointers are often used in scenarios where different functions need to be used based on runtime conditions or requirements. One common example is sorting an array of integers using a custom comparison function. By using a function pointer, the sorting algorithm can be made flexible to sort in ascending or descending order based on the provided comparison function.

Test: Pointers - 2 - Question 30

What is the syntax to declare a function pointer in C++?

Detailed Solution for Test: Pointers - 2 - Question 30

Pointers to member functions in C++ allow accessing and invoking member functions of objects through pointers. This provides a way to manipulate objects and their behaviors dynamically at runtime by using function pointers to member functions.

70 videos|44 docs|15 tests
Information about Test: Pointers - 2 Page
In this test you can find the Exam questions for Test: Pointers - 2 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Pointers - 2, EduRev gives you an ample number of Online tests for practice
70 videos|44 docs|15 tests
Download as PDF
Download the FREE EduRev App
Track your progress, build streaks, highlight & save important lessons and more!