Monday, April 4, 2016

Oral questions for Programming Lab(C Language)

Dear Folkz ! Here is a List of question and answers for your oral exams...
1. What is programming?
Ans: It is writing a set of instructions to computer to perform a particular task automatically which is being done manually.

2. Why C is called powerful language ?
Ans: It provides an interface to interact with hardware of the system, hence it is best suited for system programming. 

3. How C is different from C++.
Ans: It is a procedure oriented programming language, where emphasis is on actions rather than data, where as C++ is object oriented programming language where emphasis is on data .

4. How many keywords are there in C?
Ans: Standard C language has 32 keywords.

5. What are storage classes in C?
Ans: Storage class represent the kind storage being used for storing data. C has four types storage.
i)Automatic(auto) : life and scope are local to the block{} in which it is created
ii)Static(static) : scope is local but life persists between calls
iii)Register(register) : CPU registers are used, purpose to have faster access
iv)External(extern): scope is Global, and life till end of the program.

6. What is the use of keyword volatile?
Ans: If your program is using any shared variable, if there is chance of getting it modified by some other program/routine, we declare it as volatile. i.e it's value can change without our knowledge.

7. Can you apply modulus operator on floating point variables in C ?
Ans: No, C doesn't allow it.It gives an error for it.

8. What is the output this code snippet?
 main(){ int a=0;
  if(a=5)
 printf("Hello");
else
printf("Hi");
}
Ans: Hello. Bcoz, a=5 is not a relational operator, here 5 is assigned to 'a' which is nonzero, hence true.

9. Why we need functions?
Ans: Functions are subprograms written to perform a particular task, They are basis of modular programming. Dividing a big/complex task into small/simple task.
Primarily It avoid repetition of code and saves memory.

10. Is main a built-in function?
Ans: No, It is a user defined function, as the user specifies what to be done in it's body. Of course the name is given by the language system.

11. What is a Pointer?
Ans: It is a variable, which can store address of another variable. These are used to refer variables using their addresses.
Basically, when we make use of local variables in one function and if their values are getting modified in another function, we need to send addresses of these variables. In such scenarios, we need variable which can hold addresses of other variable. Pointers serve this purpose.

12. What is static allocation of memory ?
Ans: It is allocating memory at the time of compile. Hence the size must be known in advance.
Ex: Array uses static allocation.

13. What is dynamic memory allocation?
Ans: It allocates memory at run time, Here we need know the size before run time. The advantage of this type is that, neither we fall short nor waste.
C provide malloac() and calloc()
malloc() can be used allocate single block of memory, where as calloc() can be used allocate contiguous multiple blocks.

14. What is sizeof operator?
Ans: If the no of bytes of a primitive(like int, float..) or user defined data type(stucture) is not known , we can use this to find it.

15. What is the difference between union and structure?
Ans: Structure allocates memory for all of it's elements where as union allocates memory to single variable of its member, which has largest size.

16. What is a function pointer?
Ans: Basically it is a pointer which can store address of some function. We can call the function using it's address.
An example to demonstrate function pointer.

void disp(int);
main()
{
 void (*fp)(int);
 fp=disp;
 fp(5);

}
void disp(int x)
{
 printf("%d",x);
}

17. Why me need explicit typecasting in malloc()?
Ans: By default malloc return address of type void( some compilers return char*), So a void pointer can not be assigned to the other type, so we need to explicitly convert it to the type of left side pointer.

Ex: int *p;
p=(int*) malloc(10);

18. What are the segments of memory?
Ans: Stack: All local variable reside here.
         Heap: memory for run-time allocation comes from here
         Code(Text): program resides here
         Extra: global variable are stored here.
19. What is a dangling pointer?
Ans: If any pointer represent an address which is no more reachable, then it is called dangling pointer.
Ex;
  main()
 {
   int * p;
   p=init();
   printf("%d",*p);
}
 int* init()
{
  int x=5;
  return(&x);
}
Here 'x' is local, when the control exits init, x will be destroyed, but p is still representing an address which is no more valid(unreachable);

20. What is memory leakage ?
Ans: When we allocate memory at run time , it comes from Heap segment, The characteristic of heap memory is that, it must be deallocated by programmer, when it's job is over, If programmer forgets to free it, and comes out of the program, This memory will be held occupied by the system, Though we have finished our task. This creates a hole in memory, leads to memory segmentation issue and thus memory leakage.

Ex;
 main()
{
 int * p;
 p=init();
printf("%d",*p);
}

int* init()
{
 int * q;
q=(int*)malloc(sizeof(int));
return(q);
}

Here the programmer has forgotten to free the memory , once the job is over. i.e he should have given a free() statement in main()
The  correct version of main() would be

main(){
 int * p;
 p=init();
printf("%d",*p);
free(p);
}


Did you find this article useful ? Please comment...
More more such stuff and FREE online courses visit 

www.kpotential.academy



No comments:

Post a Comment