ii mid answers
TRANSCRIPT
-
7/29/2019 II MID Answers
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 wouldbe 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, becausethese 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
#include
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
-
7/29/2019 II MID Answers
2/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
#include
#include
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
-
7/29/2019 II MID Answers
3/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
Storage Classes Table:
S.No Name Keyword Initial
value
Scope Lifetime Memory
Location
1 Automatic
storage class
auto Garbage
value
Within the
function(localto block)
Within the
function(local toblock)
RAM
2 External storage
class
extern Zero Throughout the
program
Throughout the
program
RAM
3 Static storage
class
static Zero Within the
function(local
to block)
Retain the value
for different
function calls
RAM
4 Register storage
class
register Garbage
value
Within the
function(local
to block)
Within the
function(local to
block)
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.
[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
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage
3
-
7/29/2019 II MID Answers
4/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
#include
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
-
7/29/2019 II MID Answers
5/56
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
-
7/29/2019 II MID Answers
6/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
6 7 8
3 2 5
2 4 1
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
#include
long int fact(long int);
void main()
{
long int n;
clrscr();
printf("Enter n value ");
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage
6
-
7/29/2019 II MID Answers
7/56
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 isstored 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
-
7/29/2019 II MID Answers
8/56
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 Forexample. 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 somevalues 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
-
7/29/2019 II MID Answers
9/56
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. Functions with no arguments and no return values.
2. Functions with arguments and no return values.
3. Functions with arguments and return values.4. 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 typeof 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
-
7/29/2019 II MID Answers
10/56
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 forfurther 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
-
7/29/2019 II MID Answers
11/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
An array in C language is a collection of similardata-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
http://rajkishor09.hubpages.com/_gsm/hub/Data-Types-in-C-Languagehttp://rajkishor09.hubpages.com/_gsm/hub/Data-Types-in-C-Languagehttp://hubpages.com/_eknow/hub/How-to-work-with-Two-Dimensional-Arrays-in-Chttp://hubpages.com/_eknow/hub/How-to-work-with-Two-Dimensional-Arrays-in-Chttp://hubpages.com/_eknow/hub/How-to-work-with-Multidimensional-Array-in-C-Programminghttp://hubpages.com/_eknow/hub/How-to-work-with-Multidimensional-Array-in-C-Programminghttp://hubpages.com/_eknow/hub/How-to-work-with-Two-Dimensional-Arrays-in-Chttp://hubpages.com/_eknow/hub/How-to-work-with-Multidimensional-Array-in-C-Programminghttp://rajkishor09.hubpages.com/_gsm/hub/Data-Types-in-C-Language -
7/29/2019 II MID Answers
12/56
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
#include
void main()
{
int a[10]={10,20,30,40,50},i;
clrscr();
printf("given elements are\n");
for(i=0;i
-
7/29/2019 II MID Answers
13/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
Memory Map of 2D Array
#include
#include
void main()
{
int a[10][10]={{1,2,3},{4,5,6}},i,j;
printf{"given matrix is \n");
for(i=0;i
-
7/29/2019 II MID Answers
14/56
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
-
7/29/2019 II MID Answers
15/56
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
#include
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
-
7/29/2019 II MID Answers
16/56
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
#include
void swap(int *,int *);
void main()
{
int a,b;
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage
16
-
7/29/2019 II MID Answers
17/56
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
-
7/29/2019 II MID Answers
18/56
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 elementsDynamic 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 header file.
S NoFunction
NameSyntax Description
1 malloc() void *malloc(int num); This function allocates an array
ofnum elements each of which sizein bytes will be size.
2 calloc() void *calloc(int num, int size); This function allocates an array
ofnum elements each of which size
in bytes will be size.
3 realloc() void *realloc(void *address, int
newsize);
This function re-allocates memory
extending it upto newsize.
4 free() void free(void *address); 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
#include
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
-
7/29/2019 II MID Answers
19/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
for(i=0;i
-
7/29/2019 II MID Answers
20/56
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
Some of the functions present in 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
-
7/29/2019 II MID Answers
21/56
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
-
7/29/2019 II MID Answers
22/56
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
-
7/29/2019 II MID Answers
23/56
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 usuallyoccupies 4 bytes of memory (then it can address cells from 0 to 232-1).
Advantages of Pointers :
1. A pointer enables us to access a variable that is defined out side the function.
2. Pointers are more efficient in handling the data tables.
3. Pointers reduce the length and complexity of a program.
4. They increase the execution speed.
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage
23
-
7/29/2019 II MID Answers
24/56
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; /* pointer to int */
char *s; /* pointer to char */
float *fp; /* pointer to float */
char **s; /* 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
-
7/29/2019 II MID Answers
25/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
Output
Printf(The Value of x is %d,x); 5
Printf(The Address of x is %u,&x); 2000
Printf(The Address of x is %u,p); 2000
Printf(The Value of x is %d,*p); 5
Printf(The Value of x is %d,*(&x)); 5
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
#include
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
-
7/29/2019 II MID Answers
26/56
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 STRUCTURESenter 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
struct address{
char*name;
charstreet[10];
int pin;
};
int main()
{
struct address cus={"A.Kumar","H-2",456003}*p=&cus;
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage
26
-
7/29/2019 II MID Answers
27/56
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 singlememory location.
Union is similar to that of stucture. Syntax of union is similar to stucture. But the
majordifference 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
{ element 1;
element 2;
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
-
7/29/2019 II MID Answers
28/56
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
#include
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
-
7/29/2019 II MID Answers
29/56
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 self-
referential 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 typedefwhich you can use to give a
type a new name.
syntax:
typedef data_type userdefined_data_type;
ex:VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage
29
-
7/29/2019 II MID Answers
30/56
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 namedinteger 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 identifiernames 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
http://void%280%29/http://void%280%29/ -
7/29/2019 II MID Answers
31/56
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
{
element 1;
element 2;- - - - - - - - - - -
- - - - - - - - - - -
element n;
struct structure_nm
{
element 1;
element 2;
- - - - - - - - - - -
- - - - - - - - - - -
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
-
7/29/2019 II MID Answers
32/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
s.d.year
Program :
#include
#include
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
#include
struct employee{
int id;
char name[50];
}emp[2];
void main()
{
int i;
clrscr();
for(i=0;i
-
7/29/2019 II MID Answers
33/56
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
-
7/29/2019 II MID Answers
34/56
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;
union union_name
{
union element 1;
union element 2;
--------------------
union element n;
}union_var_nm;
vi. Example
struct item_mst
{
int rno;
char nm[50];
}it;
union item_mst
{
int rno;
char nm[50];
}it;Fig.: Difference between Structure and Union
Difference Between Array and Structure :
Array Structure
i. Data Collection
Array is a collection of homogeneous
data.
Stucture is a collection of heterogeneous
data.
ii. Element Reference
Array elements are referred by
subscript.
Structure elements are referred by its
unique name.
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
-
7/29/2019 II MID Answers
35/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
iv. Data type
Array is a derived data type. Structure is user defined data type.
v. Syntax
array_name[size];
struct struct_name
{
structure element 1;
structure element 2;
----------
----------
structure element n;
}struct_var_nm;
vi. Example
int rno[5];
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
-
7/29/2019 II MID Answers
36/56
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
-
7/29/2019 II MID Answers
37/56
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 #include
struct student {
int id;
char *name;
floatpercentage;
} 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
-
7/29/2019 II MID Answers
38/56
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
-
7/29/2019 II MID Answers
39/56
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) 214. When compared to call by value, the call by reference is in execution(a) equal (b) fast (c) slow (d) neither slow nor fast15. 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 variableis stored16. 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 B. two times C. three times
D. any no.of times
20. A function gets called when the function name is followed by
A. comma( ,) B. Period ( .) C. Colon( :)
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
-
7/29/2019 II MID Answers
40/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
22. Array elements can be accessed using
A. Tree B. Pointer C. stack D.
Queue
23. An array is represented in Memory by using a
A.Sequential map B. Parallel map C. element map D. circular
map
24. The general form of array declaration is
A. Type array name[type] B. Type variable name[size]
C. Type data name[type] D. Type data name[size]
26. int b[5];
main( )
{ static int a[5];
int I;
for(I=0;I
-
7/29/2019 II MID Answers
41/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
A. 10 20 30 40 B. 10 20 30 C. 10 20 D.compilier
error
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 type31. 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.h33. What is the output of the following program# includes
# includevoid main(){int a1[5] ={1};int b = 0, k = 0;for (b=0; b
-
7/29/2019 II MID Answers
42/56
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 value41. The library function sqrt operates on a double pression argument and I is aninteger variable, which one of thefollowing 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 error43. 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) 144. The variables which are declared outside the program are called(a) global variables (b) formal variables (c) local variables (d)register variable45. #include is a directive(a) processor (b) complier (c) pre-compiler (d) pre-processor46. 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 order47. Under which of the following conditions,the size of the array need not bespecified?(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 declaration48. 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 zero49. 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
-
7/29/2019 II MID Answers
43/56
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, 1050. An external variable definition can appear in(a) only two files (b) only three files (c) only one file (d) more thanone file51. 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 bits52. The default return type of a function is(a) float (b) Integer (c) character (d) double53. What is the output of the following programvoid 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)2 454. What is the output of the following programmain(){static int y;printf (%d\n, y);}(a) compilation error (b) run-time error (c) 0 (d) undefined55. An array which contains two subscripts, to represent each element is calledas(a) two dimensional (b) multidimensional (c) three dimensional (d)one dimensional56. 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) Runtimeerror57. The parameters of the called function are called(a) casual parameters (b) actual parameters (c) usual parameters (d)formal parameters58. output of the following program is#include< stdio.h>main(){int a,count;int funct( int count);for( count=1;count
-
7/29/2019 II MID Answers
44/56
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 1659. Register variables can hold values(a) float (b) int (c) complex (d)double60. The parameter passing mechanism for an array is(a) Call by value (b) Call by value-result (c) call by name (d)Call by reference19. 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) double62. 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) anundetermined value63. main() is(a) user defined function (b) friendly function (c) library function (d)member function64. If the value of the formal argument is changed in the called function; thecorresponding change in the callingfunction, if it is call by value(a) machine dependent (b) does not reflects (c) unpredictabl e
(d) reflects65. 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 initialvalue(c) it specifies the life of a variable (d) by defualt a storage class isstatic66. 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 type67. Under which of the following conditions,the size of the array need not bespecified?(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 issmart68. Automatic variable are active(a) outside the function (b) only in the function where it isdefined(c) surroundings of that function(d) throughout the program69. Which of the following is the symbol for preprocessor
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage
44
-
7/29/2019 II MID Answers
45/56
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 (d)
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 (d)
single quotes
3. What is wrong with the following programmain() { 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 (d)
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 (c) Joining (d)
Addition
7.main( )
{int *j;
{ int i=10; j=&i;}
}
A. 7 B. 10 C. 5 D. compiler error
8. The general format of realloc function is
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage
45
-
7/29/2019 II MID Answers
46/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
A. ptr=realloc(size) B. ptr=realloc(ptr) C. ptr=
realloc(ptr,newsize) D. ptr= realloc(free)
9. Which of the following standard file pointer is used for standard auxiliary
device
A. std prn B. std aux C. std io D. std
conio
10. Array elements can be accessed using
A. pointer B. Tree C. stack D.
queue
11. Address x[ I ]=where I is of type int
A. Base address + ( I ) B. Base address + (I+ scale
factor of int)
C. Index address + ( I + scale factor of int) D. Index address + ( I )
12. The pointer accessing method is _________ than Array indexing
A. same efficient B. slower C. faster D. less
efficient
13. The operators are called
A. in direction B. in out direction C. out direction D.
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 B. a=4,b=4 C. a=6,d=6
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
-
7/29/2019 II MID Answers
47/56
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! B. compiler error C.0! D. 1!
16. main()
{char s[6]="HELLO"
printf("%d",s[6]);
}
output is
A. compiler error B. 0 C. 34 D. 45
17. main()
{ int *j;
{ int i=10;j=&i;
}printf("%d",*j);
}
A. 10 B. 20 C. 15 D. 5
18. main( ) { int arr[ ]={'A', 'B', 'C', 'D'}
int I; for( I=0; I
-
7/29/2019 II MID Answers
48/56
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 B. a C. Compiler error D.
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
21. Which of the following allows a program to read from or write to files at
Command prompt
A. in out direction B. in direction C. out direction D.
redirection
22. main( )
{ char *str1="abcd";
char str2[ ]="abcd";
printf("%d%d%d", sizeof(str1),sizeof(str2),sizeof("abcd"));}
A. 2 5 5 B. 5 5 2 C. Compiler error D. 5 2
5
23. The variable can be accessed by a Pointer using the Operators
A. & and - B. & and * C. // and ++
D. & and +
24. A block of memory is allocated using the function
A. falloc( ) B. dalloc( ) C. malloc( )
D. calloc( )
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
-
7/29/2019 II MID Answers
49/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
A. go to( ) B. goto xy( ) C. go ( ) D. goto
screen( )
27. The process of call a function using pointers to pass the address of varaibles
is known as
A. call by argument B. call by parameter C. call by value D. call
by reference
28. The arguments of calling functions are called
A. parameter arguments B. Function call C. Actual arguments
D. Formal 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 eachdata
A. free B. realloc C. malloc D. calloc
30. A character array always ends with
A. Full stop B. NULL character C. &
D. ?
31. The indirection( * )operator is also called as
A. deference operator B. Star operator C. Inference operator D.
bit wise operator
32. Struct {int I; float J;) *s; if the address stores in S is 2000, what is the value of
s+3?
A. 2009 B. 2000 C. 2006 D. 2012
33. void main( )
{ int const * p=5;
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage
49
-
7/29/2019 II MID Answers
50/56
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 B. Pointer structure C. Pointer direction
D. pointer indirection
35. main( )
{ char *p; p="HELLO";
printf("%c \n ",*&*p);
}
A. HE B. HELLO C. HELL D. H
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) B. ptr=calloc(cast type*)bytesize
C. ptr=(cast type*) D. ptr=(cast
type*)calloc(n,elementsize)
38. The first parameter in he command line is always
A. struct name B. Pointer name C. program name D.
function name
39. main( )
{
char *p; p="%d \n";
p++;
p++;
printf(p-2,300);
}
A. 296 B. 300 C. 298 D. compiler error
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage
50
-
7/29/2019 II MID Answers
51/56
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 strcmpfunction 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:
# includemain( )
{ 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 another47. 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 strVIDYA VIKAS INSTITUTE OF TECHNOLOGYPage
51
-
7/29/2019 II MID Answers
52/56
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 memorya. 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 B. Nodes in a Tree
C. Elements in a quene 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
-
7/29/2019 II MID Answers
53/56
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 B. compiler error C. 0,1 D. black
7. Struct a{int I;
float j;
}; Which of the following syntax is correct to declare a structure variable
A. struct union a; B. union struct a; C. union a t; D. Struct a
t;
8. Which of the following are Parameters supplied to a program when te program is invoked
A. Command line argument B. parameter argument
C. reference argument D. Actual argumenent
9. Structure elements can be accessed through a pointer to a sructure Using the operator
A. ( , ) B. & C. * D. ->
10. struct{ int I;
float j; } s; size(s) will be
A. 6 bytes B. 0 bytes C. 2 bytes D. 4 bytes
11. The body of union should end with
A. colon( :) B. Period ( . ) C. Semicolon( ; ) D. comma( ,
)
12. If struct member is array, then an individual array element can be accessed by writting a
variable
A. Stucture(member) B. member(datatype)
C. member(structure) D. member(expression)
13. Which of the following is a costruct that allows memory to be shared by different types of
data
A. union B. struct C. Bit field D. typedef VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage
53
-
7/29/2019 II MID Answers
54/56
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 B. Declaring variant data types
C. for assigning values to vareiables 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 B. variables and operands
C. operands and arguments D. Expressions and operators
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage
54
-
7/29/2019 II MID Answers
55/56
COMPUTER PROGRAMMING AND DATA STRUCTURES
20. #include
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 B. Machine dependent
C. Human dependent D. Machine independent
22. puts(argv[0])prints
A. argv B. the number of command line arguments
C. the name of executable code file D. the name of the source code file
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 B. Refers to the same structure
C. Refers to different structure D. Refers to pointers
25.#include
main( )
{
struct xx
VIDYA VIKAS INSTITUTE OF TECHNOLOGYPage
55
-
7/29/2019 II MID Answers
56/56
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 B. declaring variant variables
C. for assigning values to variables D. for type casting of variables
28. The keyword is used to define enumerated data type
(a) array (b) enum (c) typedef (d) struct