Monday, December 1, 2008

Technical Placement C++ Question with Solution

1. void main()
{
int a, *pa, &ra;
pa = &a;
ra = a;
}

/*
Answer : 
Compiler Error: 'ra',reference must be initialized

Explanation : 
Pointers are different from references. One of the main 
differences is that the pointers can be both initialized and assigned,
whereas references can only be initialized. So this code issues an error.
*/

2.
const int size = 5;
void print(int *ptr)
{
cout<
}

void print(int ptr[size])
{
cout<
}

void main()
{
int a[size] = {1,2,3,4,5};
int *b = new int(size);
print(a);
print(b);
}
/*
Answer:
Compiler Error : function 'void print(int *)' already has a body

Explanation:
Arrays cannot be passed to functions, only pointers (for arrays, base addresses) 
can be passed. So the arguments int *ptr and int prt[size] have no difference  
as function arguments. In other words, both the functoins have the same signature and
so cannot be overloaded. 
*/

3.class some{
public:
~some()
{
cout<<"some's destructor"<
}
};

void main()
{
some s;
s.~some();
}
/*

Answer:
some's destructor
some's destructor
Explanation:
Destructors can be called explicitly. Here 's.~some()' explicitly calls the 
destructor of 's'. When main() returns, destructor of s is called again,
hence the result.
*/

4.
#include

class fig2d
{
int dim1;
int dim2;

public:
fig2d() { dim1=5; dim2=6;}

virtual void operator<<(ostream & rhs);
};

void fig2d::operator<<(ostream &rhs)
{
rhs <dim1<<" "<dim2<<" ";
}

/*class fig3d : public fig2d
{
int dim3;
public:
fig3d() { dim3=7;}
virtual void operator<<(ostream &rhs);
};
void fig3d::operator<<(ostream &rhs)
{
fig2d::operator <<(rhs);
rhs<dim3;
}
*/

void main()
{
fig2d obj1;
// fig3d obj2;

obj1 <<>
// obj2 <<>
}
/*

Answer : 
5 6 
Explanation:
In this program, the <<>
This enables the 'cout' to be present at the right-hand-side. Normally, 'cout' 
is implemented as global function, but it doesn't mean that 'cout' is not possible 
to be overloaded as member function.
    Overloading <<>
it is overloaded is inherited, and this becomes available to be overrided. This is as opposed 
to global friend functions, where friend's are not inherited.
*/

6. class opOverload{
public:
bool operator==(opOverload temp);
};

bool opOverload::operator==(opOverload temp){
if(*this  == temp ){
cout<<"The both are same objects\n";
return true;
}
else{
cout<<"The both are different\n";
return false;
}
}

void main(){
opOverload a1, a2;
a1= =a2;
}

Answer : 
Runtime Error: Stack Overflow

Explanation :
Just like normal functions, operator functions can be called recursively. This program just illustrates that point, by calling the operator == function recursively, leading to an infinite loop. 


7.class complex{
double re;
double im;
public:
complex() : re(1),im(0.5) {}
bool operator==(complex &rhs);
operator int(){}
};

bool complex::operator == (complex &rhs){
if((this->re == rhs.re) && (this->im == rhs.im))
return true;
else
return false;
}

int main(){
complex  c1;
cout<<  c1;
}

Answer : Garbage value

Explanation:
The programmer wishes to print the complex object using output
re-direction operator,which he has not defined for his lass.But the compiler instead of giving an error sees the conversion function
and converts the user defined object to standard object and prints
some garbage value.

c++ placement interview Question with solution

1.class complex{
double re;
double im;
public:
complex() : re(0),im(0) {}
complex(double n) { re=n,im=n;};
complex(int m,int n) { re=m,im=n;}
void print() { cout<<
};

void main(){
complex c3;
double i=5;
c3 = i;
c3.print();
}

Answer: 
5,5 

Explanation:
Though no operator= function taking complex, double is defined, the double on the rhs is converted into a temporary object using the single argument constructor taking double and assigned to the lvalue.


2.void main()
{
int a, *pa, &ra;
pa = &a;
ra = a;
}

Answer : 
Compiler Error: 'ra',reference must be initialized

Explanation : 
Pointers are different from references. One of the main 
differences is that the pointers can be both initialized and assigned,
whereas references can only be initialized. So this code issues an error.

placement c++ programming language Question

1) Determine the output of the 'C++' Codelet.
class base
{  
public : 
out() 
cout<<"base ";  
}  
};
class deri{ 
public : out() 
cout<<"deri "; 
}   
};
void main()
{ deri dp[3];
base *bp = (base*)dp;
for (int i=0; i<3;i++)
(bp++)->out();
}


2) Justify the use of virtual constructors and destructors in C++.


3) Each C++ object possesses the 4 member fns,(which can be declared by the programmer explicitly or by the implementation if they are not available). What are those 4 functions?


4) What is wrong with this class declaration?
class something
{
char *str;
public:
  something(){
  st = new char[10]; }
 ~something()
 {
  delete str; 
 }
};

5) Inheritance is also known as -------- relationship. Containership as   ________ relationship.

6)  When is it necessary to use member-wise initialization list  (also known as header initialization list) in C++?

7) Which is the only operator in C++ which can be overloaded but NOT inherited.

8) Is there anything wrong with this C++ class declaration?
class temp
{
 int value1; 
 mutable int value2;
 public :
  void fun(int val) 
const{
  ((temp*) this)->value1 = 10;
  value2 = 10;
  }
};
 

Technical /logical interview questions and solution in C language

1.)
void main()
  {
   int  const * p=5;
   printf("%d",++(*p));
  }

Answer:
Compiler error: Cannot modify a constant value. 

Explanation:-    
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".

2.)
main()
   {
    char s[ ]="man";
    int i;
    for(i=0;s[ i ];i++)
    printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
   }

Answer:
             mmmm
                aaaa
                nnnn

Explanation:-

s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally  array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the  case of  C  it is same as s[i].

3.)
main()
  {
   float me = 1.1;
   double you = 1.1;
   if(me==you)
   printf("I love U");
   else
   printf("I hate U");
  }

Answer: 

I hate U


Explanation:-
For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value  represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.
Rule of Thumb: 
Never compare or at-least be cautious when using floating point numbers with relational operators (== , >, <, <=, >=,!= ) .  

4.)
main()
   {
   static int var = 5;
   printf("%d ",var--);
   if(var)
   main();
   }

Answer:
5 4 3 2 1
        
Explanation:
When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.  

5.)
main()
  {
   int c[ ]={2.8,3.4,4,6.7,5};
   int j,*p=c,*q=c;
   for(j=0;j<5;j++)>
   printf(" %d ",*c);
   ++q;  
  }
  for(j=0;j<5;j++)
  {
   printf(" %d ",*p);
   ++p;  
 }
}

Answer:
             2 2 2 2 2 2 3 4 6 5
  
Explanation: 
Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed. 

ALL PLACEMENT TECHNICAL INTERVIEW C PROGRAMMING LANGUAGE QUESTION  BANCK   

1Technical /logical interview questions and solution in C language |2Technical / logical interview questions in C language |3C programming Language Technical Questions And Answers |4C Language Technical Questions And Answers |5C Language Technical Interview Questions And solution |6C Language Technical Interview Questions And Answers |7C Language Interview Questions And Solution |8online Placement Question of c programming languge |9online placement c programming question|10online test of c programming languge|11placement c programming quiz with solution |12c programming quiz with solution |13C programming languge quiz with solution |14C quiz with solution |15C languge quiz with solution  |16C programming quiz with solution |17questions with answer in C programming language|18Technical questions with solution in C programming language  |19Technical questions with answer in C programming language |20placement Question and answer in Simple C Language |21Simple C Language placement Question and answer22Simple C programming Question and answer |23Simple C programming Question and answer |24Simple C Language Question and answer |25c progrmming Interview question with solution  |26Technical placement question with solution in c progrmming |27placement c progrmming Question with Answer  |28c progrmming placement question with solution  |29 placement c progrmming languge question with Answer |30placement c progrmming languge question with solution  |31c progrmming languge placement question with solution  |32c languge placement question with solution |33placement c programming languge problem with solution  |34c programming languge placement problem with solution  |35c language placement problem with solution  |36Placement c languge problem with solution  |37c language problem with answer  |38c language problem with solution  |


My Blog List