Sunteți pe pagina 1din 38

Previous Year Solved

Question Paper of
BCA in Programming
in C of Madras
University.
03/29/16

Referred Book:
Programming

in ANSI C, Second Edition, E.Balagurusamy

Programming

Language C with practicals ,Ananthi


Seshasaayee,G.Sheshasayee
Let

Us C by Yashavant P.Kanetkar

03/29/16

Madras University

(April 2010)

Part A(10*2=20)
Answer any ten questions.
1)

What are constants?


Ans: A constant does not change its value during the exection of the program
from the beginning till the end of a program.

2)

What are relational operator in C?


Ans: The relational operators are used to form simple condition.Thay are
embedded between two operands of similar data type. The following are the
relational operators available in C
03/29/16

Operator
<
<=
>
>=
==
!=
3)

Meaning
Less than
Less than or equal to
Greater than
Greater Than or equal to
Equal to
Not equal to

What is the purpose of conditional operator?


Ans:The C language has an unusual operator, useful for making two decisions.
This operator is a combination of ? And :, and takes three operands.This
operator is popularly Known as the Conditional operator.
03/29/16

Syntax:
Conditional expression?expression1:expression2
The Conditional expression is evaluated first. If the result is nonzero,
expression1 is evaluated and is returned as the value of the conditional
expression. Otherwise, expression2 is evaluated and its value is returned.
4) What is the use of \n?
Ans: C support special backslash character constants that are in output
function.The symbol \n stands for newline character
5) Define: recursion.
Ans: Recursion is a function that call itself repeatedly, until the required
condition is satisfied.

03/29/16

1. Iterative problems can be solved very effectively using the technique of


recursion
2. A recursive function should be able to call itself as well as check, whether the
condition for stopping the process is satisfied.
6) What is the static variable? When should it be used?
Ans:Static Variable-Local variable which exists and retains its value even
after the control is transferred to the calling function
Static should be used when we can tolerate or want only one copy of data and
also want our data secure from the outside world.
7) What is meant by prototyping?
Ans: A function prototype declares a function before it is used and prior to its
definition. A prototype consists of a function name, its return type and its
parameter list. However, the main() function does not need a prototype, since it
is predefined by the C language.
03/29/16

8)What is an array variable? How does it differ from an ordinary variable?


Ans: An array variable is declared in the same manner as ordinary variables. The array
variable declaration includes the size specification, which specifies the number of
elements that an array can hold at the maximum. The declaration of an array takes the
following form:

Type variable-name[size]
9)What are self-referential structures?
Ans: Structure which contain a member field that point to the same structure type are called
self-referential structures.
10) Mention the features of 'union' data type.
Ans: Union is similar to that of structure. The difference between Unions and structures is
the member of the union share the same storage area. However, only one variable can use
the memory location at any one time.

03/29/16

11) Write the advantages of pointers.


Ans: Advantage of Pointer:
1. A pointer enables us to access a variable that is defined outside
the function.
2. Pointer are more efficient in handling the data tables.
3. Pointers reduce the length and complexity of a program.
4. They increase the execution speed.
5. The use of a pointer array to character strings results in saving of data storage space
in memory.
12) What is the use of append mode in file handling?
Ans: Opens an existing file for appending ie., new information will be added at the end of
the file. This mode is safe, as the file, if its already exists, then the old contents are not
overwritten but added to the end of the file. If the file name specified does not exists, then
anew file is created.

03/29/16

Part- B (5 * 5 = 25)
Answer any five questions.
13) Write short notes on : declarations and expressions in c.
Ans: Declaration of Variable:
1. It defines the name of the variable.
2. It defines the type of the data the variable will hold.(int,float,char,etc)
Primary type declaration:
A variable can be used to store a value of any data type. That is, the
name has nothing to do its type. The syntax for declaring a variable is as
follows:
data-type v1,v2vn;
v1,v2,vn are the names of variables. Variables are seperated by commas. A
declaration statement must end with a semi colon. For example valid
declaration are:
int count;
int are the keywords to represent integer type data value respectively.
03/29/16

Expression:
Expression in c are any valid combination of operators, operands, function and
variables. Expression are evaluated using an assignment statement of the form.

variable =expression;
variable is any valid C variable name. When the statement is encountered, the
expression is evaluated first and the result then replaces the previous values of the
variable on the left-hand side. All variable used in the expression must be assigned values
before evaluation is attempted. Examples of evaluated statements are

x= a*b-c;
y=b/c*d;
Expression :

03/29/16

10

Algebra expression

C expression

ab-c
(m+n)(x+y)_
ab
c

a*b-c
(m+n)*(x+y)
a*b/c

14) Explain any four library functions with examples.


Ans: The C language is accomplished by a number of library functions that perform various
tasks. The ANSI committee has standardized header files which contain these functions.
What follows is a list of commonly used functions and the header files where they are
defined.
The header files that are included is
<math.h> Mathematical functions
<stdio.h> Standard I/O library functions
<string.h> String manipulation functions
<time.h>
Time manipulation functions

03/29/16

11

Library function example


#include<stdio.h>
main()
{
clrscr();
printf(Welcome to C program.");
getch();
}
15) Write a c program to find the sum of odd integers between 1 and n.
Ans: Program :
C program to find the sum of odd integers between 1 and n

03/29/16

12

#include<stdio.h>
main()
{
int i=1,sum=0,n;
printf("Enter the number ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
if(i%2!=0)
{
sum=sum+i;
}
}
printf("sum of odd number=%d",sum);
getch();
}

03/29/16

13

16) Compare 'while and 'do-while' statements.


Ans: While statement:
The simplest of all structure in C is the while statement. The
general form of while statement is
while (test condition)
{
body of the loop;
}
The while is an entry-controlled loop statement. The test
condition is evaluated and if the condition is true then the body of the loop is
executed .After execution of the body, the test condition is once again evaluated
and if its true the body is executed once again. This process of repeated
execution of the body continues until the test condition finally becomes false and
the control is transferred out of the loop.

03/29/16

14

Example program :

03/29/16

15

#include<stdio.h>
main()
{
int i=1;
printf(" Display the number from 1 to 100:\n");
while(i<=100)
{
printf("%d\n",i);
i++;
}
getch();
}

03/29/16

16

Do statement:
In the case of do while loop, the statement are executed at least
once before the conditional expression is tested for its truth. The difference
between the while and do-while loop is that the while loop test the conditional
expression before executing the statements, whereas the do-while loop
executes the range of the loop first and then tests the condition. Hence even if
the conditional expression returns a false value, the body of the loop would
have been executed at least once in the case of do-while loop. The general form
of the do-while loop is
do
{
body of the loop;
}while (test condition);
03/29/16

17

Example program:
#include<stdio.h>
main()
{
int x,y=0;
printf("\nEnter a number :");
scanf("%d",&x);
03/29/16

18

do
{
y=y+x%10;
x=x/10;
}while(x>0);
printf("\n The sum of the number is %d",y);
getch();
}

03/29/16

19

17) Write a C program to find the biggest of given n numbers.

Ans: Program:
#include<stdio.h>
main()
{
int m,n,i;
int larg;
printf("Enter the values of n: ");
scanf("%d",&m);
printf("Number %d :",1);
scanf("%d",&larg);
for(i=2;i<=m;i++)
{
printf("Number %d: ",i);
scanf("%d",&n);
03/29/16

20

if(larg<n)
larg=n;
}
printf("Largest number is: %d",larg);
getch();
}

18) Explain about the logical bitwise operators with examples.


Ans: The Logical bitwise operators are used for combining simple conditions,
and forming a complex condition. They return either true or false. The

03/29/16

21

The following are the three logical bitwise operator available in C:

Operator

Meaning

&&
||
!

Logical AND
Logical OR
Logical NOT

Example Program:
#include<stdio.h>
long fib(int);
main()
{
int n,i;
printf("Enter the values of n: ");
scanf("%d",&n);

03/29/16

22

for(i=1;i<n;i++)
{
printf("%d ",fib(i));
} getch();
}
long fib(int n)
{
if((n==1)||(n==2))
return(1);
else
return(fib(n-1)+fib(n-2));
}

03/29/16

23

19) Discuss briefly about pointer declarations with examples.


Ans: In C, every variable must be declared for its type. Since pointer variable
contain addresses that belong to a sepearte data type, they must be declared as
pointers before we use them. The declaration of a pointer variable takes the
following form:
data type *pt_name;
This tells the compiler three things about the variable pt_name.
1. The asterisk(*) tells that the variable pt_name is a pointer variable.
2. pt_name needs a memory location.
3. pt_name points to a variable of type data type.
Example:
int *p;
declares the variable p as pointer variable that points to an integer data type.
03/29/16

24

Part-C (3 * 10 = 30)
Answer any three questions.
20) Write a C program to evaluate the series:s = 1 + 1/2 + 1/3 + ... + 1/n.
Ans : Program:
#include<stdio.h>
main()
{
double n,sum=0,i;
printf("\n Give The Value of N: ");
scanf("%lf",&n);
for(i=1;i<=n;i++)
{
sum = sum + (1/i);
if(i==1)
printf("\n 1 +");
else

03/29/16

25

if(i==n)
printf(" (1/%lf) ",i);
else
printf(" (1/%lf) + ",i);
}
printf("\n\n THE SUM OF THIS SERIES IS %.2lf",sum);
getch();
}

03/29/16

26

21) Explain about various data input and output functions in c with suitable examples
Ans: Various input and output function available in c language are discussed below
(i) Putchar() function:
The standard function that prints a single character on the standard
output device [Monitor] is called putchar.
Example Program:
#include<stdio.h>
main()
{
putchar('M');
putchar('a');
putchar('d');
putchar('a');
putchar('m');
getch();
}

03/29/16

27

(ii)Getchar() function:
The getchar() function is used to accept a character from
the standard input device [keyboard]. The use of getchar is illustrated in the
following example
#include<stdio.h>
main()
{
char c;
c=getchar();
putchar(c);
}

03/29/16

28

(iii) Printf() function:


The printf() function is used for formatted output. The syntax
of the printf() function is
printf(control- string ,argument list);
In the above syntax, the control string refers to a string that
contains formatting information and the argument list refers to the data item
that have to be printed. The control string consists a set of characters proceeded
by a percentage sign (%d). These character are used to represent the appropriate
data type that has to be printed and they are called conversion characters

03/29/16

29

(iv)Scanf() function:
The scanf() function may contain only variable following the control string. It
should not contain any constant (or) values because the scanf() function is used to read values.
The syntax of the scanf() function is

scanf(control-string, argument list);


22) Write a C program which calls a function reverse() which accepts a string and displays its
reverse.
Ans: Program:
#include<stdio.h>
void reverse();
main()
{

03/29/16

30

printf("Enter the input string:");


reverse();
getch();
}
void reverse()
{
char s[80],a[10];
gets(s);
strrev(s);
printf("The reversed string is :");
puts(s);
}

03/29/16

31

23) Explain about passing arrays to functions with an example C program.


Ans: An array can be passed to a function as a parameter. To achieve this, the name
of the array is used as an argument to a function. The array name should not be
enclosed within brackets neither should it include any subscript. Also, the size of the
array should not be specified within the formal arguments declaration.
When an array is passed to function, the values of the array are not passed to
the function, but only the address of the first array element is passed. Hence
arguments are passed by reference and not by value.
Program:
#include <stdio.h>
int maximum( int [] );
int maximum( int x[5] )
{
int v, i;
v = x[0];
for( i = 0; i < 5; ++i )

03/29/16

32

if( x[i] > v )


v = x[i];
return v;
}
main()
{
int x[5], i, max;
printf("Enter 5 numbers\n");
for( i = 0; i < 5; ++i )
scanf("%d", &x[i] );
max = maximum( x );
printf("\nMaximum value is %d\n", max );
}

03/29/16

33

24) Describe in detail, file handling functions in c with examples.


Ans: File:
A group of releated records forms a file. Generally this type of file is called
data files.
File declaration:
In C program, all files are referred through file pointers. A file pointer is a
pointer variable of the type FILE. The syntax for defining a file pointer variable
fp is as follows

03/29/16

34

FILE *fp;
Opening a file:
A data file must be opened prior to reading from it or writing on to it. The
library function fopen() is used to open a file
syntax:
fp=fopen(filename,mode);
This function takes two arguments. The first argument is a pointer to a
string containing name of the file to be opened while the second argument is the
mode in which the file is to be opened. The mode can be :
r : Open text file for reading. The stream is positioned at the beginning of
the file.
r+ : Open for reading and writing. The stream is positioned at the beginning of
the file.

03/29/16

35

w : Truncate file to zero length or create text file for writing. The stream is
positioned at the beginning of the file.
w+ : Open for reading and writing. The file is created if it does not exist,
otherwise it is truncated. The stream is positioned at the beginning of the file.
a : Open for appending (writing at end of file). The file is created if it does not
exist. The stream is positioned at the end of the file.
a+ : Open for reading and appending (writing at end of file). The file is created
if it does not exist. The initial file position for reading is at the beginning of the
file, but output is always appended to the end of the file.
Example:
#include<stdio.h>
main()
{

03/29/16

36

FILE *fp1,*fp2;
.
.
fp1=fopen(emp.dat,r);
fp2=fopen(salary.txt,w);

..
}
Closing a file:
Every file opened must be closed when it is no longer required by the
program. When all the input/output operation are completed on it. The fclose()
function is used to close an file.
The general form of fclose() function is
03/29/16

37

fclose(fp);
Example:
#include<stdio.h>
main()
{
FILE *fp;
..
..
fp1=fopen(emp.dat,r);
..
fclose(fp1);
}
03/29/16

38

S-ar putea să vă placă și