C Language interview question and Answers(latest)
1)what are storage classes in c?How many?
2)what is the difference between Static and External(extern)?explain with example?
3)what is the difference between Array and a List?
4)what is Function Pointer?with an example?
5)what is dangling Pointer?
6)what is the difference between structure and Union?
7)what is the difference between function and macro?
8)what is the difference between Marco and Inline?
9)what is the difference between Macro and Const?
10)what is the difference between typedef and enum?
11)Explain Compilation stages in C?
12)what is an executable file and what exactly it contains?
13)what is Self-Referential Structure?
14)what is a Memory Leak?
15)what is Structure Padding?
16)What is a bit Fields?It's usage?
17)what is a Volatile Keyword?Explain it is used in your project?
18)How to find the size of datatype without using sizeof operator?
C++ Interview Question and Answers
Latest and Frequently Asking
1)what is the difference between c and c++?
2)what are OOPS Concepts?Explain with an example?
3)what is the difference between Procedure oriented and Object Oriented Programming?
4)what is Reference Variable?Explain with an example?
5)what is the difference between Reference and Pointers?
6)what is the Syntax for a NEW operator?with an example?
7)what is the difference between Malloc, Calloc and New Operator?
8)what is Namespace?Explain with an example?
9)what is a Constructor?how many types of Constructors? with an example?
10)what is a Destructor?
11)Can we Place Virtual for Constructor and Destructor?
12)what is an Inheritance?how many types of inheritance?explain with example?
13)what is "Dimond shape Problem"?with an example?
14)what is Polymorphism?how many types of Polymorphism?
15)what is Run Time Polymorphism and compile time Polymorphism? explain with example?
16)what is Deep Copy and Shallow Copy?with an example?
17)what is a Virtual Function? with an example?
18)what is a VTables and VPtr?explain with a diagram?
19)what is a Template?How many types of Templates?
20)How will you restrict the template for a Specific Datatype?
21)what is Friend Class and Function ?with an example?
22)what is a Smart Pointer in c++?
23)what is Abstract Class?
24)what is an Interface in c++?
25)what is Pure Virtual Function?
26)what is the difference between c structure and c++ class?
27) what is Dynamic cast and static cast in c++?
28) what is binary literals in c++ with an example?
Attaching a large project to leak tracer is faster than valgrind.
How to use leaktracer and valgrind step by step:
A memory leaking program which would be used to check for memory leak using LeakTracer.
#include<iostream>
using namespace std;
#include<unistd.h>
int main()
{
char* name = new char[20];//allocates 20 bytes
//delete[] name;//Now leaks 20 byte
//sleep (30);
return 0;
}
------------------------STEPS to use LeakTracer----------
STEP-0: Compile with -g flag to capture gdb debugging symbols.
g++ -g leakProgram.cpp
Here "a.out" would be my application in which I want check memory leak
STEP-1: run command "LeakCheck" with your application, here a.out
/usr/bin/LeakCheck /home/admin/a.out
STEP-2: run command "leak-analyze" with your application and generated file leak.out
/usr/bin/leak-analyze a.out /home/admin/leak.out
----------------------------Example----------------------------
Ubuntu:~/$ g++ -g leakProgram.cpp
Ubuntu:~/$ /usr/bin/LeakCheck /home/admin/a.out
Ubuntu:~/$ /usr/bin/leak-analyze a.out /home/admin/leak.out
Gathered 1 (1 unique) points of data.
Reading symbols from a.out...done.
(gdb)
#-- Leak: counted 1x / total Size: 20
0x80485c2 is in main() (leakProgram.cpp:6).
5 {
6 char* name = new char[20];//allocates 20 bytes but not freed.
--------------------STEPS to use Valigrind----------
Ubuntu:~$ valgrind --tool=memcheck --leak-check=yes ./a.out
==6829== Memcheck, a memory error detector
==6829== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==6829== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==6829== Command: ./a.out
==6829==
==6829==
==6829== HEAP SUMMARY:
==6829== in use at exit: 20 bytes in 1 blocks
==6829== total heap usage: 1 allocs, 0 frees, 20 bytes allocated
==6829==
==6829== 20 bytes in 1 blocks are definitely lost in loss record 1 of 1
==6829== at 0x402ADFC: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==6829== by 0x80485C1: main (leakProgram.cpp:6)
==6829==
==6829== LEAK SUMMARY:
==6829== definitely lost: 20 bytes in 1 blocks
==6829== indirectly lost: 0 bytes in 0 blocks
==6829== possibly lost: 0 bytes in 0 blocks
==6829== still reachable: 0 bytes in 0 blocks
==6829== suppressed: 0 bytes in 0 blocks
==6829==
==6829== For counts of detected and suppressed errors, rerun with: -v
==6829== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
I will update this page with more ............stay with updates
1)what are storage classes in c?How many?
2)what is the difference between Static and External(extern)?explain with example?
3)what is the difference between Array and a List?
4)what is Function Pointer?with an example?
5)what is dangling Pointer?
6)what is the difference between structure and Union?
7)what is the difference between function and macro?
8)what is the difference between Marco and Inline?
9)what is the difference between Macro and Const?
10)what is the difference between typedef and enum?
11)Explain Compilation stages in C?
12)what is an executable file and what exactly it contains?
13)what is Self-Referential Structure?
14)what is a Memory Leak?
15)what is Structure Padding?
16)What is a bit Fields?It's usage?
17)what is a Volatile Keyword?Explain it is used in your project?
18)How to find the size of datatype without using sizeof operator?
C++ Interview Question and Answers
Latest and Frequently Asking
1)what is the difference between c and c++?
2)what are OOPS Concepts?Explain with an example?
3)what is the difference between Procedure oriented and Object Oriented Programming?
4)what is Reference Variable?Explain with an example?
5)what is the difference between Reference and Pointers?
6)what is the Syntax for a NEW operator?with an example?
7)what is the difference between Malloc, Calloc and New Operator?
8)what is Namespace?Explain with an example?
9)what is a Constructor?how many types of Constructors? with an example?
10)what is a Destructor?
11)Can we Place Virtual for Constructor and Destructor?
12)what is an Inheritance?how many types of inheritance?explain with example?
13)what is "Dimond shape Problem"?with an example?
14)what is Polymorphism?how many types of Polymorphism?
15)what is Run Time Polymorphism and compile time Polymorphism? explain with example?
16)what is Deep Copy and Shallow Copy?with an example?
17)what is a Virtual Function? with an example?
18)what is a VTables and VPtr?explain with a diagram?
19)what is a Template?How many types of Templates?
20)How will you restrict the template for a Specific Datatype?
21)what is Friend Class and Function ?with an example?
22)what is a Smart Pointer in c++?
23)what is Abstract Class?
24)what is an Interface in c++?
25)what is Pure Virtual Function?
26)what is the difference between c structure and c++ class?
27) what is Dynamic cast and static cast in c++?
28) what is binary literals in c++ with an example?
C++ STL interview questions
Q) What do you mean by " every iterator and reference after the point of resize/erase is invalidated "?(except when erased member is first or last member of container)
Q) What is the difference between capacity and size in vector container in STL?
Q) If initially size allocated to vector is 10. What would be the capacity of the vector? Will it be doubled?
Q) Write a program to demonstrate vector's algorithms ?
Q) Write a program to demonstrate map and multi map's algorithms?
Q) Write a program to demonstrate set and multi set's operations ?
Q) What is the return type of find algorithm in STL container?
Q) Write a program to find the first match of pattern in given strings and its position? Use vector of strings?
Q) Write a program to find all matches of pattern in given strings and its position? Use vector of strings?
Q) What is the difference between array an vector?
Q) What is the difference between vector, list, set and map?
Q) Why vectors are thread safe?
Q) What do you mean by " every iterator and reference after the point of resize/erase is invalidated "?
Invalidation in vector means pointing out side of range of vector or pointing to altogether different value.
Insertion consequence in a vector:
i)All iterators and references before the point of insertion are unaffected, unless the new container size is greater than the previous capacity (in which case all iterators and references are invalidated.
ii)Erase consequences in vector:
Every iterator and reference after the point of erase is invalidated.
iii)Resize effect in vector:
As per insertion and erase.
example code of vector invalidation in c++ STL vector:
#include<iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
int arr[] = { 10, 20, 30 };
std::vector<int> vec(arr, arr + 3);
// vector contains 1, 2, 3
//size 3 and capasity 4
std::vector<int>::iterator i = vec.begin();
cout <<"*i="<< *i << endl; // prints 10
int &ref = *i;
cout <<"ref="<<ref << endl; // prints 10
vec.resize(5, 100);
//vector now contains 1, 2, 3, 100, 100
//now capasity is 8 > size 5
// WRONG! might work, crash or invalid result
cout <<"*i=="<< *i << endl; // prints 0
// WRONG! invalid reference
cout <<"ref=="<<ref << endl; // prints 0
return 0;
}
Q) What is the difference between capacity and size in vector container in STL?
Q) If initially size allocated to vector is 10. What would be the capacity of the vector? Will it be doubled?
Q) Write a program to demonstrate vector's algorithms ?
Q) Write a program to demonstrate map and multi map's algorithms?
Q) Write a program to demonstrate set and multi set's operations ?
Q) What is the return type of find algorithm in STL container?
Q) Write a program to find the first match of pattern in given strings and its position? Use vector of strings?
Q) Write a program to find all matches of pattern in given strings and its position? Use vector of strings?
Q) What is the difference between array an vector?
Q) What is the difference between vector, list, set and map?
Q) Why vectors are thread safe?
Q) What do you mean by " every iterator and reference after the point of resize/erase is invalidated "?
Invalidation in vector means pointing out side of range of vector or pointing to altogether different value.
Insertion consequence in a vector:
i)All iterators and references before the point of insertion are unaffected, unless the new container size is greater than the previous capacity (in which case all iterators and references are invalidated.
ii)Erase consequences in vector:
Every iterator and reference after the point of erase is invalidated.
iii)Resize effect in vector:
As per insertion and erase.
example code of vector invalidation in c++ STL vector:
#include<iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
int arr[] = { 10, 20, 30 };
std::vector<int> vec(arr, arr + 3);
// vector contains 1, 2, 3
//size 3 and capasity 4
std::vector<int>::iterator i = vec.begin();
cout <<"*i="<< *i << endl; // prints 10
int &ref = *i;
cout <<"ref="<<ref << endl; // prints 10
vec.resize(5, 100);
//vector now contains 1, 2, 3, 100, 100
//now capasity is 8 > size 5
// WRONG! might work, crash or invalid result
cout <<"*i=="<< *i << endl; // prints 0
// WRONG! invalid reference
cout <<"ref=="<<ref << endl; // prints 0
return 0;
}
Leak Tracer vs valgrind for memory leak analysis of a C++ project(GDB)
Attaching a large project to leak tracer is faster than valgrind.
How to use leaktracer and valgrind step by step:
A memory leaking program which would be used to check for memory leak using LeakTracer.
#include<iostream>
using namespace std;
#include<unistd.h>
int main()
{
char* name = new char[20];//allocates 20 bytes
//delete[] name;//Now leaks 20 byte
//sleep (30);
return 0;
}
------------------------STEPS to use LeakTracer----------
STEP-0: Compile with -g flag to capture gdb debugging symbols.
g++ -g leakProgram.cpp
Here "a.out" would be my application in which I want check memory leak
STEP-1: run command "LeakCheck" with your application, here a.out
/usr/bin/LeakCheck /home/admin/a.out
STEP-2: run command "leak-analyze" with your application and generated file leak.out
/usr/bin/leak-analyze a.out /home/admin/leak.out
----------------------------Example----------------------------
Ubuntu:~/$ g++ -g leakProgram.cpp
Ubuntu:~/$ /usr/bin/LeakCheck /home/admin/a.out
Ubuntu:~/$ /usr/bin/leak-analyze a.out /home/admin/leak.out
Gathered 1 (1 unique) points of data.
Reading symbols from a.out...done.
(gdb)
#-- Leak: counted 1x / total Size: 20
0x80485c2 is in main() (leakProgram.cpp:6).
5 {
6 char* name = new char[20];//allocates 20 bytes but not freed.
--------------------STEPS to use Valigrind----------
Ubuntu:~$ valgrind --tool=memcheck --leak-check=yes ./a.out
==6829== Memcheck, a memory error detector
==6829== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==6829== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==6829== Command: ./a.out
==6829==
==6829==
==6829== HEAP SUMMARY:
==6829== in use at exit: 20 bytes in 1 blocks
==6829== total heap usage: 1 allocs, 0 frees, 20 bytes allocated
==6829==
==6829== 20 bytes in 1 blocks are definitely lost in loss record 1 of 1
==6829== at 0x402ADFC: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==6829== by 0x80485C1: main (leakProgram.cpp:6)
==6829==
==6829== LEAK SUMMARY:
==6829== definitely lost: 20 bytes in 1 blocks
==6829== indirectly lost: 0 bytes in 0 blocks
==6829== possibly lost: 0 bytes in 0 blocks
==6829== still reachable: 0 bytes in 0 blocks
==6829== suppressed: 0 bytes in 0 blocks
==6829==
==6829== For counts of detected and suppressed errors, rerun with: -v
==6829== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
I will update this page with more ............stay with updates
Nice article. visit good way to iterate vector
ReplyDeleteCould you please provide the answers of this questions. C language interview and c++ .
ReplyDelete