C++ Interview and Viva Questions
इस page का इस्तेमाल कैसे करें
ये वे questions हैं जो C++ practical viva (Class 11–12, BCA, B.Sc., B.Tech) और fresher interviews में बार-बार पूछे जाते हैं। हर उत्तर इतना छोटा है कि 20–30 सेकंड में बोला जा सके। पूरी व्याख्या के लिए linked lesson पढ़ें।
Basics (1–10)
1. C++ क्या है?
Bjarne Stroustrup की बनाई general-purpose, compiled language जो C का विस्तार है। यह procedural, object-oriented और generic programming support करती है। देखें C++ का परिचय।
2. C और C++ में अंतर?
C procedural है; C++ classes, inheritance, polymorphism, references, function overloading, templates, exceptions, namespaces और STL जोड़ती है। देखें C vs C++।
3. C++ में tokens क्या हैं?
Program की सबसे छोटी इकाइयाँ: keywords, identifiers, literals (constants), operators और punctuators (;, {} जैसे separators)।
4. main() की भूमिका क्या है?
यह entry point है जहाँ से execution शुरू होता है। यह ठीक एक होना चाहिए और int लौटाता है (0 = सफल)।
5. Namespace क्या है?
नाम वाला scope जो नाम टकराव रोकने के लिए identifiers को समूहित करता है। Standard library std में है; std::cout उसमें से cout चुनता है। देखें namespaces।
6. cin/cout और scanf/printf में अंतर?
cin/cout type-safe C++ streams हैं जिन्हें format specifiers नहीं चाहिए और operator overloading से user-defined types के साथ भी चलते हैं; scanf/printf C functions हैं जो %d जैसी format strings पर निर्भर हैं।
7. Reference क्या है?
मौजूदा variable का दूसरा नाम: int& r = x;। इसे initialise करना ज़रूरी है, यह null नहीं हो सकता और दूसरे variable से जोड़ा नहीं जा सकता। देखें references।
8. Pointer vs reference?
Pointer address रखता है, null हो सकता है, दोबारा assign हो सकता है और value के लिए * चाहिए। Reference object का दूसरा नाम है, initialise होना ज़रूरी है और जिससे जुड़ा है उसे बदल नहीं सकता।
9. const क्या है?
Value को read-only बनाने वाला qualifier। यह variables, pointers (const int* vs int* const), references और member functions पर लगता है। देखें constants और literals।
10. Inline functions क्या हैं?
inline लिखे (या class के अंदर defined) functions, जिनकी calls को compiler call overhead बचाने के लिए body से बदल सकता है। देखें inline functions।
OOP (11–22)
11. OOP क्या है? इसके pillars?
Programs को data और व्यवहार वाले objects के रूप में बनाने का तरीका। Pillars: encapsulation, abstraction, inheritance, polymorphism। देखें OOP concepts।
12. Class vs object?
Class blueprint है (class Student { … };); object उसका instance है (Student s;) जिसकी अपनी memory होती है।
13. Encapsulation क्या है?
Data को उसके functions के साथ बाँधना और private/protected से सीधा access रोकना, सिर्फ public interface दिखाना।
14. Abstraction क्या है?
सिर्फ ज़रूरी features दिखाना और implementation की details छिपाना — classes, access specifiers और abstract classes से।
15. Constructor क्या है? प्रकार?
Class के नाम वाला, बिना return type का खास member function जो object बनते समय चलता है। प्रकार: default, parameterised, copy और move constructors (साथ में delegating और converting)। देखें constructors।
16. Destructor क्या है?
~ClassName() member function जो object नष्ट होने पर अपने-आप चलता है, resources छोड़ने के लिए। एक class में सिर्फ एक destructor होता है और वह कोई argument नहीं लेता। देखें destructors।
17. Copy constructor क्या है और कब call होता है?
ClassName(const ClassName& other) दूसरे object की copy के रूप में object बनाता है। यह तब call होता है जब object दूसरे object से initialise हो, by value भेजा जाए या (elision के बिना) by value लौटाया जाए। देखें copy constructor।
18. Shallow copy vs deep copy?
Shallow copy pointer values copy करती है, इसलिए दो objects एक ही heap memory share करते हैं (double-delete का ख़तरा)। Deep copy नई memory लेकर data copy करती है, हर object की अपनी memory होती है।
19. this pointer क्या है?
Non-static member functions के अंदर मिलने वाला छिपा pointer जो उस object की ओर इशारा करता है जिस पर function call हुआ। देखें this pointer।
20. Static members क्या हैं?
Data या functions जो हर object के नहीं, class के होते हैं — एक साझा copy। Static function Class::func() से call होता है और उसमें this नहीं होता। देखें static members।
21. Friend function क्या है?
Non-member function जिसे class के अंदर friend declare करके private और protected members का access दिया जाता है। देखें friend functions।
22. Inheritance के प्रकार?
Single, multiple, multilevel, hierarchical और hybrid। Multiple inheritance की diamond problem virtual base classes से हल होती है। देखें inheritance।
Polymorphism और virtual functions (23–28)
23. Compile-time vs run-time polymorphism?
Compile-time: function overloading, operator overloading और templates — compiler तय करता है। Run-time: base pointer/reference से call होने वाले virtual functions — चलते समय तय। देखें polymorphism।
24. Overloading vs overriding?
Overloading = एक नाम, अलग parameters, एक ही scope। Overriding = derived class base के virtual function को उसी signature से फिर परिभाषित करे।
25. Virtual function क्या है?
Base class का virtual declared member function, ताकि base pointer से call होने पर derived class का version चले। देखें virtual functions।
26. Pure virtual function और abstract class क्या हैं?
virtual void draw() = 0; का base class में body नहीं होता। कम से कम एक pure virtual function वाली class abstract होती है और उसका object नहीं बन सकता। देखें abstract classes।
27. Base class में virtual destructor क्यों होना चाहिए?
अगर derived object base pointer से delete हो और destructor virtual न हो, तो सिर्फ base destructor चलता है — derived हिस्सा साफ़ नहीं होता (undefined behaviour, अक्सर leak)।
28. vtable क्या है?
Virtual functions वाली classes के लिए compiler द्वारा बनाई गई function pointers की table; हर object में उसका छिपा pointer (vptr) होता है — इसी से run-time dispatch होता है।
Memory और pointers (29–33)
29. new/delete vs malloc/free?
new operator memory लेकर constructor भी call करता है, सही type लौटाता है और असफल होने पर bad_alloc throw करता है। malloc सिर्फ raw bytes देता है, void* लौटाता है और असफल होने पर NULL। इन्हें कभी मिलाएँ नहीं। देखें dynamic memory।
30. Memory leak क्या है?
new से ली गई memory जो कभी delete नहीं हुई, इसलिए program ख़त्म होने तक दोबारा इस्तेमाल नहीं हो सकती।
31. Dangling pointer क्या है?
ऐसा pointer जो अब भी free हो चुकी memory या अब मौजूद न रहे local variable का address रखे।
32. Smart pointers क्या हैं?
Classes जो heap memory की मालिक होकर उसे अपने-आप free करती हैं: unique_ptr (अकेला मालिक), shared_ptr (reference-counted साझा मालिक), weak_ptr (बिना ownership का observer)। देखें smart pointers।
33. RAII क्या है?
"Resource Acquisition Is Initialisation": resource (memory, file, lock) को object की lifetime से बाँधना — constructor में लेना, destructor में छोड़ना — ताकि exceptions आने पर भी वह अपने-आप free हो।
Templates, STL और exceptions (34–38)
34. Template क्या है?
किसी भी type के साथ चलने वाले functions या classes का blueprint: template <typename T> T maxOf(T a, T b);। देखें templates।
35. STL क्या है? इसके components?
Standard Template Library: containers, iterators, algorithms और function objects। देखें STL परिचय।
36. vector vs array?
Built-in array का size compile time पर तय और fixed होता है, और उसे अपनी सीमा नहीं पता। std::vector dynamically बढ़ता है, अपना size() जानता है, अपनी memory खुद संभालता है और at() से bounds checking देता है।
37. map vs unordered_map?
map keys को sorted रखता है (balanced tree, O(log n) operations)। unordered_map hashing इस्तेमाल करता है (औसत O(1)) और कोई क्रम नहीं रखता।
38. Exception handling कैसे काम करती है?
जो code fail हो सकता है वह try block में; error throw से बताई जाती है; मिलता हुआ catch block उसे संभालता है। Unwinding के दौरान local objects के destructors चलते हैं। देखें exception handling।
Modern C++ (39–40)
39. auto क्या करता है?
Compiler को variable का type उसकी initial value से निकालने देता है (auto it = v.begin();)। Type फिर भी compile time पर तय होता है। देखें modern C++।
40. Lambda expression क्या है?
Inline, बिना नाम का function, जैसे [](int a, int b) { return a > b; }, जो अक्सर STL algorithms को दिया जाता है; capture list [ ] बाहरी variables का access तय करती है। देखें lambda।
Bonus: output बताइए
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() { cout << "Base\n"; }
void hello() { cout << "Hello from Base\n"; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived\n"; }
void hello() { cout << "Hello from Derived\n"; }
};
int main() {
Base* p = new Derived();
p->show();
p->hello();
delete p;
return 0;
}show() virtual है, इसलिए derived version चलता है; hello() virtual नहीं है, इसलिए p के Base* होने से base version चलता है। (असली code में base pointer से delete करने से पहले Base को virtual destructor दें — question 27 देखें।)
Viva tips
- अपने practical programs की हर line और हर header क्यों include है, यह जानें।
- Constructor, destructor और एक member function वाली class याद से लिख सकें।
- Loops और pointer code को कागज़ पर trace करने का अभ्यास करें।
- उत्तर न आए तो उस topic के बारे में जो पता है वह बताएँ — examiners ईमानदार अधूरे उत्तर को भी नंबर देते हैं।
उत्तर ढककर हर एक को 30 सेकंड से कम में बोलने की कोशिश करें। जिन पर अटकें उन्हें mark करें और उनका linked lesson दोहराएँ। फिर C++ Test 2 और C++ MCQ quiz दें।
सारांश
- Basics: C vs C++, namespaces, references,
const, inline। - OOP: class/object, चार pillars, constructors, destructors, copy semantics, static और friend।
- Polymorphism: overloading vs overriding, virtual functions, abstract classes, virtual destructors।
- Memory:
new/delete, leaks, dangling pointers, smart pointers, RAII। - Templates, STL, exceptions और
auto, lambdas जैसे modern features।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
Freshers से सबसे ज़्यादा पूछे जाने वाले C++ interview questions कौन-से हैं?
new/delete vs malloc/free, और vector, map जैसे basic STL containers।C++ practical viva की तैयारी कैसे करें?
C++ में overloading और overriding में क्या अंतर है?
virtual function का उसी signature के साथ अपना version दे — base pointer या reference से run time पर तय।