C/C++ Programming Questions

Q. - What is the full form of OOPS?

Ans. - Object Oriented Programming System.

Q. - What is a class ?

Ans. - Class is a blue print which reflects the entities attributes and actions. Technically defining a class is designing a user defined data type.

Q. - What is an object ?

Ans. - An instance of the class is called as object.

Q. - What is encapsulatio

Ans. - The process of binding the data and the functions acting on the data together in an entity (class) is called as encapsulation.

Q. - What is inheritance ?

Ans. - Inheritance is the process of acquiring the properties of the exiting class into the new class. The existing class is called as base/parent class and the inherited class is called as derived/child class.

Q. - What is an inline function ?

Ans. - A function prefixed with the keyword inline before the function definition is called as inline function. The inline functions are faster in execution when compared to normal functions as the compiler treats inline functions as macros.

Q. - List the types of inheritance supported in C++.

Ans. - Single Multilevel Multiple Hierarchical and Hybrid.

Q. - What is the role of protected access specifier?

Ans. - If a class member is protected then it is accessible in the inherited class. However outside both the private and protected members are not accessible.

Q. - Explain the purpose of the keyword volatile.

Ans. - Declaring a variable volatile directs the compiler that the variable can be changed externally. Hence avoiding compiler optimization on the variable reference.

Q. - Mention the storage classes names in C++.

Ans. - The following are storage classes supported in C++ - auto - static - extern - register - mutable

Q. - What is the role of mutable storage class specifier?

Ans. - A constant class object's member variable can be altered by declaring it using mutable storage class specifier. Applicable only for non-static and non-constant member variable of the class.

Q. - Distinguish between shallow copy and deep copy.

Ans. - Shallow copy does memory dumping bit-by-bit from one object to another. Deep copy is copy field by field from object to another. Deep copy is achieved using copy constructor and or overloading assignment operator.

Q. - What is a pure virtual function?

Ans. - A virtual function with no function body and assigned with a value zero is called as pure virtual function.

Q. - What is a storage class ?

Ans. - Storage class specifies the life or scope of symbols such as variable or functions.

Q. - What is an abstract class in C++ ?

Ans. - A class with at least one pure virtual function is called as abstract class. We cannot instantiate an abstract class.

Q. - What is a reference variable in C++?

Ans. - A reference variable is an alias name for the existing variable. Which mean both the variable name and reference variable point to the same memory location. Therefore updation on the original variable can be achieved using reference variable too.

Q. - What is role of static keyword on class member variable?

Ans. - A static variable does exit though the objects for the respective class are not created. Static member variable share a common memory across all the objects created for the respective class. Astatic member variable can be referred using the class name itself.

Q. - Name the data type which can be used to store wide characters in C++.

Ans. - wchar_t

Q. - Explain static member function.

Ans. - A static member function can be invoked using the class name as it exits before class objects comes into existence. It can access only static members of the class.

Q. - What is function overloading ?

Ans. - Defining several functions with the same name with unique list of parameters is called as function overloading.

Q. - What are/is the operator/operators used to access the class members?

Ans. - Dot (.) and Arrow ( -> )

Q. - Can we initialize a class/structure member variable as soon as the same is defined?

Ans. - No Defining a class/structure is just a type definition and will not allocate memory for the same.

Q. - What is the data type to store the Boolean value?

Ans. - bool is the new primitive data type introduced in C++ language.

Q. - What is operator overloading?

Ans. - Defining a new job for the existing operator w.r.t the class objects is called as operator overloading.

Q. - Do we have a String primitive data type in C++?

Ans. - No it's a class from STL (Standard template library).

Q. - Name the default standard streams in C++.

Ans. - cin cout cerr and clog.

Q. - Which access specifier/s can help to achieve data hiding in C++?

Ans. - Private & Protected.

Q. - When a class member is defined outside the class which operator can be used to associate the function definition to a particular class?

Ans. - Scope resolution operator (::)

Q. - What is a destructor? Can it be overloaded?

Ans. - A destructor is the member function of the class which is having the same name as the class name and prefixed with tilde (~) symbol. It gets executed automatically w.r.t the object as soon as the object loses its scope. It cannot be overloaded and the only form is without the parameters.

Q. - What is a constructor?

Ans. - A constructor is the member function of the class which is having the same as the class name and gets executed automatically as soon as the object for the respective class is created.

Q. - What is a default constructor? Can we provide one for our class?

Ans. - Every class does have a constructor provided by the compiler if the programmer does not provides one and known as default constructor. A programmer provided constructor with no parameters is called as default constructor. In such case compiler does not provides the constructor.

Q. - Which operator can be used in C++ to allocate dynamic memory?

Ans. - 'new' is the operator that can be used for the same.

Q. - What is the purpose of 'delete' operator?

Ans. - 'delete' operator is used to release the dynamic memory which was created using 'new' operator.

Q. - Can I use malloc() function of C language to allocate dynamic memory in C++?

Ans. - Yes as C is the subset of C++ we can use all the functions of C in C++.

Q. - Can I use 'delete' operator to release the memory which was allocated using malloc() function of C language?

Ans. - No we need to use free() of C language for the same.

Q. - What is a copy constructor?

Ans. - A copy constructor is the constructor which take same class object reference as the parameter. It gets automatically invoked as soon as the object is initialized with another object of the same class at the time of its creation.

Q. - Does C++ supports exception handling?

Ans. - If so what are the keywords involved in achieving the same. C++ does supports exception handling. try catch & throw are keyword used for the same.

Q. - What is the difference between the keywords struct and class in C++?

Ans. - By default the members of struct are public and by default the members of the class are private.

Q. - 5 Can we implement all the concepts of OOPS using the keyword struct?

Ans. - Yes

Q. - What is the block scope variable in C++?

Ans. - A variable whose scope is applicable only within a block is said so. Also a variable in C++ can be declared anywhere within the block.

Q. - What is the role of the file opening mode ios::trunk?

Ans. - If the file already exists its content will be truncated before opening the file.

Q. - What is the scope resolution operator?

Ans. - The scope resolution operator is used to - Resolve the scope of global variables. - To associate function definition to a class if the function is defined outside the class.

Q. - What are command line arguments?

Ans. - The arguments/parameters which are sent to the main() function while executing from the command line/console are called so. All the arguments sent are the strings only.

Q. - What is a class template?

Ans. - A template class is a generic class. The keyword template can be used to define a class template.

Q. - How can we catch all kind of exceptions in a single catch block?

Ans. - The catch block with ellipses as follows catch(...) { }

Q. - What is keyword auto for?

Ans. - By default every local variable of the function is automatic (auto). In the below function both the variables 'i' and 'j' are automatic variables. void f() { int i; auto int j; }

Q. - What is a static variable?

Ans. - A static local variables retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice. void f() {\tstatic int i; \t++i; \tprintf("%d "i); } If a global variable is static then its visibility is limited to the same source code.

Q. - What is the purpose of extern storage specifier.

Ans. - Used to resolve the scope of global symbol #include using namespace std; main() { \textern int i; \tcout<

Q. - What is the meaning of base address of the array?

Ans. - The starting address of the array is called as the base address of the array.

Q. - When should we use the register storage specifier?

Ans. - If a variable is used most frequently then it should be declared using register storage specifier then possibly the compiler gives CPU register for its storage to speed up the look up of the variable.

Q. - Can a program be compiled without main() function?

Ans. - Yes it can be but cannot be executed as the execution requires main() function definition.

Q. - Where an automatic variable is stored?

Ans. - Every local variable by default being an auto variable is stored in stack memory.

Q. - What is a container class?

Ans. - A class containing at least one member variable of another class type in it is called so.

Q. - What is a token?

Ans. - A C++ program consists of various tokens and a token is either a keyword an identifier a constant a string literal or a symbol.

Q. - What is a preprocessor?

Ans. - Preprocessor is a directive to the compiler to perform certain things before the actual compilation process begins.

Q. - What are command line arguments?

Ans. - The arguments which we pass to the main() function while executing the program are called as command line arguments. The parameters are always strings held in the second argument (below in args) of the function which is array of character pointers. First argument represents the count of arguments (below in count) and updated automatically by operating system. main(int count char *args[]) { }

Q. - What are the different ways of passing parameters to the functions? Which to use when?

Ans. - Call by value: We send only values to the function as parameters. We choose this if we do not want the actual parameters to be modified with formal parameters but just used. Call by address: We send address of the actual parameters instead of values. We choose this if we do want the actual parameters to be modified with formal parameters. Call by reference: The actual parameters are received with the C++ new reference variables as formal parameters. We choose this if we do want the actual parameters to be modified with formal parameters.

Q. - What is reminder for 5.0 % 2?

Ans. - Error It is invalid that either of the operands for the modulus operator (%) is a real number.

Q. - What is a friend function ?

Ans. - A function which is not a member of the class but still can access all the member of the class is called so. To make it happen we need to declare within the required class following the keyword 'friend'.

Q. - Explain pointer - this.

Ans. - This is the pointer variable of the compiler which always holds the current active object's address.

Q. - What is the namespace ?

Ans. - A namespace is the logical division of the code which can be used to resolve the name conflict of the identifiers by placing them under different name space.

Q. - Write a C/C++ program to reverse a number.

Ans. - int absum; printf(\ Enter any two integers: \ ); scanf(\ %d%d\ &a&b); sum = a + ~b + 1; printf(\ Difference of two integers: %d\ sum); return 0;

Q. - Have you ever worked on an C/C++ project ?'

Ans. - Mention project and advantages of the same - Mention the concepts learned