Output of C++ programs | Set 50

Predict the output of the following C++ programs:

  • Question 1:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int main()
    {
        int ran = rand();
        cout << ran << endl;
        return 0;
    }

    chevron_right

    
    

    Output:

    1804289383
    

    Explanation: As the declared number is an integer, It will produce the random number from 0 to RAND_MAX. The value of RAND_MAX is library-dependent but is guaranteed to be at least 32767 on any standard library implementation.

  • Question 2:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <cstdlib>
    #include <iostream>
    using namespace std;
      
    int main()
    {
        cout << RAND_MAX << endl;
        return 0;
    }

    chevron_right

    
    

    Output:

    2147483647
    

    Explanation: The output is Compiler Dependent. RAND_MAX is a function used by the compiler to create a maximum random number.

  • Question 3:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
    int main()
    {
        void a = 10, b = 10;
        int c;
        c = a + b;
        cout << c;
        return 0;
    }

    chevron_right

    
    

    Output:

    Compile time error
    

    Explanation: void will not accept any values to its type.

  • Question 4:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
      
    using namespace std;
      
    int array1[] = { 1200, 200, 2300, 1230, 1543 };
    int array2[] = { 12, 14, 16, 18, 20 };
    int temp, result = 0;
    int main()
    {
        for (temp = 0; temp < 5; temp++) {
            result += array1[temp];
        }
        for (temp = 0; temp < 4; temp++) {
            result += array2[temp];
        }
        cout << result;
        return 0;
    }

    chevron_right

    
    

    Output:



    6533
    

    Explanation: In this program we are adding the every element of two arrays. All the elements of array1[] and array2[] will be added and the sum will be stored in result and hence output is 6533.

  • Question 5:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
    int main()
    {
        int a = 5, b = 10, c = 15;
        int arr[3] = { &a, &b, &c };
        cout << *arr[*arr[1] - 8];
        return 0;
    }

    chevron_right

    
    

    Output:

    Compile time error!
    

    Explanation: The conversion is invalid in this array. The array arr[] is declared to hold integer type value but we are trying to assign references(addresses) to the array so it will arise error. The following compilation error will be raised:

    cannot convert from ‘int *’ to ‘int’
  • Question 6:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
    int main()
    {
        int array[] = { 10, 20, 30 };
        cout << -2 [array];
        return 0;
    }

    chevron_right

    
    

    Output:

    -30
    

    Explanation: -2[array]: this statement is equivalent to -(array[2]). At the zero index 30 is stored hence negation of 30 will be printed due to unary operator (-).

  • Question 7:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
    int main()
    {
        int const p = 5;
        cout << ++p;
        return 0;
    }

    chevron_right

    
    

    Output:

    Compile time Error!
    

    Explanation: Constant variables are those whose value can’t be changed throughout the execution. ++p statement try to change the value hence compiler will raise an error.

  • Question 8:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
    int main()
    {
        char arr[20];
        int i;
        for (i = 0; i < 10; i++)
            *(arr + i) = 65 + i;
        *(arr + i) = '\0';
        cout << arr;
        return (0);
    }

    chevron_right

    
    

    Output:

    ABCDEFGHIJ
    

    Explanation: Each time we are assigning 65 + i. In first iteration i = 0 and 65 is assigned. So it will print from A to J.

  • Quesion 9:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
    int Add(int X, int Y, int Z)
    {
        return X + Y;
    }
    double Add(double X, double Y, double Z)
    {
        return X + Y;
    }
    int main()
    {
        cout << Add(5, 6);
        cout << Add(5.5, 6.6);
        return 0;
    }

    chevron_right

    
    

    Output:

    Compile time error!
    

    Explanation: Here we want to add two element but in the given functions we take 3 argumants.So compiler doesn’t get the reqired function(function with 2 arguments)

  • Question 10:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
    #define PR(id) cout << "The value of " #id " is " << id
    int main()
    {
        int i = 10;
        PR(i);
        return 0;
    }

    chevron_right

    
    

    Output:

    The value of i is 10
    

    Explanation: In this program, we are just printing the declared value through a macro. Carefully observe that in macro there is no semicolon(;) used as a termination statement.

This article is contributed by Avinash Kumar Singh email: avinash.sing.inbox@gmail.com. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Milan1999



Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.