RSS Subscribe

Sunday 26 June 2011

C++ Sample Technical Questions

Here are some sample C sums for a successful job interviewing.

 

 1.

Arithmetic and Assignment Operators

C++Purpose
x++Postincrement
++xPreincrement
x--Postdecrement
--xPredecrement
+xUnary plus
-xUnary minus
x*yMultiply
x/yDivide
x%yModulus
x+yAdd
x-ySubtract
pow(x,y) or  TMath::Power(x,y)Exponation
x = yAssignment
x += y (-=,*=,/=,%=,...)Updating assignment
 
  
 2.When we use void return type?

We use it, when the method need not to return a value.
A void return type indicates that a method does not return a value
 
  
 3.What is an loop Statement?

A Loop statement is one which execute a certain set of statement repeatedly until certain condition satisfied.
For example::
int i=0;
do{
cout << "hai"; 
i=i++;
}while(i<5)
O/P:: hai
hai
hai
hai
hai
  
 4.What do you mean by Swapping variables ? Swapping means the value of two or more variable is interchanging.
For Example::
int i=india;
int j=china;
int temp = i;
i = j;
j = temp;
cout << i;
Output:: china
  
 6.What is a class? 

A class is a programming language construct that is used to group related instant variables and methods.
  
 7.What is an object?
In OOP , an object is an instantiation (implementation) of a class. 
  
 8.What is the difference between an object and a class?
A Class is constant one. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change
Objects are created during execution and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.
  
9.Differnce between public and Private?
Private::Declaring a variable or member function private means that the variable & the function can be accessed only within the class or subclass.
Public::Declaring a variable or member function public means that the variable & the function can be accessed from any where in the code.
Private::Declaring a variable or member function private means that the variable & the function can be accessed only within the class or subclass.

10Princeples of OOPS?
The 4 principles of OOPS are Abstraction, Ineheritance, Polymorhpism and Encapsulation. Method Overloading and Overriding are a part of the Polymorphism
  
11What is abstraction? 
Abstraction refers to the act of representing essential features without including the background details.
  
12void 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".
  
13main()
{
char s[ ]="hai";
int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Answer:
hhhh
aaaa
iiii
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].
  
14main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d %d %d %d %d",i,j,k,l,m);
}
Answer:
0 0 1 3 1
Explanation :
Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR (||) operator. So the expression ‘i++ && j++ && k++’ is executed first. The result of this expression is 0 (-1 && -1 && 0 = 0). Now the expression is 0 || 2 which evaluates to 1 (because OR operator always gives 1 except for ‘0 || 0’ combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1.
  
15Difference between function overloading & operator overloading? 
Function overloading: C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters (at least as far as their types are concerned). This capability is called function overloading. When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of the arguments in the call. Function overloading is commonly used to create several functions of the same name that perform similar tasks but on different data types. 

Operator overloading allows existing C++ operators to be redefined so that they work on objects of user-defined classes. Overloaded operators are syntactic sugar for equivalent function calls. They form a pleasant facade that doesn't add anything fundamental to the language (but they can improve understandability and reduce maintenance costs).
  
16Diffrence between Realloc() and Free()? 
Realloc() is used to reallocate the memory for variable
Free() is used to free the allocated memory of a variable

1 comments:

Thanks for sharing, I will bookmark and be back again


Fresher Jobs in India

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More