C++ Interview: Technical Questions

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the block scope variable in C++?

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.

What is a pure virtual function?

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

What is abstraction?

Abstraction refers to hiding the internal implementation and exhibiting only the necessary details.

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

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

What is the full form of OOPS?

Object Oriented Programming System.

When should we use the register storage specifier?

blah

Name the default standard streams in C++.

cin, cout, cerr and clog.

What is the purpose of 'delete' operator?

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

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

'new' is the operator can be used for the same.

What is an inline function?

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.

What is an object?

An instance of the class is called as object.

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

Dot (.) and Arrow ( -> )

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

Scope resolution operator (::)

What are the differences between C and C++?

1) C++ is a kind of superset of C, most of C programs except few exceptions (See this and this) work in C++ as well. 2) C is a procedural programming language, but C++ supports both procedural and Object Oriented programming. 3) Since C++ supports object oriented programming, it supports features like function overloading, templates, inheritance, virtual functions, friend functions. These features are absent in C. 4) C++ supports exception handling at language level, in C exception handling is done in traditional if-else style. 5) C++ supports references, C doesn't. 6) In C, scanf() and printf() are mainly used input/output. C++ mainly uses streams to perform input and output operations. cin is standard input stream and cout is standard output stream. There are many more differences, above is a list of main differences.

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

If the file already exists, its content will be truncated before opening the file

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

Yes, as C is the subset of C++, we can all the functions of C in C++ too.

Can we implement all the concepts of OOPS using the keyword struct?

Yes.

What are VTABLE and VPTR?

vtable is a table of function pointers. It is maintained per class. vptr is a pointer to vtable. It is maintained per object (See this for an example). Compiler adds additional code at two places to maintain and use vtable and vptr. 1) Code in every constructor. This code sets vptr of the object being created. This code sets vptr to point to vtable of the class. 2) Code with polymorphic function call (e.g. bp->show() in above code). Wherever a polymorphic call is made, compiler inserts code to first look for vptr using base class pointer or reference (In the above example, since pointed or referred object is of derived type, vptr of derived class is accessed). Once vptr is fetched, vtable of derived class can be accessed. Using vtable, address of derived derived class function show() is accessed and called.

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

wchar_t

What is a reference variable in C++?

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.

What is an abstract class in C++?

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

What is the role of mutable storage class specifier?

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.

What is a constructor?

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.

What is a copy constructor?

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.

What is a destructor? Can it be overloaded?

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.

What is a friend function?

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'.

What is a namespace?

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.

What is a static variable?

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() { static int i; ++i; printf("%d ",i); } If a global variable is static then its visibility is limited to the same source code.

Explain the static member function.

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.

What is role of static keyword on class member variable?

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. A static member variable can be referred using the class name itself.

What is a class template?

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

What are the differences between references and pointers?

Both references and pointers can be used to change local variables of one function inside another function. Both of them can also be used to save copying of big objects when passed as arguments to functions or returned from functions, to get efficiency gain. Despite above similarities, there are following differences between references and pointers. References are less powerful than pointers 1) Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers. 2) References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing to any valid thing. 3) A reference must be initialized when declared. There is no such restriction with pointers Due to the above limitations, references in C++ cannot be used for implementing data structures like Linked List, Tree, etc. In Java, references don't have above restrictions, and can be used to implement all data structures. References being more powerful in Java, is the main reason Java doesn't need pointers. References are safer and easier to use: 1) Safer: Since references must be initialized, wild references like wild pointers are unlikely to exist. It is still possible to have references that don't refer to a valid location (See questions 5 and 6 in the below exercise ) 2) Easier to use: References don't need dereferencing operator to access the value. They can be used like normal variables. '&' operator is needed only at the time of declaration. Also, members of an object reference can be accessed with dot operator ('.'), unlike pointers where arrow operator (->) is needed to access members.

What is keyword auto for?

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; }

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

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

Explain the purpose of the keyword volatile.

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

What is operator overloading?

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

Does C++ supports exception handling? 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.

What is a class?

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

What is function overloading?

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

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

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

What is the role of protected access specifier?

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

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

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

What is inheritance?

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.

Explain the pointer - this.

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

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

No, Defining a class/structure is just a type definition and will not allocated memory for the same.

Which access specifier/s can help to achive data hiding in C++?

Private & Protected.

Distinguish between shallow copy and deep copy.

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.

List the types of inheritance supported in C++.

Single, Multilevel, Multiple, Hierarchical and Hybrid.

What is a storage class?

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

What is this pointer?

The 'this' pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. 'this' pointer is a constant pointer that holds the memory address of the current object. 'this' pointer is not available in static member functions as static member functions can be called without any object (with class name).

What are command line arguments?

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.

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

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

Mention the storage classes names in C++.

The following are storage classes supported in C++ auto, static, extern, register and mutable

What is encapsulation?

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

What is the scope resolution operator?

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.

What is the meaning of base address of the array?

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

What is the purpose of extern storage specifier.

Used to resolve the scope of global symbol #include <iostream> using namespace std; main() { extern int i; cout<<i<<endl; } int i = 20;

What are virtual functions - Write an example?

Virtual functions are used with inheritance, they are called according to the type of object pointed or referred, not according to the type of pointer or reference. In other words, virtual functions are resolved late, at runtime. Virtual keyword is used to make a function virtual. Following things are necessary to write a C++ program with runtime polymorphism (use of virtual functions) 1) A base class and a derived class. 2) A function with same name in base class and derived class. 3) A pointer or reference of base class type pointing or referring to an object of derived class. For example, in the following program bp is a pointer of type Base, but a call to bp->show() calls show() function of Derived class, because bp points to an object of Derived class. #include<iostream> using namespace std; class Base { public: virtual void show() { cout<<" In Base \n"; } }; class Derived: public Base { public: void show() { cout<<"In Derived \n"; } }; int main(void) { Base *bp = new Derived; bp->show(); // RUN-TIME POLYMORPHISM return 0; }

What is the data type to store the Boolean value?

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

website

https://www.geeksforgeeks.org/quiz-corner-gq/#C++%20Programming%20Mock%20Tests https://www.geeksforgeeks.org/easy/ https://www.tutorialspoint.com/cplusplus/cpp_interview_questions.htm


संबंधित स्टडी सेट्स

Chapter 32: Skin Integrity and Wound Care PrepU

View Set

Criminal Law & Procedure Learning Questions

View Set

Chapter 8 Therapeutic Relationship, Chapter 3 Biological Basis for Understanding Psychiatric Disorders and Treatments, Chapter 7 The Nursing Process and Standards of Care for Psychiatric Mental Health Nursing, Chapter 9 Communication and the Clinical...

View Set

Chapter 26 - Soft-Tissue Injuries

View Set

FNH 200 - Preservation by low temperatures

View Set

Domain 1.4 - Analyzing Assessment Output

View Set

Econ 101 Final: Miyoung Oh Iowa State

View Set