Set - 4

Question 1 :

What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator?

Answer :

An internal iterator is implemented with member functions of the class that has items to step through. .An external iterator is implemented as a separate class that can be "attach" to the object that has items to step through. .An external iterator has the advantage that many difference iterators can be active simultaneously on the same object.

Question 2 :

What is a scope resolution operator?

Answer :

A scope resolution operator (::), can be used to define the member functions of a class outside the class.

Question 3 :

What do you mean by pure virtual functions? 

Answer :

A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero.

class Shape { public: virtual void draw() = 0; };


Question 4 :

What is polymorphism? Explain with an example? 

Answer :

"Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object.
Example: function overloading, function overriding, virtual functions. Another example can be a plus '+' sign, used for adding two integers or for using it to concatenate two strings.

Question 5 :

What's the output of the following program? Why?

Answer :

#include <stdio.h>
	typedef union{
		int a;
		char b[10];
		float c;

	Union x,y = {100};
	x.a = 50;
	x.c = 21.50;

	printf(\"Union x : %d %s %f \n\",x.a,x.b,x.c );
	printf(\"Union y :%d %s%f \n\",y.a,y.b,y.c);

Given inputs X, Y, Z and operations | and & (meaning bitwise OR and AND, respectively)
What is output equal to in

output = 

(X & Y) | (X & Z) | (Y & Z)


Question 6 :

Why are arrays usually processed with for loop?

Answer :

The real power of arrays comes from their facility of using an index variable to traverse the array, accessing each element with the same expression a[i]. All the is needed to make this work is a iterated statement in which the variable i serves as a counter, incrementing from 0 to a.length -1. That is exactly what a loop does.

Question 7 :

What is an HTML tag?

Answer :

An HTML tag is a syntactical construct in the HTML language that abbreviates specific instructions to be executed when the HTML script is loaded into a Web browser. It is like a method in Java, a function in C++, a procedure in Pascal, or a subroutine in FORTRAN.

Question 8 :

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 

Answer :

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.

Question 9 :

You're given a simple code for the class Bank Customer. Write the following functions: 
* Copy constructor 
* = operator overload
* == operator overload
* + operator overload (customers' balances should be added up, as an example of joint account between husband and wife)

Answer :

Note:Anyone confusing assignment and equality operators should be dismissed from the interview. The applicant might make a mistake of passing by value, not by reference. The candidate might also want to return a pointer, not a new object, from the addition operator. Slightly hint that you'd like the value to be changed outside the function, too, in the first case. Ask him whether the statement customer3 = customer1 + customer2 would work in the second case.

Question 10 :

What problems might the following macro bring to the application? 

Answer :

#define sq(x) x*x


Question 11 :

Anything wrong with this code?

T *p = new T[10]; 
delete p;


Answer :

Everything is correct, Only the first element of the array will be deleted", The entire array will be deleted, but only the first element destructor will be called.

Question 12 :

Anything wrong with this code?

T *p = 0;
delete p;


Answer :

Yes, the program will crash in an attempt to delete a null pointer.

Question 13 :

How do you decide which integer type to use?

Answer :

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.

Question 14 :

What does extern mean in a function declaration?

Answer :

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.

Question 15 :

What can I safely assume about the initial values of variables which are not explicitly initialized?

Answer :

It depends on complier which may assign any garbage value to a variable if it is not initialized.

Question 16 :

What is an accessor?

Answer :

An accessor is a class operation that does not modify the state of an object. The accessor functions need to be declared as const operations

Question 17 :

Differentiate between a template class and class template ?

Answer :

Template class: A generic definition or a parameterized class not instantiated until the client provides the needed information. It's jargon for plain templates. Class template: A class template specifies how individual classes can be constructed much like the way a class specifies how individual objects can be constructed. It's jargon for plain classes.

Question 18 :

When does a name clash occur?

Answer :

A name clash occurs when a name is defined in more than one place. For example., two different class libraries could give two different classes the same name. If you try to use many class libraries at the same time, there is a fair chance that you will be unable to compile or link the program because of name clashes.

Question 19 :

Define namespace?

Answer :

It is a feature in C++ to minimize name collisions in the global name space. This namespace keyword assigns a distinct name to a library that allows other libraries to use the same identifier names without creating any name collisions. Furthermore, the compiler uses the namespace signature for differentiating the definitions.

Question 20 :

What is the use of 'using' declaration. ?

Answer :

A using declaration makes it possible to use a name from a namespace without the scope operator.