100 C++ Basic Viva Questions and Answers for BCA/Btech/CS Students

Q1. What is C++?

C++ is a general-purpose programming language created by Bjarne Stroustrup in 1983. It is an extension of the C programming language and provides object-oriented and generic programming features.

Q2. What are the main features of C++?

  • Object-oriented programming
  • Template classes and functions
  • Exception handling
  • Standard Template Library (STL)
  • Namespaces
  • Operator overloading

Q3. What is the difference between C and C++?

C is a procedural programming language, while C++ is an object-oriented programming language. C++ also has additional features such as classes, templates, and exception handling, which are not present in C.

Q4. What is an object in C++?

An object is an instance of a class in C++. It has its own state and behavior, defined by the class's variables and methods.

Q5. What are the different access specifiers in C++?

  • private: variables and methods can only be accessed within the class
  • protected: variables and methods can be accessed within the class and by derived classes
  • public: variables and methods can be accessed by any code

Q6. What is the purpose of the main() function in C++?

The main() function is the entry point of a C++ program. It is where the execution of the program begins.

Q7. What is inheritance in C++?

Inheritance is a feature of object-oriented programming that allows a new class to inherit the properties and methods of an existing class. This allows for code reuse and a hierarchical organization of classes.

Q8. What is polymorphism in C++?

Polymorphism is the ability of a single function or operator to have multiple meanings depending on the context in which it is used. In C++, polymorphism is achieved through function overloading and operator overloading.

Q9. What is a constructor in C++?

A constructor is a special member function of a class that is automatically called when an object of the class is created. It is used to initialize the object's properties and allocate memory.

Q10. What is a destructor in C++?

A destructor is a special member function of a class that is automatically called when an object of the class goes out of scope or is deleted. It is used to deallocate memory and perform any other cleanup tasks.

Q11. What is an operator overloading in C++?

Operator overloading is a feature in C++ that allows operators to have different meanings depending on the context in which they are used. This means that operators can be redefined to work with user-defined types.

Q12. What is a template in C++?

A template is a feature in C++ that allows for the creation of generic functions and classes. This means that the same function or class can work with different data types without the need for explicit type casting.

Q13. What is the Standard Template Library (STL) in C++?

The Standard Template Library (STL) is a collection of template classes and functions that provide common data structures and algorithms. It includes classes such as vector, list, and map, and algorithms such as sorting and searching.

Q14. What is the difference between a stack and a queue?

A stack is a data structure that follows the Last-In-First-Out (LIFO) principle, meaning that the last element added to the stack will be the first one to be removed. On the other hand, a queue is a data structure that follows the First-In-First-Out (FIFO) principle, meaning that the first element added to the queue will be the first one to be removed.

Q15. What is an exception in C++?

An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. C++ provides a mechanism for handling exceptions called try-catch, which allows the program to catch and handle exceptions, rather than crashing or behaving unpredictably.

Q16. What is a virtual function in C++?

A virtual function is a member function of a class that can be overridden by a derived class. It is used to achieve polymorphism and allows for different behavior in derived classes while maintaining a common interface.

Q17. What is a friend function in C++?

A friend function is a non-member function that has been given access to the private and protected members of a class. It is declared using the "friend" keyword and can be used to overload operators or to provide additional functionality.

Q18. What is a namespace in C++?

A namespace is a container for identifiers (variables, functions, classes, etc.) in C++. It is used to avoid naming conflicts between different identifiers and to organize code into logical groups.

Q19. What is a reference variable in C++?

A reference variable is an alias for an existing variable. It is created using the "&" operator and can be used to refer to the original variable.

Q20. What is a pointer variable in C++?

A pointer variable is a variable that stores the memory address of another variable. It is created using the "*" operator and can be used to access and manipulate the data stored at that memory address.

Q21. What is a static member in C++?

A static member is a member of a class that is shared among all objects of that class. It is defined using the "static" keyword and can be accessed using the class name, rather than an object.

Q22. What is a const member function in C++?

A const member function is a member function of a class that guarantees not to modify the object on which it is called. It is defined using the "const" keyword and can be called on a const object.

Q23. What is the difference between a struct and a class in C++?

A struct and a class are both user-defined data types, but there are some differences in the default access levels. By default, members of a struct are public, while members of a class are private.

Q24. What is a smart pointer in C++?

A smart pointer is a C++ object that acts like a pointer, but with additional features such as automatic memory management and thread-safety. It provides a safer and more convenient way to work with dynamic memory.

Q25. What is a lambda function in C++?

A lambda function is a C++11 feature, also known as anonymous function, that allows for the creation of small, unnamed functions. They can be used as a function argument or to define a function object. They are useful for short, simple operations that are used only once in the code.

Q26. What is a type alias in C++?

A type alias is a feature in C++11 that allows for the creation of a new name for an existing type. This can be useful for making code more readable, or for creating more general templates. It is defined using the keyword "using".

Q27. What is a move constructor in C++?

A move constructor is a special constructor that takes an rvalue reference as an argument and transfers ownership of the resources from the rvalue to the newly created object. It is used to improve the performance of a class by avoiding unnecessary copies.

Q28. What is a move assignment operator in C++?

A move assignment operator is a special operator that takes an rvalue reference as an argument and transfers ownership of the resources from the rvalue to the existing object. It is used to improve the performance of a class by avoiding unnecessary copies.

Q29. What is a null pointer constant in C++?

A null pointer constant is a special value that is used to represent a null pointer. In C++, it is represented by the keyword "nullptr". It can be used to initialize a pointer to point to nothing.

Q30. What is a function pointer in C++?

A function pointer is a pointer that points to a function. It can be used to pass a function as an argument to another function or to call a function indirectly. It is defined using the "*" operator and the function's return type and arguments.

Q31. What is a range-based for loop in C++?

A range-based for loop is a feature in C++11 that allows for iterating over a range of elements using a simplified syntax. It uses the "for" keyword followed by a variable that takes the value of each element in the range, and can be used with arrays, containers or any classes that implement the begin() and end() functions.

Q32. What is a variadic template in C++?

A variadic template is a feature in C++ that allows for the creation of templates that take a variable number of arguments. It is useful for creating generic functions or classes that can handle different number of arguments. It is defined using the "..." operator.

Q33. What is a default constructor in C++?

A default constructor is a constructor that takes no arguments or has default values for all its parameters. It is automatically generated by the compiler if no other constructor is defined in the class. It is useful for creating objects with default properties.

Q34. What is a copy constructor in C++?

A copy constructor is a constructor that creates an object as a copy of another object. It is defined using the copy constructor's signature and is used to make a copy of an existing object when it is passed by value or returned by value.

Q35. What is a reference wrapper in C++?

A reference wrapper is a class template in C++ that wraps a reference. It is useful for creating objects that can store a reference but can be passed by value. It is defined in <functional> header and is useful when working with STL algorithms.

Q36. What is a constexpr in C++?

A constexpr is a keyword in C++ that allows for the creation of compile-time constants. These constants are evaluated at compile-time and can be used wherever a constant expression is required. They can be used to optimize code and improve performance.

Q37. What is a static assertion in C++?

A static assertion is a feature in C++11 that allows for the checking of a Boolean condition at compile-time. It is useful for checking the validity of template arguments, or for validating the value of a constant expression. It is defined using the keyword "static_assert"

Q38. What is a const cast in C++?

A const cast is a feature in C++ that allows for the removal of the constness of an object. It is used to cast a const object to a non-const object, and it should be used with caution as it can lead to undefined behavior if the object is modified.

Q39. What is a reinterpret cast in C++?

A reinterpret cast is a feature in C++ that allows for the reinterpretation of the bit pattern of an object. It is used to cast an object to a different type, and it should be used with caution as it can lead to undefined behavior if the object's layout is changed.

Q40. What is a dynamic_cast in C++?

A dynamic_cast is a feature in C++ that allows for the checking of an object's type at runtime. It is used to cast an object to a derived class, and it returns a null pointer if the cast is not valid. It is typically used for runtime type checking and downcasting.

Q41. What is a function pointer in C++?

A function pointer is a pointer that points to a function. It can be used to pass a function as an argument to another function or to call a function indirectly. It is defined using the "*" operator and the function's return type and arguments.

Q42. What is a explicit constructor in C++?

A explicit constructor is a constructor that is defined with the keyword "explicit". This keyword specifies that the constructor can only be used for direct initialization and not for implicit conversions. It is useful for preventing unwanted type conversions.

Q43. What is a noexcept in C++?

A noexcept is a keyword in C++11 that indicates that a function or a destructor does not throw any exceptions. It is useful for indicating to the compiler that the function can be optimized for performance, and also for indicating to the developer that the function is expected to not throw any exception.

Q44. What is a using declaration in C++?

A using declaration is a feature in C++ that allows for the creation of an alias for a name or a function. It is defined using the keyword "using" followed by the name of the alias and the original name or function. It is useful for creating a shorthand for commonly used names or functions.

Q45. What is a template specialization in C++?

A template specialization is a feature in C++ that allows for the creation of a specific implementation of a template for a specific type or set of types. It is useful for creating optimized implementations for specific types, or for handling types that are not compatible with the generic template.

Q46. What is a SFINAE in C++?

SFINAE stands for "Substitution Failure Is Not An Error" and it's a technique in C++ template metaprogramming that allows the compiler to silently ignore certain template instantiations that would otherwise result in a compile-time error. It's used to enable some template-based overloading, and many other advanced template metaprogramming techniques.

Q47. What is a union in C++?

A union is a user-defined data type in C++ that allows for the storage of multiple data types in the same memory location. It is defined using the keyword "union" and can be used to save memory by using only the necessary space for the currently active data type.

Q48. What is a bit field in C++?

A bit field is a feature in C++ that allows for the storage of individual bits in a variable. It is defined using the keyword ":" followed by the number of bits needed and can be used to create flags or to pack data into a compact representation.

Q49. What is a POD in C++?

POD stands for "Plain Old Data" and it's a term used in C++ to describe objects that have the same memory layout as built-in types. PODs are not required to have a constructor, copy constructor, destructor, or any other special member functions.

Q50. What is a type trait in C++?

A type trait is a feature in C++ that allows for the introspection of types. It provides a way to query the properties of a type, such as whether it's a primitive, an array, a class, or whether it has certain member functions.

Q51. What is a scope resolution operator in C++?

The scope resolution operator "::" is used to specify the scope of a name. It can be used to access a global name that has been hidden by a local name, or to specify the scope of a member function or a static member variable. It is also used to access namespaces and enclosing class members.

Q52. What is a preprocessor directive in C++?

A preprocessor directive is a line of code that is processed by the preprocessor before the compilation begins. They begin with a '#' symbol and they are used to include files, define constants, and conditionally compile code. Examples include #include, #define, #ifdef and #pragma.

Q53. What is a function template in C++?

A function template is a feature in C++ that allows for the creation of a generic function that can operate on different types. It is defined using the keyword "template" followed by the template parameters and the function body. It can be used to create functions that work with different data types without the need for explicit type casting.

Q54. What is a class template in C++?

A class template is a feature in C++ that allows for the creation of a generic class that can have members of different types. It is defined using the keyword "template" followed by the template parameters and the class body. It can be used to create classes that work with different data types without the need for explicit type casting.

Q55. What is a template parameter in C++?

A template parameter is a placeholder for a type or value that is used in a template. It is defined using angle brackets <> and can be used to create a generic template that can be instantiated with different types or values.

Q56. What is a template argument in C++?

A template argument is the actual type or value that is used to instantiate a template. It is passed to the template when it is defined and replaces the template parameter in the template body.

Q57. What is a template instantiation in C++?

A template instantiation is the process of creating a specific version of a template for a given set of template arguments. The compiler generates a new version of the template with the template arguments replaced by the actual values or types.

Q58. What is a template metaprogramming in C++?

Template metaprogramming is a technique in C++ that uses templates and template instantiation to perform computation at compile-time. It allows for the creation of highly optimized and efficient code by using the power of the compiler to perform complex operations at compile-time.

Q59. What is a SFINAE rule in C++?

SFINAE (Substitution Failure Is Not An Error) is a technique in C++ template metaprogramming that allows the compiler to ignore certain template instantiations that would otherwise result in a compile-time error. It is used to enable template-based overloading and many other advanced template metaprogramming techniques.

Q60. What is a type alias template in C++?

A type alias template is a feature in C++11 that allows for the creation of a new name for a template. It is defined using the keyword "template" followed by the name of the alias and the original template. It is useful for creating a shorthand for commonly used templates.

Q61. What is a constexpr function in C++?

A constexpr function is a feature in C++11 that allows for the creation of compile-time constant functions. These functions are evaluated at compile-time and can be used wherever a constant expression is required. They can be used to optimize code and improve performance.

Q62. What is a default template argument in C++?

A default template argument is a feature in C++ that allows for the specification of a default value for a template parameter. This default value will be used when the template is instantiated without specifying a value for that parameter. It is defined by adding the "= default_value" after the template parameter.

Q63. What is a template parameter pack in C++?

A template parameter pack is a feature in C++11 that allows for the creation of a template that can take a variable number of template arguments. It is defined using the "..." operator and can be used to create variadic templates.

Q64. What is a fold expression in C++?

A fold expression is a feature in C++17 that allows for the application of a binary operator to a parameter pack. It is defined using the "..." operator and can be used to perform operations such as summing, concatenating or finding the minimum or maximum of a parameter pack.

Q65. What is a variadic template class in C++?

A variadic template class is a feature in C++ that allows for the creation of a class template that can take a variable number of template arguments. It is defined using the "..." operator and can be used to create classes that work with different data types without the need for explicit type casting.

Q66. What is a non-type template parameter in C++?

A non-type template parameter is a feature in C++ that allows for the use of non-type values, such as integers or pointers, as template arguments. It is defined by specifying the type and the value of the parameter and can be used to create templates that can be instantiated with different values.

Q67. What is a template template parameter in C++?

A template template parameter is a feature in C++ that allows for the use of a template as a template argument. It is defined by specifying the template parameter followed by the template arguments and can be used to create templates that can work with other templates.

Q68. What is a variadic constructor in C++?

A variadic constructor is a constructor that can take a variable number of arguments. It is defined using the "..." operator and can be used to create constructors that can be called with different number of arguments.

Q69. What is a extern template in C++?

A extern template is a feature in C++ that allows for the separation of template declarations and definitions. It is used to specify that the definition of a template is in another file, and it helps to reduce the code size and compilation time.

Q70. What is a template friend function in C++?

A template friend function is a friend function that is defined as a template. It is defined using the keyword "template" before the friend keyword and can be used to create friend functions that can operate on different types.

Q71. What is a specialization in C++?

Specialization is a feature in C++ that allows for the creation of a specific implementation of a template for a specific type or set of types. It is useful for creating optimized implementations for specific types, or for handling types that are not compatible with the generic template.

Q72. What is a template type deduction in C++?

A template type deduction is a feature in C++ that allows for the automatic deducing of template arguments from the types of the function arguments. It is used to deduce the template arguments for a template function or class and make the code more readable and easier to write.

Q73. What is a decltype in C++?

A decltype is a keyword in C++ that allows for the identification of the type of an expression. It is used to deduce the type of a variable, a function or an expression and can be used to improve the readability and maintainability of the code.

Q74. What is a type trait in C++?

A type trait is a feature in C++ that allows for the introspection of types. It provides a way to query the properties of a type, such as whether it's a primitive, an array, a class, or whether it has certain member functions. It is defined in the <type_traits> header and can be used to enable or disable certain features of a class or function based on the properties of its template arguments.

Q75. What is a template variable in C++?

A template variable is a feature in C++ that allows for the creation of variables that can be instantiated with different types or values. It is defined using the keyword "template" followed by the template parameter and the variable's type and can be used to create variables that can be used with different types without the need for explicit type casting.

Q76. What is a variadic class template in C++?

A variadic class template is a feature in C++ that allows for the creation of a class template that can take a variable number of template arguments. It is defined using the "..." operator and can be used to create classes that can handle a variable number of types or values without the need for explicit type casting or overloading.

Q77. What is a variadic member function in C++?

A variadic member function is a member function that can take a variable number of arguments. It is defined using the "..." operator and can be used to create functions that can handle a variable number of arguments without the need for explicit type casting or overloading.

Q78. What is an inline function in C++?

An inline function is a feature in C++ that allows for the function to be expanded inline at the point of call, rather than calling the function as a separate block of code. It is defined using the keyword "inline" before the function definition and can be used to improve performance by reducing function call overhead.

Q79. What is a static_assert in C++?

A static_assert is a feature in C++11 that allows for the checking of a Boolean condition at compile-time. It is useful for checking the validity of template arguments, or for validating the value of a constant expression. It is defined using the keyword "static_assert" followed by the Boolean condition and a string that will be displayed if the condition is false.

Q80. What is a nullptr in C++?

A nullptr is a feature in C++11 that represents a null pointer. It is defined as "nullptr" and can be used to replace the use of "NULL" or "0" for pointers. It improves type safety by making it clear that a null value is intended for a pointer, rather than an integer.