C++
What is the purpose of the Extern Storage Specifier?
"Extern" specifier is used to resolve the scope of a global symbol.
HASA
"HASA" relationship depicts that an entity may have another entity as its member or a class has another object embedded inside it.
ISA
"ISA" relationship usually exhibits inheritance as it implies that a class "ISA" specialized version of another class. For Example, An employee ISA person. That means an Employee class is inherited from the Person class.
State the difference between delete and delete[].
"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.
What is the basic structure of a C++ program?
#include <iostream> int main() a return statement
What are the various Arithmetic Operators in C++?
+ - * / %
What are the various Compound Assignment Operators in C++?
+=, -=, *=, /=, %=, >>=, <<=, &=, ^=,|=
What is a Default Constructor?
Default constructor is a constructor that either has no arguments or if there are any, then all of them are default arguments.
What is the difference between equal to (==) and Assignment Operator (=)?
Equal to (==) is an equality relational operator that evaluates two expressions to see if they are equal and returns true if they are equal and false if they are not. The assignment operator (=) is used to assign a value to a variable.
Function can be overloaded based on the parameter which is a value or a reference. Explain if the statement is true.
False. Both, Passing by value and Passing by reference look identical to the caller.
How do you define/declare constants in C++?
In C++, we can define our own constants using the #define preprocessor directive below #include
Function Overloading
allows the programmer to have more than one function with the same name but different parameter list. In other words, we overload the function with different arguments i.e. be it the type of arguments, number of arguments or the order of arguments.
Multithreading
allows your computer to run two or more programs concurrently
Operator Overloading
an operator is overloaded, so that it can operate on the user-defined types as well with the operands of the standard data type. But while doing this, the standard definition of that operator is kept intact.
Declare and initialize a reference
int a; int &ref = a;
Declare and initialize a pointer
int a; int p* = &a;
Does a derived class inherit or doesn't inherit?
it basically inherits all the features and ordinary members of the base class. But there are some exceptions to this rule. For instance, a derived class does not inherit the base class's constructors and destructors.
What is wrong with this code? T *p = 0; delete p;
the pointer is a null pointer. Hence automatically, the program will crash in an attempt to delete the null pointer.
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
What are the differences between C and C++?
1. C++ is a kind of superset of C, most of C programs except few exceptions work in C++ as well. 2. C is a procedural programming language, but C++ supports both procedural and OOP. 3. Since C++ supports OOP, it supports features like function overloading, templates, inheritance 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. cin is standard input stream and cout is standard output stream.
What is the difference between references and pointers
1. References are used to refer to an existing variable whereas pointers store the address of a variable. 2. References can't be NULL whereas pointers can be 3. A reference variable can be referenced by pass by value whereas a pointer can be referenced but pass by reference 4.A reference must be initialized on declaration while it is not necessary in case of pointer. 5. A reference shares the same memory address with the original variable but also takes up some space on the stack whereas a pointer has its own memory address and size on the stack. 6. A pointer can be reassigned but a reference cant
What is a Constant? Explain with an example
A constant is an expression that has a fixed value. They can be divided into integer, decimal, floating-point, character or string constants depending on their data type. int result = 10;
Explain the Static Member Function.
A static member function can access only the static member variable of the class. Same as the static member variables, a static member function can also be accessed using the class name.
What is a Static Variable?
A static variable is a local variable that retains its value across the function calls. Static variables are declared using the keyword "static".
Standard exceptions
Allocation was bad runtime error type id
Implicit constructor call?
Constructors are implicitly called by the compiler when an object of the class is created. This creates an object on a Stack. A Myobj;
What's the order in which the local objects are destructed?
the destructor for each will be called in the reverse order in which they were constructed.
What is an algorithm?
An algorithm is a sequence of unambiguous instructions for solving a problem
Why are arrays usually processed with for loop?
Array uses the index to traverse each of its elements. If A is an array then each of its elements is accessed as A[i].
What is the assignment Operator in C++?
Assignment operator in C++ is used to assign a value to another variable
What is a Class?
Class is a user-defined data type in C++. After creation, the user is not required to know the details of the working of a class. class acts as a blueprint of a project
Polymorphism
Compile-time polymorphism, and Run-time polymorphism.
What is a Constructor ?
Constructor is a member function of the class having the same name as the class. It is mainly used for initializing the members of the class. By default constructors are public.
What is the difference between while and do while loop?
In case of the while loop, we can directly exit the loop at the beginning, if the condition is not met whereas in the do-while loop we execute the loop statements at least once.
Compile-time Polymorphism
In compile-time polymorphism, we achieve many forms by overloading. Hence, we have an Operator overloading and function overloading.
What are Multiple Inheritances (virtual inheritance)? What are its advantages and disadvantages?
In multiple inheritances, we have more than one base classes from which a derived class can inherit. Hence, a derived class takes the features and properties of more than one base class.
What is the difference between an Object and a Class?
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.
What is Inheritance?
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.
Difference between ++a and a++?
In the case of the pre-increment/decrement operator, the increment/decrement operation is carried out first and then the result passed to an lvalue. Whereas for post-increment/decrement operations, the lvalue is evaluated first and then increment/decrement is performed accordingly.
What are the Extraction and Insertion operators in C++?
In the iostream.h library of C++, cin, and cout are the two data streams that are used for input and output respectively. Cout is normally directed to the screen and cin is assigned to the keyboard.
What are the advantages of Inheritance?
Inheritance allows code re-usability, thereby saving time on code development.
What is the this pointer?
It holds the address of the current object of the class If a class has local variables with the same name as parameters in a function and you want to assign the local variable value to the data members, use this pointer
What do you mean by 'void' return type?
It returns nothing. It may just be a function that prints something out tot he screen.
Local vs global scope of a variable
Local Scope: A variable is said to have a local scope or is local when it is declared inside a code block. The variable remains active only inside the block and is not accessible outside the code block. Global Scope: A variable has a global scope when it is accessible throughout the program. It is declared on top of the program before all the function definitions.
What are members of a class?
Members can include various parameters and functions or actions operating on these parameters
What is the difference between Method Overloading and Method Overriding in C++?
Method overloading is having functions with the same name but different argument lists. 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.
What is Namespace?
Namespace allows us to group a set of global classes, objects and/or functions under a specific name.
What's the order in which the objects in an array are destructed?
Objects in an array are destructed in the reverse order of construction: First constructed, last destructed.
What are the various Access Specifiers in C++?
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.
What is a template?
Templates allow creating functions that are independent of data type (generic) and can take any data type as parameters and return value without having to overload the function with all the possible data types. template <class identifier> function_declaration;
Difference between Declaration and Definition of a variable?
The declaration of a variable is specifying the data type of a variable and its name. The compiler reserves space for a variable in memory The definition is an implementation of the declared variable where we tie up the appropriate value to the declared variable
When to use a pointer vs an array
Things like linked lists and trees need pointers because they can change value and we need to iterate References are safer since they can't be NULL
Run-time Polymorphism
This is the polymorphism for classes and objects. General idea is that a base class can be inherited by several classes. A base class pointer can point to its child class and a base class array can store different child class objects.
What are Default Parameters? How are they evaluated in the C++ function?
This value is used if that parameter is left blank while calling to the function. To specify a default value for a particular parameter, we simply assign a value to the parameter in the function declaration. int multiply(int a, int b=2)
When there are a Global variable and Local variable with the same name, how will you access the global variable?
Using the scope resolution operator ::
What is Exception Handling?
When an exception has occurred, the compiler has to throw it so that we know an exception has occurred. When an exception has been thrown, the compiler has to ensure that it is handled properly, so that the program flow continues or terminates properly Try Throw Catch
Explicit constructor call?
When the object of a class is created using new, constructors are called explicitly. This usually creates an object on a Heap. A * pPoint = new A();
What is the precedence when there are a Global variable and a Local variable in the program with the same name?
Whenever there is a local variable with the same name as that of a global variable, the compiler gives precedence to the local variable.
Pass by value vs pass by reference
While passing parameters to the function using "Pass by Value", we pass a copy of the parameters to the function. Modifications are made to the parameters in the called function are not passed back to the calling function so variables in the calling function remain unchanged. if we want to get the changed values from the function back to the calling function, then we use the "Pass by Reference" technique.
Can you do delete this?
Yes. But only on an object created using new
What is wrong with this code? T *p = new T[10]; delete p;
it will just delete the first element of the array. Though the entire array is deleted, only the destructor of the first element will be called and the memory for the first element is released.
Give an example of a this pointer being used
private: int num char ch public: void setMyValues (int num, char ch) { this->num=num; this->ch=ch; }
Difference between Class and Structure?
structure is used to bundle different types 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 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.