C++ 2 Marks Important Questions
Q: What is Encapsulation
- Encapsulation is the process of wrapping data and functions into a single unit called a class.
- It hides internal details of objects and only exposes what is necessary.
- It helps in protecting data from unauthorized access.
- It is achieved using access specifiers like private, public, and protected.
Q: What is Abstraction
- Abstraction means showing only essential features and hiding internal details.
- It helps in reducing complexity of the program.
- Abstract classes and interfaces are used to implement abstraction.
- It focuses on what an object does rather than how it does it.
Q: What is Inline function
- An inline function is a function in which the code is inserted at the point of function call.
- It reduces function call overhead.
- It is defined using the keyword inline before the function.
- Suitable for small, frequently called functions.
Q: What is virtual function
- A virtual function allows function overriding in inheritance.
- It is declared using the keyword virtual in the base class.
- It enables runtime polymorphism.
- It allows the derived class to override the base class function.
Q: Define Constructor
- A constructor is a special function used to initialize objects.
- It has the same name as the class.
- It is automatically called when an object is created.
- It has no return type.
Q: Define Destructor
- A destructor is used to destroy objects and release resources.
- It has the same name as the class with a tilde ~ prefix.
- It is called automatically when an object goes out of scope.
- It has no arguments and no return type.
Q: Explain get() and put() function
- get() is used to read a single character from input.
- Syntax: cin.get(ch); where ch is a character variable.
- put() is used to write a single character to output.
- Syntax: cout.put(ch);
Q: Explain tellg() and tellp() with syntax
- tellg() tells the current position of the get pointer in an input file stream.
- Syntax: pos = fin.tellg();
- tellp() tells the current position of the put pointer in an output file stream.
- Syntax: pos = fout.tellp();
Q: What is Stream?
- A stream is a flow of data between a program and input/output devices.
- Input stream reads data; output stream writes data.
- It is used in file handling and I/O operations.
- Examples: cin, cout, ifstream, ofstream.
Q: Explain any two manipulators
- endl is used to insert a newline character and flush the output buffer.
- setw(n) sets the width of the next input/output field to n characters.
- Both are found in the <iomanip> header file.
- They help format output in a better way.
Q: What is default argument
- A default argument provides a value to a function parameter if no argument is passed.
- It is specified in the function declaration.
- It must be assigned from right to left.
- It helps reduce the number of overloaded functions.
Q: What is extraction and insertion operator?
- Extraction operator >> is used to take input from cin.
- Insertion operator << is used to output data using cout.
- They are also called stream operators.
- These operators can be overloaded in C++.
Q: What is Run time polymorphism
- It is also called dynamic polymorphism.
- It occurs when a function is called at runtime using a base class pointer.
- Achieved using virtual functions.
- The exact function to be called is decided at runtime.
Q: What is Compile time polymorphism
- Also known as static polymorphism.
- Achieved through function overloading and operator overloading.
- Function to be executed is decided at compile time.
- It increases code readability and reusability.
Q: Explain any two use of Scope resolution operator
- It is used to define a function outside the class.
- It accesses a global variable when there is a local variable with the same name.
- It helps in referencing class members.
- Syntax: class_name::function_name()
Q: What are the access specifier used in C++
- Public - members are accessible from anywhere.
- Private - members are accessible only within the class.
- Protected - members are accessible in the class and its derived classes.
- These control access levels to class members.
Q: What is Class. Give its syntax
- A class is a user-defined data type.
- It contains data members and member functions.
- It is a blueprint for creating objects.
- Syntax: class ClassName { // members };
Q: List operators which cannot be overloaded
- :: (Scope resolution operator)
- . (Member access operator)
- .* (Pointer-to-member operator)
- ?: (Ternary conditional operator)
Q: State user define data types in C++
- Class
- Structure
- Union
- Enumeration (enum)
Q: Define Friend function
- A friend function is not a member of the class but can access private members.
- It is declared using the keyword friend.
- It helps in operator overloading.
- Defined outside the class but can access its private data.
Q: Define multiple inheritance
- Multiple inheritance means a class inherits from more than one base class.
- Syntax: class C : public A, public B
- It allows combining features of multiple classes.
- It may lead to ambiguity if not handled properly.
Q: What is OOP
- OOP stands for Object-Oriented Programming.
- It is a programming approach based on classes and objects.
- Main features: encapsulation, abstraction, inheritance, and polymorphism.
- C++ supports OOP principles.
Q: What's is 'this' pointer
- this is a pointer that holds the address of the current object.
- It is used inside non-static member functions.
- Helps in differentiating between local and class variables.
- It is implicitly passed to all non-static functions.
Q: Define pure virtual function
- A pure virtual function has no definition in the base class.
- It is declared by assigning 0 to the function in the base class.
- Syntax: virtual void func() = 0;
- It makes the class abstract.
Q: What is static polymorphism
- Static polymorphism is resolved at compile time.
- Achieved using function and operator overloading.
- It is also called compile-time polymorphism.
- It improves performance due to early binding.
Q: What are the visibility labels used in C++
- Visibility labels control access to class members.
- The labels are: public, private, and protected.
- Public allows access from anywhere.
- Private and protected limit access to the class or derived classes.