You are on page 1of 15

When dynamic checking is necessary?

Dynamic checking is necessary in the following scenarios:

* Whenever the definition of a variable is not necessary before its usage.


* When implicit conversion of variables into other types.
* When the program is to be compiled independently as there is no type checking at
compile time.

What are the main differences between Java and C++?


The main differences between Java and C++ are

* Pointers are supported by C++, where as Java does not pointers.


* Operator overloading and multiple inheritance are supported by java where as Java
does not.
* Java is a platform independent language, where as C++ depends on different
platforms and machines.
* Java utilizes both compiler and interpreter, where as C++ is only compiler.
* Java has data type boolean, where as C++ does not.

Define structured programming.


Answer - Structured programming techniques use functions or subroutines to organize
the programming code. The programming purpose is broken into smaller pieces and
organized together using function. This technique provides cleaner code and simplifies
maintaining the program. Each function has its own identity and isolated from other, thus
change in one function doesn’t affect other.

Question - Explain Object oriented programming.


Answer - Object oriented programming uses objects to design applications. This
technique is designed to isolate data. The data and the functions that operate on the data
are combined into single unit. This unit is called an object. Each object can have
properties and member functions. You can call member function to access data of an
object. It is based on several techniques like encapsulation, modularity, polymorphism,
and inheritance.

Question - What is function prototype in C++?


Answer - A function prototype is a declaration of a function that omits the function body.
It specifies the function's name, argument types and return type. E.g. int add(int,int)

Question - What are the ways to comment statement in C++?


Answer - C++ supports two types of comments.
/* */ is used for commenting a block of code.
// is used for single line comments.
Question - Define Structure in C++.
Answer - The C++ programming technique allows defining user defined datatypes
through structure. The syntax to declare structure is as follows:
struct student
{
char name[100]
char address[250]
};

Question - Explain typecasting.


Answer - Typecasting enables data type conversion. C++ supports Implicit conversions
and Explicit conversion. Implicit conversions automatically performed when a value is
copied to a compatible type. If there is an operation between an int and a float, the int is
promoted to float before performing operation.

You can cast types explicitly as follows.


int i, j, k;
k = i * long(j);

Question - Define void pointer using C++.


Answer - In C++, void represents the absence of type, so void pointers are pointers that
point to a value that has no type. The void pointers can point to any data type.
We can declare void pointer as follows.
Void *p;

Question - When do you use :: Operator in C++?


Answer - :: is the scope resolution operator. When local variable and global variable are
having same name, local variable gets the priority. C++ allows flexibility of accessing
both the variables through a scope resolution operator.

Question - Define reference variable in C++.


Answer - A reference variable is just like pointer with few differences. It is declared
using & operator. A reference variable must always be initialized. The reference variable
once defined to refer to a variable can’t be changed to point to other variable. You can't
create an array of references the way it is possible with pointer.

Question - What is const qualifier?


Answer - const qualifier is used to specify the variable that can’t be change throughout
the program. Variable with const qualifier is often named in uppercase.

Question - When do you use bool data type?


Answer - The bool data type can take only two values true or false.

Question - What is function overloading in C++?


Answer - You can have multiple functions with same name using function overloading
facility of C++. You can use same name for multiple functions when all these functions
are doing same thing.

Question - What is operator overloading in C++?


Answer - With this facility in C++, you can give additional meaning to operators.

Question - Define Inline Function.


Answer - When the function is defined Inline, the C++ compiler puts the function body
inside the calling function. You can define function as Inline when the function body is
small and need to be called many times, thus reduces the overhead in calling a function
like passing values, passing control, returning values, returning control.

Question - Define class using C++.


Answer - A class holds the data and functions that operate on the data. It serves as the
template of an object.

Question - When do you use this pointer?


Answer - 'this pointer' is used as a pointer to the class object instance by the member
function. The address of the class instance is passed as an implicit parameter to the
member functions.

Question - What is new and delete operator?


Answer - In C++, when you want dynamic memory, you can use operator new. It returns
a pointer to the beginning of the new block of memory allocated. It returns a pointer to
the beginning of the new block of memory allocated.

When memory allocated by new operator is no longer required, it is freed using operator
delete.

Question - Define local class in C++.


Answer - Local class is define within the scope of a function and nested within a
function.
E.g.
int func1()
{
class localclass1
{.....};
}

Question - Define namespace in C++.


Answer - Namespaces groups together entities like classes, objects and functions under a
name. Namespaces provide a way to avoid name collisions of variables, types, classes or
functions. Namespaces reduces the use of nested class thus reduces the inconvenience of
handling nested class.
Question - What is the default access level?
Answer - The access privileges in C++ are private, public and protected. The default
access level assigned to members of a class is private. Private members of a class are
accessible only within the class and by friends of the class. Protected members are
accessible by the class itself and its sub- classes. Public members of a class can be
accessed by anyone.

Question - Explain friend class in C++.


Answer - When a class declares another class as its friend, it is giving complete access to
all its data and methods including private and protected data and methods to the friend
class member methods. Friendship is one way only, which means if A declares B as its
friend it does NOT mean that A can access private data of B. It only means that B can
access all data of A.

Question - What is virtual function?


Answer - Virtual function is the member function of a class that can be overriden in its
derived class. It is declared with virtual keyword. Virtual function call is resolved at run-
time (dynamic binding) whereas the non-virtual member functions are resolved at
compile time (static binding).

Question - Define default constructor.


Answer - Default constructor is the constructor with no arguments or all the arguments
has default values.

Question - Define abstraction.


Answer - The process of hiding unnecessary data and exposing essential features is called
abstraction. Abstraction is separating the logical properties from implementation details.

Question - What is overriding?


Answer - Defining a function in the derived class with same name as in the parent class is
called overriding. In C++, the base class member can be overridden by the derived class
function with the same signature as the base class function. Method overriding is used to
provide different implementations of a function so that a more specific behavior can be
realized.

Question - What is copy constructor?


Answer - A copy constructor is a special type of constructor that is used to create an
object as a copy of an existing object. It takes an argument which is a reference to the
object to be copied.

Question - Define private, protected and public access control.


Answer
Private
Private is the default access specifier for every declared data item in a class. Private data
belongs to the same class in which it is created and can only be used by the other
members of the same class.

Protected
When a data item is declared as protected it is only accessible by the derived class
member.

Public
Public allows to use the declared data item used by anyone from anywhere in the
program. Data items declared in public are open to all and can be accessed by anyone
willing to use their values and functions they provide.

Question - What are the characteristics of Object Oriented programming language?


Answer
Some key features of the Object Oriented programming are:

* Emphasis on data rather than procedure


* Programs are divided into entities known as objects
* Data Structures are designed such that they characterize objects
* Functions that operate on data of an object are tied together in data structures
* Data is hidden and cannot be accessed by external functions
* Objects communicate with each other through functions
* New data and functions can be easily added whenever necessary
* Follows bottom up design in program design

Question - What are the basic Concepts used in the Object-Oriented Programming
language?
Answer

Object
Class
Data Abstraction and Encapsulation
Polymorphism
Inheritance
Message passing
Dynamic binding

Question - What is Turbo C++?


Answer
Turbo C++ provides an environment called IDE (Integrated Development Environment).
The editor is used to create the source file, compile it, link it and then execute it.

Question - How to control the number of chars read in a string by using scanf()?
Answer
By using the a format specifier
char buf[25];
scanf("%20s", buf);

Doing this limits the length of the characters that will be read by scanf() to 20 characters
maximum although the buffer can hold 25 characters.

Question - How to detect an end of a file?


Answer
You can either use the ifstream object ‘fin’ which returns 0 on an end of file or you can
use eof() which is a member function of the ios class. It returns a non zero value on
reaching the end of file.

Describe the virtual function and virtual function table.


A virtual function in C++ is

- A simple member function of a class which is declared with “virtual” keyword


- It usually performs different functionality in its derived classes.
- The resolving of the function call is done at run-time.

Virtual Table:

A virtual table is a mechanism to perform dynamic polymorphism i.e., run time binging.
Virtual table is used to resolve the function calls at runtime. Every class that uses virtual
functions is provided with its own virtual functions.

Every entry in the virtual table is a pointer that points to the derived function that is
accessible by that class. A hidden pointer is added by a compiler to the base class which
in turn calls *_vptr which is automatically set when an instance of the class is created and
it points to the virtual table for that class.

What are recursive functions? What are the advantages and disadvantages of
Recursive algorithms?
A recursive function is a function which calls itself.

The advantages of recursive functions are:

-Avoidance of unnecessary calling of functions.


-A substitute for iteration where the iterative solution is very complex. For example to
reduce the code size for Tower of Honai application, a recursive function is bet suited.
- Extremely useful when applying the same solution

The disadvantages of Recursive functions:

-A recursive function is often confusing.


-The exit point must be explicitly coded.
-It is difficult to trace the logic of the function.

What is the difference between an external iterator and an internal iterator?


Describe an advantage of an external iterator.
An internal iterator is implemented by the member functions of the class which has the
iteration logic.
An external iterator is implemented by a separate class which can be attached to the
object which has iteration logic.
The advantage of external iterator is that, many iterators can be made active
simultaneously on the existing or same object.

Explain passing objects by reference, passing objects by value and passing objects
by pointer.

Pass by value:

The callee function receives a set of values that are to be received by the parameters. All
these copies of values have local scope, i.e., they can be accessed only by the callee
function. The simplicity and guarantee of unchanging of values passed are the advantages
of pass by value.

Pass by reference:

The callee function receives a set of references which are aliases to variables. If a change
is made to the reference variable, the original value (passed by the caller function) will
also be changed. All the references are handled by the pointers. Multiple values
modification can be done by passing multiple variables.

Pass by pointer:

The callee function receives a pointer to the variable. The value of the pointer in the
caller function can then be modified. The advantages of this process are that the changes
are passed back to the caller function and multiple variables can be changed.

Explain the concepts of Throwing and catching exceptions with an example using
C++.
C++ provides a mechanism to handle exceptions which occurs at runtime. C++ uses the
keywords – throw, catch and try to handle exception mechanism. An entity called an
exception class need to be created.
The application should have a separate section called catch block. The code in which an
exception is predicted is authored in the try block.

The following code illustrates to handle the exception.

#include <iostream>
class Excep {
public:
const char* error;
Excep(const char* arg) : error(arg) { }
};

class Test {
public:
int i;
// A function try block with a member initializer
Test() try : i(0) {
throw Excep("Exception thrown in A()");
}
catch (Excep& e) {
cout << e.error << endl;
}
};

// A function try block


void f() try {
throw E("Exception thrown in f()");
}
catch (Excep& e) {
cout << e.error << endl;
}
void g() {
throw Excep("Exception thrown in g()");
}

int main() {
f();
// A try block
try {
g();
}
catch (Excep& e) {
cout << e.error << endl;
}
try {
Test x;
}
catch(...) { }
}

The following is the output of the above example:


Exception thrown in f()
Exception thrown in g()
Exception thrown in A()
What is an abstraction and why is it important?
Abstraction is to specify what to do (the necessary details) but not how to do (all details).
For example, monitor information like size, screen type, power switches are specified for
abstraction. But the inner parts and the functionality need not be known. In other words,
making the details available for the user does matters.

For example, Employee class has a method calculateSalary() which receives the ‘empid’
as parameter and returns the salary of the employee concerned. The user who invokes the
calculateSalary() method does not concerned about how the salary calculated. So,
abstraction specifies to expose only the details which are concerned with the user.

Abstraction is important because, the code reusability is one of the benefits to the user.
There is flexibility to implement the business logic for various objects which accesses the
method. In the above example, salaries of employees of various cadres can use the neither
same method with minor nor changes.

What are the advantages of “throw.....catch” in C++?


The following are the advantages of throw and catch in c++.

- Code isolation: All code that is to be handled when an exception raises is placed in the
catch block, which is not part of the original code.
- The logic is pertaining to the type of the error, so that the developer can concentrate
only on that exception.
- Developer can be selective about trapping various exceptions.

What is the difference between an inspector and a mutator?


An object’s state is returned without modifying the object’s abstract state by using a
function called inspector. Invoking an inspector does not cause any noticeable change in
the object’s behavior of any of the functions of that object.

A mutator, on the other hand, changes the state of an object which is noticeable by
outsiders. It means, it changes the abstract state of the object.

The following code snippet depicts the usage of these two functions:
class ShoppingCart {
public:
int addItem(); //Mutator
int numItems() const; //Inspector
};

The function addItems() is a mutator. The reason is that it changes the ‘ShoppingCart’ by
adding an item.

The function numItems() is an inspector. The reason is that it just updates the count of
number of items in the ‘ShoppingCart’. The const declaration followed by int numItems()
specifies that numItems() never change the ‘ShoppingCart’ object.

Define Friend classes. What are advantages of using friend classes?


The friend function is a ‘non member function’ of a class. It can access non public
members of the class. A friend function is external to the class definition.

A friend function has the following advantages:

- Provides additional functionality which is kept outside the class.


- Provides functions that need data which is not normally used by the class.
- Allows sharing private class information by a non member function.

What are zombie objects in C++. Explain its purpose.


An object creation of a class is failed before executing its constructor. As a constructor
would not return a value, there is no confirmation that whether the constructor is
executed completely or not.

An object that is freed does not crash the applications. The application debugging is
allowed by making the all states of the object intact.
When are memory leaks important? What are the easiest ways to avoid memory leak?

Memory Leaks:
Memory leaks take up more memory space and causes slow calculation.
Ensure that the loops or conditional statements are executed completely to prevent
memory leak occurrence.

The best and easiest way to avoid memory leaks is to make changes at the time of writing
programs. The cause for this is, a properly planned program can avoid memory leaks. In
addition to it, make a code snippet can be made simple, small which executes fast.

Explain the difference between Stack and Queue.


Stack is a Last in First out (LIFO) data structure whereas Queue is a First in First out
(FIFO) data structure.

What is Stacks? Explain its uses.


Stack is a Last in First out (LIFO) data structure. It is a linear structure and one can insert
and delete from one end only which is called the top. It is very effective in rectifying
syntax errors like missing parentheses etc.

What is pure virtual function?


A pure virtual function is one which is initialized with 0 at the time of declaration.

What is a Wrapper class?


Wrapper classes are classes that allow primitive types to be accessed as objects.

What do you mean by Stack unwinding?


When an exception is thrown, C++ calls destructors to destroy all the objects formed
since the beginning of the try block. The objects are destroyed in the reverse order of
their formation. This process is called Stack unwinding.

What is the use of ‘using’ declaration?


In using-declaration, we have using keyword followed by a fully qualified name of a
namespace member. This allows using the non-qualified name of that member in the
scope of the using-declaration.

What is the difference between Mutex and Binary semaphore?


Semaphore synchronizes processes where as mutex synchronizes threads running in the
same process.

What is the scope resolution operator?


Scope resolution operator allows a program to reference an identifier in the global scope
that is hidden by another identifier with the same name in the local scope.

What is abstraction?
It is the process of hiding unwanted details from the user.

What are the advantages of inheritance?


Code reusability
Saves time in program development.

What is a conversion constructor?


It is a constructor that accepts one argument of a different type.

What is a default constructor?


A constructor that has no argument is a default constructor.
Explain the advantages if inline functions.
It relieves the burden involved in calling a function. It is used for functions that need fast
execution.

Explain the different access specifiers for the class member in C++.
private: It is default one and can be access from class member of the same class.
protected: The protected members can be access from member functions of the same
class or friend classes or from the members of their immediate derived class.
public: The public members can be access from anywhere the class is visible.

What is the importance of mutable keyword?


The mutable keyword allows the data member of a class to change within a const
member function.

Pointer vs. reference.


A reference must be initialized at the time of declaration whereas not needed in case of
pointer. A pointer can point to a null object.

A reference once initialize to refer to the object can't be reassigned to refer to other
whereas reassignment is possible with pointer.

Explain how to call C functions from C++.


The extern keyword is used to call C functions from C++

extern "C" void showme()

What is static member?


A static member is created and initialize once and shared among all class objects.
Explain static member functions.

The static member functions are created to access static variables of a class.

How do we declare a class as a friend of another class?

class a
{
friend class b; // with this declaration, class b can access all members of a
};

Explain the private, protected and public inheritance.


private inheritance: all the public and protected members in base become private.
protected inheritance: all the public and protected members in base class become
protected.
public inheritance: in case of public inheritance, public remains public and protected
remains protected.

Class vs. Structure

Class

Class has private as default access specifier.


Default inheritance type is private.
Structure

Structure has public as default access specifier


Default inheritance type is public.

Explain with an example how to call a base member function from derived class
member function.
class emp
{
public:
void empName()
{
cout << "emp-XYZ" << endl;
}
};

class dept : public emp


{
public:
void empName()
{
cout << "dept-XYZ" << endl;
emp::empName();
}
};

Output

dept-XYZ
emp-XYZ

Is it possible to overload a constructor?


Yes it is possible. A constructor can be overloaded to pass different arguments to the
object.

Overriding vs. overloading


Overloading means having methods with same name but different signature
Overriding means rewriting the virtual method of the base class

What do you mean by binding? Static vs. dynamic binding


Binding means connecting the function call to a function implementation.

Connecting the function call to the function implementation before execution is static
binding whereas connection them at the run-time is dynamic binding.
Define abstract class.
A class whose object can't be created is abstract class. You can create a class as abstract
by declaring one or more virtual functions to pure.

Pure virtual functions are declared as below

virtual void show() = 0 ;

Vector vs. Map


In a vector, all the elements are in a sequence whereas Map stores element in the form of
a key-value association pair.

Define namespaces
Namespaces is used to group together all the related classes.

To use namespace, using keyword or scope resolution operator can be used.

namespace emp
{
class Manager
{
public:
static void showDetail()
{
...
...
}
};
}

To use

emp::Manage::showDetail();

or

using namespace emp;


Manager::showDetail();

What is class?
Answer - A class is a user defined data type. It serves as a template of the objects.....

Explain constructors and destructors.


Answer - Constructors are the member functions of the class that executes automatically
whenever an object.......
Define access privileges in C++.
Answer - You have access privileges in C++ such as public, protected and private that
helps.....

Explain the difference between structures and classes


Answer - Syntactically and functionally, both structures and classes are....

Explain container class and its types in C++.


Answer - A container stores many entities and provide sequential or direct access to
them. List, vector and strings are......

Define storage classes in C++


Answer - Storage class defined for a variable determines the accessibility and longevity
of the variable. The accessibility.......

Define an Iterator class


Answer - A container class hold group of objects and iterator class is used to traverse
through the objects maintained..........

What are pure virtual functions?


Answer - The base class with pure virtual function can't be instantiated since there is no
definition...........

You might also like