Output of C++ programs | Set 44

3

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.

This article is contributed by Gyayak Jain . 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.

GATE CS Corner    Company Wise Coding Practice

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

Recommended Posts:



3 Average Difficulty : 3/5.0
Based on 5 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.