C++ Static Data Interview Questions And Answers
Download C++ Static Data Interview Questions and Answers PDF
Strengthen your C++ Static Data interview skills with our collection of 17 important questions. These questions will test your expertise and readiness for any C++ Static Data interview scenario. Ideal for candidates of all levels, this collection is a must-have for your study plan. Don't miss out on our free PDF download, containing all 17 questions to help you succeed in your C++ Static Data interview. It's an invaluable tool for reinforcing your knowledge and building confidence.
17 C++ Static Data Questions and Answers:
C++ Static Data Job Interview Questions Table of Contents:
1 :: Can you please explain what is class using C++?
A class holds the data and functions that operate on the data. It serves as the template of an object.
Read More2 :: What is static variable?
Static variables are the variables which has exactly one copy per class. They belong to the class as a whole but not for its instances (objects). All static variables are declared by using the modifier ‘static’.
For example:
Read MoreFor example:
3 :: What is static type varidentifier?
where type is the data type and varidentifier is the variable.
All static variables are initialized automatically with a default value but not explicitly initialized. The default value is depended on the data type of the variables.
Read MoreAll static variables are initialized automatically with a default value but not explicitly initialized. The default value is depended on the data type of the variables.
4 :: What is reference variable in C++?
A reference variable is just like pointer with few differences. It is declared using & operator. A reference variable must always be initialized. The reference variable once defined to refer to a variable can’t be changed to point to other variable. You can't create an array of references the way it is possible with pointer.
Read More5 :: What is local class in C++?
Local class is define within the scope of a function and nested within a function.
E.g.
int func1()
{
class localclass1
{.....};
}
Read MoreE.g.
int func1()
{
class localclass1
{.....};
}
6 :: What is static class data?
Static data members of a class are declared by preceding the member variable’s declaration with the keyword static. Only one copy of static data members exist and all objects of the class share that variable. Unlike regular data members, individual copies of a static member variable are not made for each object. How many ever no of objects of a class are created, only one copy of static data member is shared amongst all of them. All static variables are initialized to zero before the first object is created.
When a member variable is declared static within a class, it is not defined (ie storage is not allocated for it) We must provide a global definition for it outside the class. This is done by redeclaring the static variable using scope resolution operator (‘::’) to identify the class it belongs to. This causes storage for the class to be allocated.
Read MoreWhen a member variable is declared static within a class, it is not defined (ie storage is not allocated for it) We must provide a global definition for it outside the class. This is done by redeclaring the static variable using scope resolution operator (‘::’) to identify the class it belongs to. This causes storage for the class to be allocated.
7 :: Explain static type checking?
Static type checking performs the type checking operation before the execution of the program. To perform this operation, the arguments, expressions, variables must be given a data type.
Read More8 :: Explain dynamic type checking?
Dynamic type checking performs the type checking operation at the time of the program execution. To perform this operation, the arguments, expressions, variables must be given a data type.
Read More9 :: Explain what are volatile variables?
A volatile variable is a variable which is modified asynchronously by the threads that are concurrently running in a java application. A volatile variable does not allow having a copy of variable that is local. It is a different value from the value which is currently available in main memory. A volatile variable mandatorily have its data synchronized for all the threads. So that, whenever the value of the volatile variable is updated by any thread, all other threads can access the same value immediately. Higher access and update overhead are likely to the volatile variables on contrast to plain variables, as all threads have their own set of data for efficiency considerations.
Read More10 :: Explain the uses of static class data?
1. To provide access control mechanism to some shared resource used by all the objects of a class
2. To keep track of no of objects in existence of a particular class
Following example illustrates first case, to make use of static data member for access control:
#include <iostream>
using namespace std;
class MyClass
{
static int resource;
public:
int get_resource()
{
if (resource)
return 0;
else
{
resource = 1;
return 1;
}
}
void free_resource()
{
resource =0;
}
};
int MyClass::resource;
int main()
{
MyClass ob1, ob2;
if(ob1.get_resource())
cout <<”Resources with ob1”;
if(!ob2.get_resource())
cout <<”Resources denied to ob2”;
ob1.free_resource();
return 0;
}
Thus, the static member variable resource makes sure at a time only one object can access it.
Now, consider the second use: to keep track of no of objects:
#include <iostream>
using namespace std;
class MyClass
{
public:
static int cnt;
MyClass()
{
cnt++;
}
~MyClass()
{
cnt--;
}
};
void func()
{
MyClass temp;
cout << “No of Objects : “<< MyClass::cnt<<”\n”;
}
int MyClass::cnt;
int main()
{
cout <<”Entered main()\n”:
MyClass ob1;
cout << “No of Objects : “<< MyClass::cnt <<”\n”;
MyClass ob2;
cout << “No of Objects : “<< MyClass::cnt<<”\n”;
func();
cout << “No of Objects : “<< MyClass::cnt<<”\n”;
return 0;
}
Output would be:
Entered main()
No of Objects: 1
No of Objects: 2
No of Objects: 3
No of Objects: 2
Thus, only one copy of static member variable cnt is maintained for all the objects created and its value is incremented or decremented whenever and object is created or destroyed.
Read More2. To keep track of no of objects in existence of a particular class
Following example illustrates first case, to make use of static data member for access control:
#include <iostream>
using namespace std;
class MyClass
{
static int resource;
public:
int get_resource()
{
if (resource)
return 0;
else
{
resource = 1;
return 1;
}
}
void free_resource()
{
resource =0;
}
};
int MyClass::resource;
int main()
{
MyClass ob1, ob2;
if(ob1.get_resource())
cout <<”Resources with ob1”;
if(!ob2.get_resource())
cout <<”Resources denied to ob2”;
ob1.free_resource();
return 0;
}
Thus, the static member variable resource makes sure at a time only one object can access it.
Now, consider the second use: to keep track of no of objects:
#include <iostream>
using namespace std;
class MyClass
{
public:
static int cnt;
MyClass()
{
cnt++;
}
~MyClass()
{
cnt--;
}
};
void func()
{
MyClass temp;
cout << “No of Objects : “<< MyClass::cnt<<”\n”;
}
int MyClass::cnt;
int main()
{
cout <<”Entered main()\n”:
MyClass ob1;
cout << “No of Objects : “<< MyClass::cnt <<”\n”;
MyClass ob2;
cout << “No of Objects : “<< MyClass::cnt<<”\n”;
func();
cout << “No of Objects : “<< MyClass::cnt<<”\n”;
return 0;
}
Output would be:
Entered main()
No of Objects: 1
No of Objects: 2
No of Objects: 3
No of Objects: 2
Thus, only one copy of static member variable cnt is maintained for all the objects created and its value is incremented or decremented whenever and object is created or destroyed.
11 :: We can initialize a value of static variable of a class only when its object is created. No other initialization is permitted.
a. True
b. False
b. False
Read More12 :: Static variable in a class is initialized when:
a. every object of the class is created
b. last object of the class is created
c. first object of the class is created
d. No need to initialize static variable
c. first object of the class is created
Read More13 :: Which of the following are true about static member function?
1. They can access non-static data members
2. They can call only other static member functions
3. They can access global functions and data
4. They can have this pointer
5. They cannot be declared as const or volatile
a. Only 2
b. Only 2,5
c. Only 2,3,4,5
d. Only 2 , 3 , 5
e. All of these
d. Only 2 , 3 , 5
Read More14 :: Static variable declared in a class are also called_________.
a. instance variable
b. named constant
c. global variable
d. class variable
d. class variable
Read More15 :: Static variable must be declared in public section of the class.
a. True
b. False
b. False
Read More16 :: If a class contains static variable, then every object of the class has its copy of static variable.
a. True
b. False
b. False
Read More17 :: Default value of static variable is_____.
a. 0
b. 1
c. Garbage value
d. Compiler dependent
a. 0
Read More