C++ Programs and their outputs

C++ Programs and their outputs

Question 1:

#include <iostream>

int main() {
    int x = 5;
    int y = ++x + x++;
    std::cout << y;
    return 0;
}

Explanation: The expression ++x + x++ involves both pre-increment and post-increment operators. The order of evaluation of operands is not defined, leading to undefined behavior. Therefore, the output of this program is unpredictable.

Question 2:

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int* ptr = arr;
    std::cout << *ptr++;
    return 0;
}

Explanation: The post-increment operator ++ has higher precedence than the dereference operator *. Therefore, *ptr++ first dereferences the pointer ptr, printing the value 1, and then increments the pointer itself. The output will be 1.

Question 3:

#include <iostream>

int main() {
    char str[] = "Hello";
    char* ptr = str;
    ptr += 2;
    std::cout << *ptr;
    return 0;
}

Explanation: The pointer ptr is initially pointing to the first character 'H' in the string "Hello". By adding 2 to the pointer, it advances by 2 elements, pointing to the third character 'l'. Therefore, the output will be l.

Question 4:

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int* ptr = arr + 3;
    std::cout << ptr[-1];
    return 0;
}

Explanation: The expression ptr[-1] is equivalent to *(ptr - 1), which accesses the element preceding the address pointed to by ptr. In this case, ptr points to the fourth element of the array, so ptr[-1] accesses the third element with a value of 3. The output will be 3.

Question 5:

#include <iostream>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int* ptr = arr;
    std::cout << ptr[10];
    return 0;
}

Explanation: The expression ptr[10] attempts to access the element at an index beyond the bounds of the array. This results in undefined behavior as it accesses memory that is outside the allocated range for the array arr. The output of this program is unpredictable.

Question 6:

#include <iostream>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int* ptr = arr + 2;
    std::cout << *(ptr - 1);
    return 0;
}

Explanation: The expression *(ptr - 1) accesses the element preceding the address pointed to by ptr. In this case, ptr points to the third element of the array, so *(ptr - 1) accesses the second element with a value of 2. The output will be 2.

Question 7:

#include <iostream>

int main() {
    int x = 5;
    int& ref = x;
    ref++;
    std::cout << x;
    return 0;
}

Explanation: The reference ref is an alias

to the variable x. When ref is incremented, it modifies the original variable x. Therefore, the value of x is incremented to 6, and the output will be 6.

Question 8:

#include <iostream>

int main() {
    int x = 5;
    int* ptr = &x;
    *ptr = 10;
    std::cout << x;
    return 0;
}

Explanation: The pointer ptr holds the address of x. By dereferencing the pointer with *ptr and assigning a value of 10, it directly modifies the variable x. Therefore, the value of x is updated to 10, and the output will be 10.

Question 9:

#include <iostream>

void swap(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 5;
    int y = 10;
    swap(x, y);
    std::cout << x << " " << y;
    return 0;
}

Explanation: The swap function takes two integer references (int&) as parameters and swaps their values using a temporary variable. When x and y are passed to the swap function, their values are swapped. Therefore, x becomes 10 and y becomes 5. The output will be 10 5.

Question 10:

#include <iostream>

int& getValue() {
    int x = 5;
    return x;
}

int main() {
    int& ref = getValue();
    std::cout << ref;
    return 0;
}

Explanation: The getValue function returns a reference to a local variable x. However, once the function exits, the local variable goes out of scope and is destroyed. Therefore, the reference ref in the main function becomes a dangling reference, pointing to invalid memory. The output of this program is unpredictable.

Question 11:

#include <iostream>

int* createArray(int size) {
    int arr[size];
    for (int i = 0; i < size; i++) {
        arr[i] = i + 1;
    }
    return arr;
}

int main() {
    int* ptr = createArray(5);
    for (int i = 0; i < 5; i++) {
        std::cout << ptr[i] << " ";
    }
    return 0;
}

Explanation: The createArray function creates a local array arr and initializes its elements. However, once the function exits, the local array goes out of scope and is destroyed. Therefore, the pointer ptr in the main function points to an invalid memory location, resulting in undefined behavior.

Question 12:

#include <iostream>

void printString(const char* str) {
    std::cout << str;
}

int main() {
    std::string name = "John";
    printString(name.c_str());
    return 0;
}

Explanation: The printString function takes a const char* as a parameter and attempts to print it as a C-style string. The c_str() function converts the std::string name to a C-style string. Therefore, the output will be John.

Question 13:

#include <iostream>

int main() {
    int x = 5;
    int* ptr1 = &x;
    int* ptr2 = ptr1;
    std::cout <<

 *ptr2;
    return 0;
}

Explanation: The pointer ptr1 holds the address of x. Then, ptr2 is assigned the value of ptr1, making it also point to x. Therefore, *ptr2 dereferences ptr2 and accesses the value of x, which is 5. The output will be 5.

Question 14:

#include <iostream>

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

Explanation: The pointer ptr points to the first element of the array arr. By using the index operator [], ptr[2] accesses the element at index 2, which has a value of 3. The output will be 3.

Question 15:

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int* ptr = arr;
    std::cout << *(ptr + 3);
    return 0;
}

Explanation: The pointer ptr points to the first element of the array arr. By adding 3 to the pointer and dereferencing it with *(ptr + 3), it accesses the element at an offset of 3 from the initial pointer position, which is the fourth element with a value of 4. The output will be 4.

Question 16:

#include <iostream>

int main() {
    int arr[5];
    int* ptr = arr;
    std::cout << sizeof(arr) / sizeof(*ptr);
    return 0;
}

Explanation: The expression sizeof(arr) / sizeof(*ptr) calculates the size of the array arr divided by the size of an individual element (*ptr). Since arr is an array of 5 integers and *ptr is an integer, the result will be 5. The output will be 5.

Question 17:

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int* ptr = &arr[2];
    std::cout << ptr[-1];
    return 0;
}

Explanation: The expression ptr[-1] is equivalent to *(ptr - 1), which accesses the element preceding the address pointed to by ptr. In this case, ptr points to the third element of the array, so ptr[-1] accesses the second element with a value of 2. The output will be 2.

Question 18:

#include <iostream>

int main() {
    int x = 5;
    int* ptr = &x;
    *ptr = 10;
    ptr = nullptr;
    std::cout << *ptr;
    return 0;
}

Explanation: After setting ptr to nullptr, the pointer no longer points to a valid memory location. Therefore, dereferencing ptr with *ptr results in undefined behavior. The output of this program is unpredictable.

Question 19:

#include <iostream>

int main() {
    int* ptr = new int;
    std::cout << *ptr;
    delete ptr;
    std::cout << *ptr;
    return 0;
}

Explanation: After deleting the memory allocated by new, the pointer ptr becomes a dangling pointer, as it

points to memory that has been deallocated. Dereferencing ptr with *ptr after deletion results in undefined behavior. The output of this program is unpredictable.

Question 20:

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int* ptr = arr;
    ptr += 2;
    std::cout << *ptr;
    return 0;
}

Explanation: By adding 2 to the pointer ptr, it advances by 2 elements, pointing to the third element of the array with a value of 3. Therefore, *ptr dereferences the pointer and retrieves the value 3. The output will be 3.

Question 21:

#include <iostream>

int main() {
    char str[] = "Hello";
    char* ptr = str;
    std::cout << ptr++;
    return 0;
}

Explanation: The expression ptr++ increments the pointer ptr to point to the next memory location. When ptr is printed directly, it prints the memory address it points to, not the string content. Therefore, the output will be the memory address of the string.

Question 22:

#include <iostream>

int main() {
    int x = 5;
    int* ptr = &x;
    std::cout << ptr;
    return 0;
}

Explanation: The pointer ptr holds the address of the variable x. When ptr is printed directly, it prints the memory address it points to. Therefore, the output will be the memory address of x.

Question 23:

#include <iostream>

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

Explanation: The pointer ptr points to the first element of the array arr. When ptr is printed directly, it prints the memory address it points to. Therefore, the output will be the memory address of the first element of the array.

Question 24:

#include <iostream>

int main() {
    int x = 5;
    int* ptr = &x;
    std::cout << *ptr++;
    return 0;
}

Explanation: The expression *ptr++ is equivalent to *(ptr++). The post-increment operator ++ has higher precedence than the dereference operator *. Therefore, the pointer ptr is incremented after the dereferencing operation. The value of x is printed, which is 5. The output will be 5.

Question 25:

#include <iostream>

int main() {
    int x = 5;
    int* ptr = &x;
    std::cout << (*ptr)++;
    return 0;
}

Explanation: The expression (*ptr)++ dereferences ptr and increments the value it points to. Therefore, the value of x is incremented to 6, and the output will be 5.

Question 26:

#include <iostream>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int* ptr = arr;
    std::cout << *(ptr + 2);
    return 0;
}

Explanation: By adding 2 to the pointer ptr and dereferencing it with *(ptr + 2), it accesses the element at an

offset of 2 from the initial pointer position, which is the third element with a value of 3. The output will be 3.

Question 27:

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int* ptr = arr;
    std::cout << ptr[10];
    return 0;
}

Explanation: The expression ptr[10] attempts to access the element at an index beyond the bounds of the array. This results in undefined behavior as it accesses memory that is outside the allocated range for the array arr. The output of this program is unpredictable.

Question 28:

#include <iostream>

int main() {
    int* ptr = nullptr;
    std::cout << *ptr;
    return 0;
}

Explanation: Dereferencing a nullptr pointer results in undefined behavior, as it does not point to a valid memory location. The output of this program is unpredictable.

Question 29:

#include <iostream>

int main() {
    int* ptr = new int;
    std::cout << *ptr;
    delete ptr;
    std::cout << *ptr;
    return 0;
}

Explanation: After deleting the memory allocated by new, the pointer ptr becomes a dangling pointer, as it points to memory that has been deallocated. Dereferencing ptr with *ptr after deletion results in undefined behavior. The output of this program is unpredictable.

Question 30:

#include <iostream>

int main() {
    int arr[5];
    int* ptr = arr;
    std::cout << sizeof(arr) / sizeof(ptr);
    return 0;
}

Explanation: The expression sizeof(arr) / sizeof(ptr) calculates the size of the array arr divided by the size of a pointer (ptr). Since arr is an array of 5 integers and ptr is a pointer, the result will be the ratio of their sizes, which depends on the architecture and compiler. The output may vary.

Question 31:

#include <iostream>

int main() {
    int x = 5;
    int& ref = x;
    std::cout << ref++;
    return 0;
}

Explanation: The expression ref++ increments the referenced variable x and returns its original value before the increment. Therefore, the value of x is incremented to 6, but the output will be 5.

Question 32:

#include <iostream>

int main() {
    int x = 5;
    int& ref = x;
    std::cout << ++ref;
    return 0;
}

Explanation: The expression ++ref increments the referenced variable x and returns the updated value. Therefore, the value of x is incremented to 6, and the output will be 6.

Question 33:

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int* ptr = &arr[2];
    std::cout << ptr[1];
    return 0;
}

Explanation: The pointer ptr points to the third element of the array arr. By using the index operator [], ptr[1] accesses the element at index 1 after the current pointer position, which is the fourth element with a value of 4. The output will be 4.

Question 34:

#include <iostream>

int main() {
    int arr

[] = {1, 2, 3, 4, 5};
    int* ptr = &arr[2];
    std::cout << *(ptr - 1);
    return 0;
}

Explanation: By subtracting 1 from the pointer ptr and dereferencing it with *(ptr - 1), it accesses the element preceding the address pointed to by ptr. In this case, ptr points to the third element of the array, so *(ptr - 1) accesses the second element with a value of 2. The output will be 2.

Question 35:

#include <iostream>

int main() {
    int x = 5;
    int* ptr1 = &x;
    int* ptr2 = ptr1;
    *ptr1 = 10;
    std::cout << *ptr2;
    return 0;
}

Explanation: The pointer ptr1 points to the variable x, and ptr2 is assigned the value of ptr1, making it also point to x. Therefore, modifying the value pointed to by ptr1 with *ptr1 = 10 also affects the value accessed through ptr2. The value of x is updated to 10, and the output will be 10.

Question 36:

#include <iostream>

int main() {
    int arr[3] = {1, 2, 3};
    int* ptr = arr;
    std::cout << ptr[3];
    return 0;
}

Explanation: The expression ptr[3] attempts to access the element at index 3, which is outside the bounds of the array. This results in undefined behavior as it accesses memory that is beyond the allocated range for the array arr. The output of this program is unpredictable.

Question 37:

#include <iostream>

int main() {
    int arr[5];
    int* ptr = arr + 2;
    std::cout << *ptr;
    return 0;
}

Explanation: By adding 2 to the array arr and assigning it to the pointer ptr, the pointer points to the third element of the array. Therefore, *ptr dereferences the pointer and retrieves the value at that position. Since the array is uninitialized, the output will be unpredictable.

Question 38:

#include <iostream>

int main() {
    int x = 5;
    int* ptr = &x;
    std::cout << ptr[0];
    return 0;
}

Explanation: The expression ptr[0] is equivalent to *(ptr + 0), which dereferences the pointer and accesses the element at index 0. In this case, it retrieves the value of x, which is 5. The output will be 5.

Question 39:

#include <iostream>

int main() {
    int x = 5;
    int* ptr = &x;
    std::cout << *ptr--;
    return 0;
}

Explanation: The expression *ptr-- is equivalent to *(ptr--). The post-decrement operator -- has higher precedence than the dereference operator *. Therefore, the pointer ptr is decremented after the dereferencing operation. The value of x is printed, which is 5. The output will be 5.

Question 40:

#include <iostream>

int main() {
    int x = 5;
    int* ptr = &x;
    std::cout << (*ptr

)--;
    return 0;
}

Explanation: The expression (*ptr)-- dereferences ptr and decrements the value it points to. Therefore, the value of x is decremented to 4, and the output will be 5.

Question 41:

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int* ptr = &arr[4];
    std::cout << *(ptr - 3);
    return 0;
}

Explanation: By subtracting 3 from the pointer ptr and dereferencing it with *(ptr - 3), it accesses the element at an offset of 3 before the current pointer position, which is the second element with a value of 2. The output will be 2.

Question 42:

#include <iostream>

int main() {
    int arr[3] = {1, 2, 3};
    int* ptr = arr;
    std::cout << *(ptr++);
    return 0;
}

Explanation: The expression *(ptr++) first dereferences ptr and retrieves the value at that position (1). Then, the post-increment operator ++ is applied to the pointer ptr, incrementing it to point to the next memory location. The output will be 1.

Question 43:

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int* ptr = &arr[3];
    std::cout << ptr[-2];
    return 0;
}

Explanation: The expression ptr[-2] is equivalent to *(ptr - 2), which accesses the element preceding the address pointed to by ptr by 2 positions. In this case, ptr points to the fourth element of the array, so ptr[-2] accesses the second element with a value of 2. The output will be 2.

Question 44:

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int* ptr1 = &arr[0];
    int* ptr2 = &arr[4];
    std::cout << ptr2 - ptr1;
    return 0;
}

Explanation: By subtracting the pointer ptr1 from ptr2, we can determine the number of elements between the two pointers. In this case, ptr1 points to the first element of the array, and ptr2 points to the fifth element. Therefore, the difference ptr2 - ptr1 will be 4, representing the number of elements between them. The output will be 4.

Question 45:

#include <iostream>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int* ptr = &arr[4];
    std::cout << ptr - arr;
    return 0;
}

Explanation: By subtracting the array arr from the pointer ptr, we can determine the offset in terms of elements between the two pointers. In this case, ptr points to the fifth element of the array, and arr points to the first element. Therefore, the difference ptr - arr will be 4, representing the offset of 4 elements between them. The output will be 4.

Question 46:

#include <iostream>



int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int* ptr = arr;
    std::cout << ptr + 5;
    return 0;
}

Explanation: By adding 5 to the pointer ptr, it advances by 5 elements. However, when printing the pointer directly, it prints the memory address it points to, not the string content. Therefore, the output will be the memory address after advancing 5 elements from the initial position.

Question 47:

#include <iostream>

int main() {
    int x = 5;
    int* ptr = &x;
    std::cout << ptr[-1];
    return 0;
}

Explanation: The expression ptr[-1] is equivalent to *(ptr - 1), which accesses the element preceding the address pointed to by ptr. In this case, ptr points to the variable x, so ptr[-1] accesses memory before x, which results in undefined behavior. The output of this program is unpredictable.

Question 48:

#include <iostream>

int main() {
    int* ptr = new int[5];
    std::cout << *ptr;
    delete[] ptr;
    std::cout << *ptr;
    return 0;
}

Explanation: After deleting the memory allocated by new[], the pointer ptr becomes a dangling pointer, as it points to memory that has been deallocated. Dereferencing ptr with *ptr after deletion results in undefined behavior. The output of this program is unpredictable.

Question 49:

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int* ptr = arr;
    std::cout << *(ptr + 5);
    return 0;
}

Explanation: The expression *(ptr + 5) attempts to access the element at an offset of 5 from the initial pointer position. However, the pointer ptr initially points to the first element of the array, and adding 5 to it exceeds the bounds of the array. This results in undefined behavior as it accesses memory that is outside the allocated range for the array arr. The output of this program is unpredictable.

Question 50:

#include <iostream>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int* ptr = arr + 1;
    std::cout << ptr[-1];
    return 0;
}

Explanation: By subtracting 1 from the pointer ptr and dereferencing it with ptr[-1], it accesses the element preceding the address pointed to by ptr. In this case, ptr points to the second element of the array, so ptr[-1] accesses the first element with a value of 1. The output will be 1.