Sunday 26 February 2012

What are the differences between public, private, protected, static, transient, final and volatile? | PHP

Public: Public declared items can be accessed everywhere.
Protected: Protected limits access to inherited and parent classes (and to the class that defines the item).
Private: Private limits visibility only to the class that defines the item.
Static: A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.
Final: Final keyword prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
transient: A transient variable is a variable that may not be serialized.
volatile: a variable that might be concurrently modified by multiple threads should be declared volatile. Variables declared to be volatile will not be optimized by the compiler because their value can change at any time.

What is the use of friend function? | PHP


   Sometimes a function is best shared among a number of different classes. Such functions can be declared either as member functions of one class or as global functions. In either case they can be set to be friends of other classes, by using a friend specifier in the class that is admitting them. Such functions can use all attributes of the class which names them as a friend, as if they were themselves members of that class.
  A friend declaration is essentially a prototype for a member function, but instead of requiring an implementation with the name of that class attached by the double colon syntax, a global function or member function of another class provides the match.
 

What are the differences between procedure-oriented languages and object-oriented languages? | PHP

There are lot of difference between procedure language and object oriented like below

  1. Procedure language easy for new developer but complex to understand whole software as compare to object oriented model.
  2. In Procedure language it is difficult to use design pattern mvc , Singleton pattern etc but in OOP you we able to develop design pattern.
  3. IN OOP language we able to ree use code like Inheritance ,polymorphism etc but this type of thing not available in procedure language on that our Fonda use COPY and PASTE .

What are the features and advantages of object-oriented programming? | PHP

One of the main advantages of OO programming is its ease of modification; objects can easily be modified and added to a system there by reducing maintenance costs. OO programming is also considered to be better at modeling the real world than is procedural programming. It
allows for more complicated and flexible interactions. OO systems are also easier for non-technical personnel to understand and easier for them to participate in the maintenance and enhancement of a system because it appeals to natural human cognition patterns.
For some systems, an OO approach can speed development time since many objects are standard across systems and can be reused. Components that manage dates, shipping, shopping carts, etc. can be purchased and easily modified for a specific system

How can we encrypt the username and password using PHP? | PHP

The functions in this section perform encryption and decryption, and compression and uncompression:
encryption decryption
AES_ENCRYT() AES_DECRYPT()
ENCODE() DECODE()
DES_ENCRYPT() DES_DECRYPT()
ENCRYPT() Not available
MD5() Not available
OLD_PASSWORD() Not available
PASSWORD() Not available
SHA() or SHA1() Not available
Not available UNCOMPRESSED_LENGTH()

How can we encrypt and decrypt a data present in a MySQL table using MySQL? | PHP

AES_ENCRYPT () and AES_DECRYPT ()

What are the reasons for selecting lamp (Linux, apache, MySQL, PHP) instead of combination of other software programs, servers and operating systems? | PHP

All of those are open source resource. Security of Linux is veryv ery more than windows. Apache is a better server that IIS both in functionality and security. MySQL is world most popular open source database. PHP is more faster that asp or any other scripting language.

How we get IP address of client, previous reference page etc ? | PHP

By using $_SERVER['REMOTE_ADDR'],$_SERVER['HTTP_REFERER'] etc.

What is htaccess? Why do we use this and Where? | PHP

.htaccess files are configuration files of Apache Server which provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.
 

What is meant by nl2br()? | PHP


Inserts HTML line breaks (<BR />) before all newlines in a string.
 

Shopping cart online validation i.e. how can we configure Paypal, etc.? | PHP


Nothing more we have to do only redirect to the payPal url after submit all information needed by paypal like amount,adresss etc.
 

Shopping cart online validation i.e. how can we configure Paypal, etc.?

Nothing more we have to do only redirect to the payPal url after submit all information needed by paypal like amount,adresss etc.
 

Saturday 25 February 2012

Suppose your Zend engine supports the mode <? ?> Then how can u configure your PHP Zend engine to support <?PHP ?> mode ?


In php.ini file:
set
short_open_tag=on
to make PHP support

How can I execute a PHP script using command line? |PHP


Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line argument.
 

What is use of header() function in php ? |PHP


The header() function sends a raw HTTP header to a client.We can use herder() function for redirection of pages. It is important to notice that header() must be called before any actual output is seen..
 

What are the different tables(Engine) present in MySQL, which one is default? |PHP What are the different tables(Engine) present in MySQL, which one is default?


 Following tables (Storage Engine) we can create
1. MyISAM(The default storage engine IN MYSQL Each MyISAM table is stored on disk in three files. The files have names that begin with the table name and have an extension to indicate the file type. An .frm file stores the table format. The data file has an .MYD (MYData) extension. The index file has an .MYI (MYIndex) extension. )
2. InnoDB(InnoDB is a transaction-safe (ACID compliant) storage engine for MySQL that has commit, rollback, and crash-recovery capabilities to protect user data.)
3. Merge
4. Heap (MEMORY)(The MEMORY storage engine creates tables with contents that are stored in memory. Formerly, these were known as HEAP tables. MEMORY is the preferred term, although HEAP remains supported for backward compatibility. )
5. BDB (BerkeleyDB)(Sleepycat Software has provided MySQL with the Berkeley DB transactional storage engine. This storage engine typically is called BDB for short. BDB tables may have a greater chance of surviving crashes and are also capable of COMMIT and ROLLBACK operations on transactions)
6. EXAMPLE
7. FEDERATED (It is a storage engine that accesses data in tables of remote databases rather than in local tables. )
8. ARCHIVE (The ARCHIVE storage engine is used for storing large amounts of data without indexes in a very small footprint. )
9. CSV (The CSV storage engine stores data in text files using comma-separated values format.)
10. BLACKHOLE (The BLACKHOLE storage engine acts as a "black hole" that accepts data but throws it away and does not store it. Retrievals always return an empty result)
 

Can we use include ("xyz.PHP") two times in a PHP page "index.PHP"? | PHP

 Yes we can use include("xyz.php") more than one time in any page. but it create a prob when xyz.php file contain some funtions declaration then error will come for already declared function in this file else not a prob like if you want to show same content two time in page then must incude it two time not a problem.
 

What are the differences between require and include? | PHP

Both include and require used to include a file but when included file not found include end Warning where as Require send Fatal Error .

How can we create a database using PHP and MySQL?


We can create MySQL database with the use of
mysql_create_db("Database Name")
 

How can we extract string "pcds.co.in " from a string "http://info@pcds.co.in using regular expression of PHP?


preg_match("/^http:\/\/.+@(.+)$/","http://info@pcds.co.in",$matches);
echo $matches[1];
 

What are the differences between Get and post methods? | PHP


There are some defference between GET and POST method
1. GET Method have some limit like only 2Kb data able to send for request.But in POST method unlimited data can we send
2. when we use GET method requested data show in url but Not in POST method so POST method is good for send sensetive request

What is the difference between mysql_fetch_object and mysql_fetch_array? | PHP


mysql_fetch_object() is similar tomysql_fetch_array(), with one difference -an object is returned, instead of an array. Indirectly, that means that you can only access the data by the field names, and not by their offsets (numbers are illegal property names).

In how many ways we can retrieve the data in the result set of MySQL using PHP? | PHP


We can do it by 4 Ways
1. mysql_fetch_row. , 2. mysql_fetch_array , 3. mysql_fetch_object
4. mysql_fetch_assoc

What is the difference between $name and $$name? | PHP

$name is variable where as $$name is reference variable like $name=sonia and $$name=singh so $sonia value is singh.
 

Who is the father of PHP ? PHP


Rasmus Lerdorf is known as the father of PHP.
 

PHP Interview Questions and Answers for Freshers and Experienced


1. Who is the father of PHP ?
2. What is the difference between $name and $$name?
3. How can we submit a form without a submit button?
4. In how many ways we can retrieve the data in the result set of MySQL using PHP?
5. What is the difference between mysql_fetch_object and mysql_fetch_array?
6. What are the differences between Get and post methods.
7. How can we extract string "pcds.co.in " from a string "http://info@pcds.co.in using regular expression of PHP?
8. How can we create a database using PHP and MySQL?
9. What are the differences between require and include? 
10. Can we use include ("xyz.PHP") two times in a PHP page "index.PHP"?
11.  What are the different tables(Engine) present in MySQL, which one is default?  
12. What is use of header() function in php ?
13. How can I execute a PHP script using command line?
14. Suppose your Zend engine supports the mode <? ?> Then how can u
15. Shopping cart online validation i.e. how can we configure Paypal, etc.?
16. What is meant by nl2br()?
17. What is htaccess? Why do we use this and Where?
18. How we get IP address of client, previous reference page etc ?
19. What are the reasons for selecting lamp (Linux, apache, MySQL, PHP) instead of combination of other software programs, servers and operating systems?
20. How can we encrypt and decrypt a data present in a MySQL table using MySQL?
21. How can we encrypt the username and password using PHP?
22. What are the features and advantages of object-oriented programming?
23. What are the differences between procedure-oriented languages and object-oriented languages?
24. What is the use of friend function?
25. What are the differences between public, private, protected, static, transient, final and volatile?
26. What are the different types of errors in PHP?
27. What is the functionality of the function strstr and stristr?
28. What are the differences between PHP 3 and PHP 4 and PHP 5?
29. What does is NaN function do?
30. What is the functionality of the function htmlentities?
31. How can we get second of the current time using date function?
32. How can we convert the time zones using PHP?
33. What is meant by urlencode and urldocode?
34. What is the difference between the functions unlink and unset?
35. How can we register the variables into a session?
36. How can we get the properties (size, type, width, height) of an image using PHP image functions?
37. How can we get the browser properties using PHP?
38. What is the maximum size of a file that can be uploaded using PHP and how can we change this?
39. How can we increase the execution time of a PHP script?
40. How can we take a backup of a MySQL table and how can we restore it. ?
41. How can we optimize or increase the speed of a MySQL select query?
42. How many ways can we get the value of current session id?
43. How can we destroy the session, how can we unset the variable of a session?
44. How can we set and destroy the cookie n php?
45. How many ways we can pass the variable through the navigation between the pages?
46. What is the difference between ereg_replace() and eregi_replace()?
47. What are the different functions in sorting an array?
48. How can we know the count/number of elements of an array?
49. what is session_set_save_handler in PHP?
50. How can I know that a variable is a number or not using a JavaScript?
51. List out some tools through which we can draw E-R diagrams for mysql.
52. How can I retrieve values from one database server and store them in other database server using PHP?
53. List out the predefined classes in PHP?
54. How can I make a script that can be bi-language (supports English, German)?
55. What are the difference between abstract class and interface?
56. How can we send mail using JavaScript?
57. How can we repair a MySQL table?
58. What are the advantages of stored procedures, triggers, indexes?
59. What is the maximum length of a table name, database name, and fieldname in MySQL?
60. How many values can the SET function of MySQL take?
61. What are the other commands to know the structure of table using MySQL commands except explain command? 
62. How many tables will create when we create table, what are they?
63. What is the purpose of the following files having extensions 1) .frm 2) .myd 3) .myi? What do these files contain?
64. What is maximum size of a database in MySQL?
65. Give the syntax of Grant and Revoke commands?
66. Explain Normalization concept?
67. How can we find the number of rows in a table using MySQL?
68. How can we find the number of rows in a result set using PHP?
69. How many ways we can we find the current date using MySQL?
70. What are the advantages and disadvantages of Cascading Style Sheets?
71. What type of inheritance that PHP supports?
72. What is the difference between Primary Key and Unique key?
73. what is garbage collection? default time ? refresh time?
74. What are the advantages/disadvantages of MySQL and PHP?
75. What is the difference between GROUP BY and ORDER BY in Sql?
76. What is the difference between char and varchar data types?
77. What is the functionality of md5 function in PHP?
78. How can I load data from a text file into a table?
79. How can we know the number of days between two given dates using MySQL?
80. How can we know the number of days between two given dates using PHP?
81. How we load all classes that placed in different directory in one PHP File , means how to do auto load classes
82. How many types of Inheritances used in PHP and how we achieve it
83. PHP how to know user has read the email?
84. What are default session time and path?
85. how to track user logged out or not? when user is idle ?
86. how to track no of user logged in ?
87. In PHP for pdf which library used?
88. for image work which library?
89. what is design pattern? singleton pattern?
90. what are magic methods?
91. what is magic quotes?
92. what is cross site scripting? SQL injection?
93. what is URL rewriting?
94. what is the major php security hole? how to avoid?
95. whether PHP supports Microsoft SQL server ?
96. what is MVC? why its been used?
97. what is framework? how it works? what is advantage?
98. what is CURL?
99. what is PDO ?
100. What is PHP's mysqli Extension?
101. What is the difference between the functions unlink and unset?
102. How can we register the variables into a session?
103. How can we destroy the session, how can we unset the variable of a session?
104. What is the difference between echo and print statement?
105. How can you enable error using PHP Code?
106. how we know browser name?
107. What function is used to convert an array into a string?
108. How can we encrypt and decrypt a data present in a mysql table using mysql?
109. What are the different database storage engines present in MySQL?
110. How can you get no of records from a table?

Friday 24 February 2012

Should the this pointer can be used in the constructor? C++

 We can use the this pointer in the constructor in the initialization list and also in the body. However, there is a feeling that the object is not fully formed so we should not use this pointer. Let's understand the use of the this pointer with the help of the following example. The declared data members of a base class and/or the declared data members of the constructor's own class can be accessed by the constructor {body} and/or a function called from the constructor. This is possible because of the full construction of constructor's body at the time of execution. The preceding example always works.

The override in the derived class is not possible for a function called from the constructor and the {body} of a constructor which is independent of calling the virtual member function by the explicit use of the this pointer.

Please make sure that the initialization of other data member has already been done before passing the data member to another data member's initializer in this object. With the help of some straightforward language rules (independent of the specific compiler that you are using), you can confirm about the determination of initialization of data members.

Without the prior knowledge of these rules, there is no use of passing any data member from the this object.

Can we use initialization lists or assignments in a constructor? | C++

The constructor uses initialization lists. In fact, all the member objects in the initialization list should be initialized by a constructor. Let's take an example of One exception.
Consider the following constructor that initializes member object r__ using an initialization list: Harry::Harry() : "-(whatever) { }. This results in the improved performance. For example, if a member variable r_ is of the same type as the whatever expression, then the compiler do not have to make a separate copy of the object and the outcome related to the whatever expression is constructed directly inside r_.
Even if the types are not same, the compiler is able to initialize objects with initialization lists, even if the types are different than assignments.
Assignment is another way of building constructors, such as, Jimmy::Jimmy() { j_ = whatever; }. In this case, the temporary and separate objects are created as a result of the expression whatever and the temporary object is passed into the jL object's assignment operator.
By the use of an assignment operator in a constructor, the default constructor has fully constructed the member object and results in the allocation of some amount of memory by default or the opening of some default file. The major drawback of using the assignment operator is that all the work done may be lost if the whatever expression or the object of the assignment operator closes the particular file or release that memory. For example, if a wrong file is opened.

On creating an array of bird_class objects, which constructor will get called? | C++

The default constructor will get called for the bird_class.

Example:

If a normal constructor Is provided, the compiler will not create a default constructor. A constructor can be simply created with no arguments, as described in the preceding example.
No, not always. A default constructor is a constructor which takes no arguments, and it exists so that you can create objects of your new class. An example of this is a constructor that takes no parameters:
Class bred
{
public:
Bred(); //default constructor: can be called with no args
};
Another example of a default constructor is one that can take arguments, provided they are given default values: Example:
class Bred{
public
Bred(int i=3, int j=5); //Default constructor: can be called with args
};

Can one constructor of a class be called by another constructor of the same class for initializing the this object? | C++

a No, not possible. Let's understand it with the help of an example.
Take a human class. Now, we want our constructor Human::Human(char) to call another constructor of the same class, say Human: :Human(char,  int), in order that

Human::Human(char, int) would help to initialize the this object. Unfortunately, there is no way to do this in C++.
It always occur in its specific form and not the way a user wants. For example, the line Human(h, 0); does not call Human::Human(char, int) on the this object. Instead it calls Human::Human(char, int) to initialize a temporary, local object (not this), then it immediately destructs that temporary object when control flows over the ;.
Example:

If there is no default parameter for combining the two constructors, then there is a need of sharing of their common code in a private init() member function:
Another example:

How does List r; differs from List r();? | C++

Suppose that Member is the name of some class. Then, function f() declares a local member object called r:
Example:
void f()
{
list r; //local object named r (of class member)
}
//But function g()declares a function called r()that returns a member:
void g(){
member r(); //Function named r(that returns a member)
}
In the preceding code snippet, r is declared as a local variable and the other r() is declared as a function. That is the main difference.

On implementing a destructor of the derived class, is there any need for the explicit calling of the destructor for my base class?

 No, there is no need to call the destructor explicitly except in the case of placement hew. A destructor of a class, whether you explicitly define one or not, invokes a destructor automatically for base class sub objects. Base classes are destructed after member objects. In the multiple inheritance, the destruction of the direct base classes occur in the reverse order of their appearance in the inheritance list.
Example:

On implementing a destructor, is there any need for the explicit calling of the destructors for member objects? | C++

No, there is no need to call the destructor explicitly. A destructor of a class, whether you explicitly define one or not, invokes a destructor automatically for its member objects. Their destruction is done in the reverse order of their declaration in the class.
Example:
PROGRAM

Thursday 23 February 2012

How is the MVC design pattern used in Struts framework? | Java Structs

In the MVC design pattern, application flow is mediated by a central Controller. The Controller delegates requests to an appropriate handler. The handlers are tied to a Model, and each handler acts as an adapter between the request and the Model. The Model represents, or encapsulates, an application's business logic or state. Control is usually then forwarded back through the Controller to the appropriate View. The forwarding can be determined by consulting a set of mappings, usually loaded from a database or configuration file. This provides a loose coupling between the View and Model, which can make an application significantly easier to create and maintain.
Controller--Servlet controller which supplied by Struts itself; View --- what you can see on the screen, a JSP page and presentation components; Model --- System state and a business logic JavaBeans.

What is Struts? | Structs in java


Struts is a web page development framework and an open source software that helps developers build web applications quickly and easily. Struts combines Java Servlets, Java Server Pages, custom tags, and message resources into a unified framework. It is a cooperative, synergistic platform, suitable for development teams, independent developers, and everyone between.

Struts Interview Questions and Answers for Freshers & Experienced pdf

 1. What is Struts?
2.  How is the MVC design pattern used in Struts framework?
3.  Who makes the Struts?
4.  Why it called Struts?
5.  Do we need to pay the Struts if being used in commercial purpose?
6.  Describe Jakarta Struts Framework?
7.  What are the components of Struts?
8.  Describe is ActionServlet?
9.  What is an Action Class used for?
10.  What is ActionForm?
11.   What are the core classes of Struts?
12.  What is the design role played by Struts?
13.  How Struts control data flow?
14.  What configuration files are used in Struts?
15.  What helpers in the form of JSP pages are provided in Struts framework?
16.  What is Struts Validator Framework?
17.  What are the core classes of the Struts Framework?
18.  What are Tag Libraries provided with Struts?
19.  What are difference between ActionErrors and ActionMessage?
20.  How you will handle exceptions in Struts?
21.  Is Struts efficient?
22.  How you will enable front-end validation based on the xml in validation.xml?
23.  What is ActionServlet?
24.  How you will make available any Message Resources Definitions file to the Struts Framework Environment?
25.  What is Action Class?
26.  Can I setup Apache Struts to use multiple configuration files?
27.  How you will make available any Message Resources Definitions file to the Struts Framework Environment?
28.  What is Struts Flow?
29.  What is LookupDispatchAction?
30.  Write code of any Action Class?
31.  What is ActionForm?
32.  What is Struts Validator Framework?
33.  Give the Details of XML files used in Validator Framework?
34.  Why do we need Struts?
35.  How does Struts work?
36.  Is Struts compatible with other Java technologies?
37.  Who wrote Struts?
38.  Why is it called Struts?
39.  Do I have to credit Struts on my own website?
40.  Where can I get a copy of Struts?
41.  How do I install Struts?
42.  When do I need "struts.jar" on my classpath?
43.  Does Struts include its own unit tests?
44.  If the framework doesn't do what I want, can I request that a feature be added?
45.  Where can I get help with Struts?
46.  What's the difference between Struts and Turbine? What's the difference between Struts and Espresso?
47.  Why aren't the Struts tags maintained as part of the Jakarta Taglibs project ?
48.  Are the Struts tags XHTML compliant ?

Wednesday 22 February 2012

Explain the use of the placement new method? | Constructor in C++

The placement new method is used to place an object at a particular location in memory. For placing an object to its particular location, it allocates a pointer parameter to a new part of a new expression.
 PROGRAM::
Line #1 creates an array of size of (Tom) bytes of memory, which is big enough to hold a Tom object. Line #2 creates a pointer place and points to the first byte of this memory. Line #3 essentially calls the constructor Tom::Tom (). The this pointer in the Tom constructor will be equal to place. The returned pointer j will therefore be equal to place.
 The sole responsibility of a programmer is to destruct the placed object. To do this, the explicit calling of the destructor is needed:
 void mno()
{
char memory[sizeof(tom)];
void* b = memory;
Bred* j = new(b) Tom();
j>~Tom();
e placed object
}
In the rare scenario like the preceding one, you need to explicitly call the destructor.

Can a destructor be explicit y called if an object is created using the new operator? | Constructor in C++

No, a destructor cannot be explicitly called if an object is created using the new operator. When you allocate the memory with the new operator, you are responsible for managing it yourself. To avoid cluttering up your program with memory that you are no longer using, you should delete it with the delete operator. The new operator returns a pointer to the newly allocated memory. Therefore, deleting the object is more prefe able than to call the destructor explicitly. For example, if you allocate the object via a typical new expression, such as:
Jerry* j = new ZJerryQ;
Then, the destructor Jerry: :~Jerry () will automatically get called on deleting it via:
delete j; //Automatically calls j->~3erry()
An explicit calling of the destructor would not release the memory that has been allocated for the Jerry object itself. The following are the two advantages of using the delete operator:
? It calls the destructor
? It deal locates the memory

What do you understand by zombie objects in C++? | Constructor in C++

In a situation, where an object is created in a class, a constructor fails before its full execution. It is very difficult to ensure about the execution of the constructor whether the constructor would return a value or not. Objects that are no more required for an application are called zombie objects. These zombie objects occupy a space in the memory and wait for their termination.

What are shallow and deep copies? | Constructor in C++

A shallow copy is used to copy actual values of the data. It means, if the pointer points to dynamically allocated memory, the new object's pointer in the copy still points to items inside the old objects and the returned object will be left pointing to items that are no longer in scope. A copy of the dynamically allocated objects is created with the help of a deep copy. This is done with the help of an assignment operator, which needs to be overloaded by the copy constructor.

What happens if the local variable cannot be wrapped in an artificial block? | Constructor in C++


Most of the times, the time duration of wrapping local variables is limited in an artificial block ({...}). In other situations, a member function with similar effects can be added as a destructor instead of calling the destructor itself.For example, in the case of class of Filel, a close() method can be added. Usually, the close() method can be simply called by the destructor. Note that there is a need of marking Filel by the close method(). It avoids the reclosing of an already closed file by a subsequent call of a destructor. For example, the value of the filelHandle can be set by the call of a destructor, such as -1 and the checking can be done at the beginning for ensuring that the filelHandle is already equal to value (-1):
PROGRAM::
Note that the other file methods may also need to check if the filelHandle_ is -1 (i.e., check if the Filel is closed).
Also note that any constructors that do not actually open a file should set filelHandle_ to -1.

Explain how would you handle a situation where you cannot call the destructor of a local explicitly? | Constructor in C++

To avoid the explicit calling of a destructor, the time extent of the local variable can be wrapped in an artificial block {...}:
void time()
{
{
copy c;
// ..insert code that should execute when c is still open..
}

What happens if a local variable is destroyed within its scope before a program is completely executed? Can a destructor be called if needed? | Constructor in C++

No, the destructor cannot be called if needed. Let's understand the preceding statement with the help of an example. There is a local variable n of a class Notebook. Now, the destruction of the local variable n occurs before the execution of a program:
Class Notebook
{
int n;
void bcd()
Notebook n;
// ..Insert code that should execute when n is still in it's scope..
we want the side-effect of n's destructor here!
// ..Insert code that should execute after n is destroyed..
}
On calling a destructor explicitly, an undefined behavior is occurred, because the destructor may invoke twice for the local variable. Firstly, on explicit calling and secondly automatic calling when the local variable destroys. Therefore, a destructor cannot be called explicitly if needed.

Can you explicitly call a destructor on a local variable? | Constructor in C++

No, the destructor is called when the block closes in which the local variable was created.
If a destructor is called explicitly twice on the same object, the result ends in an undefined behavior because the local variable gets destroyed when the scope ends.

Is it possible to overload the destructor of a class? | Constructor in C++

 No, the destructor of a class cannot be overloaded. We can have only one destructor for a class, called class name ::~ destructor name(). For example, for a class named fruit, the syntax is - fruit::~ fruit(). It never takes any parameters and it never returns anything.
We cannot pass parameters to the destructor anyway, since you never explicitly call a destructor.

Write about the sequence in which objects in an array are destructor?

The destructor is executed in the reverse order of constructor. Firstly, the objects are constructed, methods are invoked on them, and then they are destructed.
In the following example, 
the order for destructors will be b[19], b[18],b[17],b[16],b[15],b[14],b[13],b[12], b[ll], b[10]:
void def()
{
Bat b[20];
}

What is the sequence of destruction of local objects? | Constructor in C++

The destructor is executed in the reverse order of a constructor. Firstly, the objects are constructed, methods are invoked on them, and then they are destructed. In the following example, a's destructor will be executed after b's destructor.
void abc()
{
cat a;
cat b;
}

What is a virtual destructor? | Constructor in C++

 Virtual destructors help in destroying objects without knowing their type. With the help of the virtual function mechanism, the appropriate destructor for the object is invoked. In the case of abstract classes, destructors can also be declared as pure virtual functions. For example, class A derives from class B. Then, on calling the derived class dynamically at the execution time, the destructor will first call the derived class that is class A, and then the base class that is class B.
It is important to note that theVirtual keyword, when used with the destructor, ensures the calling of all the derived and base class destructors and therefore helps in the proper execution and closing of the program.

Define the process of handling in case of destructor failure? | Constructor in C++

In order to handle a failed destructor, you need to write a message to a log file; however, do not throw an exception. There is a rule in C++ that exception cannot be thrown from a destructor, which is called when the process of "stack unwinding" occurs in other exceptions. For example, if someone says throw waste files(), the stack frames between the throw waste files() and the catch (waste files) will get popped. This is known as stack unwinding. It is the process of destroying all the local objects related to those stack frames and calling destructors in case of throwing of an exception by one of those destructors. For example, if an object named Bar is thrown, then the C++ runtime system is in a neutral situation means either to avoid the Bar and end up in the catch (waste files) or ignore the function Foo and look for a catch (Bar) handler. It will call in the terminate () process to end the program.

What is a conversion operator? | Constructor in C++

 A conversion operator converts an object of a class in which you define the function to an object of a different data type. A conversion operator uses the C++ operator keyword in its declaration. C++ uses public as an access specifier, which can be used in a class as a public method for specific data type conversions.
Program:
In the preceding code snippet, operator double has no function assigned for the modification of the object/so it is better to make it constant.

Define a conversion constructor? | Constructor in C++

A conversion constructor is a single argument constructor. It is used by the compiler to convert a type of object as an argument to a class type.
Program:
The above example has the following two conversion constructors:
?Y(inti)— Helps in the conversion of integers to objects of class Y
?Y(const char* n, int j = 0) —Helps in the conversion of pointers from strings to objects of the Y class

Is it possible to pass an object of the same class in place of object reference to the copy constructor? | Constructor in C++

 Yes, when an object is created without the reference formal argument, then the copy constructor is called for the argument object. The object that is passed as a parameter to the function is then passed as a parameter to the copy constructor.
For example:
 void xyz(A);
A A1;
xyz(A1);
void xyz(A A2)
{
//definition of xyz()
}
In this case, the copy constructor is called for A2 while Al is passed as a parameter to the copy constructor.

When are copy constructors called? | Constructor in C++

A copy constructor is defined by the programmer or by compiler itself. A call to copy constructor is embedded under the following three conditions:
1. When an object is created and at the same time equated to some other existing object.
 For example:
A A1; //default constructor called.
A A2=A1; //copy constructor called.
(or)
A A2(A1) //copy constructor called.
(or)
A *Aptr= new A(A1); //copy constructor called.
2. When an object is created without having the reference of the argument, then the copy constructor is called by default for the argument object.
For example:
A A1;
xyz(A1);
void xyz(A A2)
{
//defination of xyz()
}
3. When an object is created and at the same time equated to a call to a function that returns an object.
For example:
A xyz();
{
A A1; //remaining defination of xyz();
return A1;
}
A A2=xyz();

What is a default constructor?

 A zero-argument constructor or a constructor in which all the arguments have default values is called a default Constructor.
For example:
A A1;    // default constructor called.

Explain the concept of copy constructor? | Constructor in C++

a Copy constructor is a special type of parameterized constructor. It copies one object to another. It is called when an object is created and equated to an existing object at the same time. An existing object is passed as a parameter to it.
For Example:
A A1; //default constructor called.
A A2=A1; //copy constructor called.
(or)
A A2(A1) //copy constructor called.
(or)
A *Aptr= new A(A1); //copy constructor called.

Define the process of error-handling in case of constructor failure? | Constructor in C++

If the constructor does not have the return statement, then it indicates failure in handling the error by throwing an exception.

Define two methods of constructor invocation? | Constructor in C++

The following are the two ways to invoke a constructor:
1. The compiler embeds a call to the constructor for each object that has got created. For example:
class A
{
int x;
public: void setx(const int=0);
int getx();
};
main()
{
A A1; //object declared constructor called.
A1.A(); //constructor called implicity.
}
2. The constructor is called for each object that is created dynamically in the heap by the new operator. For example:
A *Aptr
A*ptr=new A; //constructor called implicity by compiler.

How does a copy constructor differs from an overloaded assignment operator? | Constructor in C++

A copy constructor uses the value of an argument to construct a new object. We can use an overload assignment operator to assign the value of an existing object of the same class to another existing object in that class.

Can we declare a base-class destructor as virtual? | Constructor in C++

Yes, we can declare a base-class destructor as virtual that makes all derived-class destructors as virtual even if they do not have the same name as base-destructor. The problem arises when the derived class's pointer refers to a base class.
For this reason, the base class destructor should be declared as virtual so that the appropriate destructor is called on calling the delete method of the base class object.

Can we define a constructor as virtual in C++? | Constructor in C++

 No, we cannot define constructors as virtual because constructors have the same name as their classes and no two constructors of base-derived classes can have the same name. So, when we initialize an object of the base or derived class with the help of virtual constructors, the base constructor is invoked instead of the derived constructor. Therefore, it is not possible to define a constructor as virtual.

Define what is constructor? | Constructors in C++

A constructor is a method that is called when a new instance of an object of a class is created. It initializes all class members whenever you create an object of the class. It is a member function with the same name as its class. It may or may not take any parameters. It does not have any return type. For example, the compiler embeds a call to the constructor for each object when it is created. Suppose a class Z has been declared as follows:
class Z
{
public:
Z(); //Constructor for class Z
};
We cannot declare a constructor as virtual or static, nor we can declare a constructor as const, volatile, or const volatile.
The short form of constructor is ctor.

C++ Interview Questions and Answers for Freshers

 Constructors/ Destructors
1. Define what is constructor or Destructors?
2. Can we define a constructor as virtual in C++?
3. Can we declare a base-class destructor as virtual?
4. How does a copy constructor differs from an overloaded assignment operator?
5. Define two methods of constructor invocation?
6. Define the process of error-handling in case of constructor failure?
7. Explain the concept of copy constructor?
8. What is a default constructor?
9. When are copy constructors called?
10. Is it possible to pass an object of the same class in place of object reference to the copy constructor?
11. Define a conversion constructor?
12. What is a conversion operator?
13. Define the process of handling in case of destructor failure?
14. What is a virtual destructor?
15. What is the sequence of destruction of local objects?
16. Write about the sequence in which objects in an array are destructor?
17. Is it possible to overload the destructor of a class?
18. Can you explicitly call a destructor on a local variable?
19. What happens if a local variable is destroyed within its scope before a program is completely executed? Can a destructor be called if needed?
20. Explain how would you handle a situation where you cannot call the destructor of a local explicitly?
21. What happens if the local variable cannot be wrapped in an artificial block?
22. What are shallow and deep copies?
23. What do you understand by zombie objects in C++?
24. Can a destructor be explicit y called if an object is created using the new operator?
25. Explain the use of the placement new method?
26. On implementing a destructor, is there any need for the explicit calling of the destructors for member objects?
27. On implementing a destructor of the derived class, is there any need for the explicit calling of the destructor for my base class?
28. How does List r; differs from List r();?
29. Can one constructor of a class be called by another constructor of the same class for initializing the this object?
30. Is the default constructor for a class Bred always Bred::Bred()?
31. On creating an array of bird_class objects, which constructor will get called?
32. Can we use initialization lists or assignments in a constructor?
33. Should the this pointer can be used in the constructor?


 VIRTUAL FUNCTION
1. What is a virtual function?
2. Define pure virtual function.
3. What do you understand by a pure virtual member function?
4. How can virtual functions in C++ be implemented?
5. What do you understand by pure virtual function? Write about its use?
6. How the virtual functions maintain the call up?
7. Write a note on virtual destructor.


 INHERITANCE
1. Write a note about inheritance.
2. What is the need of multiple inheritance?
3. Write about the IS A and HAS A class relationships and also explain about their implementation in a class.
4. Explain the concept of template?
5. How can you say that a template is better than a base class?
6. Explain the concept of multiple inheritance (virtual inheritance). Write about its advantages and disadvantages.
7. Describe inheritance and non-inheritance of a derived class.
8. Write a note about the virtual member function.
9. How static typing is achieved by dynamic binding in C++?
10. How calling of virtual member functions differ from that of non-virtual member functions?
11. Write about the performance cost of using virtual functions in C++.
12. Write about the functional overriding of the base class by the member function in the derived class.
13. Explain the concept of dynamic binding with the help of an example.
14. When should a destructor be made virtual?
15. Should the member functions which are made public in the base class be hidden?
16. Write a note about upcasting.
17. Can a derived array be called a kind-of-base array?
18. Can an array be replaced with a template container class?
19. Can circle be called an ellipse?
20. Why do we separate interface from implementation?
21. How can you differentiate between  inheritance and implementation in C++?
22. Write about abstract base classes.
23. Write about pure virtual member function.
24.  Write about private inheritance.
26.  Write about the relationship between the private and protected inheritance.
27. Which should be more useful: the protected and public virtuals?


 POLYMORPHISM
1. Explain polymorphism with the help of an example.
2. Write  about the  issues  related  to  the  runtime  type identification.


CLASS & OBJECTS
1. What is a class?
2. How do C++ struct differs from the C++ class?
3. Write about the use of the virtual destructor?
4. Write a note on encapsulation.
5. Write a note about this pointer.
6. What are the effects after calling the delete this operator ?
7. Write a note about assignment operators.
8. Write about all the implicit member functions of a class?
9. Write about the container class and mention its types.
10. What is overriding.
11. Write about a nested class and mention its use.
12. Write about the local class and mention its use.
13. Write about the members that a derived class can add.
14. Describe the process of creation and destruction of a derived class object.
15. Define a good interface.
16. Give reason why the interface of the MyMatrix class should not look similar to array-of-array.
17. Write about the role of C++ in the tradeoff of safety vs.usability.
18. What is an object?
19.  How can you prevent accessing of the private parts of my class by other programmers (violating encapsulation)?
20. Can non-public members of another instance of the class be retrieved by the method of the same class?
21. Can encapsulation be called as a security device?
22.  How the keyword struct is different from the keyword class in C++?


MEMORY ALLOCATION/DEALLOCATION
1. How new/delete differs from malloc()/free?
2. How the delete operator differs from the delete[]operator?
3. How a new operator differs from the operator new?
4. Explain the term memory alignment.
5. Explain the concept of dynamic allocation of memory.
6. In the expression, char *c = (char *)malloc(20); followed by free(r); explain the role of mallocQ operator in allocating the number of bytes?
7. Discuss the effects occur, after an exception thrown by a member function is unspecified by an exception specification.
8. Is it possible to use the free operator with the new allocated pointers and the delete operatorQ with the
      malloc allocated pointers?
9. Can a new be used in place of old mallocQ? If yes, why?
10. Is it possible to use a new for the reallocation of pointers ?
11. Is there any need for checking of NULL after executing p = new Animal ()?
12. What is the role of copy constructor in copying of thrown objects?
13. Can a NULL be checked before deleting the p variable froi the expression, Delete p; and not if (p != NULL) ?
14. Enlist the processes involved in deleting a variable.
15. On throwing an exception by the Animal constructor in p = new AnimalQ, can memory leak occur?
16. What is the use of [] in the delete statement of the following expression:
17. What would happen on forgetting [], while deallocating an array through new?
18. Can [] be dropped on deletion of some built-in type array (char, int etc)?
19. Write about the retrieval of n number of objects during the process of delete[]p?
20. Is it possible for a member function to use delete this?


POINTERS
1. What is a dangling pointer?
2. Explain the concept of memory leak.
3. What is an auto pointer?
4. List the issue that the auto_ptr object handles.
5. What are smart pointers?
6. Write about the complexities related to the following code:
     int*b = NULL;
     int&p = *b;
7. How a pointer differs from a reference?
8. How   const   int   *ourPointer   differs   from   int   const *ourPointer?
9. Mention the conditions in which we should use references and pointers separately.


STRINGS
1. What happens on executing the following code?
   string&alphabates()
{
return "Welcome"
}
cout<<alphabates()<<endl;
2. Explain the ways in which type conversion can be done.


 OVERLOADING OPERATORS
1. List the operators that cannot be overloaded.
2. Explain how overloading takes place in C++.
3. What is the difference between prefix and postfix versions of operator++()?
4. Describe the advantages of operator overloading.
7. Provide some examples of operator overloading.
8. Operator overloading is supposed to make the code clearer and more understandable but it makes the code  more complex to understand. Why?
9. Can the operator == be overloaded for comparing two arrays consisting of characters by using string comparison?
11. Can the creation of operator** is allowed to perform the to-the-power-of operations?
12. What is the main purpose of overloading operators?