1. What is C++?
C++ is a general-purpose programming language that was developed as an extension of the C programming language. It provides additional features such as object-oriented programming and support for generic programming.
2. What are the key features of C++?
The key features of C++ include:
- Object-oriented programming
- Inheritance and polymorphism
- Templates and generic programming
- Exception handling
- Standard Template Library (STL)
3. What is the difference between C and C++?
C++ is an extension of the C programming language, so it includes all the features of C. However, C++ also introduces new features such as classes, objects, and inheritance, which make it an object-oriented language.
4. How do you declare a variable in C++?
In C++, variables are declared by specifying the data type followed by the variable name. For example, to declare an integer variable named “num”, you would write:
int num;
5. What is the difference between “cout” and “cin” in C++?
“cout” is used for outputting data to the console, while “cin” is used for taking input from the user. For example, to display a message on the console, you would use “cout”, and to get input from the user, you would use “cin”.
6. What is a class in C++?
A class in C++ is a user-defined data type that encapsulates data and functions into a single unit. It serves as a blueprint for creating objects, which are instances of the class.
7. What is an object in C++?
An object in C++ is an instance of a class. It represents a specific entity that can have its own data and functions. Objects are created based on the blueprint provided by the class.
8. What is inheritance in C++?
Inheritance is a feature of object-oriented programming in C++ that allows a class to inherit properties and behaviors from another class. The class that inherits is called the derived class, and the class from which it inherits is called the base class.
9. What is polymorphism in C++?
Polymorphism is a concept in C++ that allows objects of different classes to be treated as objects of a common base class. It enables the use of a single interface to represent different types of objects.
10. What are constructors and destructors in C++?
Constructors are special member functions of a class that are used to initialize objects of that class. They have the same name as the class and are automatically called when an object is created. Destructors, on the other hand, are used to clean up resources and are called when an object is destroyed.
11. What is the difference between a class and an object?
A class is a blueprint or template for creating objects, while an object is an instance of a class. In other words, a class defines the properties and behaviors that an object of that class will have.
12. What is encapsulation in C++?
Encapsulation is a concept in C++ that refers to the bundling of data and functions within a class. It allows for the hiding of data and implementation details, and provides a way to access and modify the data through public interfaces.
13. What is a namespace in C++?
A namespace in C++ is a declarative region that provides a scope for the identifiers within it. It helps to organize code and prevent naming conflicts by allowing the same name to be used in different namespaces.
14. What is the use of “const” keyword in C++?
The “const” keyword in C++ is used to declare variables as constants, which means their values cannot be changed once they are assigned. It is also used to specify that a member function does not modify the object’s data.
15. What is function overloading in C++?
Function overloading is a feature in C++ that allows multiple functions with the same name but different parameters to be defined. The appropriate function is selected based on the number and types of arguments passed to it.
16. What is a template in C++?
A template in C++ is a mechanism that allows for the creation of generic classes and functions. It enables the definition of a class or function without specifying the data type(s) it will operate on, allowing for code reusability.
17. What is the difference between pass by value and pass by reference?
When passing an argument by value in C++, a copy of the value is made and passed to the function. Changes made to the parameter within the function do not affect the original value. Pass by reference, on the other hand, passes a reference to the original value, allowing changes made within the function to modify the original value.
18. What is the difference between a pointer and a reference?
A pointer in C++ is a variable that stores the memory address of another variable. It allows direct manipulation of memory and can be reassigned to point to different variables. A reference, on the other hand, is an alias for an existing variable. It cannot be reassigned to refer to a different variable once it is initialized.
19. What is the difference between a structure and a class in C++?
In C++, a structure is similar to a class, but its members are public by default, whereas in a class, they are private by default. Additionally, a class can have member functions and inherit from other classes, while a structure cannot.
20. What is the use of the “new” operator in C++?
The “new” operator in C++ is used to dynamically allocate memory for objects or arrays. It returns a pointer to the allocated memory, which can then be used to access and manipulate the object or array.
21. What is the use of the “delete” operator in C++?
The “delete” operator in C++ is used to free the memory allocated by the “new” operator. It deallocates the memory and calls the destructor of the object if necessary.
22. What is the difference between stack and heap memory?
In C++, stack memory is used for storing local variables and function call information. It is automatically managed by the compiler. Heap memory, on the other hand, is used for dynamically allocated memory and must be managed manually using the “new” and “delete” operators.
23. What is the difference between a shallow copy and a deep copy?
A shallow copy of an object in C++ copies the values of its member variables, but if the object contains pointers, the copied object will still point to the same memory locations as the original object. A deep copy, on the other hand, creates a new object with its own copies of the dynamically allocated memory, ensuring that changes to one object do not affect the other.
24. What is the use of the “const” member function in C++?
A “const” member function in C++ is a member function that does not modify the object’s data. It is used to indicate that the function will not change the state of the object and can be called on constant objects.
25. What is the difference between static and dynamic binding in C++?
Static binding, also known as early binding, occurs when the function call is resolved at compile-time based on the type of the pointer or reference. Dynamic binding, also known as late binding, occurs when the function call is resolved at runtime based on the actual type of the object.
26. What is the difference between virtual functions and pure virtual functions?
A virtual function in C++ is a member function of a base class that can be overridden by a derived class. A pure virtual function, on the other hand, is a virtual function that has no implementation in the base class and must be overridden by any derived class.
27. What is multiple inheritance in C++?
Multiple inheritance is a feature in C++ that allows a class to inherit from multiple base classes. It enables a class to acquire properties and behaviors from more than one parent class.
28. What is function overriding in C++?
Function overriding is a feature in C++ that allows a derived class to provide a different implementation of a virtual function that is already defined in the base class. It enables polymorphism and runtime binding.
29. What is function hiding in C++?
Function hiding is a concept in C++ that occurs when a derived class defines a function with the same name as a function in the base class, but the function in the base class is not virtual. This can lead to unexpected behavior when calling the function through a pointer or reference to the base class.
30. What is an abstract class in C++?
An abstract class in C++ is a class that cannot be instantiated and is meant to be used as a base class for other classes. It contains at least one pure virtual function, which makes it an abstract class.
31. What is the difference between public, private, and protected access specifiers?
In C++, public, private, and protected are access specifiers that determine the visibility and accessibility of class members. Public members are accessible from anywhere, private members are only accessible within the class, and protected members are accessible within the class and its derived classes.
32. What is a friend function in C++?
A friend function in C++ is a non-member function that has access to the private and protected members of a class. It is declared inside the class with the keyword “friend” and can be useful for accessing or modifying private data.
33. What is the difference between an inline function and a normal function in C++?
An inline function in C++ is a function that is expanded in place at the point of function call, similar to a macro. It can improve performance by eliminating the overhead of function call. A normal function, on the other hand, is a separate entity that is called using a function call mechanism.
34. What is the use of the “this” pointer in C++?
The “this” pointer in C++ is a special pointer that holds the address of the current object. It is used to refer to the members of the current object and can be useful in cases where there is a name conflict between a parameter and a member variable.
35. What is the difference between static variables and instance variables?
A static variable in C++ is a variable that is shared among all instances of a class. It is declared using the “static” keyword and retains its value between function calls. An instance variable, on the other hand, is a variable that is unique to each instance of a class and is declared without the “static” keyword.
36. What is the use of the “sizeof” operator in C++?
The “sizeof” operator in C++ is used to determine the size, in bytes, of a data type or an object. It can be useful for memory allocation and manipulation.
37. What is a const pointer in C++?
A const pointer in C++ is a pointer that points to a constant value. It cannot be used to modify the value it points to, but it can be reassigned to point to a different constant value.
38. What is the use of the “static” keyword in C++?
The “static” keyword in C++ can be used in various contexts:
- When used with a variable inside a function, it makes the variable retain its value between function calls.
- When used with a variable inside a class, it makes the variable shared among all instances of the class.
- When used with a function inside a class, it makes the function a class-level function that can be called without an object.
39. What is the difference between a reference and a pointer?
A reference in C++ is an alias for an existing variable. It must be initialized when declared and cannot be reassigned to refer to a different variable. A pointer, on the other hand, is a variable that stores the memory address of another variable. It can be reassigned to point to different variables.
40. What is the use of the “typeid” operator in C++?
The “typeid” operator in C++ is used to obtain the type information of a variable or an expression at runtime. It returns a reference to a “type_info” object, which can be used to compare types or retrieve type names.
41. What is the use of the “try-catch” block in C++?
The “try-catch” block in C++ is used for exception handling. Code that may throw an exception is placed inside the “try” block, and if an exception is thrown, it is caught and handled by the corresponding “catch” block.
42. What is the difference between stack unwinding and exception propagation?
Stack unwinding is the process of deallocating resources and calling destructors for objects in the call stack when an exception is thrown. Exception propagation, on the other hand, is the process of searching for a suitable “catch” block to handle the exception.
43. What is the use of the “throw” keyword in C++?
The “throw” keyword in C++ is used to explicitly throw an exception. It is typically used in conjunction with the “try-catch” block to handle exceptional situations.
44. What is the difference between a compiler error and a runtime error?
A compiler error in C++ occurs during the compilation phase and prevents the program from being successfully compiled. It is typically caused by syntax errors or type mismatches. A runtime error, on the other hand, occurs during the execution of the program and can cause the program to terminate abnormally. It is typically caused by logical errors or exceptional conditions.
45. What is the use of the “extern” keyword in C++?
The “extern” keyword in C++ is used to declare a variable or function that is defined in another translation unit (source file). It is used to indicate that the variable or function has external linkage.
46. What is the difference between a preprocessor directive and a compiler directive?
A preprocessor directive in C++ is a statement that is processed by the preprocessor before the compilation phase. It is used to perform text substitution and conditional compilation. A compiler directive, on the other hand, is a statement that is processed by the compiler during the compilation phase. It is used to control the behavior of the compiler or optimize the code.
47. What is the use of the “volatile” keyword in C++?
The “volatile” keyword in C++ is used to indicate that a variable may be modified by external factors that are not visible to the compiler. It prevents the compiler from performing certain optimizations that may assume the variable’s value does not change.
48. What is the use of the “namespace” keyword in C++?
The “namespace” keyword in C++ is used to define a namespace, which is a declarative region that provides a scope for the identifiers within it. It helps to organize code and prevent naming conflicts by allowing the same name to be used in different namespaces.
49. What is the difference between a header file and a source file in C++?
A header file in C++ contains declarations of classes, functions, and variables that are used in multiple source files. It is typically included at the beginning of a source file using the “#include” directive. A source file, on the other hand, contains the implementation of classes, functions, and variables.
50. What is the use of the “inline” keyword in C++?
The “inline” keyword in C++ is used to suggest to the compiler that a function should be expanded in place at the point of function call, similar to a macro. It can improve performance by eliminating the overhead of function call.