Jan 19, 2026

C/CPP Interview Questions

 


✅ C & C++ Interview Question Bank (With Answers)


🔹 SECTION 1: C LANGUAGE (Core)


Q1. What is the difference between malloc() and calloc()?


Answer:


malloc calloc

Allocates memory Allocates & initializes to zero

Faster Slightly slower

Takes 1 argument Takes 2 arguments



⸻


Q2. What is a dangling pointer?


Answer:

A pointer that points to a memory location that has been freed or gone out of scope.


⸻


Q3. Difference between const int *p and int *const p?


Answer:

• const int *p → value cannot be changed

• int *const p → pointer cannot be changed


⸻


Q4. What is static keyword used for?


Answer:

• Preserves variable value between function calls

• Limits scope to file when used globally

• Shares value across objects in C++


⸻


Q5. What is volatile keyword?


Answer:

Tells compiler not to optimize variable since it can change externally (used in hardware registers, ISR).


⸻


Q6. What is segmentation fault?


Answer:

Accessing invalid or unauthorized memory location.


⸻


Q7. What is structure padding?


Answer:

Extra bytes added by compiler to align data for faster access.


⸻


Q8. What is difference between stack and heap?


Answer:


Stack Heap

Automatic allocation Manual allocation

Faster Slower

Limited size Larger size



⸻


🔹 SECTION 2: POINTERS (Very Important)


Q9. What is pointer to pointer?


Answer:

A pointer that stores address of another pointer.


⸻


Q10. What is NULL pointer?


Answer:

Pointer that points to nothing (NULL or 0x0).


⸻


Q11. What is function pointer?


Answer:

A pointer that points to a function’s address.


⸻


Q12. Difference between array and pointer?


Answer:

• Array name is constant

• Pointer can be reassigned


⸻


🔹 SECTION 3: C++ BASICS


Q13. What is OOP?


Answer:

Programming model based on Object, Class, Encapsulation, Inheritance, Polymorphism, Abstraction.


⸻


Q14. Difference between C and C++?


Answer:

• C → procedural

• C++ → object oriented

• C++ supports classes, inheritance, STL


⸻


Q15. What is constructor?


Answer:

Special function automatically called when object is created.


⸻


Q16. What is destructor?


Answer:

Function called automatically when object is destroyed.


⸻


Q17. Can constructor be private?


Answer:

Yes (used in Singleton pattern).


⸻


Q18. What is copy constructor?


Answer:

Initializes object using another object of same class.


⸻


Q19. What is virtual function?


Answer:

Function that supports runtime polymorphism using base class pointer.


⸻


Q20. What is virtual destructor?


Answer:

Ensures proper cleanup of derived class object.


⸻


🔹 SECTION 4: OOP CONCEPTS


Q21. What is encapsulation?


Answer:

Binding data and methods together and hiding internal details.


⸻


Q22. What is inheritance?


Answer:

Derived class inherits properties of base class.


⸻


Q23. What is polymorphism?


Answer:

Same function name behaving differently.


⸻


Q24. Difference between compile time and runtime polymorphism?


Answer:

• Compile time → function overloading

• Runtime → function overriding


⸻


Q25. What is abstraction?


Answer:

Showing only essential features, hiding implementation.


⸻


🔹 SECTION 5: ADVANCED C++


Q26. What is STL?


Answer:

Standard Template Library providing containers, algorithms, iterators.


⸻


Q27. Difference between vector and list?


Answer:


vector list

Contiguous memory Non-contiguous

Fast access Slow access

Slow insertion Fast insertion



⸻


Q28. What is smart pointer?


Answer:

Pointer that automatically manages memory.

Examples:

• unique_ptr

• shared_ptr

• weak_ptr


⸻


Q29. What is RAII?


Answer:

Resource Acquisition Is Initialization — resource tied to object lifetime.


⸻


Q30. What is namespace?


Answer:

Used to avoid name conflicts.


⸻


🔹 SECTION 6: EMBEDDED / SYSTEM C & C++


Q31. Why use volatile in embedded systems?


Answer:

Variables can change due to hardware or interrupts.


⸻


Q32. What is ISR?


Answer:

Interrupt Service Routine — function executed on interrupt.


⸻


Q33. What is memory leak?


Answer:

Allocated memory not freed.


⸻


Q34. Difference between new/delete and malloc/free?


Answer:

• new/delete call constructors/destructors

• malloc/free do not


⸻


Q35. What is deadlock?


Answer:

Two processes waiting for each other indefinitely.


⸻


🔹 SECTION 7: COMMON TRICK QUESTIONS


Q36. Can main() be overloaded?


Answer:

No.


⸻


Q37. Can we have virtual constructor?


Answer:

No, but destructor can be virtual.


⸻


Q38. Can sizeof return zero?


Answer:

No.


⸻


Q39. Is C pass by value or reference?


Answer:

C supports only pass by value (reference via pointers).


⸻


Q40. Why sizeof(char) is always 1?


Answer:

Defined by C standard.


⸻

No comments:

Post a Comment

SiteMap