Important C and C++ Interview Questions
Kittu Patel
Contents
1 C Programming Interview Questions (100) 2
2 C++ Programming Interview Questions (50) 7
3 C Programming MCQs (9) 8
3.1 Basics and Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.2 Pointers and Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 Functions and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.4 Strings and Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.5 Tricky and Output Based . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4 C++ Programming MCQs (5) 11
4.1 OOPs Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.2 Inheritance and Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . 11
4.3 Templates and Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1
1 C Programming Interview Questions (100)
Basics and Syntax
C-1. What is the difference between declaration and definition?
C-2. Explain the structure of a C program.
C-3. What is the use of the main() function?
C-4. What is the difference between getchar() and scanf()?
C-5. What are identifiers in C?
C-6. Why is void main() technically incorrect?
C-7. What are the storage classes in C?
C-8. What are header files and how are they used?
C-9. What is the difference between const and #define?
C-10. How is a comment added in C?
Data Types and Operators
C-11. What are the different data types in C?
C-12. What is the difference between signed and unsigned data types?
C-13. What is typecasting? Give an example.
C-14. What is the result of integer division in C?
C-15. Explain the precedence and associativity of operators.
C-16. What is the sizeof() operator used for?
C-17. How are bitwise operators used?
C-18. What is the output of: int a = 5/2;?
C-19. Difference between pre-increment and post-increment operators.
C-20. What is the result of: 3 % 0?
2
Control Flow and Loops
C-21. What is the syntax of a for loop?
C-22. What is the difference between break and continue?
C-23. How does a switch-case work in C?
C-24. How does goto work?
C-25. How do you use nested loops?
C-26. What are infinite loops? Give examples.
C-27. How can a loop be terminated prematurely?
C-28. How to avoid dangling else problem?
C-29. Explain use of do-while loop.
C-30. What is the difference between while(1) and for(;;)?
Functions
C-31. What is recursion?
C-32. Difference between call by value and call by reference.
C-33. What is a function prototype?
C-34. What are inline functions?
C-35. Can we return multiple values from a function?
C-36. Explain function pointer with example.
C-37. What is the difference between global and static functions?
C-38. How are default parameters handled in C?
C-39. What is the purpose of void in function declaration?
C-40. Can main() be called recursively?
3
Pointers and Memory
C-41. What is a pointer?
C-42. How do you declare and initialize a pointer?
C-43. What is the use of malloc(), calloc(), realloc(), free()?
C-44. Difference between NULL and void pointer.
C-45. What is a dangling pointer?
C-46. What are wild pointers?
C-47. What is pointer arithmetic?
C-48. How are arrays and pointers related?
C-49. What is a pointer to a function?
C-50. Can we have an array of pointers?
Arrays and Strings
C-51. What is the difference between char[] and char*?
C-52. How are multi-dimensional arrays declared?
C-53. How to pass an array to a function?
C-54. What is the use of strlen(), strcpy(), strcmp()?
C-55. How are strings stored in memory?
C-56. What is a null-terminated string?
C-57. Difference between strncpy() and strcpy().
C-58. Write a program to reverse a string.
C-59. Write a C program to count vowels in a string.
C-60. Write a program to remove spaces from a string.
4
Structures and Unions
C-61. What is a structure in C?
C-62. How is a structure passed to a function?
C-63. What is the use of typedef?
C-64. Difference between structure and union.
C-65. What is bit field in structure?
C-66. How are nested structures declared?
C-67. Write a program using structures to store student info.
C-68. What is self-referential structure?
C-69. What are advantages of unions?
C-70. Can we have pointer to a structure?
Files and Preprocessor
C-71. How to open and close a file in C?
C-72. Difference between text and binary file?
C-73. Use of fread(), fwrite(), fscanf(), fprintf()?
C-74. What are macros?
C-75. Difference between #include <file.h> and #include "file.h"?
C-76. What is conditional compilation?
C-77. What are command line arguments?
C-78. What is the use of #ifdef and #ifndef?
C-79. Can macros have parameters?
C-80. How to define multi-line macros?
5
Tricky and Output-Based
C-81. Predict output of: int x = 10; printf("%d", x++ + ++x);
C-82. Predict output of: int a = 0; if(a = 5) printf("yes");
C-83. Can you use recursion without base condition?
C-84. Is C compiled or interpreted?
C-85. What happens when you divide by 0 in C?
C-86. Can a C program run without main()?
C-87. What is segmentation fault?
C-88. Can we modify a string literal?
C-89. How to handle stack overflow?
C-90. Write a code to swap 2 numbers without using third variable.
6
2 C++ Programming Interview Questions (50)
Object-Oriented Basics
C++-1. What is OOP?
C++-2. What are the four pillars of OOP?
C++-3. What is the difference between class and structure?
C++-4. What is constructor and destructor?
C++-5. What is a copy constructor?
C++-6. What is function overloading?
C++-7. What is operator overloading?
C++-8. What is inheritance?
C++-9. What is polymorphism?
C++-10. What is abstraction?
Advanced Concepts
C++-11. What is virtual function?
C++-12. What is pure virtual function?
C++-13. What is difference between compile-time and run-time polymorphism?
C++-14. What is the use of this pointer?
C++-15. What is friend function?
C++-16. What is static member?
C++-17. What is namespace in C++?
C++-18. What is STL in C++?
C++-19. Difference between map, set, and vector.
C++-20. What is exception handling in C++?
Memory Management and Pointers
C++-21. Difference between new and malloc.
C++-22. What is delete operator?
C++-23. What is smart pointer?
C++-24. What is memory leak?
C++-25. What is shallow copy and deep copy?
7
Output-Based and Tricky
C++-26. Predict output: int a=5; cout<<a++ + ++a;
C++-27. Can constructor be private?
C++-28. Can we inherit constructor?
C++-29. Can you overload destructor?
C++-30. Can you declare class inside class?
Templates and Files
C++-31. What is function template?
C++-32. What is class template?
C++-33. Difference between template and macro.
C++-34. File handling in C++ vs C.
C++-35. What are manipulators?
Code and Logic
C++-36. Write a class to implement bank account.
C++-37. Write a program to reverse a string using class.
C++-38. Write a program using function template.
C++-39. Write code to demonstrate inheritance.
C++-40. Write code to demonstrate function overloading.
Comparison and Theory
C++-41. Difference between C and C++?
C++-42. What is the advantage of using C++ over C?
C++-43. Difference between procedural and object-oriented programming?
C++-44. Is C++ still used in industry?
C++-45. What is RAII in C++?
3 C Programming MCQs (9)
3.1 Basics and Syntax
C-MCQ-1. Which of the following is a valid C identifier?
i. 2variable
ii. var123
iii. @main
8
iv. int
C-MCQ-2. Which function is the entry point of every C program?
i. start()
ii. init()
iii. main()
iv. entry()
C-MCQ-3. What is the default return type of a function in C if not specified?
i. float
ii. void
iii. int
iv. double
3.2 Pointers and Memory
C-MCQ-4. What is the size of a pointer in a 64-bit system?
i. 2 bytes
ii. 4 bytes
iii. 8 bytes
iv. 16 bytes
C-MCQ-5. What does the malloc() function return when it fails?
i. 0
ii. 1
iii. -1
iv. NULL
3.3 Functions and Loops
C-MCQ-6. What is the correct syntax to call a function in C?
i. function[]
ii. function();
iii. function();
iv. call function;
C-MCQ-7. Which loop checks condition after executing the body?
i. for
ii. while
iii. do-while
iv. goto
9
3.4 Strings and Structures
C-MCQ-8. Which header file is used for string functions?
i. stdlib.h
ii. input.h
iii. string.h
iv. conio.h
3.5 Tricky and Output Based
C-MCQ-9. What is the output of printf("%d", 5 + 2 * 3);?
i. 21
ii. 11
iii. 10
iv. 13
10
4 C++ Programming MCQs (5)
4.1 OOPs Concepts
C++-MCQ-1. Which is not a pillar of OOP?
i. Encapsulation
ii. Inheritance
iii. Compilation
iv. Polymorphism
C++-MCQ-2. What is a class in C++?
i. A template
ii. A variable
iii. A blueprint for an object
iv. A memory block
4.2 Inheritance and Polymorphism
C++-MCQ-3. What type of inheritance is shown when a class inherits from more than one
class?
i. Single
ii. Multiple
iii. Hierarchical
iv. Multilevel
C++-MCQ-4. What is a virtual function used for?
i. To declare global methods
ii. To achieve run-time polymorphism
iii. To reduce memory
iv. To avoid constructors
4.3 Templates and Memory
C++-MCQ-5. What is the correct syntax for a function template?
i. template ¡typename T¿
ii. template [T]
iii. template type(T)
iv. template¡class T¿
11