Earn upto Rs. 9,000 pm checking Emails. Join now!

Enter your email address:

Delivered by FeedBurner

Sunday, January 13, 2013

Technical Interview Questions - important for TCS, Wipro, Cognizent, Accenture, IBM




Technical Interview Questions - important for TCS, Wipro, Cognizent, Accenture, IBM


  • What does static variable mean?
A static variable is a special variable that is stored in the data segment unlike the default automatic variable that is stored in stack. A static variable can be initialised by using keyword static before variable name.
For Example:
static int a = 5;
A static variable behaves in a different manner depending upon whether it is a global variable or a local variable. A static global variable is same as an ordinary global variable except that it cannot be accessed by other files in the same program / project even with the use of keyword extern. A static local variable is different from local variable. It is initialised only once no matter how many times that function in which it resides is called. It may be used as a count variable.
Example:
#include 
//program in file f1.c
void count(void) {
 static int count1 = 0;
 int count2 = 0;
 count1++;
 count2++;
 printf("\nValue of count1 is %d, Value of count2 is %d", count1, count2);
}
 
/*Main function*/
int main(){
 count();
 count();
 count();
 return 0;
}
Output:
Value of count1 is 1, Value of count2 is 1
Value of count1 is 2, Value of count2 is 1
Value of count1 is 3, Value of count2 is 1



  • What is a pointer?
·  What is a pointer?
A pointer is a special variable in C language meant just to store address of any other variable or function. Pointer variables unlike ordinary variables cannot be operated with all the arithmetic operations such as ‘*’,'%’ operators.
It follows a special arithmetic called as pointer arithmetic.
A pointer is declared as:
int *ap;
int a = 5;
In the above two statements an integer a was declared and initialized to 5. A pointer to an integer with name ap was declared.
Next before ap is used
ap=&a;
This operation would initialize the declared pointer to int. The pointer ap is now said to point to a.
Operations on a pointer:
  • Dereferencing operator ‘ * ‘:
This operator gives the value at the address pointed by the pointer . For example after the above C statements if we give
printf("%d",*ap);
Actual value of a that is 5 would be printed. That is because ap points to a.
  • Addition operator ‘ + ‘:
Pointer arithmetic is different from ordinary arithmetic.
ap=ap+1;
Above expression would not increment the value of ap by one, but would increment it by the number of bytes of the data type it is pointing to. Here ap is pointing to an integer variable hence ap is incremented by 2 or 4 bytes depending upon the compiler.
A pointer is a special variable in C language meant just to store address of any other variable or function. Pointer variables unlike ordinary variables cannot be operated with all the arithmetic operations such as ‘*’,'%’ operators. It follows a special arithmetic called as pointer arithmetic.
A pointer is declared as:
int *ap;
int a = 5;
In the above two statements an integer a was declared and initialized to 5. A pointer to an integer with name ap was declared.
Next before ap is used
ap=&a;
This operation would initialize the declared pointer to int. The pointer ap is now said to point to a.

  • What is a structure?
A structure is a collection of pre-defined data types to create a user-defined data type. Let us say we need to create records of students. Each student has three fields:
int roll_number;
char name[30];
int total_marks;
This concept would be particularly useful in grouping data types. You could declare a structure student as:
struct student {
 int roll_number;
 char name[30];
 int total_marks;
} student1, student2;
The above snippet of code would declare a structure by name student and it initializes two objects student1, student2. Now these objects and their fields could be accessed by saying student1. style='font-size:10.0pt;font-family:"Courier New";color:#0000C0'>roll_number for accesing roll number field of student1 object, similarly student2. style='font-size:10.0pt;font-family:"Courier New";color:#0000C0'>name for accesing name field of student2 object.

  • What are the differences between structures and arrays?
 following are the differences between structures and arrays:
- Array elements are homogeneous. Structure elements are of different data type.
- Array allocates static memory and uses index / subscript for accessing elements of the array. Structures allocate dynamic memory and uses (.) operator for accessing the member of a structure.
- Array is a pointer to the first element of it. Structure is not a pointer
- Array element access takes less time in comparison with structures.    
  • In header files whether functions are declared or defined?

  • What are the differences between malloc() and calloc()?
  • What are macros? what are its advantages and disadvantages?
  • Difference between pass by reference and pass by value?
  • What is static identifier?
  • Where are the auto variables stored?
  • Where does global, static, local, register variables, free memory and C Program instructions get stored?
  • Difference between arrays and linked list?
  • What are enumerations?
  • What is a class?
  • What is an object?
  • What is the difference between an object and a class?
  • What is the difference between class and structure?
  • What is public, protected, private?
  • What are virtual functions?
  • What is friend function?
  • What is a scope resolution operator?
  • What do you mean by inheritance?
  • What is abstraction?
  • What is a data structure?
  • What does abstract data type means?
  • Evaluate the following prefix expression " ++ 26 + - 1324" (Similar types can be asked)
  • Convert the following infix expression to post fix notation ((a+2)*(b+4)) -1 (Similar types can be asked)
  • How is it possible to insert different type of elements in stack?
  • Stack can be described as a pointer. Explain.
  • Write a Binary Search program
  • What is the difference between an Abstract class and Interface?
  • What is user defined exception?
  • What do you know about the garbage collector?
  • What is the difference between java and c++?
  • In an HTML form I have a button which makes us to open another page in 15 seconds. How will you do that?
  • What is the difference between process and threads?
  • What is update method called?
  • Have you ever used HashTable and Directory?
  • What are statements in Java?
  • What is RMI?
  • Explain about RMI Architecture?
  • What are Servelets?
  • What is the use of servlets?
  • Explain RMI Architecture?
  • How will you pass values from HTML page to the servlet?
  • How do you load an image in a Servelet?
  • What is purpose of applet programming?
  • How will you communicate between two applets?
  • What are the basic functions of an operating system?
  • Explain briefly about, processor, assembler, compiler, loader, linker and the functions executed by them.
  • What are the difference phases of software development? Explain briefly?
  • Differentiate between RAM and ROM?
  • What is DRAM? In which form does it store data?
  • What is cache memory?
  • What is hard disk and what is its purpose?
  • Differentiate between Complier and Interpreter?
  • What are the different tasks of Lexical analysis?
  • What are the different functions of Syntax phase, Sheduler?


·  What is recursion? Write a program using recursion (factorial)?
Recursion: A function is called ‘recursive’ if a statement within the body of a function calls the same function. It is also called ‘circular definition’. Recursion is thus a process of defining something in terms of itself.
Program: To calculate the factorial value using recursion.
#include
int fact(int n);

int main() {
 int x, i;
 printf("Enter a value for x: \n");
 scanf("%d", &x);
 i = fact(x);
 printf("\nFactorial of %d is %d", x, i);
 return 0;
}

int fact(int n) {
 /* n=0 indicates a terminating condition */
 if (n <= 0) {
  return (1);
 } else {
  /* function calling itself */
  return (n * fact(n - 1));
  /*n*fact(n-1) is a recursive expression */
 }
}
Output:
Enter a value for x:
4
Factorial of 4 is 24
Explanation:
fact(n) = n * fact(n-1)
       If n=4
       fact(4) = 4 * fact(3)  there is a call to fact(3)
       fact(3) = 3 * fact(2)
       fact(2) = 2 * fact(1)
       fact(1) = 1 * fact(0)
       fact(0) = 1

       fact(1) = 1 * 1 = 1
       fact(2) = 2 * 1 = 2
       fact(3) = 3 * 2 = 6
       Thus fact(4) = 4 * 6 = 24
Terminating condition(n <= 0 here;) is a must for a recursive program. Otherwise the program enters into an infinite loop.

·  To which numbering system, can the binary number 1101100100111100 be easily converted to?
1101100100111100 can be easily converted to hexadecimal numbering system. Hexa-decimal integer constants consist of combination of digits from 0 to 9 and alphabets ‘A’ to ‘F’. The alphabets represent numbers 10 to 15 respectively. Hexa-decimal numbers are preceeded by ’0x’.
1101,1001,0011,1100
      1101 = D
      1001 = 9
      0011 = 3
      1100 = C
1101,1001,0011,1100 = 0xD93C
Thus the given binary number 1101100100111100 in hexadecimal form is 0xD93C

·  What are the differences between structures and unions?
Structures and Unions are used to store members of different data types.
STRUCTURE
UNION
a)Declaration:
struct
  {
   data type member1;
   data type member2;
  };
a)Declaration:
union
  {
   data type member1;
   data type member2;
  };
b)Every structure member is allocated memory when a structure variable is defined.
Example:
struct emp {
 char name[5];
 int age;
 float sal;
};

struct emp e1;
Memory allocated for structure is 5+4+4=13 bytes(assuming sizeof int is 4, float is 4, char is 1). 5 byte for name, 4 bytes for age and 4 bytes for sal.
b)The memory equivalent to the largest item is allocated commonly for all members.
Example:
union emp1 {
 char name[5];
 int age;
 float sal;
};

union emp1 e2;
Memory allocated to a union is equal to size of the largest member. In this case, char name[5] is the largest-sized member. Hence memory allocated to this union is 5 bytes.
c)All structure variables can be initialized at a time
struct st {
 int a;
 float b;
};
struct st s = { .a=4, .b=10.5 };
Structure is used when all members are to be independently used in a program.
c)Only one union member can be initialized at a time
union un {
 int a;
 float b;
};

union un un1 = { .a=10 };
Union is used when members of it are not required to be accessed at the same time.
·  What are the advantages of using unions?
Union is a collection of data items of different data types.
It can hold data of only one member at a time though it has members of different data types.
If a union has two members of different data types, they are allocated the same memory. The memory allocated is equal to maximum size of the members. The data is interpreted in bytes depending on which member is being accessed.
Example:
union pen {
 char name;
 float point;
};
Here name and point are union members. Out of these two variables, ‘point’ is larger variable which is of float data type and it would need 4 bytes of memory. Therefore 4 bytes space is allocated for both the variables. Both the variables have the same memory location. They are accessed according to their type.
Union is efficient when members of it are not required to be accessed at the same time.

·  What is scope & storage allocation of global and extern variables? Explain with an example
Extern variables: belong to the External storage class and are stored in the main memory. extern is used when we have to refer a function or variable that is implemented in other file in the same project. The scope of the extern variables is Global.
Example:
/***************
Index: f1.c
****************/
#include
extern int x;

int main() {
 printf("value of x %d", x);
 return 0;
}
/***************
Index: f2.c
****************/
int x = 3;
Here, the program written in file f1.c has the main function and reference to variable x. The file f2.c has the declaration of variable x. The compiler should know the datatype of x and this is done by extern definition.
Global variables: are variables which are declared above the main( ) function. These variables are accessible throughout the program. They can be accessed by all the functions in the program. Their default value is zero.
Example:
#include
int x = 0;
/* Variable x is a global variable.
It can be accessed throughout the program */
void increment(void) {
 x = x + 1;
 printf("\n value of x: %d", x);
}

int main(){
 printf("\n value of x: %d", x);
 increment();
 return 0;
}
·  What is scope & storage allocation of static, local and register variables? Explain with an example.
Register variables: belong to the register storage class and are stored in the CPU registers. The scope of the register variables is local to the block in which the variables are defined. The variables which are used for more number of times in a program are declared as register variables for faster access.
Example: loop counter variables.
register int y=6;
Static variables: Memory is allocated at the beginning of the program execution and it is reallocated only after the program terminates. The scope of the static variables is local to the block in which the variables are defined.
Example:
#include
void decrement(){
  static int a=5;
  a--;
  printf("Value of a:%d\n", a);
}

int main(){
 decrement();
 return 0;
}
Here ‘a’ is initialized only once. Every time this function is called, ‘a’ does not get initialized. so output would be 4 3 2 etc.,
Local variables: are variables which are declared within any function or a block. They can be accessed only by function or block in which they are declared. Their default value is a garbage value.
·  What is Pass by Value? Write a C program showing this concept.
Pass by Value: In this method, the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. In pass by value, the changes made to formal arguments in the called function have no effect on the values of actual arguments in the calling function.
Example:
#include

void swap(int x, int y) {
 int t;
 t = x;
 x = y;
 y = t;
}

int main() {
 int m = 10, n = 20;
 printf("Before executing swap m=%d n=%d\n", m, n);
 swap(m, n);
 printf("After executing swap m=%d n=%d\n", m, n);
 return 0;
}
Output:
Before executing swap m=10 n=20
After executing swap m=10 n=20
Explanation:
In the main function, value of variables m, n are not changed though they are passed to function ‘swap’. Swap function has a copy of m, n and hence it can not manipulate the actual value of arguments passed to it.

·  What is Pass by Reference? Write a C program showing this concept.
Pass by Reference: In this method, the addresses of actual arguments in the calling function are copied into formal arguments of the called function. This means that using these addresses, we would have an access to the actual arguments and hence we would be able to manipulate them. C does not support Call by reference. But it can be simulated using pointers.
Example:
#include
/* function definition */
void swap(int *x, int *y) {
 int t;
 t = *x; /* assign the value at address x to t */
 *x = *y; /* put the value at y into x */
 *y = t; /* put the value at to y */
}

int main() {
 int m = 10, n = 20;
 printf("Before executing swap m=%d n=%d\n", m, n);
 swap(&m, &n);
 printf("After executing swap m=%d n=%d\n", m, n);
 return 0;
}
Output:
Before executing swap m=10 n=20
After executing swap m=20 n=10
Explanation:
In the main function, address of variables m, n are sent as arguments to the function ‘swap’. As swap function has the access to address of the arguments, manipulation of passed arguments inside swap function would be directly reflected in the values of m, n.

·  What is an Enumeration?
Enumeration is a data type. We can create our own data type and define values that the variable can take. This can help in making program more readable. enum definition is similar to that of a structure.
Example: consider light_status as a data type. It can have two possible values – on or off.
enum light_status
{
  on, off
};

enum light_status bulb1, bulb2;
/* bulb1, bulb2 are the variables */
Declaration of enum has two parts:
a) First part declares the data type and specifies the possible values, called ‘enumerators’.
b) Second part declares the variables of this data type.
We can give values to these variables:
bulb1 = on;
bulb2 = off;

·  What is the use of typedef?
typedef declaration helps to make source code of a C program more readable. Its purpose is to redefine the name of an existing variable type. It provides a short and meaningful way to call a data type. typedef is useful when the name of the data type is long. Use of typedef can reduce length and complexity of data types.
Note: Usually uppercase letters are used to make it clear that we are dealing with our own data type.
Example:
struct employee {
 char name[20];
 int age;
};

struct employee e;
The above declaration of the structure would be easy to use when renamed using typedef as:
struct employee {
 char name[20];
 int age;
};

typedef struct employee EMP;
EMP e1, e2;
·  What are register variables? What are advantages of using register variables?
Register variables are stored in the CPU registers. Its default value is a garbage value. Scope of a register variable is local to the block in which it is defined. Lifetime is till control remains within the block in which the register variable is defined. Variable stored in a CPU register can always be accessed faster than the one that is stored in memory. Therefore, if a variable is used at many places in a program, it is better to declare its storage class as register
Example:
register int x=5;
Variables for loop counters can be declared as register. Note that register keyword may be ignored by some compilers.
·  What are storage memory, default value, scope and life of Automatic and Register storage class?
  1. Automatic storage class:
Storage : main memory.
Default value : garbage value.
Scope : local to the block in which the variable is defined.
Lifetime : till control remains within the block.
  1. Register storage class:
Storage : cpu registers.
Default value : garbage value.
Scope : local to the block in which the variable is defined.
Lifetime : till control remains within the block.
·  What are storage memory, default value, scope and life of Static and External storage class?
  1. Static storage class:
Storage : main memory
Default value : zero
Scope : local to the block in which the variable is defined
lifetime : till the value of the variable persists between different function calls.
  1. External storage class:
Storage : main memory
Default value : zero
Scope : global
Lifetime : as long as the program execution doesn’t come to an end.
·  What are the advantages of using pointers in a program?
  1. Pointers allow us to pass values to functions using call by reference. This is useful when large sized arrays are passed as arguments to functions. A function can return more than one value by using call by reference.
  2. Dynamic allocation of memory is possible with the help of pointers.
  3. We can resize data structures. For instance, if an array’s memory is fixed, it cannot be resized. But in case of an array whose memory is created out of malloc can be resized.
  4. Pointers point to physical memory and allow quicker access to data.
Pointers are special variables which store address of some other variables.
Syntax: datatype *ptr;
Here * indicates that ptr is a pointer variable which represents value stored at a particular address.
Example: int *p;
‘p’ is a pointer variable pointing to address location where an integer type is stored.
Advantages:
·  Which bitwise operator is suitable for checking whether a particular bit is ON or OFF?
Bitwise AND operator.
Example: Suppose in byte that has a value 10101101 . We wish to check whether bit number 3 is ON (1) or OFF (0) . Since we want to check the bit number 3, the second operand for AND operation we choose is binary 00001000, which is equal to 8 in decimal.
Explanation:
ANDing  operation :

         10101101       original bit pattern
         00001000       AND mask
        ---------
         00001000       resulting bit pattern
        ---------
The resulting value we get in this case is 8, i.e. the value of the second operand. The result turned out to be a 8 since the third bit of operand was ON. Had it been OFF, the bit number 3 in the resulting bit pattern would have evaluated to 0 and complete bit pattern would have been 00000000. Thus depending upon the bit number to be checked in the first operand we decide the second operand, and on ANDing these two operands the result decides whether the bit was ON or OFF.
·  Which bit wise operator is suitable for turning OFF a particular bit in a number?
Bitwise AND operator (&), one’s complement operator(~)
Example: To unset the 4th bit of byte_data or to turn off a particular bit in a number.
Explanation:
Consider,
char byte_data= 0b00010111;
byte_data= (byte_data)&(~(1<<4 o:p="o:p">
1 can be represented in binary as 0b00000001 = (1<<4 o:p="o:p">

<< is a left bit shift operator,
it shifts the bit 1 by 4 places towards left.

(1<<4 0b00010000="0b00010000" becomes="becomes" nbsp="nbsp" o:p="o:p">

And ~ is the one's complement operator in C language.
So ~(1<<4 0b00010000="0b00010000" complement="complement" nbsp="nbsp" o:p="o:p" of="of">
                 = 0b11101111

Replacing value of byte_data and  ~(1<<4 in="in" o:p="o:p">
 (byte_data)&(~(1<<4 o:p="o:p">
we get (0b00010111) & (0b11101111)

Perform AND operation to below bytes.
                         00010111
                         11101111
                        -----------
                         00000111
                        -----------

Thus the 4th bit is unset.

Define macros. What are the advantages and disadvantages of Macros?

A macro is a name given to a block of C statements as a pre-processor directive. Being a pre-processor, the block of code is communicated to the compiler before entering into the actual coding (main () function). A macro is defined with the preprocessor directive, #define.
The advantage of using macro is the execution speed of the program fragment. When the actual code snippet is to be used, it can be substituted by the name of the macro. The same block of statements, on the other hand, need to be repeatedly hard coded as and when required.
The disadvantage of the macro is the size of the program. The reason is, the pre-processor will replace all the macros in the program by its real definition prior to the compilation process of the program.     

Describe the advantages of using macro.

A macro is a name given to a block of the code which can be substituted where the code snippet is to be used for more than once.
- The speed of the execution of the program is the major advantage of using a macro.
- It saves a lot of time that is spent by the compiler for invoking / calling the functions.
- It reduces the length of the program.

List out differences between pass by reference and pass by value
Pass by value always invokes / calls the function or returns a value that is based on the value. This value is passed as a constant or a variable with value.
Pass by reference always invokes / calls the function by passing the address or a pointer to a memory location which contains the value. The memory location / pointer is populated with a value, so the function could look at the value through that location. The function can update the value available in the memory location by referencing the pointer.
A string in C language is passed by reference.     


Some questions relegated to general knowledge.


1. Which of the following awards is given for excellence in the field of Literature?
A)Dada Saheb Phalke Award   B)Shanti Swarup BhatnagarAward C)Padma BhushanD)Nirja Bhanot Award  E)Pulitzer Prize   Ans-E
2.  Which of the following countries is not a permanent member of UN Security Council?
A)China  B)India  C)Russia  D)France  E)U.S.A  Ans – B
3. India recently conducted joint naval exercise SLINEX II with which of the following countries?  A)Sri lanka  B)China  C)Japan  D)Bangladesh  E)Russia  Ans-A
4. As per the report by the U.S Agencies, which of the following countries was the largest purchaser of weapons in 2010?  A)China  B)India  C)Pakistan  D)Srilanka  E)Iran  Ans -B
5. Which of the following Social Welfare Schemes is launched by the Government of IndiaA)Mid-day Meal Scheme  B)Jeevan Asha  C)Jeevan Bharti  D)Look east policy  E)Bharat Nirman  Ans – A
6.Which of the following awards was given to Mrs. Nilima Mishra recently?
A)Pulitzer prize  B)Ramon Magsaysay  C)Nobel Peace prize   D)Golden pen award E)Kaliga Award  Ans- B
7. Mr. Mahinda Rajapaksa is the _____?  A)President of Srilanka  B)Prime Minister of Srilanka
C)President of Nepal  D)Prime Minister of Nepal  E)Foreign Minister of Fiji   Ans-A
8.Expand the term A L M __________?  A)Asset Liability Management  B) Asset Liability Maturity   C)Asset Liability Mismatch  D)Asset liability Manpower E)Asset Liability Maintenance  Ans-A
9.Which of the following is the overall female literacy rate in India as per recent census?
A)50%  B)60%   C)65%   D)70%   E)73%   Ans-C
10.What does the letter ‘L’ stands for in the term LAF commonly used?
A)Liquidity  B)Least   C)Liabilities  D)Long  E)Liquid   Ans -A
11.Thumba’ India’s Rocket Launching centre is located in _______?
A)Haryana  B)Maharashtra     C)Tamilnadu    D)Gujarat  E)Orissa   Ans- None of these
12. Mr. TonyTan is the New President of ________?
A)China  B)Japan  C)Singapore  D)Hongkong  E)NorthkoreaAns – C

13. Which one of the following is observed as ‘World Food Day’?
A)6th Nov  B)16thNov C)6thOct  D)16thOct  E)17thJan Ans- D
14. Which of the following books is written by Sunil Gavaskar?
A)A Brief History Of Time B)A Sence of Time C)Sunny Days  D)Half A Life E)Great Expectations
Ans –C
15.Who among the following players got Honorary Rank of Lt.Colonel in the Territorial Army of India ?
A)Rahul Dravid  B)Sachin Tendulkar C)Abhinav Bindra  D)Leander Paes  E)Gagan Narang
Ans-C




No comments:

 
Thanks

Total Pageviews