You are on page 1of 9

Question 1

// Assume that integers take 4 bytes.


#include<iostream>
using namespace std;
class Test
{
static int i;
int j;
};
int Test::i;
int main()
{
cout << sizeof(Test);
return 0;
}
• Output: 4 (size of integer)
static data members do not contribute in size
of an object. So ‘i’ is not considered in size of
Test. Also, all functions (static and non-static
both) do not contribute in size.
Question 2
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
cout << sizeof("GeeksforGeeks") << endl;
cout << strlen("GeeksforGeeks");
return 0;
}
• Sizeof operator returns the size of string
including null character so output is 14. While
strlen() function returns the exact length of
string excluding null character so output is 13.
Question 3
#include <iostream>
using namespace std;

int fun(int a, int b = 1, int c =2)


{
return (a + b + c);
}

int main()
{
cout << fun(12, ,2);
return 0;
}
Question 4
#include<iostream> cout << "x = " << x << endl;
using namespace std; }
class Test };
{ int main()
private: {
int x; Test obj;
public: int x = 40;
void setX (int x) obj.setX(x);
{ obj.print();
Test::x = x; return 0;
} }
void print()
{
Question 5
#include <iostream>
using namespace std;
class X {
private:
static const int a = 76;
public:
static int getA()
{
return a;
}
};
int main() {
cout <<X::getA()<<endl;
return 0;
}
Question 6
#include<iostream>
using namespace std;
int x = 10;
void fun()
{
int x = 2;
{
int x = 1;
cout << ::x << endl;
}
}
int main()
{
fun();
return 0;
}

You might also like