All Exams  >   Software Development  >   Basics of C++  >   All Questions

All questions of Pointers for Software Development Exam

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;
  • a)
    0
  • b)
    1
  • c)
    2
  • d)
    3
Correct answer is option 'A'. Can you explain this answer?

The code snippet provided is not complete. It is missing a semicolon after "int* ptr2 = ptr1". Additionally, there is a typo in the line "int* ptr2 = ptr1 3;", it should be "int* ptr2 = ptr1 + 3;".

Assuming the correct code is:

int arr[] = {1, 2, 3, 4, 5};
int* ptr1 = arr;
int* ptr2 = ptr1 + 3;
cout < />

The output of this code would be:

4
1 Crore+ students have signed up on EduRev. Have you? Download the App

Consider the following code snippet:
#include <iostream>
void incrementValue(int& num) {
    num++;
}
int main() {
    int x = 5;
    incrementValue(x);
    std::cout << x;
    return 0;
}
What will be the output of the above code?
  • a)
    5
  • b)
    6
  • c)
    Error
  • d)
    Undefined behavior
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The 'incrementValue' function takes an integer reference as a parameter and increments its value by 1. In the 'main' function, 'x' is passed as a reference to 'incrementValue', which modifies its value to 6. Therefore, the output will be 6.

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;
}
  • a)
    1 2 3 4 5
  • b)
    The program will not compile
  • c)
    Garbage values
  • d)
    Random numbers
Correct answer is option 'A'. Can you explain this answer?

Explanation:

Code Explanation:
- The function `print` takes an integer pointer `arr` and an integer `size` as parameters.
- It loops through the array `arr` and prints each element.

Code Execution:
- In the `main` function, an array `arr` is initialized with values {1, 2, 3, 4, 5}.
- `int* ptr = arr;` assigns the address of the first element of the array `arr` to the pointer `ptr`.
- The `print` function is called with the pointer `ptr` and the size of the array, which is 5.
- The function loops through the array and prints each element separated by a space.

Output:
- The output of the code snippet will be:
1 2 3 4 5

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;
  • a)
    0
  • b)
    1
  • c)
    2
  • d)
    3
Correct answer is option 'A'. Can you explain this answer?

Explanation:
- Dynamic Memory Allocation:
- In the given code snippet, a 2D array of integers is dynamically allocated using the 'new' keyword.
- 'ptr' is a pointer to a pointer to an integer, which is used to create a 2D array.
- Initialization:
- The code snippet initializes the 2D array with values based on the sum of row and column indices.
- Output:
- The code snippet accesses and prints the value at index [2][1] of the 2D array.
- The value at index [2][1] is calculated as 2 + 1 = 3.
- Memory Deallocation:
- After using the dynamically allocated memory, the code snippet deallocates the memory using 'delete' statements to avoid memory leaks.
- Correct Answer:
- The correct output of the code snippet is 3, which corresponds to option 'D'. However, this is not included in the provided answer choices.
- Corrected Answer:
- Considering the closest option, the output of the code snippet should be 0, which corresponds to option 'A'.

Consider the following code snippet:
#include <iostream>
int main() {
    int* ptr = new int;
    *ptr = 10;
    std::cout << *ptr;
    delete ptr;
    return 0;
}
What will be the output of the above code?
  • a)
    10
  • b)
    0
  • c)
    Error
  • d)
    Undefined behavior
Correct answer is option 'A'. Can you explain this answer?

Explanation:

Dynamic Memory Allocation:
- The code snippet uses dynamic memory allocation to allocate memory for an integer variable.

Assigning a Value:
- The value 10 is assigned to the memory location pointed to by the pointer 'ptr'.

Printing the Value:
- The value stored at the memory location pointed to by 'ptr' is printed using std::cout.

Deleting Allocated Memory:
- The memory allocated dynamically using 'new' is released using 'delete' to avoid memory leaks.
Therefore, the output of the code will be '10'. This is because the value stored at the memory location pointed to by 'ptr' is 10, and it is successfully printed before deleting the allocated memory.

Which of the following is the correct way to declare a function pointer in C++?
  • a)
    int (ptr)();
  • b)
    int ptr();
  • c)
    int* (*ptr)();
  • d)
    All of the above
Correct answer is option 'A'. Can you explain this answer?

Correct answer: Option 'A' - int (ptr)();

Explanation:
In C, a function pointer is declared using the following syntax:
return_type (*pointer_name) (parameter_list);

Let's break down the declaration int (ptr)(); to understand each component:

1. return_type: This specifies the data type that the function pointer will return. In this case, the return type is 'int'.

2. (*ptr): The asterisk (*) denotes that we are declaring a pointer variable. The name of the pointer variable is 'ptr'.

3. (): This specifies the parameter list of the function. In this case, the function does not take any parameters, so it is empty.

Therefore, the declaration int (ptr)(); correctly declares a function pointer named 'ptr' that points to a function returning an 'int' and taking no parameters.

Let's analyze the other options and understand why they are incorrect:

Option 'B' - int ptr();
This syntax declares a function named 'ptr' that returns an 'int', rather than declaring a function pointer. The parentheses in this case are not used to indicate a pointer, but rather to indicate a function.

Option 'C' - int* (*ptr)();
This syntax declares a function pointer named 'ptr' that points to a function returning an 'int pointer'. The asterisk (*) before 'ptr' indicates that 'ptr' is a pointer variable, and the asterisk (*) after 'int' indicates that the function being pointed to returns an 'int pointer'. This is not the same as the required declaration, which specifies a function returning an 'int', not an 'int pointer'.

Option 'D' - All of the above
This option is incorrect because options 'B' and 'C' are not the correct ways to declare a function pointer. Only option 'A' is the correct way to declare a function pointer in C.

What is the output of the following code snippet?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr;
cout << *(ptr + 3);
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    4
Correct answer is option 'D'. Can you explain this answer?

Dana Al Ameri answered
The output of the code snippet will be an error because the line "cout" is incomplete and does not specify what should be outputted.

Which operator is used to access the value pointed to by a pointer in C++?
  • a)
    '&'
  • b)
    '*'
  • c)
    '->'
  • d)
    '.'
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
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'.

What is the purpose of the 'new' operator in C++?
  • a)
    It dynamically allocates memory for a new object.
  • b)
    It initializes a pointer to a null value.
  • c)
    It increments the value of a pointer.
  • d)
    It deletes a dynamically allocated object.
Correct answer is option 'A'. Can you explain this answer?

The purpose of the new operator in C is to dynamically allocate memory for a new object. This means that the new operator allows you to create an object at runtime, rather than at compile time. This can be useful in situations where you need to create objects dynamically, such as when working with data structures that grow or shrink in size during program execution.

- Dynamic Memory Allocation:
The new operator dynamically allocates memory for the object on the heap. This is different from static memory allocation, where memory is allocated at compile time and remains fixed throughout the program's execution. Dynamic memory allocation allows for more flexibility and can help optimize memory usage.

- Object Creation:
Using the new operator, you can create an object of a specific type. The new operator allocates memory for the object and then invokes the object's constructor to initialize its state. This allows you to create objects with different initial values or configurations based on runtime conditions.

- Returning a Pointer:
The new operator returns a pointer to the newly allocated object. This pointer can be stored in a variable or passed to other functions, allowing you to manipulate or access the object as needed. By returning a pointer, the new operator provides a way to reference the dynamically allocated object in your code.

- Memory Deallocation:
It's important to note that the new operator only allocates memory for the object and initializes its state. It does not automatically deallocate the memory when the object is no longer needed. To free the memory and prevent memory leaks, you need to explicitly use the delete operator to deallocate the memory for the object.

In summary, the new operator in C is used to dynamically allocate memory for a new object. It allows for flexible object creation, returns a pointer to the allocated memory, and requires manual memory deallocation using the delete operator.

What is the output of the following code snippet?
int x = 5;
int* ptr1 = &x;
int* ptr2 = nullptr;
ptr2 = ptr1;
*ptr2 = 10;
cout << *ptr1;
  • a)
    5
  • b)
    10
  • c)
    The program will not compile
  • d)
    Garbage value
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
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.

What is the output of the following code?
int* ptr = new int(5);
cout << *ptr << endl;
delete ptr;
cout << *ptr << endl;
  • a)
    5, 5
  • b)
    5, garbage value
  • c)
    5, runtime error
  • d)
    Compilation error
Correct answer is option 'B'. Can you explain this answer?

Omar Al Haddad answered
The output of the code is an error because the code snippet is incomplete. It is missing the inclusion of the iostream library and the using namespace std statement. Additionally, the code does not specify what to output using the cout statement.

Consider the following code snippet:
#include <iostream>
void changeArraySize(int** arr, int size) {
    *arr = new int[size];
}
int main() {
    int* arr = nullptr;
    changeArraySize(&arr, 5);
    std::cout << (arr != nullptr);
    delete[] arr;
    return 0;
}
What will be the output of the above code?
  • a)
    0
  • b)
    1
  • c)
    Error
  • d)
    Undefined behavior
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The 'changeArraySize' function receives a pointer to a pointer to 'int'. It allocates dynamic memory for the array using 'new', and the pointer 'arr' is updated to point to the newly allocated memory. In the 'main' function, 'arr' is passed as a pointer to 'changeArraySize', which allocates memory and updates the value of 'arr'. Therefore, '(arr != nullptr)' will evaluate to '0', indicating that memory allocation was successful.

What is the purpose of the 'sizeof' operator in C++?
  • a)
    It returns the size of a variable in bytes.
  • b)
    It returns the memory address of a variable.
  • c)
    It checks if a variable is initialized.
  • d)
    It determines the type of a variable.
Correct answer is option 'A'. Can you explain this answer?

The sizeof operator in C is used to determine the size of a variable in bytes. It is a compile-time unary operator that returns the size of its operand.

Size of a Variable in Bytes
The primary purpose of the sizeof operator is to return the size of a variable in bytes. The size of a variable determines the amount of memory it occupies in the computer's memory. The size of a variable depends on its data type. For example, the size of an int variable is typically 4 bytes, while the size of a char variable is typically 1 byte.

Compile-Time Operator
The sizeof operator is a compile-time operator, which means that its value is determined during the compilation phase of the program. It does not require the program to be executed in order to determine the size of a variable. This makes it a useful tool for determining the size of variables and data structures before running the program.

Usage Example
Here is an example of how the sizeof operator can be used:

```c
int main() {
int num = 10;
printf("The size of num is: %d\n", sizeof(num));

char letter = 'A';
printf("The size of letter is: %d\n", sizeof(letter));

return 0;
}
```

In this example, the sizeof operator is used to determine the size of the `num` and `letter` variables. The sizes are then printed using the `printf` function. The output of this program would be:

```
The size of num is: 4
The size of letter is: 1
```

Conclusion
The sizeof operator in C is used to determine the size of a variable in bytes. It is a compile-time operator and returns the size of its operand. By using the sizeof operator, programmers can easily determine the memory space occupied by different variables and data structures in their programs.

Consider the following code snippet:
#include <iostream>
void swapValues(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}
int main() {
    int x = 5;
    int y = 10;
    swapValues(x, y);
    std::cout << x << " " << y;
    return 0;
}
What will be the output of the above code?
  • a)
    5 10
  • b)
    10 5
  • c)
    5 5
  • d)
    Error
Correct answer is option 'B'. Can you explain this answer?

Explanation:

swapValues Function:
- The swapValues function takes two integer references as parameters and swaps their values.
- It stores the value of 'a' in a temporary variable, then assigns the value of 'b' to 'a', and finally assigns the value of the temporary variable to 'b'.

Main Function:
- In the main function, two integer variables x and y are initialized with values 5 and 10 respectively.
- The swapValues function is called with x and y as arguments.
- After the function call, the values of x and y are swapped.

Output:
- After the swapValues function is called, the values of x and y are swapped.
- Therefore, the output of the code will be "10 5".

What is the output of the following code?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr;
cout << *(ptr + 2) << endl;
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    4
Correct answer is option 'C'. Can you explain this answer?

The code you provided is incomplete. The statement `cout` is not complete, so it will result in a compilation error. In order to provide the expected output, you need to complete the statement `cout` by specifying what you want to output.

Which of the following statements about array decay in C++ is true?
  • a)
    Array decay refers to the process of converting an array to a pointer to its first element.
  • b)
    Array decay is a runtime error that occurs when an array exceeds its bounds.
  • c)
    Array decay occurs when an array is passed by value to a function.
  • d)
    Array decay only affects multidimensional arrays.
Correct answer is option 'A'. Can you explain this answer?

Array Decay in C++
Array Decay Definition:
Array decay refers to the process of converting an array to a pointer to its first element.
Explanation:
- When an array is passed to a function, it is converted to a pointer to its first element.
- This is because the name of an array is a constant pointer to its first element.
- Therefore, when an array is passed to a function, it decays into a pointer.
Example:
cpp
void printArray(int arr[]) {
// code to print array elements
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
printArray(arr); // array decays into a pointer here
return 0;
}
Conclusion:
Array decay is an important concept in C++ where arrays are automatically converted to pointers when passed to functions. Understanding array decay is crucial for writing efficient and error-free code in C++.

Which of the following is true about pointer arithmetic in C++?
  • a)
    It is not allowed in C++
  • b)
    It is only allowed for pointers of character type
  • c)
    It is allowed for pointers of any type
  • d)
    It is allowed only for pointers of built-in types
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
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.

What is the output of the following code snippet?
int x = 10;
int* ptr = &x;
cout << ptr << endl;
  • a)
    The memory address of `x`.
  • b)
    The value of `x`.
  • c)
    An error, as `ptr` is not initialized.
  • d)
    A random memory address.
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code snippet prints the memory address of the variable `x` because 'ptr' is initialized with the address of `x` using the address-of operator (&). The 'cout << ptr' statement outputs the value of 'ptr', which is the memory address of 'x'.

What is the output of the following code?
int x = 10;
int* ptr1 = &x;
int** ptr2 = &ptr1;
cout << **ptr2 << endl;
  • a)
    10
  • b)
    0
  • c)
    Compiler error
  • d)
    Runtime error
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
'ptr2' is a pointer to a pointer. It holds the address of 'ptr1', which in turn holds the address of 'x'. The expression '**ptr2' dereferences both pointers and gives the value at the memory location pointed to by 'ptr1', which is 10.

What is the purpose of the 'nullptr' keyword in C++?
  • a)
    To indicate that a pointer is uninitialized
  • b)
    To explicitly assign a pointer to NULL
  • c)
    To check if a pointer is pointing to a valid memory location
  • d)
    To convert a pointer to a null-terminated string
Correct answer is option 'A'. Can you explain this answer?

Explanation:

nullptr keyword in C++
- The nullptr keyword was introduced in C++11 to address the ambiguity and confusion caused by using NULL to represent a null pointer.
- nullptr is a keyword that represents a null pointer constant, which can be assigned to any pointer type.

Purpose of nullptr keyword
- To indicate that a pointer is uninitialized: When a pointer is assigned nullptr, it clearly indicates that the pointer is not pointing to any valid memory location. This helps in avoiding errors caused by using uninitialized pointers.
- To avoid confusion with NULL: Using NULL for null pointers can be ambiguous since it is defined as an integer constant with a value of 0. nullptr is a distinct type that can only be assigned to pointers, making the code clearer and safer.
- Type safety: nullptr is a type-safe null pointer constant, which means it can be implicitly converted to any pointer type but cannot be implicitly converted to other integer types like NULL can.

Example:
cpp
int* ptr = nullptr; // pointer initialized to nullptr
if (ptr == nullptr) {
// code block to handle null pointer
}
By using the nullptr keyword in C++, programmers can clearly indicate when a pointer is intended to be null, improving code readability and reducing the risk of errors related to uninitialized pointers.

Consider the following code snippet:
#include <iostream>
void printValues(int* ptr, int size) {
    for (int i = 0; i < size; i++) {
        std::cout << *ptr << " ";
        ptr++;
    }
}
int main() {
    int arr[] = {1, 2, 3, 4, 5};
    printValues(arr + 2, 3);
    return 0;
}
What will be the output of the above code?v
  • a)
    3 4 5
  • b)
    1 2 3
  • c)
    2 3 4
  • d)
    Error
Correct answer is option 'C'. Can you explain this answer?

Explanation:

Main Function:
- An array arr is initialized with values {1, 2, 3, 4, 5}.
- The printValues function is called with arguments arr + 2 (pointer to the third element of the array) and 3 (size of the array).

printValues Function:
- The function takes a pointer to an integer array and its size as parameters.
- It iterates over the array starting from the given pointer and prints the values.
- In each iteration, it prints the value pointed to by the pointer and then increments the pointer to point to the next element.

Output:
- Since the printValues function is called with arr + 2 (pointer to the third element) and size 3, it will print the values starting from the third element of the array.
- The output will be: 3 4 5.
Therefore, the correct answer is option C) 2 3 4.

Consider the following code snippet:
#include <iostream>
void changeValue(int* ptr) {
    *ptr = 20;
    ptr = nullptr;
}
int main() {
    int x = 10;
    int* ptr = &x;
    changeValue(ptr);
    std::cout << (ptr == nullptr);
    return 0;
}
What will be the output of the above code?
  • a)
    0
  • b)
    1
  • c)
    Error
  • d)
    Undefined behavior
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The 'changeValue' function modifies the value at the memory location pointed to by 'ptr' to 20 and then sets 'ptr' to 'nullptr'. However, 'ptr' is a local copy of the pointer 'ptr' in the 'main' function, and changing it does not affect the original pointer. Therefore, '(ptr == nullptr)' will evaluate to '0', indicating that 'ptr' is not 'nullptr'.

Which of the following is the correct way to declare a pointer variable in C++?
  • a)
    'int* ptr';
  • b)
    'int ptr'*;
  • c)
    'int ptr';
  • d)
    *'int ptr';
Correct answer is option 'A'. Can you explain this answer?

The correct way to declare a pointer variable in C is option 'A': int* ptr.

Explanation:
- In C, a pointer is a variable that stores the memory address of another variable.
- To declare a pointer variable, we use an asterisk (*) symbol after the data type.
- The asterisk (*) indicates that the variable is a pointer.
- The name of the pointer variable can be any valid variable name.
- In this case, the pointer variable is named 'ptr'.
- The data type of the pointer variable is 'int', which means it can store the memory address of an integer variable.

Let's break down the options and see why option 'A' is correct:

a) int* ptr;
- This is the correct way to declare a pointer variable in C.
- The data type is 'int' and the pointer symbol (*) is placed after the data type.
- The variable name is 'ptr'.

b) int ptr*;
- This is an incorrect way to declare a pointer variable in C.
- The pointer symbol (*) should be placed before the variable name, not after.
- So, 'int ptr*' is not a valid declaration.

c) int ptr;
- This is a valid declaration, but it declares a regular integer variable, not a pointer variable.
- Without the pointer symbol (*), 'int ptr' declares an integer variable named 'ptr'.

d) *int ptr;
- This is an incorrect way to declare a pointer variable in C.
- The pointer symbol (*) should be placed after the data type, not before.
- So, '*int ptr' is not a valid declaration.

In summary, the correct way to declare a pointer variable in C is 'int* ptr'. This declaration indicates that 'ptr' is a pointer variable that can store the memory address of an integer variable.

When should function pointers be used in C++?
  • a)
    When passing a function as an argument to another function
  • b)
    When returning a function from another function
  • c)
    When implementing callbacks and event handlers
  • d)
    All of the above
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
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.

What is the syntax to declare a function pointer in C++?
  • a)
    'void (*ptr)();'
  • b)
    'voiid* ptr();'
  • c)
    'voiid (*ptr)();
  • d)
    'void* ptr();'
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
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.

What is the output of the following code snippet?
int x = 5;
int* ptr = &x;
cout << *ptr;
  • a)
    5
  • b)
    10
  • c)
    The program will not compile
  • d)
    Garbage value
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
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.

What is the output of the following code snippet?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr;
cout << ptr[2];
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    4
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
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.

Which keyword is used to allocate memory dynamically in C++?
  • a)
    new
  • b)
    malloc
  • c)
    alloc
  • d)
    dynamic
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The 'new' keyword is used to allocate memory dynamically in C++. It is used to allocate memory for objects and data structures during runtime.

What are smart pointers in C++?
  • a)
    Pointers that automatically deallocate memory when no longer needed
  • b)
    Pointers that automatically resize themselves when needed
  • c)
    Pointers that automatically update their values based on external changes
  • d)
    Pointers that can point to multiple memory locations simultaneously
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
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.

What is a null pointer in C++?
  • a)
    A pointer that points to the address 0
  • b)
    A pointer that is uninitialized
  • c)
    A pointer that has been deleted
  • d)
    A pointer that points to a garbage value
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
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.

What is the size of a pointer in C++?
  • a)
    It depends on the type of the variable the pointer points to
  • b)
    It is always 4 bytes
  • c)
    It is always 8 bytes
  • d)
    It is always 2 bytes
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
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.

What is the purpose of the 'const' keyword when used with a pointer in C++?
  • a)
    To make the pointer constant
  • b)
    To make the pointed value constant
  • c)
    To make both the pointer and the pointed value constant
  • d)
    To make the pointer point to a constant value
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
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.

What is the output of the following code snippet?
int* ptr = new int[5];
ptr[2] = 10;
cout << ptr[2];
delete[] ptr;
  • a)
    0
  • b)
    10
  • c)
    The program will not compile
  • d)
    Garbage value
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
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.

What is the output of the following code snippet?
int x = 5;
int* ptr = &x;
int** ptr2 = &ptr;
cout << **ptr2;
  • a)
    5
  • b)
    10
  • c)
    The program will not compile
  • d)
    Garbage value
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
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.

What is the purpose of using smart pointers in C++?
  • a)
    To avoid memory leaks
  • b)
    To increase the performance of the program
  • c)
    To enable polymorphism with pointers
  • d)
    To reduce the size of the code
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
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.

What is the difference between pass-by-value and pass-by-reference in C++?
  • a)
    Pass-by-value creates a copy of the variable, while pass-by-reference allows direct manipulation of the original variable.
  • b)
    Pass-by-value is used for primitive data types, while pass-by-reference is used for user-defined objects.
  • c)
    Pass-by-value is more efficient than pass-by-reference.
  • d)
    Pass-by-reference is only applicable to pointers.
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
Pass-by-value involves passing a copy of the variable to a function, so modifications made to the parameter inside the function do not affect the original variable. Pass-by-reference, on the other hand, allows the function to directly manipulate the original variable by passing its memory address. Changes made to the parameter inside the function affect the original variable.

What is the purpose of the dereference operator (*) in C++?
  • a)
    It creates a new pointer variable.
  • b)
    It retrieves the memory address of a variable.
  • c)
    It accesses the value stored at a memory address.
  • d)
    It assigns a value to a pointer variable.
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The dereference operator (*) in C++ is used to access the value stored at a memory address pointed to by a pointer. For example, *ptr retrieves the value stored at the memory address pointed to by the pointer variable ptr.

What is the output of the following code snippet?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr;
cout << *ptr++;
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    4
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
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.

What will be the output of the following code snippet?
#include <iostream>
void changeValue(int* ptr) {
    *ptr = 10;
}
int main() {
    int x = 5;
    int* ptr = &x;
    changeValue(ptr);
    std::cout << x;
    return 0;
}
  • a)
    5
  • b)
    10
  • c)
    Error
  • d)
    Undefined behavior
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The 'changeValue' function modifies the value at the memory location pointed to by 'ptr'. In the 'main' function, 'x' is passed as an argument to 'changeValue', which changes the value of 'x' to 10. Therefore, the output will be 10.

What is the output of the following code snippet?
int x = 5;
int* ptr1 = &x;
int* ptr2 = ptr1;
*ptr2 = 10;
cout << *ptr1;
  • a)
    5
  • b)
    10
  • c)
    The program will not compile
  • d)
    Garbage value
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
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.

Chapter doubts & questions for Pointers - Basics of C++ 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Pointers - Basics of C++ in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

Basics of C++

70 videos|45 docs|15 tests

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev