C and C++ Interview Questions Set 2
Q.1. Difference between Class and Structure.
Answer:- Structure: In C language, the structure is used to bundle different type of data types together. The variables inside a structure are called the members of the structure. These members are by default public and can be accessed by using the structure name followed by a dot operator and then the member name.
Class: Class is a successor of the Structure. C++ extends the structure definition to include the functions that operate on its members. By default all the members inside the class are private.
Q.2. What is the difference between an Object and a Class?
Answer:- Class is a blueprint of a project or problem to be solved and consists of variables and methods. These are called the members of the class. We cannot access methods or variables of the class on its own unless they are declared static.
In order to access the class members and put them to use, we should create an instance of a class which is called an Object. The class has an unlimited lifetime whereas an object has a limited lifespan only.
Q.3. What are the various Access Specifiers in C++?
Answer:- C++ supports the following access specifiers:
- Public: Data members and functions are accessible outside the class.
- Private: Data members and functions are not accessible outside the class. The exception is the usage of a friend class.
- Protected: Data members and functions are accessible only to the derived classes
Q.4. When should we use pointers in a C program?
Answer:-
- To get address of a variable
- For achieving pass by reference in C: Pointers allow different functions to share and modify their local variables.
- To pass large structures so that complete copy of the structure can be avoided.
- To implement “linked” data structures like linked lists and binary trees.
Q.5. What is NULL pointer?
Answer:- NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a pointer NULL when memory pointed by it is deallocated in the middle of a program.
Q.6. What are static functions? What is their use?
Answer:- In C, functions are global by default. The “static” keyword before a function name makes it static. Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.
Q.7. What is the difference between Method Overloading and Method Overriding in C++?
Answer:- Method overloading is having functions with the same name but different argument list. This is a form of compile-time polymorphism.
Method overriding comes into picture when we rewrite the method that is derived from a base class. Method overriding is used while dealing with run-time polymorphism or virtual functions.
Q.8. What is Inheritance?
Answer:- Inheritance is a process by which we can acquire the characteristics of an existing entity and form a new entity by adding more features to it.
In terms of C++, inheritance is creating a new class by deriving it from an existing class so that this new class has the properties of its parent class as well as its own.
Q.9. What is a friend function?
Answer:- C++ class does not allow its private and protected members to be accessed outside the class. But this rule can be violated by making use of the “Friend” function.As the name itself suggests, friend function is an external function which is a friend of the class. For friend function to access the private and protected methods of the class, we should have a prototype of the friend function with the keyword “friend” included inside the class.
Q.10. What is an Inline function in C++?
Answer:- Inline function is a function that is compiled by the compiler as the point of calling the function and the code is substituted at that point. This makes compiling faster. This function is defined by prefixing the function prototype with the keyword “inline”.
Q.11. Explain Pass by value and Pass by reference.
Answer:- Pass by value: In this approach we pass copy of actual variables in function as a parameter. Hence any modification on parameters inside the function will not reflect in the actual variable. For example:
#include<stdio.h>
int main(){
int a=5,b=10;
swap(a,b);
printf("%d %d",a,b);
return 0; }
void swap(int a,int b){
int temp;
temp =a;
a=b;
b=temp; }
Output: 5 10
Pass by reference: In this approach we pass memory address actual variables in function as a parameter. Hence any modification on parameters inside the function will reflect in the actual variable. For example:
#include<stdio.h>
int main(){
int a=5,b=10;
swap(&a,&b);
printf("%d %d",a,b);
return 0; }
void swap(int *a,int *b){
int *temp;
*temp =*a;
*a=*b;
*b=*temp; }
Output: 10 5
Q.12. What is Static Member?
Answer:- Static is a keyword in C++ used to give special characteristics to an element. Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime.
Q.13. Name the Operators that cannot be Overloaded.
Answer:-
- sizeof – sizeof operator
- . – Dot operator
- .* – dereferencing operator
- -> – member dereferencing operator
- :: – scope resolution operator
- ?: – conditional operator
Q.14. Function can be overloaded based on the parameter which is a value or a reference. Explain if the statement is true.
Answer:- False. Both, Passing by value and Passing by reference look identical to the caller.
Q.15. State the difference between delete and delete[].
Answer:- “delete[]” is used to release the memory allocated to an array which was allocated using new[]. “delete” is used to release one chunk of memory which was allocated using new.
For any queries or doubts refer to comment section mentioning question number with doubt
For more Technical MCQ's and Interview Questions Click here