<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Interview Mantra &#187; Interview Questions</title>
	<atom:link href="http://www.interviewmantra.net/category/interview-questions/feed" rel="self" type="application/rss+xml" />
	<link>http://www.interviewmantra.net</link>
	<description>Your mantra to job success</description>
	<lastBuildDate>Thu, 29 Jul 2010 06:38:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Tata Technologies Looking for C/C++/Java programmers</title>
		<link>http://www.interviewmantra.net/2010/06/tata-technologies-c-cpp-java-programmers.html</link>
		<comments>http://www.interviewmantra.net/2010/06/tata-technologies-c-cpp-java-programmers.html#comments</comments>
		<pubDate>Fri, 04 Jun 2010 13:05:04 +0000</pubDate>
		<dc:creator>guest</dc:creator>
				<category><![CDATA[Announcements]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Job Search]]></category>

		<guid isPermaLink="false">http://www.interviewmantra.net/?p=1285</guid>
		<description><![CDATA[Tata Technologies Ltd. is hiring 40 professionals for Pune Location.  Headquartered in Singapore,   Tata Technologies Limited is a leader in engineering services outsourcing with over 4,000 highly skilled employees. Do you meet the following criteria? If yes, send your resume asap to sridhar@interviewmantra.net Possess 2 to 5 years of IT Experience. Education &#8211; BE [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.tatatechnologies.com/">Tata Technologies Ltd.</a> is hiring 40 professionals for Pune Location.  Headquartered in Singapore,   Tata Technologies Limited is a leader in engineering services outsourcing with over  4,000 highly skilled employees.</p>
<p>Do you meet the following criteria? If yes, send your resume asap to <a href="mailto:sridhar@interviewmantra.net?subject=Job Application for Tata Technologies">sridhar@interviewmantra.net</a></p>
<ul>
<li>Possess 2 to 5 years of IT Experience.</li>
<li>Education &#8211; BE or MCA.</li>
<li>Programming experience  in <strong>C/C++/</strong><strong> Java</strong> (Swings, Servlets, JSP, strong OO skills).</li>
<li>Knowledge of Teamcenter  ITK programming is a huge advantage.</li>
<p><a href="mailto:sridhar@interviewmantra.net?subject=Job Application for Tata Technologies"><img title="Apply Job" src="http://www.interviewmantra.net/wp-content/uploads/2010/06/apply_now.jpg" alt="" width="50" height="50" /></a>
</ul>
<p><em>Note that this requirement is for Indian National only. </em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2010/06/tata-technologies-c-cpp-java-programmers.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HR Interview Questions &#8212; Categorized</title>
		<link>http://www.interviewmantra.net/2010/04/hr-interview-questions-categorized-pdf.html</link>
		<comments>http://www.interviewmantra.net/2010/04/hr-interview-questions-categorized-pdf.html#comments</comments>
		<pubDate>Sun, 18 Apr 2010 04:00:05 +0000</pubDate>
		<dc:creator>Sridhar Jammalamadaka</dc:creator>
				<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[Download eBook]]></category>
		<category><![CDATA[hr]]></category>

		<guid isPermaLink="false">http://www.interviewmantra.net/?p=990</guid>
		<description><![CDATA[This post categorizes the frequently asked questions in HR interviews. You can save these questions for later review by downloading the pdf of HR interview questions. HR Questions: Stability at work How long would you expect to work for us if hired? What are your career options right now? What will this job give that [...]]]></description>
			<content:encoded><![CDATA[<p>This post categorizes the frequently asked questions in HR interviews. You can save these questions for later review by downloading the <a href="http://www.interviewmantra.net/wp-content/uploads/2010/04/HR-interview-questions.pdf">pdf of HR interview questions</a>.</p>
<h1>HR Questions:</h1>
<h2>Stability at work</h2>
<ol>
<li>How long would you expect to work for us if hired?</li>
<li>What are your career options right now?</li>
<li>What will this job give that your previous job doesn&#8217;t?</li>
<li>What is the profile that you are looking for, how will this 	company  give you while previous doesn&#8217;t?</li>
<li>Why are you leaving your current position? (Or) Why are you 	looking  for a job change?  (Or) What is the reason you are leaving 	previous  organization?</li>
<li>If another company offers you better salary than 	us, what will be  your reaction?</li>
<li>Where do you see yourself 5 years from now? (Or) What are 	your  short term goals?</li>
</ol>
<h2>Work ethic questions</h2>
<ol>
<li>What is more important to you: the money or the work?</li>
<li>If you won $10 million in lottery, would you still work?</li>
<li>Tell me about a time when you were accountable for a mistake 	you  made. Did you take responsibility for the mistake?</li>
<li>Describe your work ethic.</li>
<li>What is your philosophy towards work?</li>
<li>Are you willing to put the interests of the organization ahead of  your own?</li>
</ol>
<h2>Personality test questions</h2>
<ol>
<li>What are your greatest strengths?</li>
<li>What would your previous supervisor say your strongest point is?</li>
<li>What are your greatest weaknesses?</li>
<li>Do you have any blind spots?</li>
<li>What do your co-workers say about you?</li>
<li>Tell me about a situation when your work was criticized? (Or) 	How  do you handle criticism?</li>
<li>What makes you angry at work?</li>
<li>Do you get angry with your team mates if they don&#8217;t 	cooperate?</li>
<li>What irritates you about co-workers?</li>
<li>Tell me about a time when you helped resolve a dispute between  fellow team mates.</li>
<li>Tell me about the most fun you have had on the job.</li>
<li>Tell me honestly about your boss&#8217;s strengths and weaknesses.</li>
<li>Tell me about a problem you had with a supervisor.</li>
<li>Do you prefer to work alone or do you work better in groups?</li>
<li>Looking back, what would you differently in your life?</li>
<li>What was the toughest decision in your life you ever had to 	made?</li>
<li>What are your professional goals?</li>
<li>What have you done to improve your knowledge in the last year?</li>
<li>Do you consider yourself successful? Why?</li>
<li>Who has inspired you in life and why?</li>
</ol>
<h2>Compatibility Questions</h2>
<ol>
<li>Technically, what kind of work would you prefer?</li>
<li>Would you lie for the company?</li>
<li>Briefly describe an ideal position for you?</li>
<li>Describe your ideal company, location and job?</li>
<li>What kind of person would you refuse to work with?</li>
<li>Why do you want to join this industry?</li>
<li>What makes you the best candidate for this position?</li>
<li>What motivates you to do your best at work?</li>
<li>What was the toughest part of your last job?</li>
<li>What have you learned from mistakes on the job?</li>
<li>What has disappointed you about a job?</li>
<li>What do you expect from this organization?</li>
<li>What has been your biggest professional disappointment?</li>
<li>What position do you prefer on a team working on a project?</li>
<li>How do you propose to compensate for your lack of experience?</li>
<li>Do you think you are overqualified for this position?</li>
<li>Would you be willing to relocate if required?</li>
<li>Are you willing to work overtime? Nights? Weekends?</li>
</ol>
<h2>Communication skills</h2>
<ol>
<li>Tell me about your hobbies and pastimes?</li>
<li>Which is the most unforgettable moment in your life?</li>
<li>How do you overcome stress at work?</li>
<li>What according to you, are the skills required to be 	successful in  IT industry?</li>
<li>If you were hiring a person for this job, what would you look for?</li>
<li>Why should I hire you? (Or) Why do you think you are the best 	 candidate?</li>
<li>Explain how you would be an asset to this organization.</li>
<li>Can you describe your previous company&#8217;s work culture?</li>
</ol>
<h2>Ego Questions</h2>
<ol>
<li>What is the main role of a HR in an organization?</li>
<li>Why do you want to work for this organization?</li>
<li>What do you know about this organization?</li>
<li>Can you list the clientèle of this organization?</li>
<li>On a scale of 10, rate me as an interviewer.</li>
</ol>
<h2>Salary Negotiation Questions</h2>
<ol>
<li>The Salary question – How much do you want?</li>
<li>What kind of salary do you need?</li>
</ol>
<h2>Tacit Questions</h2>
<ol>
<li>Is the candidate over qualified for this position?</li>
<li>Will this candidate be satisfied with the profile and nature 	of  work offered to him?</li>
<li>If I offer a job, is there a chance this person will quit and 	start  his own business?</li>
<li>Is this person too outspoken or too reticent?</li>
<li>The company where candidate is currently working seems to be 	an  unpopular brand. Will this hire affect my company&#8217;s brand image?</li>
<li>Will this person be able to pick up the work assigned to him?</li>
<li>Is this person lacking any basic skills or manners?</li>
<li>Is there anything that this candidate is trying to hide from 	us?</li>
</ol>
<h2>Background check questions</h2>
<ol>
<li>Tell me about yourself.</li>
<li>Questions  related to passport and 	visa.</li>
<li>Questions related to previous 	 month Salary slips.</li>
</ol>
<h2>Rationale behind asking these questions</h2>
<p><strong>Stability at Work</strong> The reason why the employee is  quitting  current job is of utmost  importance for HR managers. They  want to know  why exactly the employee  was motivated to quit the  current job. They  want to make sure that the  employee doesn&#8217;t decide  to quit the company  before working for an year  or two. So this  category of questions are  indirect ways of asking the  question, “How  long will you stay with us  after hiring you?”</p>
<p><strong>Work ethic questions</strong> are asked to check the whether the   candidates posses values such as Loyalty to  the company, reliability   and accountability at work. Is work just another menial task that   fetches you money or do you enjoy your work? Is money your only priority   at work place?</p>
<p><strong>Ego Questions</strong>. In interviews, some HRs  ask a few irrelevant  questions such as “What is the role and importance  of HR Manager in an  organization?”, to make themselves feel important  and to satisfy their  ego. Other questions that fall in this category  are those that ask about  their employer organization such as “What do  you know about this  company?”, “Why would you like to join this  company”, to make themselves  feel better from inside for working for  that company. You should try to  exaggerate and lift the interviewer&#8217;s  ego by answering in a boasting  and optimistic manner.</p>
<p><strong>Communication  skills</strong>. There are some questions that have no  relation to the job  position being offered, sometimes these questions  are personal. They go  something like “whom do you like more your mother  or father and why?&#8221;  Such questions are asked to get the candidate  talking, to determine how  good their speaking, persuasion and  presentation skills are. These  questions are especially important for  the job positions that involve  communication with customers.</p>
<p><strong>Compatibility questions</strong> are  asked by HRs to confirm if the  candidate is a good fit into the  corporate culture. For example, if the  job requires the candidate to  work in night shifts, and the candidate  doesn&#8217;t have prior experience  working in a night shift, this might  become potential problem for the  candidate in future which may result in  employee&#8217;s frustration and  eventually resignation from that job.</p>
<p><strong>Personality  test questions </strong>is a category of interview  questions which includes  all the questions that the interviewer asks to  know about the  candidate&#8217;s strengths &amp; weaknesses, personal vision  &amp; goals,  values  , attitude towards work, people and organization.  How good a  candidate is who has no clarity about his/her professional  goals? How  good is a candidate who has problems working smoothly with  people?</p>
<p><strong>Aptitude  questions</strong> are being asked by the interviewers to  determine the  smartness of the candidate. Since HR managers&#8217; job role is  often  disconnected with technical stuff, they don&#8217;t and can&#8217;t ask  technical  questions to the interview candidate. They test people by  asking  puzzles or role play questions. Puzzles in general, because they   indicate to certain extent, the problem solving ability of an   individual. Role play questions are asked to check how you behave in a   particular scenario that they are familiar with. For example, the hiring   manager may ask you to pretend to be receiving a call from an angry   customer and you are supposed to pacify him by assuring him that his   issue would be looked into immediately.</p>
<p><strong>Salary negotiation  questions</strong> are of utmost importance for the  candidates who appear  for the interview. 90% of time, the reason for job  change is for a  better pay check. So the candidate is most eager to  answer these  questions. From HR perspective, these questions are asked  to find out  whether the company can match the financial requirements of  the job  candidate.</p>
<p><strong>Tacit questions</strong> are the most dangerous of all  these categories  of interview questions. They are usually the unspoken  concerns of the  interviewer that lead to rejection of candidates. They  are also few of  the most frequent causes for a job rejection.</p>
<p><strong>Background  check questions</strong> are being asked to avoid hiring the   spurious  professionals, to check misrepresentation of facts, to check   whether  the candidate is not faking any details on his resume – such as   fake  degree or fake experience or fake salary slips etc. And in some    places, where there are region specific things to ensure that the    candidate legally qualifies to be hired. Issues such as citizenship,    visa status etc.</p>
<p>HR interview questions can be broadly classified into the following categories.</p>
<h2>HR Question Categories:</h2>
<ul>
<li>Stability at work</li>
<li>Work ethic test questions
<ul>
<li>Loyalty</li>
<li>Ownership</li>
<li>Dedication</li>
<li>Money mindedness</li>
</ul>
</li>
<li>Personality test questions
<ul>
<li>Strengths and Weaknesses</li>
<li>Honesty</li>
<li>Personal vision &amp; values</li>
<li>Attitude towards work</li>
<li>Attitude towards people</li>
<li>Attitude towards organization</li>
</ul>
</li>
<li>Compatibility Questions
<ul>
<li>Compatibility with company 		culture</li>
<li>Compatibility with job 		role or project</li>
</ul>
</li>
<li>Aptitude questions
<ul>
<li>Puzzles</li>
<li>Case studies</li>
</ul>
</li>
<li>Communication skills
<ul>
<li>Speaking skills</li>
<li>Presentation skills</li>
<li>Persuasive skills</li>
<li>Customer handling skills</li>
</ul>
</li>
<li>Ego questions
<ul>
<li>Praise the HR</li>
<li>Praise the Company</li>
</ul>
</li>
<li>Salary Negotiation Questions</li>
<li>Tacit Questions</li>
<li>Background check questions
<ul>
<li>Check previous work history</li>
<li>Check authenticity of work 		experience</li>
<li>Clarify other region-specific 		issues</li>
</ul>
</li>
</ul>
<p>Human Resource Managers have simple concerns on behalf their employer  organization. Their interviews are usually much easier to generalize  than the technical ones. That&#8217;s because HR managers have very simple  objectives &#8212; they want to check whether the candidate is loyal, honest,  convincing, smart and sane. In simple words, a HR manager wants to  avoid a bad hire. From their previous experiences, they knows what kind  of people they don&#8217;t want to hire.</p>
<p>Hiring new candidates is very responsible task for the Hiring  Managers and Human Resource Managers. Their decision whether to hire or  not to hire a candidate, can add value to the company, shape its future,  bring new energy and enthusiasm to the existing work force.</p>
<p>A bad hire can prove very expensive to the company. If the candidate  that they hire is not capable enough to perform his/her daily activities  at work, the team for which he/she works will have to suffer working  with that non-performing resource until new hiring takes place. At  times, it gets very difficult for the companies to fire people based on  employee&#8217;s poor performance, owing to many factors such as the company  policies, loss of morale among the fellow employees, etc.</p>
<p>Even though the company takes a bold step to fire the unwanted  resource, replacing this poorly performing resource is tough, as fresh  hiring means additional costs to the company and wastage of time of the  hiring managers for conducting interviews. Not only that, the hiring  managers would be held accountable for the bad hire that took place.  Which could mean that if the hiring managers do a bad hire, they may  lose their reputation among their superiors which may lead to a bad  performance review in their annual appraisal.</p>
<p>To avoid all these complications, a responsible hiring manager asks  himself/herself the following questions before confirming a candidate  with an job offer.</p>
<ul>
<li>Is this person authentic?</li>
<li>Will this person work for the 	company at least for the next few  years?</li>
<li>Will the company be able to 	recover the costs incurred to him by  the company?</li>
<li>Does this person have ethics at 	work?</li>
<li>Is this person sane?</li>
<li>Does this person fit into our 	company culture?</li>
<li>Is this person smart?</li>
<li>Can this person speak well?</li>
<li>Why should I care to hire this 	person?</li>
<li>Can we financially afford to hire 	this person?</li>
<li>Do we have any concerns related to 	this person&#8217;s profile?</li>
</ul>
<h2>References:</h2>
<ul>
<blockquote>
<li><a href="http://www.interviewmantra.net/2009/12/would-you-hire-samuel.html">HR question &amp; answers</a></li>
<li><a href="http://www.techpreparation.com/64-hr-interview-questions-answers1.htm">Techpreparation.com</a></li>
<li><a href="http://www.coolinterview.com/type.asp?iType=177">Coolinterview.com</a></li>
</blockquote>
</ul>
<h2><a href="http://www.interviewmantra.net/wp-content/uploads/2010/04/HR-interview-questions.pdf">Download HR questions in this article as PDF</a></h2>
<p><em>Comment , if you would like to add a question to this list. </em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2010/04/hr-interview-questions-categorized-pdf.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Hard Interview Questions – part2</title>
		<link>http://www.interviewmantra.net/2010/04/cpp-hard-interview-questions-part2.html</link>
		<comments>http://www.interviewmantra.net/2010/04/cpp-hard-interview-questions-part2.html#comments</comments>
		<pubDate>Wed, 14 Apr 2010 12:00:57 +0000</pubDate>
		<dc:creator>Saurabh Manchanda</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[c++ interview questions]]></category>
		<category><![CDATA[cpp interview questions]]></category>
		<category><![CDATA[RAII]]></category>
		<category><![CDATA[RTTI]]></category>

		<guid isPermaLink="false">http://www.interviewmantra.net/?p=899</guid>
		<description><![CDATA[In websites such as coolinterview.com, geekinterview.com, you will find plethora of technical interview questions. But all the questions there are posted and answered by the users. You might have found that some questions mentioned there are of any use. Also you will most of the answers not much reliable. We at interviewmantra, extensively research for [...]]]></description>
			<content:encoded><![CDATA[<p>In websites such as <a href="http://www.coolinterview.com">coolinterview.com</a>, <a href="http://www.geekinterview.com">geekinterview.com</a>, you will find plethora of technical interview questions. But all the questions there are posted and answered by the users. You might have found that some questions mentioned there are of any use. Also you will most of the answers not much reliable. We at interviewmantra, extensively research for good interview questions, categorize them according to difficulty, answer each of them in detail, make sure that the answers are correct to the best of our knowledge. Here is the last 5 questions in <a href="http://www.interviewmantra.net/tag/c-interview-questions">this series of C++ interview questions</a>. Hope these are of use to you for interview preparation. </p>
<h3>6. What&#8217;s the knack with vector&lt;bool&gt;?</h3>
<p>The std::vector&lt;bool&gt; is a specialization of the std::vector&lt;&gt; class template, i.e., when an object of vector&lt;bool&gt; is created, it uses a specialized and highly optimized version of the vector&lt;&gt; template, instead of instantiating the template for the bool type. The vector&lt;bool&gt; stores boolean data as bits and not as bool variables (which could take a whole byte to store a single true or a false).</p>
<p>The vector&lt;bool&gt;  is optimized on space, rather than on speed, unlike the vector&lt;&gt; template. Moreover, this optimization has been forced upon the users because of the specialization. Also, the vector&lt;bool&gt; items are not addressable and the specialization defines the iterator in a form different than that of vector&lt;&gt; which is not random-access. For more info, check <a href="http://www.informit.com/guides/content.aspx?g=cplusplus&amp;seqNum=98" target="_blank">here</a></p>
<h3>7. What is the difference between assignment operator and a copy constructor?</h3>
<p>As the names suggest, the copy constructor is used to &#8220;construct an object&#8221;. The new object is an exact copy of another object that is already constructed. The assignment operator is used to &#8220;assign an object&#8221; with the value(s) of some other object. Hence, the assignment operator is something that comes into action when we have an object to the LHS of the &#8216;=&#8217; operator and some expression to the RHS.</p>
<p><strong>For Example:</strong><br />
For some type T and &#8220;params&#8221; for the object&#8217;s constructor,</p>
<pre class="brush: cpp;">{
   T x (params1, ...);
   T y (params2, ...);

   T z = x; /* Copy : z created with the value of x */
   y = x+z; /* Assignment : y gets the value equivalent to x+z */
}</pre>
<p>If a programmer doesn&#8217;t write a copy constructor or an assignment operator, then the compiler provides one. The copy constructor provided by the compiler does a <a href="http://www.interviewmantra.net/2010/02/cpp-hard-interview-questions-part1.html#copy" target="_blank">shallow-copy</a> of the objects and, hence, could lead to problems. And, when the programmer overloads the assignment operator for a particular class, s/he must take care of <a href="http://www.interviewmantra.net/2010/02/cpp-hard-interview-questions-part1.html#Self" target="_blank">self-assignment</a> and free the resources before the assignment is done.</p>
<h3>8. Why we would use exception handling rather than different return values to signal an error?</h3>
<p>The probability of a program failing is never zero and, when when such conditions arise, different steps shall be taken. Two options are &#8211; Returning an Error code and using Exception Handling.<br />
Returning an error-code is fine but has some problems associated with it:</p>
<ol>
<li>The error-code returned is generally of type &#8220;int&#8221;. The problem here is that if certain integer values are associated with error-codes, then the function would have to exclude those from the valid return values, which isn&#8217;t feasible in all situations.</li>
<li>Even if the above is feasible, the code would require code to check the return value for each call and this would cause code bloat.</li>
<li>Some error codes are used to signal that the system is in non-recoverable state and needs to exit. This might not be a good thing to be done at all times, for example, if the code that generates the error and terminates the program is part of a library and is being run as a part of a system that can&#8217;t be terminated.</li>
</ol>
<p>The above problems are not seen while using Exception handling because it separates the error-handling code from the program logic. This also makes the code clean and readable.</p>
<h3>9. What is RAII?</h3>
<p>Resource Acquisition Is Initialization or RAII is a design pattern that helps in creating objects, on the heap, that are safely destructed. The RAII technique requires a wrapper to be used to hold the resource so that when the wrapper object goes out of scope or an exception is thrown, the resource is freed.</p>
<p><strong>For Example:</strong></p>
<pre class="brush: cpp;">#include&lt;exception&gt;
#include&lt;iostream&gt;
#include&lt;string&gt;

struct someClass{};

class Wrap
{
   someClass* ptr;
public:
   Wrap() : ptr(0){}
   Wrap(someClass* obptr): ptr(obptr){}

   void setNull()
   {
      if(ptr)
         delete ptr;
      ptr=0;
   }

   void changeRsrc(someClass* obptr)
   {
      if(ptr)
         delete ptr;
      ptr=obptr;
   }

 someClass&amp; operator*()   { return *ptr;}
 someClass* operator-&gt;()  { return ptr;}
 ~Wrap(){ std::cout&lt;&lt;&quot;Destructor\n&quot;;delete ptr; ptr=0;}
};

void  Foo()
{
 /* someClass *somePtr = new someClass; */

 Wrap wObj(new someClass);
 /* Some Operations on someClass object using wObj */

 throw std::exception();
}

int main()
{
 try
 {
    Foo();
 }catch(std::exception&amp;){std::cout&lt;&lt;&quot;Exception caught\n&quot;;}
}</pre>
<p>Here, the class Wrap implements the RAII. When Foo() throws an exception, the wObj, being on the stack, invokes its destructor which frees the memory space taken up by the object of someClass. If the somePtr would have been used, then the space allocated by it would not have been freed.<br />
The <strong>auto_ptr</strong> is a class template, available in C++, that implements the RAII.</p>
<h3>10. What is RTTI? What are its uses?</h3>
<p>The RTTI or Run-Time Type Identification is a feature of C++ that helps in storing and retrieving type information of the objects at runtime. This information can be used for safely typecasting objects, and to fetch and utilize the type information to set the flow of the program at runtime.</p>
<p>The RTTI requires the classes in use to be <strong>&#8220;polymorphic&#8221;</strong> in nature, which means that there should be at least one virtual function defined in it. The system is used with the help of <strong>dynamic_cast&lt;&gt;</strong> and the <strong>typeid</strong> operator.</p>
<p>The typeid operator is available under the &#8220;typeinfo&#8221; header and can be used to find out the class to which a particular object belongs to, at runtime. The value returned by the operator is implementation-defined and hence, can be different across compilers. Thus, for portability sake, the operator can be used in the following manner:</p>
<pre class="brush: cpp;">#include&lt;iostream&gt;
#include&lt;typeinfo&gt;

struct dummyBase
{virtual ~dummyBase(){}};

struct dummyDer: dummyBase{};

void check(dummyBase* a)
{
   if(typeid(*a)==typeid(dummyBase))
      std::cout&lt;&lt;&quot;It is a dummyBase object\n&quot;;

   else if(typeid(*a)==typeid(dummyDer))
      std::cout&lt;&lt;&quot;It is a dummyDer object\n&quot;;
}

int main()
{
   dummyDer d;
   dummyBase b;
   dummyBase*ptr;

   ptr=&amp;d;
   check(ptr);

   ptr=&amp;b;
   check(ptr);
}</pre>
<p>If only the object information is required and no cast is to be done, then one must use the typeid operator and not the dynamic_cast because the former takes constant-time to return the value.</p>
<p>The dynamic_cast&lt;&gt; operator is what actually utilizes the RTTI system. The dynamic_cast&lt;&gt; is safe because if the cast is not possible, it sets the pointer to NULL instead of returning a bad pointer and in case of a reference, it throws the std::bad_cast exception. The cast knows about the hierarchy of the particular object that helps it do its work correctly and can be used to cast up, down or laterally.</p>
<p><strong>For Example:</strong></p>
<pre class="brush: cpp;">#include&lt;iostream&gt;

struct ITop
{
   virtual void funcTop()=0;
   virtual ~ITop(){}
};

struct ILeft: ITop
{
   void funcTop(){ std::cout&lt;&lt;&quot;ILeft funcTop\n&quot;;}
   virtual void funcLeft()=0;
};

struct IRight: ITop
{
   void funcTop(){ std::cout&lt;&lt;&quot;IRight funcTop\n&quot;;}
   virtual void funcRight()=0;
};

struct Left: ILeft
{
   void funcLeft(){ std::cout&lt;&lt;&quot;Left heir\n&quot;;}
};

struct Right: IRight
{
   void funcRight(){ std::cout&lt;&lt;&quot;Right heir\n&quot;;}
};

void Foo(ITop* elem)
{
   elem-&gt;funcTop();

   if(ILeft* ptrL = dynamic_cast&lt;ILeft*&gt;(elem))
      ptrL-&gt;funcLeft();
   else if(IRight* ptrR = dynamic_cast&lt;IRight*&gt;(elem))
      ptrR-&gt;funcRight();
}

int main()
{
   ITop* ptr[]={new Left, new Right};

   Foo(ptr[0]);
   Foo(ptr[1]);

   delete ptr[0];
   delete ptr[1];
}</pre>
<p>In the above code, the function Foo() checks the type of &#8216;elem&#8217; and finds out where does the object lie in the hierarchy and accordingly, invokes either the funcLeft() or the funcRight().</p>
<p><a href="http://www.objectmentor.com/resources/articles/acv.pdf">Here</a> is one good use of RTTI.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2010/04/cpp-hard-interview-questions-part2.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Core Java interview questions</title>
		<link>http://www.interviewmantra.net/2010/04/core-java-interview-questions.html</link>
		<comments>http://www.interviewmantra.net/2010/04/core-java-interview-questions.html#comments</comments>
		<pubDate>Sun, 11 Apr 2010 05:54:56 +0000</pubDate>
		<dc:creator>Sridhar Jammalamadaka</dc:creator>
				<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.interviewmantra.net/?p=1143</guid>
		<description><![CDATA[Can you explain the OOPS concepts – Encapsulation, Abstraction, Inheritance and Polymorphism? How can you achieve Polymorphism using Java? What are access modifiers? What does the access modifier &#8216;protected&#8217; signify? What is the difference between an abstract class and an interface? Can you describe a business scenario where you will have to choose between an [...]]]></description>
			<content:encoded><![CDATA[<ol>
<li>Can you explain the OOPS concepts – Encapsulation, Abstraction, Inheritance and Polymorphism?</li>
<li>How can you achieve Polymorphism using Java?</li>
<li>What are access modifiers?</li>
<li>What does the access modifier &#8216;protected&#8217; signify?</li>
<li>What is the difference between an abstract class and an interface?</li>
<li>Can you describe a business scenario where you will have to choose between an abstract class and an interface?</li>
<li>What are Collections in Java?</li>
<li>Can you name few Collection classes in Java?</li>
<li>What is the difference between an ArrayList and a Vector?</li>
<li>What is the difference between HashMap and a HashTable?</li>
<li>What is the use of Properties class in Java?</li>
<li>Explain how to use Java class StringTokenizer</li>
<li>What is the difference between .equals and == operator?</li>
<li>Can we declare a try block with a finally block and without a catch?</li>
<li>How does the order of exceptions in catch block matter?</li>
<li>How to write a user defined Exception?</li>
<li>What is the difference between an Error and an Exception?</li>
</ol>
<p>These questions were compiled by <a href="http://rocksolutions.wordpress.com/">Rakesh Kumar Vashisht</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2010/04/core-java-interview-questions.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Are you a leader or a follower? &#8212; Interview Question</title>
		<link>http://www.interviewmantra.net/2010/03/leader-or-follower-interview-question.html</link>
		<comments>http://www.interviewmantra.net/2010/03/leader-or-follower-interview-question.html#comments</comments>
		<pubDate>Fri, 26 Mar 2010 03:05:46 +0000</pubDate>
		<dc:creator>Sridhar Jammalamadaka</dc:creator>
				<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[Interview Tips]]></category>

		<guid isPermaLink="false">http://www.interviewmantra.net/2010/03/are-you-a-leader-or-a-follower-interview-question.html</guid>
		<description><![CDATA[This questions was asked in interview for position of internship position in a company. Imagine that below question was put to you in an interview. Are you a leader or a follower? What would be your answer to this question? I invite readers to post their answers with justification in comments. I will choose the [...]]]></description>
			<content:encoded><![CDATA[<p>This questions was asked in interview for position of internship position in a company. Imagine that below question was put to you in an interview.</p>
<h1>Are you a leader or a follower?</h1>
<p>What would be your answer to this question?</p>
<p>I invite readers to <a href="http://www.interviewmantra.net/2010/03/leader-or-follower-interview-question.html#comments">post their answers</a> with justification in comments. I will choose the top 3 good answers among the readers&#8217; comments and mention it along with the reader&#8217;s name and their website(if any) in this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2010/03/leader-or-follower-interview-question.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Java Interview Questions @ Amdocs</title>
		<link>http://www.interviewmantra.net/2010/03/java-interview-questions-amdocs.html</link>
		<comments>http://www.interviewmantra.net/2010/03/java-interview-questions-amdocs.html#comments</comments>
		<pubDate>Tue, 16 Mar 2010 12:00:56 +0000</pubDate>
		<dc:creator>Sridhar Jammalamadaka</dc:creator>
				<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[amdocs]]></category>

		<guid isPermaLink="false">http://www.interviewmantra.net/?p=926</guid>
		<description><![CDATA[Here are the questions asked in an interview with the company Amdocs, calling for Java developers with 2 to 5 years of IT experience. Java Questions: Where and why do we use the synchronized keyword? Give an example of polymorphism in java with code? What are the classes and interfaces used in Collection framework, briefly [...]]]></description>
			<content:encoded><![CDATA[<p>Here are the questions asked in an interview with the company <a href="http://www.amdocs.com/">Amdocs</a>, calling for Java developers with 2 to 5 years of IT experience.</p>
<h2>Java Questions:</h2>
<ol>
<li>Where and why do we use the <em>synchronized </em>keyword?</li>
<li>Give an example of polymorphism in java with code?</li>
<li>What are the classes and interfaces used in Collection framework, briefly explain them?</li>
<li>What are inner classes and why do we need them?</li>
<li>Do we name the java file using the Outer class’s name?</li>
<li>How to access the inner class?</li>
<li>Why do we need an abstract class from design perspective?</li>
<li>Difference between keywords and variables?</li>
<li>What is the deployment process of your application?</li>
<li>Explain MVC?</li>
<li>What are the components in your application that correspond to Model View and Controller components?</li>
<li>Which application server do you use to deploy your app?</li>
<li>What database do you use and how do you connect your java application to the database?</li>
<li>What is JNDI?</li>
<li>What is EJB?</li>
<li>What do u mean by serialization? How do we achieve it in java?</li>
</ol>
<p>Amdocs, Pune is doing a lot of hiring these days. In case you have attended an interview with Amdocs recently, please post the questions that were asked in comments. Let&#8217;s share interview questions, experiences and make our lives easier.</p>
<p><em>Feel free to comment the answers to these questions.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2010/03/java-interview-questions-amdocs.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Revised edition of ebook &#8211; C Question Bank</title>
		<link>http://www.interviewmantra.net/2010/03/revised-edition-c-question-bank.html</link>
		<comments>http://www.interviewmantra.net/2010/03/revised-edition-c-question-bank.html#comments</comments>
		<pubDate>Thu, 11 Mar 2010 18:30:20 +0000</pubDate>
		<dc:creator>Sridhar Jammalamadaka</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Freshers]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[c programs]]></category>
		<category><![CDATA[Download eBook]]></category>

		<guid isPermaLink="false">http://www.interviewmantra.net/?p=868</guid>
		<description><![CDATA[In the past, numerous articles on frequently asked C Interview Questions had been posted. All those questions were accompanied with clear answers and supporting code examples wherever appropriate. An ebook named &#8220;Favourite 50 &#8211; C Question Bank&#8221; had been created and posted to the downloads page of Interview Mantra. That ebook was a result of [...]]]></description>
			<content:encoded><![CDATA[<p>In the past, numerous articles on <a href="http://www.interviewmantra.net/category/interview/c">frequently asked C Interview Questions</a> had been posted. All those questions were accompanied with clear answers and supporting code examples wherever appropriate.</p>
<p>An ebook named &#8220;<strong>Favourite 50 &#8211; C Question Bank</strong>&#8221; had been created and posted to the <a href="../2009/01/download-pdf-c-interview-questions.html">downloads page of Interview Mantra</a>. That ebook was a result of aggregation of all the C Interview questions and answers posted in Interview Mantra. That was the first edition of this ebook. The ebook was downloaded 8756 times till the date this post had been written.</p>
<p>Inspired by the success of first edition, we have come up with the second edition that includes few corrections, additions and major formatting changes. This book is available for free download exclusively for the readers of Interview Mantra.</p>
<h2><a href="http://www.interviewmantra.net/wp-content/uploads/2010/03/C-Question-Bank-ebook.pdf">Click here to download the second edition of the ebook &#8220;<strong>Favourite 50 &#8211; C Question Bank</strong>&#8220;</a></h2>
<p style="text-align: center;"><a href="http://www.interviewmantra.net/wp-content/uploads/2010/03/C-Question-Bank-ebook.pdf"><img class="size-large wp-image-874 aligncenter" title="Favourite 50- C interview question bank" src="http://www.interviewmantra.net/wp-content/uploads/2010/03/fav50-c-interview-bank-723x1024.jpg" alt="C interview question bank" width="354" height="502" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2010/03/revised-edition-c-question-bank.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>C++ Hard Interview Questions – part1</title>
		<link>http://www.interviewmantra.net/2010/02/cpp-hard-interview-questions-part1.html</link>
		<comments>http://www.interviewmantra.net/2010/02/cpp-hard-interview-questions-part1.html#comments</comments>
		<pubDate>Sat, 27 Feb 2010 12:00:00 +0000</pubDate>
		<dc:creator>Saurabh Manchanda</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[c++ interview questions]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[cpp interview questions]]></category>
		<category><![CDATA[destructor]]></category>
		<category><![CDATA[pointers]]></category>

		<guid isPermaLink="false">http://www.interviewmantra.net/?p=758</guid>
		<description><![CDATA[1. Why don&#8217;t we have virtual constructors? The virtual mechanism, though used without the knowledge of the complete type of the object, works only on fully-constructed objects. But, in order to create an object, the user is required to provide the complete information about the object. Thus, it is not possible for a constructor to [...]]]></description>
			<content:encoded><![CDATA[<h3>1. Why don&#8217;t we have virtual constructors?</h3>
<p>The virtual mechanism, though used without the knowledge of the complete type of the object, works only on fully-constructed objects. But, in order to create an object, the user is required to provide the complete information about the object. Thus, it is not possible for a constructor to be virtual.</p>
<h3>2. What is virtual destructor?</h3>
<p>Consider a class &#8220;Base&#8221; that is inherited by another class &#8220;Derived&#8221; and there exists a pointer-to-base referring to a heap-allocated derived object, then a virtual destructor would be required in the base class for the proper clean-up of the object. If the destructor is not virtual, then the destructor for the derived sub-object won&#8217;t be called and hence, the resources will not be freed.<span id="more-758"></span></p>
<p><strong>For Example:</strong></p>
<pre class="brush: cpp;">
#include&lt;iostream&gt;

struct Base
{
      Base(){std::cout&lt;&lt;&quot;Base object constructed\n&quot;;}
      ~Base(){std::cout&lt;&lt;&quot;Base object destructed\n&quot;;}
};

struct Derived: Base
{
      Derived(){std::cout&lt;&lt;&quot;Derived object constructed\n&quot;;}
      ~Derived(){std::cout&lt;&lt;&quot;Derived object destructed\n&quot;;}
};

int main()
{
      {
            Base b_Obj;
            Derived d_obj;
      }
      {
            Base *p_b = new Derived();
            delete p_b;
      }
}
</pre>
<p>In the above code, the b_obj and d_obj would be destructed properly but the object referred-to by the pointer p_b won&#8217;t be destructed properly until the base class&#8217; destructor is made virtual.</p>
<p>This is because when p_b is delete-ed, it calls the destructor of the static type of the pointer, which is Base, instead of the Dynamic type.</p>
<p>Thus, whenever it is possible that your class might be inherited by another class, it is safe to make the destructor virtual.<br />
<A name="Self"></A><br />
<h3>3. What is Self-Assignment? Does it have any bugs/problems associated with it?</h3>
<p>Assignment of an object to itself is known as self-assignment.</p>
<p><strong>For example:</strong></p>
<pre class="brush: cpp;">int func(int &amp;x, int &amp;y)
{
      x=x;   /* Self Assignment */

      x=y;   /* Self-Assignment, because x and y both refer to &quot;data&quot; [defined in main() ] */
}

int main()
{
      int data=10;
      fun(data, data);
}
</pre>
<p>One potential problem associated with self-assignment arises in the following code:</p>
<pre class="brush: cpp;">
class dummy
{
      int n;
      int *ptr;

public:
      dummy() : ptr(new int){ *ptr = 0; }
      ~dummy() {delete ptr;}

      dummy&amp; operator= (const dummy&amp; D)
      {
            delete ptr; /* #1 */
            ptr= D.ptr; /* #2 */
      }

      int* operator*()
      {
            return ptr;
      }
};

int main()
{
      dummy mObj(10);
      mObj = mObj;
}
</pre>
<p>Here, Line #1, delete-s the pointer ptr, which is then invalidated. Line #2 assigns that freed memory back to ptr. If the pointer is now used, it will invoke Undefined Behavior.</p>
<p>To prevent this, the = operator should first check for self-assignment.</p>
<pre class="brush: cpp;">
dummy&amp; operator= (const dummy&amp; D)
{
      if(*this==D)
            return *this;
      else
      {
            delete ptr;
            ptr = D.ptr;
            return *this;
      }
}
</pre>
<p><A name="copy"></A><br />
<h3>4. What do you mean by Shallow copy and Deep copy?</h3>
<p>Shallow copy and Deep copy are two ways of copying an object.<br />
Consider the following code</p>
<pre class="brush: cpp;">
class Dummy
{
      string d_str;
      int* d_ptr;
public:
      Dummy(string xstr, int* dptr) : d_str(xstr), d_ptr(pstr) {}
      ~Dummy() { delete d_ptr;}
};

int main()
{
      Dummy Xobj(&quot;Test_Object&quot;, new int);
      {
            Dummy Yobj = Xobj;
      }
}
</pre>
<p>A <strong>shallow copy</strong> of an object copies it bit-by-bit. This is what happens in the implicitly-defined assignment operator and copy constructor.</p>
<p>In the above example, the Xobj will be copied bit-by-bit into Yobj. Here, the copy constructor of Xobj.d_str will be first invoked to copy its contents into Yobj.d_str and then the Xobj.d_ptr would be copied  into Yobj.d_str and Yobj.d_ptr. The copy constructor of std::string has been defined to perform a Deep copy but the Xobj.d_ptr would be shallow-copied which means that Xobj.d_ptr and Yobj.d_ptr will both point to the same memory location and any change to one pointer will cause a change to the other.</p>
<p>The main problem occurs when an object goes out of scope or a pointer element is freed/invalidated. In the above example, Yobj will go out of scope and the memory pointed would be freed, causing both Xobj.d_ptr and Yobj.d_ptr to be invalidated and, thus, the user is not allowed to use Xobj.d_ptr any further.</p>
<p>A <strong>deep copy</strong> of Xobj into Yobj would cause the member Yobj.d_ptr to get hold of a new memory location and the contents of the memory be same as of those referred by the member Xobj.d_ptr. Now, as the memory location referred by Xobj.d_ptr and that by Yobj.d_ptr are different, deletion/invalidation of the memory causes no trouble to Xobj.</p>
<h3>5. What is the Placement new? Where would you use it? What are your responsibilities?</h3>
<p>The Placement new operator is used in case you want the object to be placed at a particular pre-allocated memory.</p>
<p><strong>For Example:</strong></p>
<pre class="brush: cpp;">
class test
{
      /* members and methods */
};

int main()
{
      char memory[sizeof(test)];
      char* position = memory;
      test* object = new(position) test;
}
</pre>
<p>Some situations where placement new can be used are :</p>
<ol>
<li>When writing your memory allocation unit and you want memory to be allocated from only a pre-allocated section of memory.</li>
<li>Used when it is required that I/O devices be mapped to specific memory addresses.</li>
<li>To create a shared memory region (for Inter-process Communication)</li>
</ol>
<p>There are a lot of other uses of placement new. Check <a href="http://stackoverflow.com/questions/362953/what-are-uses-of-the-c-construct-placement-new" target="_blank">here</a></p>
<p>When using placement new, the following points need to be kept in mind:</p>
<ol>
<li>Don&#8217;t use placement new unless you really need it and it is important that the object be places at some particular location in memory</li>
<li>It is the programmer&#8217;s job to check that the memory being used has enough space for the object and the memory is aligned properly.</li>
<li>The programmer is responsible for destruction the placed object, i.e., the programmer needs to call the destructor explicitly.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2010/02/cpp-hard-interview-questions-part1.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>An excellent resource for C puzzles practice</title>
		<link>http://www.interviewmantra.net/2010/02/an-excellent-resource-for-c-puzzles-practice.html</link>
		<comments>http://www.interviewmantra.net/2010/02/an-excellent-resource-for-c-puzzles-practice.html#comments</comments>
		<pubDate>Tue, 16 Feb 2010 16:53:55 +0000</pubDate>
		<dc:creator>Sridhar Jammalamadaka</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[c programs]]></category>
		<category><![CDATA[c puzzles]]></category>
		<category><![CDATA[Interview Questions]]></category>

		<guid isPermaLink="false">http://www.interviewmantra.net/?p=712</guid>
		<description><![CDATA[I discovered a great website that comprehensively covers puzzles and questions in C language. Gowrikumar.com &#8211; C Puzzles What&#8217;s special about these puzzles and questions is that some of these questions are asked in interviews of experienced C programmers at Multi National Companies. The standard of the questions and answers at the above site is [...]]]></description>
			<content:encoded><![CDATA[<p>I discovered a great website that comprehensively covers puzzles and questions in C language.</p>
<h2><a href="http://www.gowrikumar.com/c/">Gowrikumar.com &#8211; C Puzzles</a></h2>
<p>What&#8217;s special about these puzzles and questions is that some of these questions are asked in interviews of experienced C programmers at Multi National Companies. The standard of the questions and answers at the above site is pretty high, usually suits programmers with prior experience or expertise in C.<span id="more-712"></span></p>
<p>The man behind this website is <a href="http://www.gowrikumar.com/about.html">CH. Gowri Kumar</a>. He is from Hyderabad-India. He is a masters graduate from Indian Institute of science and has over 7 years of Software development experience with top IT companies such as HP, Motorola, Microsoft etc.</p>
<p>If you are a C lover, be sure to subscribe to the Google group, <a href="http://groups.google.com/group/programming-puzzles?pli=1"><strong>Programming Puzzles</strong></a> maintained by Gowrikumar himself. You can get all your C programming questions solved by the vibrant and active community there.</p>
<p><em>Comment below if you know sites similar to these which cover C programs.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2010/02/an-excellent-resource-for-c-puzzles-practice.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>10 Challenging char pattern programs</title>
		<link>http://www.interviewmantra.net/2010/01/10-challenging-char-pattern-programs.html</link>
		<comments>http://www.interviewmantra.net/2010/01/10-challenging-char-pattern-programs.html#comments</comments>
		<pubDate>Wed, 13 Jan 2010 14:00:36 +0000</pubDate>
		<dc:creator>Utsav Banerjee</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[pattern programs]]></category>

		<guid isPermaLink="false">http://www.interviewmantra.net/?p=652</guid>
		<description><![CDATA[Here are 10 pattern type problems statements with solutions. All the programs are written in C language.  This post is a part of the pattern programs series. Write a C program to print the following pattern: A B A C B A D C B A E D C B A Write a C program [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_694" class="wp-caption alignright" style="width: 310px"><a href="http://www.interviewmantra.net/wp-content/uploads/2010/01/char-pattern2.png"><img class="size-medium wp-image-694" title="Char pattern program" src="http://www.interviewmantra.net/wp-content/uploads/2010/01/char-pattern2-300x204.png" alt="Output of a char pattern program" width="300" height="204" /></a><p class="wp-caption-text">Output of a char pattern program</p></div>
<p>Here are 10 pattern type problems statements with solutions. All the programs are written in C language.  This post is a part of the <a href="http://www.interviewmantra.net/tag/pattern-programs">pattern programs series</a>.</p>
<p><!-- List of Questions in this post --></p>
<ol>
<li><a href="#ques1">Write a C program to print the following pattern: </a>
<pre><a href="#ques1">
A
B A
C B A
D C B A
E D C B A</a></pre>
</li>
<p><span id="more-652"></span></p>
<li><a href="#ques2">Write a C program to print the following pattern: </a>
<pre><a href="#ques2">A B C D E
B C D E
C D E
D E
E
</a></pre>
</li>
<li><a href="#ques3">Write a C program to print the following pattern: </a>
<pre><a href="#ques3">         A
a   a
B   B   B
b   b   b   b
C   C   C   C   C
c   c   c   c
D   D   D
d   d
E
</a></pre>
</li>
<li><a href="#ques4">Write a C program to print the following pattern: </a>
<pre><a href="#ques4">
A
Z
B   C
Y   X
D   E   F
W   V   U
G   H   I   J
T   S   R   Q
K   L   M   N   O
</a></pre>
</li>
<li><a href="#ques5">Write a C program to print the following pattern: </a>
<pre><a href="#ques5"> A   B   C   D   E
Z   Y   X   W
A   B   C   D
Z   Y   X
A   B   C
Z   Y
A   B
Z
A
Z
A   B
Z   Y
A   B   C
Z   Y   X
A   B   C   D
Z   Y   X   W
A   B   C   D   E
</a></pre>
</li>
<li><a href="#ques6">Write a C program to print the following pattern: </a>
<pre><a href="#ques6">A B C D E D C B A
A B C D   D C B A
A B C       C B A
A B           B A
A               A
A B           B A
A B C       C B A
A B C D   D C B A
A B C D E D C B A

</a></pre>
</li>
<li><a href="#ques7">Write a C program to print the following pattern: </a>
<pre><a href="#ques7">           A
B C D
E F G H I
J K L M N O P
Q R S T U V W X Y
Z
Y X W V U T S R Q
P O N M L K J
I H G F E
D C B
A
</a></pre>
</li>
<li><a href="#ques8">Write a C program to print the following pattern: </a>
<pre><a href="#ques8">N     N
N N   N
N   N N
N     N
</a></pre>
</li>
<li><a href="#ques9">Write a C program to print the following pattern: </a>
<pre><a href="#ques9"> X               X
X           X
X       X
X   X
X
X   X
X       X
X           X
X               X
</a></pre>
</li>
<li><a href="#ques10">Write a C program to print the following pattern: </a>
<pre><a href="#ques10">K             K
K           K
K         K
K       K
K     K
K   K
K K
K
K K
K   K
K     K
K       K
K         K
K           K
K             K
</a></pre>
</li>
</ol>
<ol><!-- Start of Question1 --><a name="ques1"> </a><span class="ques"> </span></p>
<li>Write a C program to print the following pattern:</li>
<pre>A
B A
C B A
D C B A
E D C B A</pre>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;

int main() {
	char prnt = 'A';
	int i, j;
	for (i = 1; i &lt;= 5; i++) { // Prints the right angle triangle
		for (j = 1; j &lt;= i; j++) {
			printf(&quot;%2c&quot;, ((prnt + (i - j))));  // Prints the characters in the required order
		}
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question1 --><!-- Start of Question2 --><a name="ques2"> </a><span class="ques"> </span></p>
<li> Write C program to print the following pattern:
<pre>A B C D E
  B C D E
    C D E
      D E
        E</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include&lt;stdio.h&gt;
int main() {
	int i, j;
	char c = 'A';
	for (i = 0; i &lt; 5; i++) {
		for (j = 0; j &lt; 5; j++) {
			if (j &lt; i) {
				printf(&quot; &quot;);
			} else {
				printf(&quot;%c&quot;, c + j);  // The order of the alphabets are increasing in terms of j
			}
			printf(&quot; &quot;);
		}
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question2 --><!-- Start of Question3 --><a name="ques3"> </a><span class="ques"> </span></p>
<li>Write C program to print the following pattern:
<pre>        A
      a   a
    B   B   B
  b   b   b   b
C   C   C   C   C
  c   c   c   c
    D   D   D
      d   d
        E</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include&lt;stdio.h&gt;
int main() {
	char prnt_even = 'a';
	char prnt_odd = 'A';
	int i, j, s, nos = 4,line=0;  //line tracks if the row is odd or even
	for (i = 1; i &lt;= 9; (i = i + 2)) {
		for (s = nos; s &gt;= 1; s--) {  // For the required spacings
			printf(&quot;  &quot;);
		}
		for (j = 1; j &lt;= i; j++) {
			if(line==0){
				if ((i % 2) != 0 &amp;&amp; (j % 2) != 0) {
					printf(&quot;%2c&quot;, prnt_odd);   // Prints capital letter if l=0
					} else {
						printf(&quot;  &quot;);
					}
				}
				else{
					if ((i % 2) != 0 &amp;&amp; (j % 2) != 0) {
						printf(&quot;%2c&quot;, prnt_even);  //else prints a small letter
						} else {
							printf(&quot;  &quot;);
						}
					}
				}
/* After one column the value of line is interchanged and prnt_even or prnt_odd is incremented accordingly */
				if(line==0){
					prnt_odd++;
					line=1;
				}
				else{
					prnt_even++;
					line=0;
				}
			nos--;  //no of space decremented by 1
			printf(&quot;\n&quot;);
		}
	nos = 1;
	/* Second half of the diamond. This one skips its first row rest goes pretty much the same */
	for (i = 7; i &gt;= 1; (i = i - 2)) {
		for (s = nos; s &gt;= 1; s--) {
			printf(&quot;  &quot;);
		}
		for (j = 1; j &lt;= i; j++) {
			if(line==0){
				if ((i % 2) != 0 &amp;&amp; (j % 2) != 0) {
					printf(&quot;%2c&quot;, prnt_odd);
					} else {
						printf(&quot;  &quot;);
					}
				}
				else{
					if ((i % 2) != 0 &amp;&amp; (j % 2) != 0) {
						printf(&quot;%2c&quot;, prnt_even);
						} else {
							printf(&quot;  &quot;);
						}
					}
				}
				if(line==0){
					prnt_odd++;
					line=1;
				}
				else{
					prnt_even++;
					line=0;
				}
				nos++;  //no of spaces is incremented by one
				printf(&quot;\n&quot;);
			}
			return 0;
		}
</pre>
<p><strong>Explanation:</strong><br />
This is a diamond formation where all the even places are hollowed. In case of odd row no a capital letter is printed and a small case alphabet otherwise. Both the capital letters n the small letters increment by one starting from &#8216;A&#8217; or &#8216;a&#8217;.<br />
<a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question3 --><!-- Start of Question4 --><a name="ques4"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre> A
   Z
 B   C
   Y   X
 D   E   F
   W   V   U
 G   H   I   J
   T   S   R   Q
 K   L   M   N   O</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
int main() {
	char prnt_odd = 'A';
	char prnt_even = 'Z';
	int i, j;
	// The right angle has nine rows
	for (i = 1; i &lt;= 9; i++) {
		for (j = 1; j &lt;= i; j++) {
			//checks for the column printing restrictions..
			if ((i % 2) != 0 &amp;&amp; (j % 2) != 0) {
				printf(&quot;%2c&quot;, prnt_odd);
				prnt_odd++;
			} else if ((i % 2) == 0 &amp;&amp; (j % 2) == 0) {
				printf(&quot;%2c&quot;, prnt_even);
				prnt_even--;
			} else {
				printf(&quot;  &quot;);
			}
		}
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><strong>Explanation:</strong><br />
This is a right angle triangle of alphabets, having two sets of alphabets. One for the odd rows which starts from &#8216;A&#8217; and increments by one. And another one for the even rows which starts from &#8216;Z&#8217; and decrements by 1. The restriction in the columns are: if the row no is odd only the odd places are printed else if the row no is even only the even places are filled, rest are skipped.<br />
<a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question4 --><!-- Start of Question5 --><a name="ques5"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre> A   B   C   D   E
   Z   Y   X   W
 A   B   C   D
   Z   Y   X
 A   B   C
   Z   Y
 A   B
   Z
 A
   Z
 A   B
   Z   Y
 A   B   C
   Z   Y   X
 A   B   C   D
   Z   Y   X   W
 A   B   C   D   E</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
//This prints the columns with guidance of the row number supplied.
int triangle(int i)
{
	char prnt_odd = 'A';
	char prnt_even= 'Z';
	int j;
	for(j=1; j&lt;=i; j++)
	{
		//Checks for the restrictions
		if((i%2)!=0 &amp;&amp; (j%2)!=0)
		{
			printf(&quot;%2c&quot;,prnt_odd);
			prnt_odd++;
		}
		else if((i%2)==0 &amp;&amp; (j%2)==0)
		{
			printf(&quot;%2c&quot;,prnt_even);
			prnt_even--;
		}
		else
		{
			printf(&quot;  &quot;);
		}
	}
	return 0;
}

int main()
{
	int i;
	//Prints the first right angle triangle with 9 rows
	for(i=9; i&gt;=1; i--)
	{
		triangle(i);
		printf(&quot;\n&quot;);
	}
	// This prints the second right angle triangle skipping its tip.
	for(i=2; i&lt;=9; i++)
	{
		triangle(i);
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><strong>Explanation:</strong><br />
This pattern can be seen as two right angle triangles of alphabets, having the same tip with two sets of alphabets. One for the odd rows which starts from &#8216;A&#8217; and increments by one. And another one for the even rows which starts from &#8216;Z&#8217; and decrements by 1. The restriction in the columns are: if the row number is odd, only the odd places are printed. Else if the row number is even only the even places are filled, rest are skipped.<br />
<a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question5 --><!-- Start of Question6 --><a name="ques6"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>A B C D E D C B A
A B C D   D C B A
A B C       C B A
A B           B A
A               A
A B           B A
A B C       C B A
A B C D   D C B A
A B C D E D C B A</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
/*
 nos   = Number of Spaces required.
 i     = Number of columns
 skip  = The row in which we have to skip the first character
 form  = Whether the alphabets has to printed in Incremental or Decremental order
 */
int triangle(int nos, int i, int skip, int form) {
	char prnt[] = &quot;ABCDE&quot;;
	int j, s;
	for (s = nos; s &gt;= 1; s--) {   // Controls the required spacings
		printf(&quot;  &quot;);
	}
	for (j = 0; j &lt;= i; j++) {     // prints the characters row wise
		if (skip != 1) {           // If we need to skip a row.
			if (i == skip &amp;&amp; j == 0) {
				continue;
			}
		}
		if (form == 0) { /* Decides whether the characters are in incremental or decremental order. */
			printf(&quot;%2c&quot;, prnt[j]);
		} else {
			printf(&quot;%2c&quot;, prnt[i - j]);
		}
	}
	return 0;
}

int main() {
	int i, nos = -1;
	for (i = 4; i &gt;= 0; i--) {
		triangle(0, i, 1, 0);    // Prints the first triangle
		triangle(nos, i, 4, 1);  // Prints the second triangle
		nos = nos + 2;    // Spacing factor
		printf(&quot;\n&quot;);
	}
	nos = 5;
	for (i = 1; i &lt;= 4; i++) {
		triangle(0, i, 0, 0);   // Prints the third triangle
		triangle(nos, i, 4, 1); // Prints the fourth triangle
		nos = nos - 2;     //Spacing factor.
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><strong>Explanation:</strong><br />
This pattern can be treated as four individual right angle triangles composed of alphabets in a sequential manner.<br />
<a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question6 --><!-- Start of Question7 --><a name="ques7"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>           A
         B C D
       E F G H I
     J K L M N O P
   Q R S T U V W X Y
                     Z
                       Y X W V U T S R Q
                         P O N M L K J
                           I H G F E
                             D C B
                               A</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
int main() {
	char prnt = 'A';
	int i, j, s, nos = 5;
	for (i = 1; i &lt;= 9; (i = i + 2)) {
		for (s = nos; s &gt;= 1; s--) {
			printf(&quot;  &quot;);
		}
		for (j = 1; j &lt;= i; j++) {
			printf(&quot;%2c&quot;, prnt);
			++prnt;           //Increments the alphabet
			if (i == 9 &amp;&amp; j == 9) //for the extra Z
			{
				printf(&quot;\f%2c&quot;, prnt);
			}
		}
		nos--;
		//for the continuation
		if (i != 9) {
			printf(&quot;\n&quot;);
		}
	}
	printf(&quot;\f&quot;); //Line Feed
	for (i = 9; i &gt;= 1; (i = i - 2)) {
		//Maintaining the required Spaces
		if (i == 9) {
			nos = 0;
		} else if (i == 7) {
			nos = 12;
		}
		for (s = nos; s &gt;= 1; s--) {
			printf(&quot;  &quot;);
		}
		for (j = 1; j &lt;= i; j++) {
			--prnt;   //decrements the alphabet before printing it.
			printf(&quot;%2c&quot;, prnt);
		}
		nos++;
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><strong>Explanation:</strong><br />
This can be seen as two isosceles triangle made up of alphabets incrementing by one starting from &#8216;A&#8217;. The connecting Z can be considered additionally.<br />
<a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question7 --><!-- Start of Question8 --><a name="ques8"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>N     N
N N   N
N   N N
N     N</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
int main() {
	char prnt = 'N';
	int i, j, k;
	for (i = 1; i &lt;= 4; i++) {
		//First right-angle triangle.
		for (j = 1; j &lt;= i; j++) {
			if (j == 1 || j == i) {    // This Ensures an empty baseless triangle
				printf(&quot;%2c&quot;, prnt);   //Prints the character after 2 spaces.
			} else {
				printf(&quot;  &quot;);
			}
		}
		//Second right-angle triangle
		for (k = 3; k &gt;= i; k--) {
			if (k == i) {    // k=0 has to skipped as it shares the same hypotenuse
				if (i == 4) { //For the joining point
					break;
				}
				printf(&quot;%2c&quot;, prnt);
			} else {
				printf(&quot;  &quot;);
			}
		}
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><strong>Explanation:</strong><br />
This pattern can be viewed as two baseless right-angle triangles arranged in such a way that they share a common hypotenuse.<br />
<a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question8 --><!-- Start of Question9 --><a name="ques9"> </a><span class="ques"> </span></p>
<li>Write a C program to print the following pattern:
<pre> X               X
   X           X
     X       X
       X   X
         X
       X   X
     X       X
   X           X
 X               X</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;
int main() {
	char prnt = 'X';
	int i, j, s, nos = 0;  //nos controls the spacing.
	//1st triangle
	for (i = 9; i &gt;= 1; (i = i - 2)) {
		for (s = nos; s &gt;= 1; s--) {  //Spacing control
			printf(&quot;  &quot;);
		}
		for (j = 1; j &lt;= i; j++) {
			if (j == 1 || j == i) { //This hollows the triangle
				printf(&quot;%2c&quot;, prnt);
			} else {
				printf(&quot;  &quot;);
			}
		}
		printf(&quot;\n&quot;);
		nos++;  // In the upper triangle the space increments by 1.
	}
/* Since both the triangles have the same peak point, while printing the second triangle we'll ignore its peak point. Thus i starts from 3 and the nos from 3.*/
	nos = 3;
	for (i = 3; i &lt;= 9; (i = i + 2)) {
		for (s = nos; s &gt;= 1; s--) {
			printf(&quot;  &quot;);
		}
		for (j = 1; j &lt;= i; j++) {
			if (j == 1 || j == i) {
				printf(&quot;%2c&quot;, prnt);
			} else {
				printf(&quot;  &quot;);
			}
		}
		printf(&quot;\n&quot;);
		nos--; //The spaces are in a decrementing order.
	}
	return 0;
}
</pre>
<p><a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question9 --><!-- Start of Question10 --><a name="ques10"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>K             K
K           K
K         K
K       K
K     K
K   K
K K
K
K K
K   K
K     K
K       K
K         K
K           K
K             K</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
int main() {
	char prnt = 'K';
	int i, j;
	//First right-angle triangle
	for (i = 7; i &gt;= 1; i--) {
		for (j = 1; j &lt;= i; j++) {
			if (j == 1 || j == i) {  //This hollows the triangle
				printf(&quot;%2c&quot;, prnt);
			} else {
				printf(&quot;  &quot;);
			}
		}
		if (i != 1) {
			printf(&quot;\n&quot;);
		}
	}
	// Second triangle
	for (i = 1; i &lt;= 7; i++) {
		for (j = 1; j &lt;= i; j++) {
			if (j == 1 || j == i) {
				printf(&quot;%2c&quot;, prnt);
			} else {
				printf(&quot;  &quot;);
			}
		}
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><strong>Explanation:</strong><br />
This pattern can be be seen as an inverted right triangle placed over a normal right-angled triangle. In addition to the triangles, there is an extra character in the intersection point.</p>
<p>Since both the triangles share the same tip and in the figure and we have an extra character in the point of intersection, we can start the second right angle triangle from the same line where the first one ends.<br />
<a style="font-size: 0.8em;" href="#top">Back to top</a><br />
<!-- End of Question10 --></ol>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2010/01/10-challenging-char-pattern-programs.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
