Main

http://open.popnc.com/pnc/16280

Monday, 23 December 2019

2 Marks Question with answer of C Language

1) How can the number of bytes allocated to each basic data type to be determined for a particular C compiler? 
➤➤ C provides sizeof operator to determine the number of bytes allocated to each basic data type. The sizeof() operator gives the size of the type passed as argument to it. 
e.g.: sizeof(int) gives 2 
 
2) What is data type? What are the data types in C language? 

➤➤Data types are the keywords used in one of the two purposes:
 (i) Starts a variable declaration to specify what amount of memory       is to be allocated for a variable and what type of data 
     (or value) it can store.
 (ii) Starts a function declaration to specify what type of data          the function can return. C provides following data types:            int, long, float, double, char 
 
3) Distinguish between constant and variable. 
➤➤The difference between constant and variable are: 
(i) A constant (or literal) is a sequence of characters to represent a constant value which may be assigned to a variable, passed to a function argument or used in an expression. On the other hand a variable is an identifier (symbol or name) of a memory location and used to store value of specific type.
(ii) Constants are system defined whereas variables are user defined. 
 
4) Is ‘*’ a unary or binary operator or both? Justify your answer with example.
 ➤➤ The ‘*’ is both unary and binary operator. 
When ‘*’ is used as ‘multiplication operator’, it operates on two operand and so called binary operator. But when ‘*’ is used as ‘pointer indirection/dereference operator’, it operates on a single operand (pointer) to obtain the data of the memory location whose address is contained in it and so called unary operator. 
 
5) Is ‘&’ a unary or binary operator or both? Justify.
 ➤➤ The ‘&’ is both unary and binary operator.
 When ‘&’ is used as ‘bitwise AND operator, it operates on two operand and so called binary operator. But when ‘&’ is used as ‘reference/address-of operator’, it operates on a single operand and so called unary operator. 
 
6) What is comma operator? What is its purpose? 
➤➤The comma (,) operator is used to separate consecutive identifiers in a variable declaration. It is also used to chain statements together inside a for-loop statement. 
 
7) What is the syntax of conditional operation statement? What is its function? 
➤➤The syntax of conditional operation statement is 
(expression1)? expression2 : expression3 ;
 If expression1 is true then expression2 is evaluated and give the results. Otherwise expression3 is evaluated and give the result. 
 
8) How can the conditional operator be combined with relational operator to form an ‘if-else’ type statement?
 ➤➤The conditional operator can be nested two form an ‘if-else’ type statement. For instance the following statement find the largest among three integers a,b,c. 
large=(a>b && a>c)?a:(b>a&&b>c)?b:c; 
 
9) For each of the statement below assuming y=20 before of the statement, what are the values of x after execution. 
(i) x=y==y--; (ii) x=5*y++ 
➤➤ For (i) y-- gives 20 then decreases the value of y to 19. So y==y-- is false and gives 0 which is assigned to x. Therefore value of x is 1.
 For (ii) y++ gives 20 which is multiplied by 5 and assigned to x. Therefore value of x is 100. 
 
10) For each of the statement below assume that x=100 prior to execution of the statement. In each case what are the value of y after execution.
 (i) y=x=x++; (ii) y=x==x++;
 ➤➤ For (i) x++ gives 100 and increases the value of x to 101, but 100 is assigned to x which is again assigned to y. Therefore value of y is 100.
 For (ii) x++ gives 100 and increases the value of x to 101, so x==x++ is false and gives 0 which is assigned to y. Therefore value of y is 0. 
 
11) Explain the meaning of each if the following is the first line of a function definition (i) long f(long a) 
(ii) float f(float a, float b)
➤➤For (i) f is a function that takes a long value as argument and returns long value.
 For (ii) f is a function that takes two float value as argument and returns a float value. 
 
12) What is the purpose of the scanf() function? Compare it with getchar() function. 
➤➤ The scanf function scans/reads a series of input fields form the input-stream and assigned to the address passed as argument to it. 
Comparison of scanf() and getchar(): 
(i) The scanf() takes a list of arguments to store scanned data. On the other hand getchar() takes no argument. 
(ii) The scanf() reads a series of input fields from stdin. Whereas getchar() reads a single character from stdin. 
(iii) On success, scanf() returns the number of input fields successfully scanned; whereas getchar() returns the character read. On error, both of them return EOF. 
 
13) What is the difference between branching and looping statements? ➤➤The difference between branching and looping statements are:
 (i) A branching/jump statement allows the program control to jump to another part of the program directly. e.g.: break, continue, goto, return. On the other hand a looping/iteration statement allows a program to repeat some statements based on some conditions. e.g.: while, do-while, for. 
(ii) The branching statement does not contain other statements; whereas the looping statements may contain one or more other statements. 
 
14) What is the difference between while and do-while statement?
 ➤➤ The only difference between do-while and while is that the while statement evaluates its condition at the top of the loop so that if it is false then the statements within the loop will not execute; whereas the do-while evaluates its condition at the bottom of the loop so that the statements within the do block are always executed at least once. 
 
15) Can any of the three expressions in for loop be omitted? Justify. 
➤➤Yes, any of the three expression of the for-loop can be omitted; but, we must provide the null statement (;) in place of the omitted expression. 
 
16) What is difference between break and continue statement? 
➤➤ The difference between break and continue statements are:
 (i) The break statement is used to terminate the innermost switch, for, while, or do-while statement. On the other hand the continue statement is used to skip the current iteration of the innermost loop and evaluates the condition part (updating-part in case of for-loop) of the loop.
 (ii) The break statement can be used terminate both the loops and switch statement; whereas the continue statement can only be used within a loop. 
 
17) What is need of return statement? 
➤➤The return statement is used to terminate the current function and pass control to the caller of the function and also returns value to the caller if desired.
 It has the following general form:
 return;           In case of not returning value. 
 return value;     In case of returning value to the caller. 

18) What is pointer? Give example. 
➤➤A pointer is a variable which is used to store memory-address of another variable of same type and can be used to access both the address and value of that variable.   
 Syntax: 
 type *pointer = & variable;
 e.g.:                            

 int x = 10;                         
 int *y = &x; 


19) What is an array? How array can be declared?
 ➤➤ An array is a collection of homogeneous type variables stored in contiguous memory locations and share the same array name. 
Syntax of array declaration:
data-type array-name[size];  
e.g.: 
int x[5]; 

20) What is the relationship between array name and pointer? 
➤➤ The array name is actually a pointer pointed to the first memory location of allocated fixedsize contagious memory-locations for an array.
 e.g.: 
/*consider the following program which declare an array and access the value like pointer */ 
#include<stdio.h> 
void main()
{  
int a[]={23,45,67,12};
  int i;  
for(i=0; i<4; i++)
 {   
printf(“%d  ”,*(a+i)); 
 }
}
The compiler automatically converts a[i] to *(a+i) when accessing the (i+1)th value of a. 
   



 21) Distinguish between (i) int* ip[10] and int (*ip)[10] (ii) int p(char *a) and int *p(char *a) 
➤➤For (i) int*ip[10] is an integer array of 10 pointers; whereas     int (*ip)[10] is a pointer to an integer array of 10 elements. 
   For (ii) int p(char *a)  is a function that takes a character       pointer as arguments and returns an integer value; whereas int      *p(char *a) is a function that takes a character pointer as           arguments and returns a pointer to integer. 

22) What is a null string and what is its length? Explain with example.
➤➤A null string is nothing except of a pair of double quotes “”. That is it contains only one ASCII character – the null character ’\0’. 

23) Does ‘x’ differ from “x”? Justify. 
➤➤’x’ is a character constant. Whereas “x” is a string; i.e. a character array whose last character is ’\0’. This can be shown as follows                                                              

That means “x” is equivalent to {’x’,’\0’}. 

24) State the difference between strcmp and stricmp. 
➤➤strcmp() compares two strings; but it distinct the uppercase and lowercase letter. That means strcmp() does not ignore the case. For example strcmp(“Hello”, “hello”) does not gives 0. 
  On the other hand stricmp() compares two strings without regard to case. That means stricmp() ignores the case. For example stricmp(“Hello”, “hello”) gives 0. 

25) Distinguish between malloc and calloc function.
 ➤➤ malloc() and calloc() has two main differences: 
(i) malloc() takes only one argument which is the size of the block of memory space to be allocated. Whereas calloc() takes two arguments – one is the number of memory block is to be allocated and the other is the size of each of the block.
 (ii) malloc() does not initialize the memory allocated, whereas calloc() initializes the allocated memory to ZERO. 

26) What are arguments in a function? 
 The variables or value passed in the parenthesized list in a function invocation are known as arguments. 
e.g.: void show(n); //here n is an argument for the function show() 

27) What is the difference between formal arguments and actual arguments?
➤➤The variables declared in the parenthesized list in a function definition are known as formal arguments or parameters.
 e.g.: 
void show(int x){ //here x is a formal argument of the function show() 
printf(“%d”, x);
 } 

On the other hand the variables or value passed in the parenthesized list in a function invocation are known as actual arguments or simply arguments.
 e.g.: 
void show(n); //here n is an actual argument for the function show() 


28) What is prototype declaration?
➤➤The prototype declaration (or function prototyping) is the method of declaring a function before it is defined. 
It has the following general form:
 return-type function-name(argument-types-separated-by- comma); 

29) How is an array name interpreted? How it is passed to function? ➤➤The array name is actually a pointer pointed to the first memory location of allocated fixedsize contagious memory-locations for an array. The compiler automatically converts a[i] to *(a+i) when accessing the (i+1)th value of a. 

Like other variables an array can also be passed to a function. But, it must be remembered that passing an array to a function is always call by reference. So, modifying the element of the array within the function will actually modify the actual array. 
It has either of the following general form: 
return-type function-name(type arrayName[])
 {  //…body
 }
 or,  return-type function-name(type *arrayName){    //…body   
 } 

30) Define recursion.
➤➤Recursion is the process of invoking a function itself until some specific-conditions (also known as base conditions or base cases) are reached. When the base case is satisfies, the recursion terminated immediately by returning control to the caller one by one. 

31) Differentiate between structure and union.
➤➤There are two main differences between a structure and a union. (i) A structure allocates separate memory space for each of its members. On the other hand a union allocates a single memory space for all of its members. 
(ii) A structure can store value for all of its members at a time. But, a union can only store value for its one member at time. The union can only remember the last stored value of its member. 

32) In what general category do the #define and #include statement fall? 
➤➤ The #define and #include both are the preprocessor directives of C. 

33) What is the purpose of a header file? Is the use of a header file absolutely necessary?
➤➤The header files are used to store library functions of C. So, when a program needs to use library functions, it must include a header file that contains the required function. 

34) How can you link multi-file programs in C considering some of the called functions are in a different file? 
➤➤Multiple files of C program can be linked together and used in a single program by including them using #include directives. A C file can be included within another using one of the following ways:
 #include<file-name>
 It search the file in the specified include directory. e.g.:   #include<stdio.h> 
 #include”file-name”
 It search the file in the path preceded the file-name. If no path   is given then it searches the file in the source directory.
 e.g.:
 #include” D:\TC\X.C”  - the file X.C is in the TC directory in the   D drive.  

35) Mention the name of different types of storage class variable. 
➤➤ There are four different storage classes in C. These are:
 (i) Automatic storage class
 (ii) External storage class 
 (iii) Static storage class 
 (iv) Register storage class 

36) What is the purpose of register storage class? What is the scope of register variables? 
➤➤A variable which is accessed and/or updated frequently can be declared as a register variable. A register is a fastest memory of a computer. So, the register variables are used for their fastest response. The variables declared as register must not need to store in one of the system register; but, the system ensure that they can be accessible as fast as possible. 
 Syntax:
 register type identifier;
 e.g.: 
 register int x = 10; 

37) How the static variables are defined? 38) What is the purpose of static variable? What is its scope?
➤➤ Static variables are defined within a function in the same manner as automatic variables, except that the variable declaration must begin with the static keyword. Like automatic variables, the static variables are local to the function where they declared. But, unlike automatic variables, static variables retain their values throughout the life of the program.
 e.g.:
 void count()
 { 
 static int n = 1;
 printf(“%d”,n);
 n++; 
 }
 Now whenever the function count() is called, it prints the value   of  n that holds the no. of times the call has been made. 

39) How the extern variables are defined? 
➤➤ The variables declared outside all of the functions are known as external variables. The variables declared within a function with the extern modifier are also external variables. An external variable has the scope global to the program. That is why they are also called global variables. 
 e.g.:
 int a=10; 
 void msg()
 {  
 extern int b =10;
 printf(“a=%d, b=%d”,a,b);
 }
 Here in the above program a and b both are external variables. 
  
40) How is program execution initiated when parameters are passed to a program through command line?
➤➤The command line arguments are the strings separated by white-spaces and given by the user at the time of running a C program from command line. To read command line arguments the main() function must have the following form:
 void main(int argc, char *argv[]){ 
 …
 }
 Where argc will indicate the number of arguments passed and argv   holds the strings entered. 

41) Write down the statements that interchange the value of the two variables a and b, without using third variable. 
➤➤ a = a + b; //let a=10 and b=20, therefore now a=10+20=30
    b = a - b; //Now b=30-20=10
    a = a - b; //Now a=30-10=20 

42) Define an algorithm. 
➤➤ An algorithm is a precise specification of a sequence of instructions to be carried out in order to solve a given problem. Each instruction tells what task is to be performed. Actually an algorithm is a series of mathematical steps written in simple English sentence.  

43) What is data structure?
➤➤The data structure is the organizing of data in the memory and provides a specific logical or mathematical model to access those data efficiently. 

44) What is stack? 
➤➤ A stack is a linear list in which insertion and deletion of the elements must take place at one end, called top of the stack. That is why it is also known as Last In First Out (LIFO) system. 

45) What is a queue?
➤➤A queue is a linear list in which insertion can take place at one end, called rear of the queue and deletion can take place at other end, called front of the queue. That is why it is also known as First In First Out (FIFO) system. 

46) What is link list? 
➤➤A linked list is a linear list of nodes in which each node linked with its next node by containing its address. 

47) Define a graph. 
➤➤A graph is a non linear data structure that consists of a collection of nodes that are linked together using a relationship which is not necessarily hierarchical. 

48) Define a binary tree. Give example. 
➤➤A binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right". e.g.:  








49) Define a strictly binary tree. Give example. 
➤➤⃞⃞⃞  A strictly / full / proper binary tree (or 2-tree) is a tree in which every node other than the leaves has two children. e.g.:  








No comments:

Post a Comment

C Language Overview

C Language Overview T he C programming language is a general-purpose, high-level language that was originally developed by Dennis M. Ri...