C++ Programming

  Home  Computer Programming  C++ Programming


“Learn C++ Programming by C++ Interview Questions and Answers”



120 C++ Programming Questions And Answers

81⟩ Explain which of the following declarations will compile and what will be constant ...

Explain which of the following declarations will compile and what will be constant - a pointer or the value pointed at: * const char *

* char const *

* char * const

Note: Ask the candidate whether the first declaration is pointing to a string or a single character. Both explanations are correct, but if he says that it’s a single character pointer, ask why a whole string is initialized as char* in C++. If he says this is a string declaration, ask him to declare a pointer to a single character. Competent candidates should not have problems pointing out why const char* can be both a character and a string declaration, incompetent ones will come up with invalid reasons.

 183 views

85⟩ How do you decide which integer type to use?

It depends on our requirement. When we are required an integer to be stored in 1 byte (means less than or equal to 255) we use short int, for 2 bytes we use int, for 8 bytes we use long int.

A char is for 1-byte integers, a short is for 2-byte integers, an int is generally a 2-byte or 4-byte integer (though not necessarily), a long is a 4-byte integer, and a long long is a 8-byte integer.

 171 views

87⟩ What does extern mean in a function declaration?

Using extern in a function declaration we can make a function such that it can used outside the file in which it is defined.

An extern variable, function definition, or declaration also makes the described variable or function usable by the succeeding part of the current source file. This declaration does not replace the definition. The declaration is used to describe the variable that is externally defined.

If a declaration for an identifier already exists at file scope, any extern declaration of the same identifier found within a block refers to that same object. If no other declaration for the identifier exists at file scope, the identifier has external linkage.

 187 views

88⟩ How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?

Answer1

If you want the code to be even slightly readable, you will use typedefs.

typedef char* (*functiontype_one)(void);

typedef functiontype_one (*functiontype_two)(void);

functiontype_two myarray[N]; //assuming N is a const integral

Answer2

char* (* (*a[N])())()

Here a is that array. And according to question no function will not take any parameter value.

 206 views

89⟩ What is the auto keyword good for in C++?

Answer1

Not much. It declares an object with automatic storage duration. Which means the object will be destroyed at the end of the objects scope. All variables in functions that are not declared as static and not dynamically allocated have automatic storage duration by default.

For example

int main()

{

int a; //this is the same as writing “auto int a;”

}

Answer2

Local variables occur within a scope; they are “local” to a function. They are often called automatic variables because they automatically come into being when the scope is entered and automatically go away when the scope closes. The keyword auto makes this explicit, but local variables default to auto auto auto auto so it is never necessary to declare something as an auto auto auto auto.

 168 views

90⟩ What does extern mean in a function declaration in C++?

It tells the compiler that a variable or a function exists, even if the compiler hasn’t yet seen it in the file currently being compiled. This variable or function may be defined in another file or further down in the current file.

 171 views

99⟩ When is a template a better solution than a base class?

When you are designing a generic class to contain or otherwise manage objects of other types, when the format and behavior of those other types are unimportant to their containment or management, and particularly when those other types are unknown (thus, the genericity) to the designer of the container or manager class.

 161 views

100⟩ Explain the ISA and HASA class relationships. How would you implement each in a class design?

A specialized class "is" a specialization of another class and, therefore, has the ISA relationship with the other class. An Employee ISA Person. This relationship is best implemented with inheritance. Employee is derived from Person. A class may have an instance of another class. For example, an employee "has" a salary, therefore the Employee class has the HASA relationship with the Salary class. This relationship is best implemented by embedding an object of the Salary class in the Employee class.

 171 views