C++ Interview Questions
C++ Interview Questions
C++ Interview Questions
int main()
{ char *myCharData[20];
for (int nLoop =0;nLoop < 20; ++nLoop) { myCharData[nLoop ] = new char[256];
strcpy(myCharData[nLoop],"SABITH");
......
}
........................
/*Some manipulations here using myCharData*/
/*Now here we have to clear the data. The place can vary according to ur program. This
being a simple program,u can clear at the end*/
for(int nLoop =0;nLoop < 20; ++nLoop)
{
delete[] myCharData[nLoop ];
}
return 0;
Question: How do you write a program which produces its own source code as its
output?
Answer :write this program and save it as pro.c....run....it will show the program to the
consol and write the source code into data.txt file...................
#include<stdio.h>
void main()
{
FILE *fp,*ft;
char buff[100];
fp=fopen("pro.c","r");
if(fp==NULL)
{
printf("ERROR");
}
ft=fopen("data.txt","w+");
if(ft==NULL)
{
printf("ERROR");
}
while(getc(fp)!=EOF)
{
fscanf(fp,"%s",buff);
printf("%s\n",buff);
fprintf(ft,"%s\n",buff);
}
}
Question: What are the things contains in .obj file ? ( compiled result of .cpp file )
Answer :C++ .obj file holds code and data suitable for linking with other object files to
create an executable or a shared object file.
int iVar1;
int iVar2 = int();
Answer :In first case a variable will be create in memeory with the default base type
value (depending upon compiler 2 compiler) bcoz it is not initialized. in second case the
variable will be created in the memory with the value retuned by the function int() (if int
is a user define function) the second statement should have been int *i = new int();
Answer :An instance of a user-defined type (i.e., a class) is called an object. We can
instantiate many objects from one class.
An object is an instance or occurrence of a class.
Answer: C++ uses name mangling when storing both local and global static varibales at
the same place. The local static variables have function name and the global variables
will have file name. Essentially the compiler uses namespace to distinguish between local
and global static variables.
Question :what is the difference betwen wait() and delay()?
Answer :Wait() and delay() works same but works on different platforms. Wait(2) will
wait processing fro 2 second on Linux/Unix while delay(2000) with wait for 2 second but
on DOS or Windows.
Delay() is under <dos.h> while one can directly use wait in his/her program.
Answer :Array name is a constant pointer pointing to the base address(address of the first
byte where the array begin) of the memory allocated. When you use arr[i], the compiler
manipulates it as *(arr + i). Since arr is the address of the first element, the value of i
must be 0 for accessing it. Hence all arrays begin with an index of 0.
Answer : In any application, there can be only one main function. In c++, main is not a
member of any class. There is no chance of overriding
Answer :1. Inline follows strict parameter type checking, macros do not.
2. Macros are always expanded by preprocessor, whereas compiler may or may not
replace the inline definitions.
num[1][1] = 34
num[1][2] = 32
num[1][3] = 30
num[2][1] = 24
num[2][2] = 22
num[2][3] = 20
Answer: Yes destructors can be private. But according to Standard Programming practice
it is not advisable to have destructors to be private.
Answer: When a class uses dynamically allocated memory internally, all sorts of
problems arise. If not properly used or handled, they can lead to memory leaks &
corrupts Data Structures.
A memory leak is the situation that occurs when dynamically allocated memory is lost to
the program.
Char * p;
p= new char[10000];
.....
p= new char[5000];
Initially, 10000 bytes are dynamically allocated & the the address of those bytes is stored
in p. later 5000 bytes are dynamically allocated & the address is stored in p. However, the
original 10000 bytes’ve not been returned to the system using delete [] operator.
Question:
class A()
{
};
int main()
{
A a;
}
Whether there will be a default contructor provided by the compiler in above case ?
Answer :yes, if the designer of the class donot define any constructor in the class. then
the compiler provides the default constructor in the class.
Answer :virtual destructor is very useful....everyone should use that......if there is no any
strong reason for not using virtual destructor....like...One class having two char
variable...........so it's size is two byte........if u use virtual destructor it's size will be 6
bytes....4 byte for virtual ptr....Now if this class have 1 millions objects...so 4 magabyte
memory will be lost...where all ptr do the same thing.....
Question: Why cant one make an object of abstract class?Give compiler view of
statement
Answer :we cant make object of abstract class becoz, in the vtable the vtable entry for
the abstract class functions will be NULL, which ever are defined as pure virtual
functions...
even if there is a single pure virtual function in the class the class becomes as abstract
class..
if there is a virtual function in your class the compiler automatically creates a table called
virtual function table .. to store the virtual function addresses.... if the function is a pure
virtual function the vtable entry for that function will be NULL.
even if there is a single NULL entry in the function table the compiler does not allow to
create the object.
Question: Can we generate a C++ source code from the binary file?
Answer: There is major difference between these three are when we want to avoid
making the copy of variable and we want to change value of actual argument on calling
function. There are we use passing by pointer, passing the reference. We can not perform
arithmetic operation on reference.
Answer: Vector and Array List are very similar. Both of them represent a 'grow able
array', where you access to the elements in it through an index.
Array List it's part of the Java Collection Framework, and has been added with version
1.2, while Vector it's an object that is present since the first version of the JDK. Vector,
anyway, has been retrofitted to implement the List interface.
The main difference is that Vector it's a synchronized object, while Array List it's not.
While the iterator that are returned by both classes are fail-fast (they cleanly thrown a
ConcurrentModificationException when the original object has been modified), the
Enumeration returned by Vector are not.
Unless you have strong reason to use a Vector, the suggestion is to use the Array List
Answer: deque
hash_map
hash_multimap
hash_multiset
hash_set
list
map
multimap
multiset
set
vector
Answer :Copy constructor is called every time a copy of an object is made. When you
pass an object by value, either into a function or as a function's return value, a temporary
copy of that object is made.
Answer: No, we cannot have virtual constructors. But if the need arises, we can simulate
the implementation of virtual constructor by calling a Init method from the constructor
which, should be a virtual function.
class A
public:
A(){}
~A(){}
void x();
void z();
};
void main()
A<int> ai;
A<float> af;
int main()
Question: What is the difference between operator new and the new operator?
Answer: This is what happens when you create a new object: 1. the memory for the
object is allocated using "operator new". 2. the constructor of the class is invoked to
properly initialize this memory. As you can see, the new operator does both 1 and 2. The
operator new merely allocates memory, it does not initialize it. Where as the new operator
also initializes it properly by calling the constructor.
Question: What is the Basic nature of "cin" and "cout" and what concept or
principle we are using on those two?
Answer: Basically "cin and cout" are INSTANCES of istream and ostream classes
respectively. And the concept which is used on cin and cout is operator overloading.
Extraction and Insertion operators are overloaded for input and ouput operations.
2: structures doesn't provide something like data hiding which is provided by the classes
3: structures contains only data while class bind both data and member functions
Question: What is virtual class and friend class?
Answer: Friend classes are used when two or more classes are designed to work together
and virtual base class aids in multiple inheritance.
Question: Is there any way to write a class such that no class can be inherited from
it. Please include code
Answer: The restriction is for safety. For example if we overload. Operator then we can’t
access member in normal way for that we have to use ->.
Answer :Because otherwise you will pass the object to copy as an argument of copy
constructor as pass by value which by definition creates a copy and so on... an infinite
call chain