ADVERTISEMENT
ADVERTISEMENT

C++ static Keyword Aptitude Questions and Answers

C++ static Keyword Aptitude: This section contains C++ static Keyword Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 22, 2021

1) In C++, the static can be?

  1. Variable
  2. Class data member
  3. Functions
  4. this pointer

Options:

  1. A and B
  2. A and C
  3. A, B, and C
  4. A, B, C, and D

2) All object of the class shares the static members of the class?

  1. Yes
  2. No

3) What is the default value of static variables?

  1. 0
  2. 1
  3. Garbage value
  4. None of the above

4) Which of the following operator is used to access static members of the class?

  1. .
  2. :
  3. ::
  4. ->

5) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

int a = 10;

static int fun()
{
    a = 20;
    cout << a << endl;
}

int main()
{
    fun();

    return 0;
}

Options:

  1. 10
  2. 20
  3. Compile-time error
  4. Runtime error

6) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
    static int A;

public:
    Sample()
    {
        A = 10;
    }
    void print()
    {
        cout << A << endl;
    }
};

int main()
{
    Sample S;

    S.print();

    return 0;
}

Options:

  1. 10
  2. 0
  3. Compile-time error
  4. Runtime error

7) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
    static int A;

public:
    static void set()
    {
        A = 20;
    }
    static void print()
    {
        cout << A << endl;
    }
};

int main()
{
    Sample.set();
    Sample.print();
    return 0;
}

Options:

  1. 20
  2. 0
  3. Compile-time error
  4. Runtime error

8) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
    static int A;

public:
    static void set()
    {
        A = 20;
    }
    static void print()
    {
        cout << A << endl;
    }
};

int Sample::A = 10;

int main()
{
    Sample.set();
    Sample.print();
    return 0;
}

Options:

  1. 10
  2. 20
  3. Compile-time error
  4. Runtime error

9) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
    static int A;

public:
    static void set()
    {
        A = 20;
    }
    static void print()
    {
        cout << A << endl;
    }
};

int Sample::A = 10;

int main()
{
    Sample::set();
    Sample::print();
    return 0;
}

Options:

  1. 10
  2. 20
  3. Compile-time error
  4. Runtime error

10) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
    static int A = 10;

public:
    static void set()
    {
        A = 20;
    }
    static void print()
    {
        cout << A << endl;
    }
};

int Sample::A;

int main()
{
    Sample::print();
    return 0;
}

Options:

  1. 10
  2. 20
  3. Compile-time error
  4. Runtime error

11) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
    static int A;

public:
    static void set()
    {
        A = 20;
    }
    static void print()
    {
        cout << A << endl;
    }
};

int Sample::A;

int main()
{
    Sample::print();
    return 0;
}

Options:

  1. 0
  2. 10
  3. 20
  4. Runtime error

12) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
    static int count;

public:
    Sample()
    {
        count++;
    }
    void printTotolObj()
    {
        cout << count << endl;
    }
};

int Sample::count = 0;

int main()
{
    Sample S1;
    Sample S2;

    Sample::printTotolObj();
    return 0;
}

Options:

  1. 10
  2. 0
  3. Compile-time error
  4. Runtime error

13) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
    static int count;

public:
    Sample()
    {
        count++;
    }
    static void printTotolObj()
    {
        cout << count << endl;
    }
};

int Sample::count = 0;

int main()
{
    Sample S1;
    Sample S2;

    Sample::printTotolObj();
    return 0;
}

Options:

  1. 2
  2. 0
  3. Compile-time error
  4. Runtime error

14) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
    static int count;

public:
    static Sample()
    {
        count++;
    }
    static void printTotolObj()
    {
        cout << count << endl;
    }
};

int Sample::count = 0;

int main()
{
    Sample S1;
    Sample S2;

    Sample::printTotolObj();
    return 0;
}

Options:

  1. 2
  2. 0
  3. Compile-time error
  4. Runtime error

15) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

static class Sample {
    static int count;

public:
    Sample()
    {
        count++;
    }
    static void printTotolObj()
    {
        cout << count << endl;
    }
};

int Sample::count = 0;

int main()
{
    Sample S1;
    Sample S2;

    Sample::printTotolObj();
    return 0;
}

Options:

  1. 2
  2. 0
  3. Compile-time error
  4. Runtime error

16) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
    static int count;

public:
    Sample()
    {
        count++;
    }

    void sayHello()
    {
        cout << "Hello, ";
    }
    static void printTotolObj()
    {
        sayHello();
        cout << count << endl;
    }
};

int Sample::count = 0;

int main()
{
    Sample S1;
    Sample S2;

    Sample::printTotolObj();
    return 0;
}

Options:

  1. Hello, 2
  2. 2
  3. Compile-time error
  4. Runtime error

17) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

struct Sample {
    static int count;
    Sample()
    {
        count++;
    }

    static void sayHello()
    {
        cout << "Hello, ";
    }
    static void printTotolObj()
    {
        sayHello();
        cout << count << endl;
    }
};

int Sample::count = 0;

int main()
{
    Sample S1;
    Sample S2;

    Sample::printTotolObj();
    return 0;
}

Options:

  1. Hello, 2
  2. 2
  3. Compile-time error
  4. Runtime error

18) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

static struct Sample {
    static int count;
    Sample()
    {
        count++;
    }

    static void sayHello()
    {
        cout << "Hello, ";
    }
    static void printTotolObj()
    {
        sayHello();
        cout << count << endl;
    }
};

int Sample::count = 0;

int main()
{
    Sample S1;
    Sample S2;

    Sample::printTotolObj();
    return 0;
}

Options:

  1. Hello, 2
  2. 2
  3. Compile-time error
  4. Runtime error

19) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

struct Sample {
    static int count;
    Sample()
    {
        count++;
    }

    static void sayHello()
    {
        cout << "Hello, ";
    }
    static void printTotolObj()
    {
        sayHello();
        cout << count << endl;
    }
};

int main()
{
    Sample S1;
    Sample S2;

    Sample::printTotolObj();
    return 0;
}

Options:

  1. Hello, 2
  2. Hello, 0
  3. Compile-time error
  4. Runtime error

20) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

struct Sample {
    static int count;
    Sample()
    {
        count++;
    }

    static void sayHello()
    {
        cout << "Hello, ";
    }
    static void printTotolObj()
    {
        sayHello();
        cout << count << endl;
    }
};

int Sample::count = 0;
int main()
{
    static Sample S1;
    static Sample S2;

    Sample::printTotolObj();
    return 0;
}

Options:

  1. Hello, 2
  2. Hello, 0
  3. Compile-time error
  4. Runtime error



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.