Social Icons

Showing posts with label c language. Show all posts
Showing posts with label c language. Show all posts

Sunday, March 31, 2013

C LANGUAGE INTERVIEW PAPERS ANSWERS & QUESTIONS



1. What does static variable mean?
Ans: Static variables are the variables which retain their values between the function calls. They are initialized only once their scope is within the function in which they are defined.

2. What is a pointer?
Ans: Pointers are variables which stores the address of another variable. That variable may be a scalar (including another pointer), or an aggregate (array or structure). The pointed-to object may be part of a larger object, such as a field of a structure or an element in an array.

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.

3. What are the uses of a pointer?
Ans: Pointer is used in the following cases
i) It is used to access array elements
ii) It is used for dynamic memory allocation.
iii) It is used in Call by reference
iv) It is used in data structures like trees, graph, linked list etc.

4. What is a structure?
Ans: Structure constitutes a super data type which represents several different data types in a single unit. A structure can be initialized if it is static or global.
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.

roll_number for accessing roll number field of student1 object, similarly student2.
name for accessing name field of student 2 object.

5. What is a union?
Ans: Union is a collection of heterogeneous data type but it uses efficient memory utilization technique by allocating enough memory to hold the largest member. Here a single area of memory contains values of different types at different time. A union can never be initialized.

6. What are the differences between structures and union?
Ans: A structure variable contains each of the named members, and its size is large enough to hold all the members. Structure elements are of same size.
A union contains one of the named members at a given time and is large enough to hold the largest member. Union element can be of different sizes.

7. What are the differences between structures and arrays?
Ans: Structure is a collection of heterogeneous data type but array is a collection of homogeneous data types.
Array
1-It is a collection of data items of same data type.
2-It has declaration only
3-.There is no keyword.
4- array name represent the address of the starting element.
Structure
1-It is a collection of data items of different data type.
2- It has declaration and definition
3- keyword struct is used
4-Structure name is known as tag it is the short hand notation of the declaration.

8. In header files whether functions are declared or defined?
Ans: Functions are declared within header file. That is function prototypes exist in a header file,not function bodies. They are defined in library (lib).

9. What are the differences between malloc () and calloc ()?
Ans: Malloc Calloc 1-Malloc takes one argument Malloc(a);where a number of bytes 2-memory allocated contains garbage values
1-Calloc takes two arguments Calloc(b,c) where b no of object and c size of object
2-It initializes the contains of block of memory to zerosMalloc takes one argument, memory allocated contains garbage values.
It allocates contiguous memory locations. Calloc takes two arguments, memory allocated contains all zeros, and the memory allocated is not contiguous.

10. What are macros? What are its advantages and disadvantages?
Ans: Macros are abbreviations for lengthy and frequently used statements. When a macro is called the entire code is substituted by a single line though the macro definition is of several lines.
The advantage of macro is that it reduces the time taken for control transfer as in case of
function.
The disadvantage of it is here the entire code is substituted so the program becomes
lengthy if a macro is called several times.
#include
#include
#define SQUARE(x) x*x
int main() {
  int i = 2;
  int j= SQUARE(i);
  printf("The value of j is: %d\n", j);
  getch();
  return 0;
}
The following line defines the macro SUM as having two parameters a and b and the replacement tokens (a + b):
#define SUM(a,b) (a + b)
This definition would cause the preprocessor to change the following statements (if the statements appear after the previous definition):
c = SUM(x,y);
c = d * SUM(x,y);
In the output of the preprocessor, these statements would appear as:
c = (x + y);
c = d * (x + y);
Use parentheses to ensure correct evaluation of replacement text. For example, the definition:
#define SQR(c)  ((c) * (c))
requires parentheses around each parameter c in the definition in order to correctly evaluate an expression like:
y = SQR(a + b);
The preprocessor expands this statement to:
y = ((a + b) * (a + b));
Without parentheses in the definition, the correct order of evaluation is not preserved, and the preprocessor output is:
y = (a + b * a + b);

11. Difference between pass by reference and pass by value?
Ans: Pass by reference passes a pointer to the value. This allows the callee to modify the variable directly.Pass by value gives a copy of the value to the callee. This allows the callee to modify the value without modifying the variable. (In other words, the callee simply cannot modify the variable, since it lacks a reference to it.)

 Pass By Reference :
In Pass by reference address of the variable is passed to a function. Whatever changes made to the formal parameter will affect to the actual parameters
- Same memory location is used for both variables.(Formal and Actual)-
- it is useful when you required to return more then 1 values
Pass By Value:
- In this method value of the variable is passed. Changes made to formal will not affect the actual parameters.
- Different memory locations will be created for both variables.
. where as
in pass by reference the change in the variable may reflect
to the original value in the main funtion.
ex : //  Pass by Reference
void Get( int &nIndex){
  nIndex = 10;
}

void main()
{
  int x = 100;
  cout<
}

o/p : 10;
in Pass by value; if any change in variable in the sub-
function may not reflected to the main function
ex : //  Pass by Value

void Get( int nIndex){
  nIndex = 10;
}

void main()
{
  int x = 999;
  cout<
}
o/p : 999

Monday, December 10, 2012

C LANGUAGE INTERVIEW PAPERS FOR FRESHERS

1.     What does static variable mean? 
2.     What is a pointer?
3.     What are the uses of a pointer?
4.     What is a structure?
5.     What is a union?
6.     What are the differences between structures and union?
7.     What are the differences between structures and arrays?
8.     In header files whether functions are declared or defined?
9.     What are the differences between malloc () and calloc ()?
10.   What are macros? What are its advantages and disadvantages?
11.   Difference between pass by reference and pass by Value?
12.   What is static identifier?
13.   Where is the auto variables stored?
14.   Where does global, static, and local, register
15.   Difference between arrays and linked list?
16.   What are enumerations?
17.   Describe about storage allocation and scope of
18.   What are register variables? What are the advantages
19.   What is the use of typedef?
20.   Can we specify variable field width in a scanf()
21.   Out of fgets() and gets() which function is safe to use and why?
22.   Difference between strdup and strcpy?
23.   What is recursion?
24.   Differentiate between a for loop and a while loop? What are it uses?
25.   What is storage class. What are the different storage classes in C?
26.   What the advantages of using Unions?
27.   What is the difference between Strings and Arrays?
28.   What is a far pointer? where we use it?
29.   What is a huge pointer?
30.   What is a normalized pointer ,how do we normalize a pointer?
31.   What is near pointer.
32.   In C, why is the void pointer useful? When would you use it?
33.   What is a NULL Pointer? Whether it is same as an uninitialized pointer?
34.   Are pointers integer ?
35.   What does the error 'Null Pointer Assignment' means and what causes this error?
36.   What is generic pointer in C?
37.   Are the expressions arr and &arr same for an array of integers?
38.   How pointer variables are initialized ?
39.   What is static memory allocation ?
40.   What is dynamic memory allocation?
41.   What is the purpose of realloc ?
42.   What is pointer to a pointer.
43.   What is an array of pointers ?
44.   Difference between linker and linkage ?
45.   Is it possible to have negative index in an array?
46.   Why is it necessary to give the size of an array in an array declaration ?
47.   What modular programming ?
48.   What is a function ?
49.   What is an argument ?
50.   What are built in functions ?
51.   Difference between formal argument and actual argument ?
52.   Is it possible to have more than one main() function in a C program ?
53.   What is the difference between an enumeration and a set of pre-processor # defines?
54.   How are Structure passing and returning implemented by the complier?
55.   What is the similarity between a Structure, Union and enumeration?
56.   Can a Structure contain a Pointer to itself?
57.   How can we read/write Structures from/to data files?
58.   Write a program which employs Recursion ?
59.   Write a program which uses Command Line Arguments?
60.   Difference between array and pointer ?
61.   What do the 'c' and 'v' in argc and argv stand for?
62.   What are C tokens ?
63.   What are C identifiers?
64.   Difference between syntax Vs. logical error?
65.   What is preincrement and post increment ?
66.   Write a program to interchange 2 variables without using the third one.
67.   What is the maximum combined length of command line arguments including the space between adjacent arguments?
68.   What are bit fields? What is the use of bit fields in a Structure declaration?
69.   What is a preprocessor, What are the advantages of preprocessor ?
70.   What are the facilities provided by preprocessor ?
71.   What are the two forms of #include directive ?