Open In App

Output of C++ programs | Set 44

Output In C++
Q.1 What is the output of below program?




#include <iostream>
using namespace std;
int main()
{
    int x = 0;
    x = printf("Hello World");
    printf(" %d", x);
    return 0;
}

Option
a) Hello World 10
b) Hello World 11
c) Hello World 12
d) Hello World 0

ans :- b

Explanation : printf() returns count of characters successfully written on screen. In this program, printf() writes 10 character + 1 space total 11 so x is 11.



Q.2 What is the output of below program assuming that a character takes 1 byte and a pointer takes 8 bytes.




#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    char a[] = "Hello World";
    char* p = a;
    printf("%d %d %d", sizeof(a), sizeof(p), strlen(a));
    return 0;
}

Option
a) 12 10 8
b) 12 4 10
c) 12 8 10
d) 12 8 11

ans :- d

Explanation :
sizeof(a) : Character array size is 12 because in Hello World there are : 10 characters 1 space and 1 NULL.
sizeof(p) : 8 is pointer size of a pointer. Note that all pointer types take same size on a particular compiler.
strlen(a) : 11 is string length because length function not contain NULL character.



Q.3 What is the output of below program?




#include <iostream>
using namespace std;
int main()
{
    printf("%d", 'C' > 'A');
    return 0;
}

Option
a) 1
b) 0
c) 67
d) Error

ans :- a

Explanation : This code compares ASCII values of A and C and return 1 because ASCII value of C greater than that of A.

Q.4 What is the output of below program?




#include <iostream>
using namespace std;
int main()
{
    for (int i = 1; i <= 10; i++) {
        int k = i & (i << 1);
        if (k)
            printf("%d, ", i);
    }
}

Option
a) 1, 2, 4, 5, 8, 9
b) 3, 6, 7
c) 0
d) None of these

Ans :b

Explanation : This program basically prints all numbers (smaller than or equal to 10) that don’t have consecutive 1’s in binary representation. Please refer this article for details.


Article Tags :