Skip to content
Related Articles

Related Articles

Output of C++ programs | Set 36
  • Difficulty Level : Easy
  • Last Updated : 15 Aug, 2017

Ques 1. What is the output of the following assuming that the size of int is 4 bytes.




#include <iostream>
using namespace std;
  
class abc {
    void f();
    void g();
    int x;
};
  
main()
{
    cout << sizeof(abc) << endl;
}


Options :
A) 12
B) 4
C) 8
D) Compile error

Answer : B

Explanation :
Only the class member variables constitutes as the size of the class or its object.So in this case we have to void and one int value so total size of class will be 0 + 0 + 4.(consider int to be 4 byte).

Ques 2. What is the output of the following




#include <iostream>
using namespace std;
  
int main()
{
    int a[] = { 1, 2 }, *p = a;
    cout << p[1];
}


Options :
A) 1
B) 2
C) Compile error
D) Runtime error



Answer : B

Explanation :
when we assign array ‘a’ to pointer ‘p’, it will hold the base address of the array. We can access array using ‘p’ just like with ‘a’


Ques 3
. What is the output of the following




#include <iostream>
using namespace std;
  
int main()
{
    int a[] = { 10, 20, 30 };
    cout << *a + 1;
}


Options :
A) 10
B) 20
C) 11
D) 21

Answer : C

Explanation :
*a refers to 10 and adding a 1 to it gives 11.


Ques 4
. What is the output of the following




#include <iostream>
using namespace std;
  
class Box {
    double width;
  
public:
    friend void printWidth(Box box);
    void setWidth(double wid);
};
void Box::setWidth(double wid)
{
    width = wid;
}
void printWidth(Box box)
{
    box.width = box.width * 2;
    cout << "Width of box : " << box.width << endl;
}
int main()
{
    Box box;
    box.setWidth(10.0);
    printWidth(box);
    return 0;
}


Options :
A) 40
B) 5
C) 10
D) 20

Answer : D

Explanation :
We are using friend function because we want to print the value of box width. Which is a private member function of a class and we can not access the private member outside the class.
setWidth(10.0) set width to 10 and printWidth( box ) print (width * 2) i.e 20.

Ques 5. What is the output of the following




#include <iostream>
using namespace std;
struct a {
    int count;
};
struct b {
    int* value;
};
struct c : public a, public b {
};
int main()
{
    c* p = new c;
    p->value = 0;
    cout << "Inherited";
    return 0;
}


Options :
A) Inherited
B) Error
C) Runtime error
D) None of the mentioned

Answer : A

Explanation : class or structure ‘c’ inheriting class ‘a’ as well as class ‘b’. When we create an object p of class c, both a and b are automatically inherited means we can access the attributes of class a and b.
So, p->value will be set to 0 as given in the question and then print inherited and exit from the code.

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

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :