Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
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?
- Variable
- Class data member
- Functions
- this pointer
Options:
- A and B
- A and C
- A, B, and C
- A, B, C, and D
Correct Answer - 3
A, B, and C
Explanation:
In C++, the static can be variable, class data members and functions, etc.
2) All object of the class shares the static members of the class?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
Yes, all object of the class shares the static members of the class. Because static members belong to the class, they are not the parts of the object.
3) What is the default value of static variables?
- 0
- 1
- Garbage value
- None of the above
Correct Answer - 1
0
Explanation:
The default value of static variables is 0.
4) Which of the following operator is used to access static members of the class?
- .
- :
- ::
- ->
Correct Answer - 3
::
Explanation:
The Scope Resolution Operator (::) is used to access static members of the class.
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:
- 10
- 20
- Compile-time error
- Runtime error
Correct Answer - 2
20
Explanation:
The above code will print "20" on the console screen.
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:
- 10
- 0
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate a compile-time 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:
- 20
- 0
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate a compile-time 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:
- 10
- 20
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate a compile-time 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:
- 10
- 20
- Compile-time error
- Runtime error
Correct Answer - 2
20
Explanation:
The above code will print "20" on the console screen.
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:
- 10
- 20
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate a compile-time 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:
- 0
- 10
- 20
- Runtime error
Correct Answer - 1
0
Explanation:
The above code will print "0" on the console screen.
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:
- 10
- 0
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate a compile-time 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:
- 2
- 0
- Compile-time error
- 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:
- 2
- 0
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate compile-time error, because constructor cannot be a static member function.
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:
- 2
- 0
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate compile-time error, because class cannot be defined as a static.
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:
- Hello, 2
- 2
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate a compile-time error because non-static member function cannot be called by static member function.
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:
- Hello, 2
- 2
- Compile-time error
- Runtime error
Correct Answer - 1
Hello, 2
Explanation:
The above code will print "Hello, 2" on the console screen.
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:
- Hello, 2
- 2
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate a compile-time error because we cannot use static keyword in structure definition.
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:
- Hello, 2
- Hello, 0
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate compile-time error, because definition of static member is missing.
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:
- Hello, 2
- Hello, 0
- Compile-time error
- Runtime error
Correct Answer - 1
Hello, 2
Explanation:
The above code will print "Hello, 2" on the console screen.