Вы находитесь на странице: 1из 56

COMPUTER PROGRAMMING AND DATA STRUCTURES II-MID IMPORTANT QUESTIONS UNIT-III 1.

Discuss with suitable examples the storage classes available in C? To fully define a variable one needs to mention not only its type, but also its 'storage class'. The general syntax of variable declaration is storage_class data_type variable_name; A variable storage class tells us 1) Where the variable would be stored. 2) What will be the initial value of the variable. if the initial value is not specifically assigned(i.e, the default initial value). 3) What is the scope of the variable, i.e., in which functions the value of the variable would be available. 4) What is the life of the variables; i.e., how long would the variable exist. There are four storage classes in C. a) Automatic storage class b) Register storage class c) Static storage class d) External storage class a) Automatic storage class:- These variables are also called as local variables, because these are available local to function. The storage is in the memory and it has a default value, which is a garbage value. it is local to the block in which it has been declared and it life is till the control remains within the block in which the variable is defined. The keyword used is 'auto'. #include<stdio.h> #include<conio.h> Void main(){ auto int a; printf(%d,a) } Output: 1285 As seen above, the output is garbage value. b) Register storage class:- The storage of this type of variable is in the CPU registers. It has a garbage value initially. The scope of the variable is local to the block in which it has been declared and it life is till the control remains within the block in which the variable is defined. A value stored in a CPU register always be accessed faster than memory. Therefore, if a variable is used at many places in a program it is declare its storage class as register. A good example of frequently used variables is loop counters. The keyword used is 'register'.
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 1

COMPUTER PROGRAMMING AND DATA STRUCTURES #include<stdio.h> #include<conio.h> Void main(){ register int a; printf(%d,a) } Output: 4587 As seen above, the output is garbage value. c) Static storage class:- The storage is in the memory and default initial value is zero. It is local to the block in which it has been defined. The value of the variable persists between different function calls. The keyword used is 'static'. Eg: value(); main() { int i; for(i=0;i<5;i++) value(); } value() { static int a=5; a=a+2; printf("%2d",a); } The output of the program is not 7 7 7 7 7 but it is 7 9 11 13 15 d) Extern storage class:- The variable has storage in the memory. Default initial value is zero. The scope of the variable is global. It is present as long as the program execution comes to an end. The keyword used is 'extern'. #include<stdio.h> #include<conio.h> int a; Void main(){ extern int b; printf(%d %d,a,b) } int b=10; Output: 0 10

VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 2

COMPUTER PROGRAMMING AND DATA STRUCTURES Storage Classes Table: S.No 1 2 3 4 Name Automatic storage class External storage class Static storage class Register storage class Keyword auto extern static register Initial value Garbage value Zero Zero Garbage value Scope Within the function(local to block) Throughout the program Within the function(local to block) Within the function(local to block) Lifetime Within the function(local to block) Throughout the program Retain the value for different function calls Within the function(local to block) Memory Location RAM RAM RAM CPU

2.Explain how two dimensional arrays can be used to represent matrices. Write C code to perform matrix multiplication. The array which is used to represent and store data in a tabular form is called as 'two dimensional array.' Such type of array specially used to represent data in a matrix form. The following syntax is used to represent two dimensional array. <data-type> <array_name> [row_subscript][column-subscript]; Example: int a[3][3]; In above example, a is an array of type integer which has storage size of 3 * 3 matrix. The total size would be 3 * 3 * 2 = 18 bytes. It is also called as 'multidimensional array.' * MEMORY ALLOCATION :

Fig : Memory allocation for two dimensional array Matrix Multiplication using Function #include<stdio.h>
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 3

COMPUTER PROGRAMMING AND DATA STRUCTURES #include<conio.h> void mul(int [10][10],int [10][10],int,int,int); void main() { int a[10][10],b[10][10],c[10][10],r1,c1,r2,c2,i,j,k; clrscr(); printf("enter first matrix order\n"); scanf("%d%d",&r1,&c1); printf("enter second matrix order\n"); scanf("%d%d",&r2,&c2); printf("enter First Matrix elements\n"); for(i=0;i<r1;i++) for(j=0;j<c1;j++) scanf("%d",&a[i][j]); printf("enter Second Matrix elements\n"); for(i=0;i<r2;i++) for(j=0;j<c2;j++) scanf("%d",&b[i][j]); if(c1==r2) { printf("Multiplication of two matrix is\n"); mul(a,b,r1,c1,c2); } else printf("\nMatrix multiplication is not possible"); getch();
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 4

COMPUTER PROGRAMMING AND DATA STRUCTURES } void mul(int a[10][10],int b[10][10],int r1,int c1,int c2) { int i,j,k,c[10][10]; for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { c[i][j]=0; for(k=0;k<c1;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; printf("%4d",c[i][j]); } printf("\n"); } }

OUTPUT: enter first matrix order 23 enter second matrix order 33 enter First Matrix elements 123 654 enter Second Matrix elements
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 5

COMPUTER PROGRAMMING AND DATA STRUCTURES 678 325 241 Multiplication of two matrix is 18 23 21 59 68 77 3.What is recursion? Write a complete C program that reads a positive integer, calculate the factorial of the number using recursion, and print the result. Recursion is a repetitive process in which a function calls itself (or) A function is called recursive if it calls itself either directly or indirectly. In C, all functions can be used recursively. recursive functions are used to when the repeatedly apply the same solution of subset of the problem. recursive is an efficient method for solve the factorial, Fibonacci series and GCD problems etc. in recursive function must have the one condition for termination. every recursive call must solve part of the problem or reduce the size of the program

Factorial program using recursive function: #include<stdio.h> #include<conio.h> long int fact(long int); void main() { long int n; clrscr(); printf("Enter n value ");
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 6

COMPUTER PROGRAMMING AND DATA STRUCTURES scanf("%ld",&n); printf("\nFactorial of %ld is %ld",n,fact(n)); getch(); } long int fact(long int n) { if(n==0) return 1; else return (n*fact(n-1)); }

OUTPUT: Enter n value 6

Factorial of 6 is 720 4.Explain different categories of functions in C with simple illustrative examples? Function is a self-contained block to perform particular task. Functions are used for Placing or Storing the Code which is to be Repeated Several Times. The Functions are Some Storage Area which Contains set of Statements and the Function Executes all the Contained Statements when a Special Call is made to them. Once a Code is stored in the Function, then we can Store that Function any time and Any Time we can call that Functions. Functions Provides us Following Features 1) Reusability of Code : Means Once a Code has Developed then we can use that Code any Time. 2) Remove Redundancy: Means a user doesnt need to Write Code Again and Again. 3) Decrease Complexity: Means a Large program will be Stored in the Two or More Functions. So that this will makes easy for a user to understand that Code.
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 7

COMPUTER PROGRAMMING AND DATA STRUCTURES

There are Two Types of Functions 1) Built in Functions 2) User Defined functions The Functions those are provided by C Language are refers to the Built in Functions For example. sacnf(),printf(),clrscr(), getch(),etc.. Pre defined and Stored in the Form of header Files so that a user doesnt need to Know how this Function has developed and a user just use that Function. But in the other hand the Functions those are developed by the user for their Programs are known as User Defined Programs. When a user wants to make his Own Function, then he must have to follow the Following Operations. 1) Function Declaration or Prototyping 2) Function Defining 3) Calling a Function 1) Function Declaration or Prototyping:For using a Function a user must have to declare a Function. The Function declaration contains the Name of Function, Return type of the Function and also the Number of Arguments that a User will takes for performing the Operation. syntax: return_type function_name(list of arguments); eample: int add(int,int); The Function Prototyping Contains 1) Return Type of a function: The Return Function determines whether a Function will return any value to the Function. If a Function is declared with the void Keyword or if a Function Contains a void then thats means a Function Never Returns a value. 2) Name of Function : The Name of Function must be valid and the name of function must be Start from any Alphabet and the Name of Function doesnt Contains any Spaces and the Doesnt Contains any Special Character For Example Space , * sign etc. 3) Argument List: A Function may have zero or More Arguments. So that if we want to call a Function. Then we must have to Supply Some Arguments or we must have to pass some values those are also called as the Argument List. So that The Argument List is the total Number of Arguments or the Parameters those a Function Will takes. So that We must have Supply Some Arguments to the Functions,. The Arguments those are used by the Function Calling are known as the Actual Arguments and the Arguments those are used in the Function declaration are Known as the Formal Arguments, When we call any Function then the Actual Arguments will Match the Formal Arguments and if a proper Match is Found, then this will Executes the Statements of the Function otherwise this will gives you an error Message.
2) Function Definition: VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 8

COMPUTER PROGRAMMING AND DATA STRUCTURES The general syntax of function definition is return_type function_name(list of arguments) { body of the function }
2) Function calling:

the function can be calling using name of the function. syntax: function_name(list of arguments); There are five types of functions and they are: 1. 2. 3. 4. Functions with no arguments and no return values. Functions with arguments and no return values. Functions with arguments and return values. Functions with no arguments and return values.

1. Functions with no arguments and no return value. A C function without any arguments means you cannot pass data (values like int, char etc) to the called function. Similarly, function with no return type does not pass back data to the calling function. It is one of the simplest types of function in C. This type of function which does not return any value cannot be used in an expression it can be used only as independent statement.

Functions with arguments and no return value. A C function with arguments can perform much better than previous function type. This type of function can accept data from calling function. In other words, you send data to the called function from calling function but you cannot send result data back to the calling function. Rather, it displays the result on the terminal. But we can control the output of function by providing various values as arguments.

VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 9

COMPUTER PROGRAMMING AND DATA STRUCTURES

Functions with arguments and return value. This type of function can send arguments (data) from the calling function to the called function and wait for the result to be returned back from the called function back to the calling function. And this type of function is mostly used in programming world because it can do two way communications; it can accept data as arguments as well as can send back data as return value. The data returned by the function can be used later in our program for further calculations.

Functions with no arguments but returns value. We may need a function which does not take any argument but only returns values to the calling function then this type of function is useful. The best example of this type of function is "getchar()" library function which is declared in the header file "stdio.h". We can declare a similar library function of own.

5.Define an array. What are the different types of arrays? Explain.


VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 10

COMPUTER PROGRAMMING AND DATA STRUCTURES An array in C language is a collection of similar data-type, means an array can hold value of a particular data type for which it has been declared. Arrays can be created from any of the C data-types int, float, and char. So an integer array can only hold integer values and cannot hold values other than integer. When we declare array, it allocates contiguous memory location for storing values whereas 2 or 3 variables of same data-type can have random locations. So this is the most important difference between a variable and an array. Types of Arrays:
1. One dimension array (Also known as 1-D array). 2. Two dimension array (Also known as 2-D array). 3. Multi-dimension array.

Declaration of One Dimensional Arrays: Syntax: data_type array_name[width]; Example: int roll[8]; In our example, int specifies the type if the variable, roll specifies the name of the variable and the value in bracket [8] is new for newbie. The bracket ([ ]) tells compiler that it is an array and number mention in the bracket specifies that how many elements (values in any array is called elements) it can store. This number is called dimension of array. So, with respect to our example we have declared an array of integer type and named it roll which can store roll numbers of 8 students. You can see memory arrangement of above declared array in the following image: One Dimensional Array

C Array Assignment and Initialization: We can initialize and assign values to the arrays in the same way as we do with variable. We can assign value to an array at the time of declaration or during runtime. Lets look at each approach. Syntax: data_type array_name[size]={list of values}; Example: int arr[5]={1,2,3,4,5}; int arr[]={1,2,3,4,5}; In our above array example we have declared an integer array and named it arr which can hold 5 elements, we are also initializing arrays in the same time. Both statements in our example are valid method to declare and initialize single dimension
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 11

COMPUTER PROGRAMMING AND DATA STRUCTURES array. In our first example we mention the size (5) of an array and assigned it values in curly brace, separating elements value by comma (,). But in second example we left the size field blank but we provided its elements value. When we only give element values without providing size of an array then C compiler automatically assumes its size from given element values. Array Initialization Example #include<stdio.h> #include<conio.h> void main() { int a[10]={10,20,30,40,50},i; clrscr(); printf("given elements are\n"); for(i=0;i<5;i++) printf("%4d",a[i]); } output: given elements are 10 20 30 40 50

2D Array in C Programming Language In C language it is possible to have more than one dimension in an array. In this tutorial we are going to learn how we can use two dimensional arrays (2D arrays) to store values. Because it is a 2D array so its structure will be different from one dimension array (1D array). The 2D array is also known as Matrix or Table, it is an array of array. See the 2D array image, in that image each row is an array. Declaration of 2D array: Syntax: data_type array_name[row_size][column_size]; Example: int arr[3][3]; So the above example declares a 2D array of integer type. This integer array has been named arr and it can hold up to 9 elements (3 rows x 3 columns).

VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 12

COMPUTER PROGRAMMING AND DATA STRUCTURES

Memory Map of 2D Array

#include<stdio.h> #include<conio.h> void main() { int a[10][10]={{1,2,3},{4,5,6}},i,j; printf{"given matrix is \n"); for(i=0;i<2;i++) { for(j=0;j<3;j++) printf("%4",a[i][j]); printf("\n"); } printf("%4d",a[i][j]); } Multi dimensional array (3D Array) C allows array of two or more dimensions and maximum numbers of dimension a C program can have is depend on the compiler we are using. Generally, an array having one dimension is called 1D array, array having two dimensions called 2D array and so on. So in C programming an array can have two or three or four or even ten or more dimensions. More
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 13

COMPUTER PROGRAMMING AND DATA STRUCTURES dimensions in an array means more data it can hold and of course more difficulties to manage and understand these arrays. A multidimensional array has following syntax: Syntax: type array_name[d1][d2][d3][d4][dn]; Where dn is the size of last dimension. Example: int table[5][5][20]; float arr[5][6][5][6][5]; In our example array table is a 3D (A 3D array is an array of arrays of arrays.) array which can hold 500 integer type elements. And array arr is a 5D array which can hold 4500 floating-point elements. Can see the power of array over variable? When it comes to hold multiple values in a C programming, we need to declare several variables (for example to store 150 integers) but in case of array, a single array can hold thousands of values (depending on compiler, array type etc). How to Declaration and Initialization 3D Array Before we move to serious programming let's have a look of 3D array. A 3D array can be assumed as an array of arrays of arrays, it is array (collection) of 2D arrays and as you know 2D array itself is array of 1D array. It sounds a bit confusing but don't worry as you will lead your learning on multidimensional array, you will grasp all logic and concept. A diagram can help you to understand this.

3D Array Conceptual View

UNIT-IV 1.Differentiate between call by value and call by reference with suitable examples?

VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 14

COMPUTER PROGRAMMING AND DATA STRUCTURES 1) Call by Value:- Call By Value means pass the value of the variable from calling function to called function 2) Call By Reference :- Call By Reference means pass the address of the variable from calling function to called function The main difference between call by value and call by reference is in call by value any changes occurred on formal arguments doesn't effect on actual argument but in call by reference any changes occurred on formal arguments directly effect on actual arguments. a) write a program to swap the two number using call by value.

#include<stdio.h> #include<conio.h>

void swap(int,int); void main() { int a,b; clrscr(); printf("enter a and b values\n"); scanf("%d%d",&a,&b); printf("\nBefore swap function call\n a = %d b = %d",a,b); swap(a,b); printf("\nAfter call swap function call\n a = %d b = %d",a,b); getch(); } void swap(int x,int y) { int temp; printf("\nin swap function before swap\n x = %d y = %d",x,y); temp=x;
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 15

COMPUTER PROGRAMMING AND DATA STRUCTURES x=y; y=temp; printf("\nin swap function after swap\n x = %d y = %d",x,y); }

Output: enter a and b values 10 20

Before swap function call a = 10 b = 20 in swap function before swap x = 10 y = 20 in swap function after swap x = 20 y = 10 After call swap function call a = 10 b = 20

b) write a program to swap the two number using call by reference.

#include<stdio.h> #include<conio.h>

void swap(int *,int *); void main() { int a,b;


VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 16

COMPUTER PROGRAMMING AND DATA STRUCTURES clrscr(); printf("enter a and b values\n"); scanf("%d%d",&a,&b); printf("\nBefore swap function call\n a = %d b = %d",a,b); swap(&a,&b); printf("\nAfter swap function call\n a = %d b = %d",a,b); getch(); } void swap(int *x,int *y) { int temp; printf("\nin swap function before swap\n x = %d y = %d",*x,*y); temp=*x; *x=*y; *y=temp; printf("\nin swap function after swap\n x = %d y = %d",*x,*y); }

Output: enter a and b values 10 20

Before swap function call a = 10 b = 20 in swap function before swap x = 10 y = 20 in swap function after swap x = 20 y = 10
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 17

COMPUTER PROGRAMMING AND DATA STRUCTURES After swap function call a = 20 b = 10

2. a) Explain about Dynamic memory allocation functions in C. Static memory allocation means allocate the memory for the variable at the time of compilation. ex: int a[100]={1.2}; here system allocated 200bytes for a, but we are using only 4bytes remaining 196bytes are wasted, no other variable can't access this memory. the main drawback of static memory allocation is to waste lot of memory and unable to store more than size of elements Dynamic memory allocation means the memory allocated for the variable at the execution time using DMA save the memory and allocated only required memory. The C programming language provides several functions for memory allocation and management. These functions can be found in the<stdlib.h> header file. Function Syntax Name malloc() void *malloc(int num); calloc() realloc() free() void *calloc(int num, int size); void *realloc(void *address, int newsize); void free(void *address);

S No 1 2 3 4

Description This function allocates an array of num elements each of which size in bytes will be size. This function allocates an array of num elements each of which size in bytes will be size. This function re-allocates memory extending it upto newsize. This function release a block of memory block specified by address.

write a c program to find sum of given list of integers using DMA #include<stdio.h> #include<conio.h> void main() { int *p,i,n; clrscr(); printf("enter n value"); scanf("%d",&n); p=(int*)malloc(n*sizeof(int)); printf("enter %d elements\n",n);
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 18

COMPUTER PROGRAMMING AND DATA STRUCTURES for(i=0;i<n;i++) { scanf("%d",p+i); sum=sum+(*(p+i)); } printf("\n sum=%d",sum); free(p); } output: enter n value 5 enter 5 elements 10 20 30 40 50 sum=150 b) Write a complete C program that reads a string and prints if it is a palindrome or not. #include<conio.h> #include<stdio.h> #include<string.h> void main() { char str1[20],str2[20]; int d; clrscr(); printf("Enter string\n"); scanf("%s",str1); strcpy(str2,str1); strrev(str2); if(!strcmp(str1,str2)) printf("\n String is palindrome "); else printf("\n String is not palindrome "); getch();
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 19

COMPUTER PROGRAMMING AND DATA STRUCTURES }

OUTPUT:

Enter string MADAM

String is palindrome 3. Explain the string Handling functions with suitable examples. A string is combination of characters. Any set or sequence of characters defined within double quotation symbols is a constant string. In c it is required to do some meaningful operations on the strings

There are number of string handling functions present in string.h. In other words if a programmer uses any of the function present instring.h then they must include the header file as #include <string.h> Some of the functions present in <string.h> are given below: strlen() This function is used to length of the string in other words the number of characters present in string . strcat() - This function is used to concatenate two strings. strcmp() This function is used to compare two strings. strcpy() This function is used to copy the second string given as second parameter to this function into first string. strrev()- This function is used to find reverse of the given string. strstr() This function is to obtain the first occurrence of substring in a string . strncat() -This function is used to first n characters concatenate two strings. strncmp() -This function is used to compare first n characters of two strings. strncpy() -This function is used to copy first n characters of two strings. strcmpi() -This function is used to compare two strings and ignore case sensitive. strncmpi() -This function is used to compare first n characters of two strings and ignore case strlwr() -This function is used to converts upper case to lower case sensitive. strupr() -This function is used to convert lower case to upper case sensitive.
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 20

COMPUTER PROGRAMMING AND DATA STRUCTURES strlen() function: This function counts and returns the number of characters in a particular string. The length always does not include a null character. The syntax of strlen() is as follows: n=strlen(string); Where n is the integer variable which receives the value of length of the string. /*write a c program to find the length of the string using strlen() function*/ #include < stdio.h > include < string.h > void main() { char name[100]; int length; printf(Enter the string); gets(name); length=strlen(name); printf(\nNumber of characters in the string is=%d,length); } strcat() function: when you combine two strings, you add the characters of one string to the end of the other string. This process is called as concatenation. The strcat() function is used to joins 2 strings together. It takes the following form: strcat(string1,string2) string1 & string2 are the character arrays. When the function strcat is executed string2 is appended to the string1. the string at string2 always remains unchanged. strcmp function: In c,you cannot directly compare the value of 2 strings in a condition like if(string1==string2) Most libraries however contain the function called strcmp(),which returns a zero if 2 strings are equal, or a non zero number if the strings are not the same. The syntax of strcmp() is given below: strcmp(string1,string2) strcmpi() function This function is same as strcmp() which compares 2 strings but not case sensitive. strcmp(string1,string2) strcpy() function: To assign the characters to a string,C does not allow you directly as in the statement name=Robert; Instead use the strcpy() function found in most compilers the syntax of the function is illustrated below.
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 21

COMPUTER PROGRAMMING AND DATA STRUCTURES strcpy(string1,string2); strrev() function: This function reverses the characters in a particular string. The syntax of the function strrev is illustrated below strrev(string); strstr() function: This function is to obtain the first occurrence of substring in a string. The syntax of the function strstr is illustrated below: strstr(string1,string2); strncat() function: This function is used to first n characters concatenate two strings. The syntax of the function strncat is illustrated below: strncat(string1,string2,n); strncmp() function: This function is used to compare first n characters of two strings. The syntax of the function strncmp is illustrated below: strncmp(string1,string2,n); strncpy() function: This function is used to copy first n characters of two strings. The syntax of the function strncpy is illustrated below: strncpy(string1,string2,n); strcmpi() function: This function is used to compare two strings and ignore case sensitive. The syntax of the function strcmpi is illustrated below: strcmpi(string1,string2); strncmpi() function: This function is used to compare first n characters of two strings and ignore case. The syntax of the function strncmpi is illustrated below: strncmpi(string1,string2,n); strlwr () function: This function converts all characters in a string from uppercase to lowercase The syntax of the function strlwr is illustrated below: strlwr(string); strupr () function: This function converts all characters in a string from lowercase to uppercase The syntax of the function strupr is illustrated below:
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 22

COMPUTER PROGRAMMING AND DATA STRUCTURES strupr(string); The following program illustrate the use of string functions: /* Example program to use string functions*/ #include < stdio.h > #include < string.h > void main() { char s1[20],s2[20],s3[20]; int x,l1,l2,l3; printf(Enter the strings); scanf(%s%s,s1,s2); x=strcmp(s1,s2); if(x!=0) {printf(\nStrings are not equal\n); strcat(s1,s2); } else printf(\nStrings are equal); strcpy(s3,s1); l1=strlen(s1); l2=strlen(s2); l3=strlen(s3); printf(\ns1=%s\t length=%d characters\n,s1,l1); printf(\ns2=%s\t length=%d characters\n,s2,l2); printf(\ns3=%s\t length=%d characters\n,s3,l3); printf("s1 upper case is %s",strupr(s1)); } 4 Define pointer? Differentiate between a pointer and a variable? How a pointer is declared and initialized? What do you mean by pointer to another pointer? One of the powerful features of C is ability to access the memory variables by their memory address. This can be done by using Pointers. The real power of C lies in the proper use of Pointers. A pointer is a variable that can store an address of a variable (i.e., 112300).We say that a pointer points to a variable that is stored at that address. A pointer itself usually occupies 4 bytes of memory (then it can address cells from 0 to 232-1).

Advantages of Pointers : 1. 2. 3. 4. A pointer enables us to access a variable that is defined out side the function. Pointers are more efficient in handling the data tables. Pointers reduce the length and complexity of a program. They increase the execution speed.
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 23

COMPUTER PROGRAMMING AND DATA STRUCTURES

Definition : A variable that holds a physical memory address is called a pointer variable or Pointer. difference between ordinary variable and pointer: ordinary variable stores the value but pointer variable stores address of the another variable. ordinary variable memory allocated based on data type but pointer variable memory 2bytes only. Declaration : Datatype * Variable-name; Eg:int *ad; char *s; float *fp; char **s; /* pointer to int */ /* pointer to char */ /* pointer to float */ /* pointer to variable that is a pointer to char */

Initialization: A pointer is a variable that contains an address which is a location of another variable in memory. Consider the Statement p=&i; Here & is called address of a variable. p contains the address of a variable i. The operator & returns the memory address of variable on which it is operated, this is called Referencing. The * operator is called an indirection operator or dereferencing operator which is used to display the contents of the Pointer Variable. Consider the following Statements : int *p,x; x =5; p= &x; Assume that x is stored at the memory address 2000. Then the output for the following printf statements is :
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 24

COMPUTER PROGRAMMING AND DATA STRUCTURES Output 5 2000 2000 5 5

Printf(The Value of x is %d,x); Printf(The Address of x is %u,&x); Printf(The Address of x is %u,p); Printf(The Value of x is %d,*p); Printf(The Value of x is %d,*(&x)); pointer to pointer:

pointer is a variable which stores address of another pointer variable is called as pointer to pointer syntax: data_type **variable_name; ex: int n=10,*a=&n,**b=&a; program: void main() { int n=10,*a,**b; *a=&n; **b=&a; printf("\n n=%d *a=%d **b=%d",n*a,**b); printf("\n the address of n is %u",a); printf("\n the address of a is %u",b); } 5. Write a complete C program that displays the position or index in the string S where the string T begins. The program displays -1 if S does not contain T. For example, if S is information processing and T is process, the value displayed is 12. The strings S and T are supplied by the user. #include<stdio.h> #include<conio.h> main() { char s[100],t[100],i,j,k,index; clrscr(); printf("enter the string\n"); gets(s); printf("enter the sub string\n"); scanf("%s",t); for(i=0;i<strlen(s);i++) { index=i for(j=0,k=i;j<strlen(t);j++,k++) { if(s[k]==t[j])
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 25

COMPUTER PROGRAMMING AND DATA STRUCTURES continue; else break; } if(j==strlen(t)) { printf("string is found at %d",index+1); exit(0); } } printf("sub string not found in string"); getch(); } OUTPUT: enter the string COMPUTER PROGRAMMING AND DATA STRUCTURES enter the sub string DATA string is found at 26

UNIT-V 1. Explain the following with examples: a) Pointers to structures b) Unions. Pointer to structure: A pointer which is pointing to a structure is know as pointer to structure. Examples of pointers to structure: #include<stdio.h> struct address{ char *name; char street[10]; int pin; }; int main() { struct address cus={"A.Kumar","H-2",456003}*p=&cus;
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 26

COMPUTER PROGRAMMING AND DATA STRUCTURES printf("%s %s",p->name,(*p).street); return 0; } Output: A.Kumar H-2 Explanation: p is pointer to structure address. -> and (*). Both are same thing. These operators are used to access data member of structure by using structures pointer. Union : Union is user defined data type used to stored data under unique variable name at single memory location. Union is similar to that of stucture. Syntax of union is similar to stucture. But the major difference between structure and union is 'storage.' In structures, each member has its own storage location, whereas all the members of union use the same location. Union contains many members of different types, it can handle only one member at a time. To declare union data type, 'union' keyword is used. Union holds value for one data type which requires larger storage among their members. Syntax: union union_name { <data-type> element 1; <data-type> element 2; <data-type> element 3; }union_variable; Example: union techno { int comp_id; char nm; float sal; }tch; In above example, it declares tch variable of type union. The union contains three members as data type of int, char, float. We can use only one of them at a time. * MEMORY ALLOCATION :

VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 27

COMPUTER PROGRAMMING AND DATA STRUCTURES

Fig : Memory allocation for union To access union members, we can use the following syntax. tch.comp_id tch.nm tch.sal Program : #include <stdio.h> #include <conio.h> union techno { int id; char nm[50]; }tch; void main() { clrscr(); printf("Enter developer id : "); scanf("%d", &tch.id); printf("\n Enter developer name : "); scanf("%s", tch.nm); printf("\n Developer ID : %d", tch.id);//Garbage printf("\n Developed By : %s", tch.nm); getch(); } Output : Enter developer id:101 Enter developer name: santo Developer ID:25972 Developed By:santo 2. Explain the following with examples: a) Self referential structures b) Typedef c) Enumerated types. Self referential structures:
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 28

COMPUTER PROGRAMMING AND DATA STRUCTURES A structure may have a member whose is same as that of a structure itself.Such structures are called self-referential. Self-Referential Structure are one of the most useful features. They allow you to create data structures that contains references to data of the same type as themselves. Self-referential Structure is used in data structure such as binary tree, linked list, stack, Queue etc. A self-referential structure is one of the data structures which refer to the pointer to (points) to another structure of the same type. For example, a linked list is supposed to be a selfreferential data structure. The next node of a node is being pointed, which is of the same struct type

syntax:

struct struct_name { datatype datatypename; struct_name * pointer_name; };

example: struct node { int value; struct node *next; };

typedef: the typedef is user defined data type the typedef is used for create user to create user defined data type The C programming language provides a keyword called typedef which you can use to give a type a new name. syntax: typedef data_type userdefined_data_type; ex:
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 29

COMPUTER PROGRAMMING AND DATA STRUCTURES typedef int integer; program: main() { typedef int integer; integer a=10; printf("a=%d",a); } output: a=10; ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration tag and defines the set of named integer identifiers (called the "enumeration set," "enumerator constants," "enumerators," or "members"). A variable with enumeration type stores one of the values of the enumeration set defined by that type. In ANSI C, the expressions that define the value of an enumerator constant always have int type Syntax enum-specifier: enum identifier { enumerator-list } enum identifier The optional identifier names the enumeration type defined by enumerator-list. This identifier is often called the "tag" of the enumeration specified by the list. A type specifier of the form Enumerated data type variables can only assume values which have been previously declared. void main() { enum month { jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec }; enum month this_month; this_month = feb; printf("this month is %d",this_month); } In the above declaration, month is declared as an enumerated data type. It consists of a set of values, jan to dec. Numerically, jan is given the value 1, feb the value 2, and so on. The variable this_month is declared to be of the same type as month, then is assigned the value associated with feb. This_month cannot be assigned any values outside those specified in the initialization list for the declaration of month.
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 30

COMPUTER PROGRAMMING AND DATA STRUCTURES

3.Explain the following with examples: a) Nested structures b) Array of structures Structures within Structures (Nested Structures) : Structures can be used as structures within structures. It is also called as 'nesting of structures'. Syntax: struct structure_nm { <data-type> element 1; <data-type> element 2; --------------------<data-type> element n; struct structure_nm { <data-type> element 1; <data-type> element 2; --------------------<data-type> element n; }inner_struct_var; }outer_struct_var; Example : struct student { int rno; char name[50]; struct dob { int day,month,year; }d; }s; In above example, the structure student consists of dob which itself is a structure with three members. Structure student is called as 'outer structure' while dob is called as 'inner structure.' The members which are inside the inner structure can be accessed as follow : s.d.day s.d.month
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 31

COMPUTER PROGRAMMING AND DATA STRUCTURES s.d.year Program : #include <stdio.h> #include <conio.h> struct student { int rno; char name[20]; struct dob { int day,month,year; }d; }; void main() { struct student s={547,ramesh,{10.18.1981}}; clrscr(); printf("\n Student Rno: %d",s.rno); printf("\n Student Name: %s",s.name); printf("\n Student DOB: %d-%d-%d",s.d.day,s.d.month,s.d.year); } Output : Student Rno: 547 Student Name: ramesh Student DOB: 10-18-1981 Array of structure: We can create structures with array for ease of operations in case of getting multiple same fields. Program : #include <stdio.h> #include <conio.h> struct employee { int id; char name[50]; }emp[2]; void main() { int i; clrscr(); for(i=0;i<2;i++)
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 32

COMPUTER PROGRAMMING AND DATA STRUCTURES { printf("\n Enter Employee ID : "); scanf("%d",&emp[i].id); printf("\n Employee Name : "); scanf("%s",emp[i].name); } for(i=0;i<2;i++) { printf("\n Employee ID : %d",emp[i].id); printf("\n Employee Name : %s",emp[i].name); } getch(); } Output : Enter Employee ID: 1 Employee Name: ABC Enter Employee ID: 2 Employee Name: XYZ Employee ID:1 Employee NAme:ABC Employee ID:2 Employee Name:XYZ

4. a) Differentiate between structures and unions? b) Differentiate between Arrays and structures? Difference Between Structure and Union : Structure i. Access Members We can access all the members of structure at anytime. Only one member of union can be accessed at anytime. Union

ii. Memory Allocation Memory is allocated for all variables. Allocates memory for variable which variable require more memory.
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 33

COMPUTER PROGRAMMING AND DATA STRUCTURES iii. Initialization All members of structure can be initialized Only the first member of a union can be initialized. iv. Keyword 'struct' keyword is used to declare structure. 'union' keyword is used to declare union. v. Syntax struct struct_name { structure element 1; structure element 2; ------------------structure element n; }struct_var_nm; struct item_mst { int rno; char nm[50]; }it; union union_name { union element 1; union element 2; ------------------union element n; }union_var_nm; vi. Example

union item_mst { int rno; char nm[50]; }it; Fig.: Difference between Structure and Union

Difference Between Array and Structure : Array i. Data Collection Array is a collection of homogeneous Stucture is a collection of heterogeneous data. data. ii. Element Reference Array elements are referred by subscript. Structure elements are referred by its unique name. Structure

iii. Access Method Array elements are accessed by it's position or subscript. Stucture elements are accessed by its object as '.' operator.

VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 34

COMPUTER PROGRAMMING AND DATA STRUCTURES iv. Data type Array is a derived data type. Structure is user defined data type. v. Syntax struct struct_name { structure element 1; structure element 2; <data_type> array_name[size]; ------------------structure element n; }struct_var_nm; vi. Example struct item_mst { int rno; char nm[50]; }it; Fig.: Difference between Array and Structure 5.What is a structure? how to declared and initialized the structures? Give an example of creating and accessing members of a structure? Structures in C defines the group of contiguous (adjacent) fields, such as records or control blocks. A structure is a collection of variables grouped together under a single name. It provides an elegant and powerful way for keeping related data together. Structure Declaration: struct struct-name { type field-name; type field-name; ... }; Once the structure is defined, you can declare a structure variable by preceding the variable name by the structure type name. In the given example, a small structure i.e struct is created student and declared three instances of it as shown below. struct student { int id;
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 35

int rno[5];

COMPUTER PROGRAMMING AND DATA STRUCTURES char name[40]; float avg; } In structures, we have assigned the values to the instances i.e, id, name, percentage in the following way: student1.id=1; student2.name = "Angelina"; student3.avg = 90.5; declared and initialized: The structure variable can be declared using struct keyword and structure name

The syntax of declare structure variable

struct structure_name structure_variable; ex: struct student s;

The initialization of structure variable is

struct structure_name structure_variable={list of values};

ex: struct student s={534,"santo",97};

Accessing Structure Members: The structure members can be accessing using structure variable and . (dot operator). if the structure variable is pointer then using -> operator.
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 36

COMPUTER PROGRAMMING AND DATA STRUCTURES

The syntax of accessing structure members is

structure_variable.member; structure_variable->member;

ex: struct student s; s.id=123 s.avg=67; struct student *s1; s1->id=547; Here is the code: #include <stdio.h> #include <conio.h> struct student { int id; char *name; float percentage; } student1, student2, student3; int main() { struct student st; student1.id=1; student2.name = "Angelina"; student3.percentage = 90.5; printf(" Id is: %d \n", student1.id); printf(" Name is: %s \n", student2.name); printf(" Percentage is: %f \n", student3.percentage); getch();

VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 37

COMPUTER PROGRAMMING AND DATA STRUCTURES return 0; }

UNIT-III
1. The index or subscript value for an array of size n ranges from (a) 1 to n-1 (b) 0 to n-1 (c) 1 to n (d) 0 to n 2. If we dont initialize a static array, what will be the elements set to: (a) 0 (b) a floating point number (c) an undetermined value (d) character constant 3. Which of the following is not a storage class (a) external (b) automatic (c) register (d) define 4. int cal sum(int a, int b); In the above, int at the beginning indicates (a) name of function (b) both function arguments are integer (c) return type of function (d) received type of function 5. When a function is recursively called, all automatic variables are (a) stored in a stack (b) stored in a list (c) stored in queue (d) stored in an array 6. void means (a) something not known (b) 0 (c) 1 (d) nothing 7. What is the output of main() { int x, change (int); x = 20; change (x); printf (%d, x); return 0; } change (int x); { x = 10; printf (%d. x); } (a) 10 30 (b) 20 20 (c) 10 10 (d) 10 20 8. Which of these complier directives access to the print function (a) #include (b) #define print (c) include stdio.h; (d) #include VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 38

COMPUTER PROGRAMMING AND DATA STRUCTURES


conio.h; 9. The number of elements in array declaration (a) dynamically identifies based on largest index used in program (b) assume default size as 0 (c) requires to be specified (d) does not require to be specified 10. Identify the incorrect declaration of arrays from the following (a) int a[50]; (b) float values[10][20]; (c) double a[50]; (d) int score[10,15]; 11. The array is declared as float a[5]; and the memory address of a[0] is 4056. What is the memory address of a[3]. (a) 4056 (b) 4056 (c) 4059 (d) 4068 12. Two dimensional array elements are stored (a) system dependent (b) in row major order (c) complier dependent (d) in column major order 13. How many values a function can return at a time (a) only one (b) depends on the system (c) infinite values (d) 2 14. When compared to call by value, the call by reference is in execution (a) equal (b) fast (c) slow (d) neither slow nor fast 15. Which of the following statement is wrong with respect to a storage class (a) it specifies the defualt initial value (b) it specifies the life of a variable (c) by defualt a storage class is static (d) if specifies where variable is stored 16. Symbolic constants are defined as (a) #define s1 s2; (b) #define s1=s2 (c) #define s1 s2 (d) #define s1=s2; 17. One dimensional array is known as (a) set (b) vector (c) matrix (d) table 18. The storage area for register variables (a) processor registers (b) cache (c) virtual memory (d) memory 19. In a program a function can be called A. one time D. any no.of times B. two times C. three times

20. A function gets called when the function name is followed by A. comma( ,) B. Period ( .) D. Semi colon(;) 21. Which is not true among the following A. Brazil( ) is a user defined function B. Printf( ) is a Library function C. A function can be called from another function D. A function can be defined in another function VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 39 C. Colon( :)

COMPUTER PROGRAMMING AND DATA STRUCTURES


22. Array elements can be accessed using A. Tree Queue B. Pointer C. stack D.

23. An array is represented in Memory by using a A.Sequential map map B. Parallel map C. element map D. circular

24. The general form of array declaration is A. Type array name[type] C. Type data name[type] 26. int b[5]; main( ) { static int a[5]; int I; for(I=0;I<5;I++) printf("%d%d",b[I],a[I]); }output is A. 0000.5times B. error C. garbage values D. 000.10times B. Type variable name[size] D. Type data name[size]

27. The amount of storage required for holding the elements of array depends on A. size B. Memory C. Type and size D. Type

28.How much memory the compiler reserves for each integer array element A. 6bytes 29. main( ) { static int a[ ]={ 10,20,30,40,50}; int j;for(j=0;j<5;j++) {printf(%d,a[j]); a++; } } output is VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 40 B.8bytes C.4bytes D2bytes

COMPUTER PROGRAMMING AND DATA STRUCTURES


A. 10 20 30 40 error B. 10 20 30 C. 10 20 D.compilier

30. Array is used to represent the following (a) A list of data items of different data type (b) A list of data items of real data type (c) A list of data items of same data type (d) A list of data items of integer data type 31. Register variable are active (a) outside the function (b) throughout the program (c) only in the function where it is defined (d) surrounding of that function. 32. The function sqrt() is part of header file. (a) math.h (b) iostream.h (c) conio.h (d) stdio.h 33. What is the output of the following program # includes # include void main() {int a1[5] ={1}; int b = 0, k = 0; for (b=0; b<=4; b++) printf (%d , ++ a1[0]); } (a) 11111 (b) 23456 (c) 12345 (d) 12222 34. In a multidimensional array with initialization (a) no demension must be omitted (b) the left most dimension may be omitted (c) both leftmost and right most may be omitted (d) the right most dimension may be omitted 35. The statement used to send back any value to the calling function is (a) exit (b) return (c) break (d) continue 36. The statement used to send back any value to the calling function is (a) continue (b) exit (c) break (d) return 37. A static variable is one (a) which cant be initialized (b) which is same as an automatic variable but it is placed at the head of the program (c) which is initialized once and cant be changed at run time (d) which retains its value through out the life of the program 38. C does no automatic array bound checking. This is (a) Disadvantage of C (b) false (c) Neither advantage nor disadvantage (d) Advantage of C 39. The following program main( ) {static int a[ ] = { 7, 8, 9 }; printf(%d, a[2] = 2[a] + a[2]); } (a) will compile successfully (b) results in segmentation violation error (c) result is buss error (d) runtime error VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 41

COMPUTER PROGRAMMING AND DATA STRUCTURES


40. What is the output of the following program? void main() { char name[4] = {R, A, V I }; printf (%s, name); } (a) RAVI followed by a garbage value (b) RAVI (c) RAV (d) only garbage value 41. The library function sqrt operates on a double pression argument and I is an integer variable, which one of the following calls would compute sqrt(I)? (a) sqrt((double)I); (b) (double) (sqrt(I)); (c) sqrt(I); (d) (double) sqrt(I); 42. main() {printf(welcome to c programming\n); main(); }output of the program code (a) Infinite loop (b) Runtime error (c) welcome to c programming (d) compile time error 43. What will be the output of the following program? void main() { char x[ ]={s, a, NULL }; printf (\n %d, sizeof (x)); } (a) 2 (b) 3 (c) 0 (d) 1 44. The variables which are declared outside the program are called (a) global variables (b) formal variables (c) local variables (d) register variable 45. #include is a directive (a) processor (b) complier (c) pre-compiler (d) preprocessor 46. Array elements are stored in (a) either in row major or column major order (b) in diagonal order (c) row major order (d) column major order 47. Under which of the following conditions,the size of the array need not be specified? (a) when it is a formal parameter (b) when the compiler is smart (c) when initialization is a part of definition (d) when it is a declaration 48. If actual arguments are more than the formal arguments then (a) extra actual arguments are discarded (b) a compilation error (c) extra actual arguments are initialized to garbage values (d) extra actual arguments are initialized to zero 49. What is the output of the following program? main() {int x , y; x = 10; y = 100; change (x,y) VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 42

COMPUTER PROGRAMMING AND DATA STRUCTURES


printf (%d, %d, x,y); } change ( int x, int y); {int k; k = a; a = b; b = k; } (a) 10, 100 (b) 100, 100 (c) 10, 10 (d) 100, 10 50. An external variable definition can appear in (a) only two files (b) only three files (c) only one file (d) more than one file 51. For the array declaration int a[5]; What is the amount of storage required (a) 20 bytes (b) 5 bytes (c) 10 bytes (d) 5 bits 52. The default return type of a function is (a) float (b) Integer (c) character (d) double 53. What is the output of the following program void main() {int x = 2; sqr(x); printf(%d, x); }sqr(x) {int y; y = x * x; printf (%d, y); } (a) 2 2 (b) 4 4 (c) 4 2 (d) 24 54. What is the output of the following program main() {static int y; printf (%d\n, y); } (a) compilation error (b) run-time error (c) 0 (d) undefined 55. An array which contains two subscripts, to represent each element is called as (a) two dimensional (b) multidimensional (c) three dimensional (d) one dimensional 56. main() { char name[5]; scanf(%s,name); printf(%s, name); }if Program is the given as input, what will be the o/p of the program; (a) Progr (b) program (c) Prog (d) Runtime error 57. The parameters of the called function are called (a) casual parameters (b) actual parameters (c) usual parameters (d) formal parameters 58. output of the following program is #include< stdio.h> main() {int a,count; int funct( int count); for( count=1;count<=5;++count) VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 43

COMPUTER PROGRAMMING AND DATA STRUCTURES


{a=funct1(count); printf(%d,a); }} int funct1(int x) {int y; y= x*x; return(y); } (a) 1 4 9 16 25 (b) 36 (c) 25 (d) 25 9 16 59. Register variables can hold values (a) float (b) int (c) complex (d) double 60. The parameter passing mechanism for an array is (a) Call by value (b) Call by value-result (c) call by name (d) Call by reference 19. What kind of variable is suitable to count how many times a function is called (a) register (b) external (c) auto (d) static 61. An array can have dimension(s). (a) single (b) multiple (c) three (d) double 62. If we dont initialize a static array, what will be the elements set to: (a) character constant (b) a floating point number (c) 0 (d) an undetermined value 63. main() is (a) user defined function (b) friendly function (c) library function (d) member function 64. If the value of the formal argument is changed in the called function; the corresponding change in the calling function, if it is call by value (a) machine dependent (b) does not reflects (c) unpredictabl e (d) reflects 65. Which of the following statement is wrong with respect to a storage class (a) if specifies where variable is stored (b) it specifies the defualt initial value (c) it specifies the life of a variable (d) by defualt a storage class is static 66. What is the difference between the 5s in the below expressions. int num[5]; num[5]=10; (a) Both specify array size (b) First is array size, second is particular element (c) First is particular element , second is array size (d) First is particular element , second is type 67. Under which of the following conditions,the size of the array need not be specified? (a) when it is a formal parameter (b) when it is a declaration (c) when initialization is a part of definition (d) when the compiler is smart 68. Automatic variable are active (a) outside the function (b) only in the function where it is defined (c) surroundings of that function (d) throughout the program 69. Which of the following is the symbol for preprocessor VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 44

COMPUTER PROGRAMMING AND DATA STRUCTURES


(a) <> (b) $ (c) * (d) #

70. Which is a self contained block of code that performs a particular task A.Stack B. Array C. Pointer D. Function

UNIT-IV
1. A string is an array of (a) integers (b) floating point numbers (c) characters boolen values 2. A string in the C language is represented by enclosing a sequence of characters in (a) flower brackets (b) double quotes (c) parenthesis single quotes 3. What is wrong with the following program main() { char m1[9]= message1; char m2[9]=message2; m2=m1; printf(msg is %s,m2); } (a) array cannot be initialized as above (b) array is not a left value and so cannot be assigned to (c) char array cannot be printed directly using printf (d) program compiles without error, but prints an unpredictable value 4. The function strrev( ) the actual string in the array (a) will reverse (b) will not reverse (c) may not reverse may reverse 5. char x[10]=whatisit; char *y=whatisit; Pick the correct answer (a) The output of puts(x) and puts(y) will be different (b) The output of puts(x) and puts(y) will be same (c) The output of puts(y) is implementation dependant (d) The outputs of pulse(x) and puls(y) compiler dependant 6. The strcat( ) function is used for (a) Concatenation (b) Multiplication Addition 7.main( ) {int *j; { int i=10; j=&i;} } A. 7 B. 10 C. 5 D. compiler error (c) Joining (d) (d)

(d)

(d)

8. The general format of realloc function is VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 45

COMPUTER PROGRAMMING AND DATA STRUCTURES


A. ptr=realloc(size) realloc(ptr,newsize) B. ptr=realloc(ptr) D. ptr= realloc(free) C. ptr=

9. Which of the following standard file pointer is used for standard auxiliary device A. std prn conio B. std aux C. std io D. std

10. Array elements can be accessed using A. pointer queue B. Tree C. stack D.

11. Address x[ I ]=where I is of type int A. Base address + ( I ) factor of int) C. Index address + ( I + scale factor of int) B. Base address + (I+ scale D. Index address + ( I )

12. The pointer accessing method is _________ than Array indexing A. same efficient efficient B. slower C. faster D. less

13. The operators <and> are called A. in direction B. in out direction redirection operators 14. main( ) {int a=5,b,*c;c=&a;b=*c; printf("\n value of a=%d & b=%d",a,b,)} A. a=3,b=3 D. a=5,d=5 15 func(a,b) int a,b; {return(a=(a= =b)); } main( ) { int process( ),func( ); VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 46 B. a=4,b=4 C. a=6,d=6 C. out direction D.

COMPUTER PROGRAMMING AND DATA STRUCTURES


printf("The value of process is %d! \n", process(func,3,6)); } process(pf,val!,val2) int (*pf) ( ); int val1,val2; { return((*pf)(val1,val2)); } the value of process is A. 3! 16. main() {char s[6]="HELLO" printf("%d",s[6]); } output is A. compiler error 17. main() { int *j; { int i=10;j=&i; }printf("%d",*j); } A. 10 B. 20 C. 15 D. 5 B. 0 C. 34 D. 45 B. compiler error C.0! D. 1!

18. main( ) { int arr[ ]={'A', 'B', 'C', 'D'} int I; for( I=0; I<3; I++) printf("%d",arr[I]); } A. 65 66 67 ABC 19. #include<stdio.h> main( ) VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 47 B. A B C D C. 65 66 67 68 D.

COMPUTER PROGRAMMING AND DATA STRUCTURES


{ char s[ ]={'a', 'b','c',' \n ', 'c', '\0'}; char *p, *str, *str1; p=&s[3];str=p;str1=s; printf("%d",++*p+ ++*str1-32); } A. b M 20. Which of the following is used with Printf( ) function for printing the Address of a variable A. %d B. %u C. %f D. %c B. a C. Compiler error D.

21. Which of the following allows a program to read from or write to files at Command prompt A. in out direction redirection 22. main( ) { char *str1="abcd"; char str2[ ]="abcd"; printf("%d%d%d", sizeof(str1),sizeof(str2),sizeof("abcd"));} A. 2 5 5 5 23. The variable can be accessed by a Pointer using the Operators A. & and D. & and + B. & and * C. // and ++ B. 5 5 2 C. Compiler error D. 5 2 B. in direction C. out direction D.

24. A block of memory is allocated using the function A. falloc( ) D. calloc( ) B. dalloc( ) C. malloc( )

25. In which of the following header files the standard file pointers have been defined A. stdconio.h B. stdpointer.h C. stdstream.h D. stdio.h

26 Which of the following function places cursur at appropriate position on the screen VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 48

COMPUTER PROGRAMMING AND DATA STRUCTURES


A. go to( ) screen( ) B. goto xy( ) C. go ( ) D. goto

27. The process of call a function using pointers to pass the address of varaibles is known as A. call by argument by reference B. call by parameter C. call by value D. call

28. The arguments of calling functions are called A. parameter arguments D. Formal arguments B. Function call C. Actual arguments

29. Which of the following data structure used to process multiple elements with the same data types when number of such elements are know A. Graph B. Array C. Atack D. Quene

28. Which is not true among the following A. Pointers decrease the Execution speed B. Pointers are more efficient the arrays C. Pointers are more efficient in handling the data tables D. Pointers reduces the length and complexity of a program 29. Which of the following function takes the total number of data and size each data A. free B. realloc C. malloc D. calloc

30. A character array always ends with A. Full stop D. ? B. NULL character C. &

31. The indirection( * )operator is also called as A. deference operator bit wise operator B. Star operator C. Inference operator D.

32. Struct {int I; float J;) *s; if the address stores in S is 2000, what is the value of s+3? A. 2009 33. void main( ) { int const * p=5; VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 49 B. 2000 C. 2006 D. 2012

COMPUTER PROGRAMMING AND DATA STRUCTURES


printf("%d",++(*p));} A. Compiler error B. 10 C. 5 D. 15

34. Pointer to pointer is also called as A. Pointer function D. pointer indirection 35. main( ) { char *p; p="HELLO"; printf("%c \n ",*&*p); } A. HE B. HELLO C. HELL D. H B. Pointer structure C. Pointer direction

36. When we pass address to a function, the parameters recieving the address should be A. Pointers B. Union C.Functions D. Structures

37. The general format of calloc is A. ptr=(cast type *)calloc(byte size) C. ptr=(cast type*) type*)calloc(n,elementsize) B. ptr=calloc(cast type*)bytesize D. ptr=(cast

38. The first parameter in he command line is always A. struct name function name 39. main( ) { char *p; p="%d \n"; p++; p++; printf(p-2,300); } A. 296 B. 300 C. 298 D. compiler error B. Pointer name C. program name D.

VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 50

COMPUTER PROGRAMMING AND DATA STRUCTURES


40. Dynamic memory allocation in array results in (a) allocation of memory at compile time (b) allocation of memory at file saving time (c) allocation of memory at runtime (d) allocation of memory at debugging time 41. If the first string and the second string both are identical, then strcmp function returns (a) a value of 0 (b) either 1 or 0 (c) a value of 1 (d) any positive integer 42. What is the objective of the string handling function strncmp(). (a) Compares only last n characters in both argument (b) Compares two string only if length of one string is exactly n (c) Similar to strcmp() function (d) Compares only first n characters in both argument 43. Give the output for the following: # include main( ) { char arr[10]; int ctr =0; printf(Enter a Sting:);. gets(arr); while(arr[ctr]!=\0) {ctr + + ; }printf(%d,ctr); } (a) length of the string only when it is less than 9 characters length (b) String (c) String length (d) length of the string only when its length is 10 characters 45. Literal means (a) A string constant (b) An alphabet (c) A character (d) A string 46. What is the use of the strlen( ) function (a) finds the length of the string (b) string comparison (c) string concatenation (d) copies one string into another 47. The following function is used to count and return the number of characters in a given string (a) strcat() (b) strrev() (c) strcmp() (d) strlen() 48. Consider the following program main() { char str[10]; scanf(%s,str); printf(%s,str); } The error in the above program is (a) the format control for the string variable is not %s (b) no error in the program (c) memery has not been allocated for the variable str VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 51

COMPUTER PROGRAMMING AND DATA STRUCTURES


(d) the parameter str to scanf is passed by value, it should be a address 49. What is the use of strcmp( ) function (a) finds the length of the string (b) string comparison (c) copies one string into another (d) string concatenation

50. How many memory management functions are there in C a. 4 b. 3 c. 2 d. 1

51. Which of the following is not a C memory allocation function a. alloc( ) b. calloc( ) c. free d. malloc()

52. All memory management functions are found in a. stdlib.c b. stdio.h c. conio.h d. math.h

53. Which of the memory function allocates a contiguous memory a. malloc( ) b. calloc( ) c. release( ) d. free( ) 54. Return type of a malloc( ) function is a. int b. float c. char d. void

UNIT-V
1. The body of the stucture should end with A. semicolon( ; ) B. comma( , ) C. colon (:) D. period(.)

2. Example of a self-referential Structure A. Elements in a stack C. Elements in a quene B. Nodes in a Tree D. Nodes in a Linked list

3. Which of the following unary operator is used to specify the size of the structure A. Length( ) B. unary( ) C. sizeof( ) D. struct( )

4. The general formate of sending a copying of entire structure to the function is A. Structure name(function variable name) B. Function name (structure variable name) C. structure name(function ) D. function name(structure) 5. In arrays of structures the members are referred using A. * B. -> C. && D. ( . )

VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 52

COMPUTER PROGRAMMING AND DATA STRUCTURES 6. enum colors{BLACK,BLUE,GREEN} main( ) { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1);} A. 0,1,2 7. Struct a{int I; float j; }; Which of the following syntax is correct to declare a structure variable A. struct union a; t; 8. Which of the following are Parameters supplied to a program when te program is invoked A. Command line argument C. reference argument B. parameter argument D. Actual argumenent B. union struct a; C. union a t; D. Struct a B. compiler error C. 0,1 D. black

9. Structure elements can be accessed through a pointer to a sructure Using the operator A. ( , ) 10. struct{ int I; float j; } s; size(s) will be A. 6 bytes B. 0 bytes C. 2 bytes D. 4 bytes B. & C. * D. ->

11. The body of union should end with A. colon( :) ) 12. If struct member is array, then an individual array element can be accessed by writting a variable A. Stucture(member) C. member(structure) B. member(datatype) D. member(expression) B. Period ( . ) C. Semicolon( ; ) D. comma( ,

13. Which of the following is a costruct that allows memory to be shared by different types of data A. union
53

B. struct

C. Bit field

D. typedef

VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage

COMPUTER PROGRAMMING AND DATA STRUCTURES 14. Which of the following sign is used to access the structure members A. -> B. && C. ( . ) D. *

15. In union all members use A. no location B. same location C. different location D. no stroage

16. struct aaa{ struct aaa *prev; int i; struct aaa*next;}; main( ) { struct aaa abc,defghi,jkl; int x=100; abc.i=0;abc.prev=&jkl;abc.next=&def; def.i=1 ;def.prev=&abc;def.next=&ghi; ghi.i=2 ;ghi.prev=&def ;ghi.next=&jkl; jkl.i=3 ;jkl.prev=&ghi; jkkl.next=&abc; x=abc.next->next prev->next-> i; printf("%d",x);} A.2 B. Compiler error C. 3 D. 4

17. The typedef Statement is used for A. Declaring user defined data types C. for assigning values to vareiables B. Declaring variant data types D. for type casting of variables

18. Portion of memory to be shared by different types of data is A. field B. array C. struct D. union

19. Each field in a structure can be accessed and manipulated using A. variables and members C. operands and arguments B. variables and operands D. Expressions and operators
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 54

COMPUTER PROGRAMMING AND DATA STRUCTURES 20. #include<stdio.h> main( ) { struct xx { int x=3; char name[ ]="hello"; }; struct xx *s=malloc(sizeof(strct xx)); printf("%d",s->x); printf("%s"s->,name); } A. Compiler error B. hello C. xx D. name

21. The internal Representation of bit field is A. Human independent C. Human dependent 22. puts(argv[0])prints A. argv C. the name of executable code file B. the number of command line arguments D. the name of the source code file B. Machine dependent D. Machine independent

23. A type defined Structure starts with the keyword A. Case B. Void C. typedef D. Struct

24.Self referential structure means A. Refers to any structure C. Refers to different structure 25.#include<stdio.h> main( ) { struct xx
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 55

B. Refers to the same structure D. Refers to pointers

COMPUTER PROGRAMMING AND DATA STRUCTURES { int x=3; char name[]="hello";}; struct xx *s; printf("%d",s->x);printf("%s",s->name);} A. he B. hello C.compiler error D.hell

26. Which of the following is not true A. pointers can be used for addressing the bit field directly B. bit field provides exact amount of bits reserved for storage value C. Unions can apper in arrays and structures D. Unions are used to conserve memory 27. The type def Statement is used for A. Declaring user defined data types C. for assigning values to variables B. declaring variant variables D. for type casting of variables (d) struct

28. The keyword is used to define enumerated data type (a) array (b) enum (c) typedef

VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage 56

Вам также может понравиться