Open In App

Output of C++ programs | Set 40

Last Updated : 18 Oct, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Q.1 What is the output of following program? 

CPP
#include <iostream> 
using namespace std; 
int main() 
{ 
    int i, j, k; 
    int sum[2][4]; 
    for (i = 0; i < 2; i++) { 
        for (j = 0; j < 3; j++) 
            sum[i][j]; 
    } 
    cout << sum[i][j]; 
    return 0; 
} 

Option a) 3 3 b) 2 0 c) garbage value d) 2 3

Answer : c

Explanation : In this program, in given array we did not assign any value so it will be pointing to any garbage value. Q.2 What is the output of following program? 

CPP
#include <iostream> 
using namespace std; 
int main() 
{ 
    char m; 
    switch (m) { 
    case 'c': 
        cout << "Computer Science"; 
    case 'm': 
        cout << "Mathematics"; 
        break; 
    case 'a': 
        cout << "Accoutant"; 
        break; 
    default: 
        cout << "wrong choice"; 
    } 
    return 0; 
} 

Option a) Computer Science, Mathematics b) Mathematics c) wrong choice d) error

Answer : c

Explanation : In this program, m is a variable and it is not assigned any value which means - m is having any garbage value. So, it doesn't match any case and by default it will be print "wrong choice". Q.3 What is the output of following program? 

CPP
#include <iostream> 
using namespace std; 
#include <iostream> 
using namespace std; 
int main() 
{ 
    int i, x[5], y, z[5]; 
    for (i = 0; i < 5; i++) { 
        x[i] = i; 
        z[i] = i + 3; 
        y = z[i]; 
        x[i] = y++; 
    } 
    for (i = 0; i < 5; i++) 
        cout << x[i] << " "; 
    return 0; 
} 

Option a) 3 4 5 6 7 b) 4 5 6 7 8 c) 2 3 4 5 6 d) none of above

Answer : a

Explanation : In this loop, simply first assign the value of y and then increment so no change. Q.4 What is the output of following program? 

CPP
#include <iostream> 
using namespace std; 
int max(int& x, int& y, int& z) 
{ 
    if (x > y && y > z) { 
        y++; 
        z++; 
        return x++; 
    } else { 
        if (y > x) 
            return y++; 
        else
            return z++; 
    } 
} 
int main() 
{ 
    int A, B; 
    int a = 10, b = 13, c = 8; 
    A = max(a, b, c); 
    cout << a++ << " " << b-- << " " << ++c << endl; 
    B = max(a, b, c); 
    cout << ++A << " " << --B << " " << c++ << endl; 
    return 0; 
} 

Option a) 10 14 8 or 14 13 8 b) 10 13 8 or 11 14 9 c) 10 14 9 or 14 12 9 d) 11 12 8 or 13 14 8

Answer : c

Explanation : Here, max function is to return max value and at the time of first print, the value of b decrements and remaining two a and c are same and remain same during second call of function. Q.5 What is the output of following program? 

CPP
#include <iostream> 
using namespace std; 
int main() 
{ 
    for (int i = 10; i > 6; i = i - 2) 
        cout << i; 
    for (int i = -5; i > -7; i--) 
        cout << i + 1; 
    return 0; 
} 

Option a) 10 8 6 -5 -6 b) 10 8 -4 -5 c) 10 8 -5 -6 d) 8 6 -4 -5

Answer : b

Explanation : First loop is executed and prints 10, 8 and then condition gets false, so loop exits. Then second loop is executed and checks the condition and print -4, -5 after that the condition becomes false then it gets terminated.


Article Tags :
Practice Tags :

Similar Reads