BCA COMING EXAM QUESTION WITH ANSWER Course Code – 106 [‘C’])
Q.
What are the characteristics of C language? Discuss the rules for writing the C
program.
Ans:
We
briefly list some of C's characteristics that define the language and also have
lead to its popularity as a programming language. Naturally we will be studying
many of these aspects throughout the course.
Small
size
Extensive
use of function calls
Loose
typing - unlike PASCAL
Structured
language
Low
level (BitWise) programming readily available
Pointer
implementation - extensive use of pointers for memory, array, structures and
functions.
C
has now become a widely used professional language for various reasons.
It
has high-level constructs.
It
can handle low-level activities.
It
produces efficient programs.
It
can be compiled on a variety of computers.
Its
main drawback is that it has poor error detection which can make it off putting
to the beginner. However diligence in this matter can pay off handsomely since
having learned the rules of C we can break them. Not many languages allow this.
This if done properly and carefully leads to the power of C programming.
C Language:
C Language:
C
for the beginner 's by Md Farrukh Asif
Operators
in C by Md. Farrukh Asif
C's
Control Flow by Md. Farrukh Asif
String handling in C by Md Farrukh Asif
The
Function of C Language by Md. Farrukh Asif
File Handling
with various statements/syntaxes in Programming and their output in C Language.
C
for beginner's programming with output. Course Code – 106 [‘C’]) TMBU,BGP
BCA
SOLVED EXAMINATION QUESTIONS WITH ANSWERS Course Code – 106 [‘C’]) TMBU,BGP
C Language Basic Syntax Rules
C
language syntax specify rules for sequence of characters to be written in C
language. In simple language it states how to form statements in a C language
program - How should the line of code start, how it should end, where to use
double quotes, where to use curly brackets etc.
The rule specifies how the character sequence will be grouped together, to form tokens. A smallest individual unit in C program is known as C Token. Tokens are either keywords, identifiers, constants, variables or any symbol which has some meaning in C language. A C program can also be called as a collection of various tokens.
In the following program,
#include
int
main()
{
printf("Hello,World");
return 0;
}
if
we take any one statement:
printf("Hello,World");
Then
the tokens in this statement are→ printf, (, "Hello,World", ) and ;.
So
C tokens are basically the building blocks of a C program.
Semicolon
;
Semicolon; is used to mark the end of a statement and the beginning of another statement. The absence of a semicolon at the end of any statement will mislead the compiler to
think that this statement is not yet finished and it will add the next
consecutive statement after it, which may lead to a compilation(syntax) error.
#include
int
main()
{
printf("Hello,World")
return 0;
}
In
the above program, we have omitted the semicolon from the
printf("...") statement, hence the compiler will think that starting
from printf uptill the semicolon after return 0 statement, is a single
statement and this will lead to compilation error.
Comments
Comments
are plain simple text in a C program that are not compiled by the compiler. We
write comments for better understanding of the program. Though writing comments
is not compulsory, but it is recommended to make your program more descriptive.
It make the code more readable.
There are two ways in which we can write comments.
Using // This is used to write a single-line comment.
Using
/* */: The statements enclosed within /* and */ , are used to write multi-line
comments.
Example
of comments :
// This is a comment
/* This is a comment */
/* This is a long
and
valid comment */
// this is not
a valid comment
Some
basic syntax rule for C program
C
is a case sensitive language so all C instructions must be written in lower
case letter.
All
C statement must end with a semicolon.
Whitespace
is used in C to describe blanks and tabs.
Whitespace
is required between keywords and identifiers. We will learn about keywords and
identifiers in the next tutorial.
Q.
Discus the data type in C language? Explain the role of type modifiers with
respect to the data types. Write the uses of data type and type modifiers in the variable declaration.
Ans:
Data types in C Language
Data
types specify how we enter data into our programs and what type of data we enter.
C language has some predefined set of data types to handle various kinds of
data that we can use in our program. These datatypes have different storage
capacities.
C language supports 2 different types of data types:
Primary
data types:
These
are fundamental data types in C namely integer(int), floating point(float),
character(char) and void.
Derived
data types:
Derived
data types are nothing but primary datatypes but a little twisted or grouped
together like array, stucture, union and pointer. These are discussed in
details later.
Data
type determines the type of data a variable will hold. If a variable x is
declared as int. it means x can hold only integer values. Every variable that
is used in the program must be declared as what data type it is.
Primary
data types in c
Integer
type
Integers
are used to store whole numbers.
Size
and range of Integer type on a 16-bit machine:
Type Size(bytes) Byte Range
int
or signed int 2 -32,768 to
32767
unsigned
int 2 0
to 65535
short
int or signed short int 1 -128
to 127
unsigned
short int 1 0 to 255
long
int or signed long int 4 -2,147,483,648
to 2,147,483,647
unsigned
long int 4 0 to 4,294,967,295
Floating
point type
Floating
types are used to store real numbers.
Size
and range of Integer type on 16-bit machine
Type Size(bytes) Range
Float 4 3.4E-38
to 3.4E+38
double 8 1.7E-308
to 1.7E+308
long
double 10 3.4E-4932
to 1.1E+4932
Character
type
Character
types are used to store characters' value.
Size
and range of Integer type on 16-bit machine
Type Size(bytes) Range
char
or signed char 1 -128 to 127
unsigned
char 1 0
to 255
void
type
void
type means no value. This is usually used to specify the type of functions
which returns nothing. We will get acquainted with this datatype as we start
learning more advanced topics in C language, like functions, pointers, etc.
Data
type modifiers in C
In
c language Data Type Modifiers are keywords used to change the properties of
current properties of data type. Data type modifiers are classified into
following types.
·
long
·
short
·
unsigned
·
signed
Modifiers
are prefixed with basic data types to modify (either increase or decrease) the
amount of storage space allocated to a variable.
For
example, storage space for int data type is 4 byte for 32-bit processor. We can
increase the range by using long int which is 8 bytes. We can decrease the range
by using short int which is 2 byte.
long:
This
can be used to increase the size of the current data type to 2 more bytes, which
can be applied on int or double data types. For example, int occupies 2 bytes of
memory if we use long with integer variable then it occupies 4 byte of memory.
datatype Modifiers usage
Syntax
long
a; --> by default which represent
long int.
short
In
general int data type occupies different memory spaces for a different
operating system; to allocate fixed memory space short keyword can be used.
Syntax
short
int a; --> occupies 2 bytes of memory space in every operating system.
unsigned
This
keyword can be used to make the accepting values of a data type is positive
data type.
Syntax
unsigned
int a =100; // right
unsigned
int a=-100; // wrong
Signed
This
keyword accepts both negative or positive value and this is default properties
or data type modifiers for every data type.
Example
int
a=10; // right
int
a=-10; // right
signed
int a=10; // right
signed
int a=-10; // right
Q. How can you perform input-output operations in ‘C’ programs?
Explain standard I/O operations along with the formatting field width precision
rnge of data and data conversion.
Ans:
C
provides standard functions scanf() and printf(), for performing formatted
input and output .These functions accept, as parameters, a format specification
string and a list of variables.
The
format specification string is a character string that specifies the data type
of each variable to be input or output and the size or width of the input and
output.
Now
to discuss formatted output in functions.
Formatted
Output
The
function printf() is used for formatted output to standard output based on a
format specification. The format specification string, along with the data to
be output, are the parameters to the printf() function.
Syntax:
printf
(format, data1, data2,……..);
In
this syntax format is the format specification string. This string contains,
for each variable to be output, a specification beginning with the symbol %
followed by a character called the conversion character.
Example:
printf
(“%c”, data1);
The
character specified after % is called a conversion character because it allows
one data type to be converted to another type and printed.
See
the following table conversion character and their meanings.
Conversion
Character Meaning
d The
data is converted to decimal (integer)
c The
data is taken as a character.
s The
data is a string and character from the string , are printed
until a NULL, character is
reached.
f The data is output as float or double with a default Precision 6.
Symbols Meaning
\n For
new line (linefeed return)
\t For
tab space (equivalent of 8 spaces)
Example
printf
(“%c\n”,data1);
The
format specification string may also have text.
Example
printf
(“Character is:”%c\n”, data1);
The
text "Character is:" is printed out along with the value of data1.
Example
with program
#include<stdio.h>
#include<conio.h>
Main()
{
Char
alphabh="A";
int
number1= 55;
float
number2=22.34;
printf(“char=
%c\n”,alphabh);
printf(“int=
%d\n”,number1);
printf(“float=
%f\n”,number2);
getch();
clrscr();
retrun
0;
}
Output
Here…
char
=A
int=
55
flaot=22.340000
Q. (a) What do you mean by recursive function? Discuss the structure
of a recursive function.
Ans:
A
recursive function is a function that calls itself during its execution. This
enables the function to repeat itself several times, outputting the result and
the end of each iteration. Below is an example of a recursive function.
function
Count (integer N)
if (N <= 0) return "Must be a
Positive Integer";
if (N > 9) return "Counting
Completed";
else return Count (N+1);
end
function
The
function Count() above uses recursion to count from any number between 1 and 9,
to the number 10. For example, Count(1) would return 2,3,4,5,6,7,8,9,10.
Count(7) would return 8,9,10. The result could be used as a roundabout way to
subtract the number from 10.
(b)
Write a recursive function for calculate the factorial.
Ans:
The
factorial of a positive number n is given by:
factorial
of n (n!) = 1 * 2 * 3 * 4 *... * n
The
factorial of a negative number doesn't exist. And the factorial of 0 is 1.
You
will learn to find the factorial of a number using recursion in this example. Visit
this page to learn how you can find the factorial of a number using a loop.
Factorial
of a Number Using Recursion
#include<stdio.h>
long
int multiplyNumbers(int n);
int
main() {
int n;
printf("Enter a positive integer:
");
scanf("%d",&n);
printf("Factorial of %d = %ld",
n, multiplyNumbers(n));
return 0;
}
long
int multiply numbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
Output
Enter
a positive integer: 6
Factorial
of 6 = 720
Q. (a) What do you mean by the nesting of functions? Discuss.
Ans:
In
computer programming, a nested function (or nested procedure or subroutine) is
a function which is defined within another function, the enclosing function.
Due to simple recursive scope rules, a nested function is itself invisible
outside of its immediately enclosing function, but can see (access) all local
objects (data, functions, types, etc.) of its immediately enclosing function as
well as of any function(s) which, in turn, encloses that function. The nesting
is theoretically possible to unlimited depth, although only a few levels are
normally used in practical programs.
Nested
functions are used in many approaches to structured programming, including
early ones, such as ALGOL, Simula 67 and Pascal, and also in many modern
dynamic languages and functional languages. However, they are traditionally not
supported in the (originally simple) C-family of languages.
(b) write a program to find sum of four integers using nested
function call.
Ans:
#include
<stdio.h>
/*
A naive solution to print all combination of 4 elements in A[]
with sum equal to X */
void
findFourElements(int A[], int n, int X)
{
// Fix the first element and find other three
for (int i = 0; i < n-3; i++)
{
// Fix the second element and find other
two
for (int j = i+1; j < n-2; j++)
{
// Fix the third element and find the
fourth
for (int k = j+1; k < n-1; k++)
{
// find the fourth
for (int l = k+1; l < n; l++)
if (A[i] + A[j] + A[k] + A[l] == X)
printf("%d, %d, %d,
%d", A[i], A[j], A[k], A[l]);
}
}
}
}
//
Driver program to test the above function
int
main()
{
int A[] = {10, 20, 30, 40, 1, 2};
int n = sizeof(A) / sizeof(A[0]);
int X = 91;
findFourElements (A, n, X);
return 0;
}
Q, Write a program to convert a Tabular Matrix to Row Matrix.
Ans:
#include
<stdio.h>
#define
N 4
//
This function stores transpose of A[][] in B[][]
void
transpose(int A[][N], int B[][N])
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
B[i][j] = A[j][i];
}
int
main()
{
int A[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
int B[N][N], i, j;
transpose(A, B);
printf("Result matrix is \n");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
printf("%d ", B[i][j]);
printf("\n");
}
return 0;
}
Output:
Result
matrix is
1
2 3 4
1
2 3 4
1
2 3 4
1
2 3 4
*** See You Again ***
*****************
Share
and Comment
*****************
No comments:
Post a Comment