BPUT MCA 2007_2009 Solved Questions

11
Previous Year MCA C Programs Solution Manual by Debesh Page 1 Written by Debesh Nayak Full Mark – 70 Time – 3 hours Only Short Types Questions Q1. a. Compare the following and comment: #include<stdio.h> and #include “stdio.h”. Ans: #include “stdio.h”: At this point, the pre-processor inserts the entire contents of stdio.h into the source code of the program. When the stdio.h is included within the double quotation marks, the search for the file is made first in the current directory and then in the standard directories. #include<stdio.h> : Here the compiler will search the file stdio.h only in the standard directories. b. Find the output of the following program segment: int a = 5; If (a = 7) printf(“Equal%d”,a); else printf(“Not equal%d”,a); Ans: Equal7, because the “test expression” of the if statement is a = 7, where the assignment operator (=) is used. Which assign value 7 to the variable a and due to lake of the relational operator the if condition is satisfied. c. Find the output of the following program segments: int I = 1; printf(“%d”, i, ++i, i++); Ans: 2, because the expression execute start from left to right inside the printf() function. So the leftmost term i = 1, then ++i = 2 because it is prefix operator is used, then i++ can not execute because of postfix operator. So the value of i = 2. d. Find the output of the following program segments: int i = 3, j; j = i && 0 || i; printf(“%d”, i); Ans: 3, because the value of i is initialized to 3 at the declaration part, which is not changed during next part of the program. e. Write the conditions for which recursion can be used in place of looping. Ans: Recursion is not a looping technique. PROGRAMMING IN ‘C’

Transcript of BPUT MCA 2007_2009 Solved Questions

Page 1: BPUT MCA 2007_2009 Solved Questions

Previous Year MCA C Programs Solution Manual by Debesh Page 1

Written by Debesh Nayak

Full Mark – 70 Time – 3 hours

Only Short Types Questions

Q1.

a. Compare the following and comment:

#include<stdio.h> and #include “stdio.h”.

Ans:

#include “stdio.h”: At this point, the pre-processor inserts the entire contents of stdio.h into

the source code of the program.

When the stdio.h is included within the double quotation marks, the search for the

file is made first in the current directory and then in the standard directories.

#include<stdio.h> : Here the compiler will search the file stdio.h only in the standard

directories.

b. Find the output of the following program segment:

int a = 5;

If (a = 7)

printf(“Equal%d”,a);

else

printf(“Not equal%d”,a);

Ans: Equal7, because the “test expression” of the if statement is a = 7, where the

assignment operator (=) is used. Which assign value 7 to the variable a and due to lake of the

relational operator the if condition is satisfied.

c. Find the output of the following program segments:

int I = 1;

printf(“%d”, i, ++i, i++);

Ans: 2, because the expression execute start from left to right inside the printf() function. So

the leftmost term i = 1, then ++i = 2 because it is prefix operator is used, then i++ can not

execute because of postfix operator. So the value of i = 2.

d. Find the output of the following program segments:

int i = 3, j;

j = i && 0 || i;

printf(“%d”, i);

Ans: 3, because the value of i is initialized to 3 at the declaration part, which is not changed

during next part of the program.

e. Write the conditions for which recursion can be used in place of looping.

Ans: Recursion is not a looping technique.

PROGRAMMING IN ‘C’

Page 2: BPUT MCA 2007_2009 Solved Questions

Previous Year MCA C Programs Solution Manual by Debesh Page 2

Written by Debesh Nayak

A loop is a group of instructions repeated several times, whereas recursion is when a

function calls itself.

Loop does exactly the same thing every time, round and round until the condition is

met.

But recursion calls itself, which is different data set that may or may not have calling

itself again.

Eg.

o (Factorial using Recursion)

factorial(int n)

{

int fact

if (n == 1)

retrun(1);

else

fact = n * factorial (n-1);

return(fact);

}

o (Factorial using loop)

for (i = n; i > 0; i--)

fact = fact * i;

return(fact);

f. Write the importance and scope of a static variable.

Ans: A static variables are of two type

1. Internal type

2. External type

The scope of internal static variables extend up to the end of the function in which they are

defined and they remain in existence throughout the remainder of the program.

The external static variable is declared outside of all functions and is available to all the

functions in that program.

g. Write and justify how many times the following for-loop will be executed:

int i;

for (i = 0; i <= 100; i++);

printf(“%d”, i);

Ans: 101, and the for-loop will executed 101 times, because i is initialized form 0 and the

condition is satisfied i.e. i <= 100.

h. Differentiate between a structure variable and a union variable.

Ans: Union:

Union allocates the memory equal to the maximum memory required by the

member of the union.

Here one block is used by all the member of the union.

Union is best in the environment where memory is less as it shares the memory

allocated.

Page 3: BPUT MCA 2007_2009 Solved Questions

Previous Year MCA C Programs Solution Manual by Debesh Page 3

Written by Debesh Nayak

As memory is shared, ambiguity is more in union.

Self-referential union cannot be implemented in any data structure.

Structure:

Structure allocates the memory equal to the total memory required by the

members.

Each member has their own memory space.

Structure cannot implement in shared memory.

Self-referential structure can be implemented in data structure.

i. Write the usefulness of command line argument.

Ans: It is a prarameter supplied to a program when the program is invoked. This parameter

may represent a filename the program should process.

e.g.: If we want to execute a program to copy the contents of a file named X_FILE to

another one named Y_FILE, then we may use a command line like

C> PROGRAM X_FILE Y_FILE

Where PROGRAM is the file name where the executable code of the program is stored. This

eliminates the need for the program to request the user to enter the filenames during

execution.

j. Differentiate between a macro and a function in C.

Ans: Macro:

Macro is preprocessed.

No type checking

Code length increases

Use of macro can lead to side effect

Speed of execution is Faster

Before Compilation macro name is replaced by macro value

Useful where small code appears many time

Generally Macros do not extend beyond one line

Macro does not Check Compile Errors

Function:

Function is compiled.

Type Checking is done.

Code Length remains same.

No side effect

Speed of Execution is slower

During function call, transfer of control takes place Useful where large code appear

many time.

Useful where large code appear many time

Function can be of any number of lines

Function checks Compile Error

Page 4: BPUT MCA 2007_2009 Solved Questions

Previous Year MCA C Programs Solution Manual by Debesh Page 4

Written by Debesh Nayak

Q1.

a. What are the function of control unit of a typical processor?

Ans.:

* Control unit directing the processes of CPU, input units, output units, storage devices,

communication interface unit..

* The control unit in a CPU does the controlling.

b. Define and differentiate between primary memory and secondary memory.

Ans: There are two classes of storage for data,

Primary Memory

Secondary Memory

Both types of storage are necessary for the operation of a computer.

Primary Memory

Fast:

Random access memory(RAM) is the main form of primary storage. The computer

processor uses this RAM to hold code, perform computations and manage the operation of

the machine because it is the fastest form of storing bytes of information.

It has faster access to the processor due to its proximity.

Volatile:

It is volatile, which means when the computer is turned off everything in RAM is

deleted.

Cost:

Primary storage usually has a higher cost than secondary storage.

Storage Capasity:

It storage capacity is less than secondary memory.

Secondary Memory

Speed:

It must transfer its data over a longer distance and through other channels before it

can reach the processor. So it is slower than Primary Memory.

Volatile

The disk drive is non-volatile and retains the information written to it after the

power is turned off to the computer.

The disk drives in the computer are the most common form of secondary storage

and other includes optical and tape drives.

Cost:

It is less costly than primary memory.

Storage Capasity:

Its storage capacity is very larger than a primary memory in a typical computer.

c. Differentiate between While and Do While loop.

Ans:

Page 5: BPUT MCA 2007_2009 Solved Questions

Previous Year MCA C Programs Solution Manual by Debesh Page 5

Written by Debesh Nayak

While:

Syntax:

while (expression)

{

statement;

statement;

}

From the above syntax it is clear that in while loop expression is tested first and then

the body is executed.

If expression is evaluated to true it is executed otherwise not.

It is a entry control loop

Do while:

Syntax:

do

{

statement;

statement;

}

while (expression);

Here body is executed first and then the expression is tested.

If test expression is evaluated to true then body of loop is again executed.

Here body of do while loop gets executed at least once, which is not necessaryin

while loop.

d. What is a self-referential structure?

Ans: In link list each structure of the list is called a node and consists of two fields:

* One containing the item

* Other containing the address of the next item (i. e. a pointer to the next item) in

the list.

So a linked list is a collection of structures ordered not by their physical placement

in memory (like an array) but by logica links that are stored as part of the data in the the

structure itself.

The link is in the form of a pointer to another structure of the same type.

Syntax:

struct <tag-name>

{

type member1;

type member2;

….

….

struct <tag-name> *next;

};

Such structures which contain a member field that points to the sme structure type

are called self-referential structure.

e. What is a stream pointer?

Page 6: BPUT MCA 2007_2009 Solved Questions

Previous Year MCA C Programs Solution Manual by Debesh Page 6

Written by Debesh Nayak

Ans: When working with stram oriented data files, such as text file,

First we have to establish a buffer area, where information is temporarily stored

while being transferred between computer’s memory and data file.

The buffer area can be established by declaring a file pointer. Which is called as

stream poiner.

f. How is an array name interpreted, when it passed to a function.

Ans: Entire array can be passed as an argument to a function.

* To pass an array as an argument to a function, the array name must be specified, without

brackets or subscripts, as an actual argument within the function call.

* The corresponding formal argument is written in the same manner, though it must be

declared as an array within the formal argument declarations.

* The size of the array is not specified within the formal argument declaration.

g. What are the fundamental data types in C? Explain the use of bitwise operators in C with

examples.

Ans: There are 5 fundamental data types available in C: Integer(int), Floating Point(float),

Character(char), Double Precision Floating Point(double) and void.

Bitwise Operator:

C supports bitwise operators for manipulation of data at bit level.

These operators are used for testing the bits, or shifting them right or left.

These operators may not applied to float or double.

There are three types of bitwise operators:

Bitwise logical

Bitwise shift

One’s Complement

Bitwise Logical:: There are 3 bitwise logical operators:

1. Bitwise AND (&):

2. Bitwise OR (|)

3. Bitwise exclusive OR (^)

Example:

If x = 13 and y = 25, then the binary representation of

x = 0000 0000 0000 1101

y = 0000 0000 0001 1001

then,

x & y = 0000 0000 0000 1001

x | y = 0000 0000 0001 1101

x ^ y = 0000 0000 0001 0100

Bitwise Shift Operators: The shift operators are used to move bit patterns either to the left

or to the right. There are 2 types of shift operators:

1. Left shift (<<)

2. Right Shift (>>)

Example:

if x = 0100 1001 1100 1011

x << 3 = 0100 1110 0101 1000

x >> 3 = 0000 1001 0011 1001

Page 7: BPUT MCA 2007_2009 Solved Questions

Previous Year MCA C Programs Solution Manual by Debesh Page 7

Written by Debesh Nayak

Bitwise Complement operators: It inverts all the bits represented by its operand.

Example:

if x = 1001 0110 1100 1011

~x = 0110 1001 0011 0100

g. Difference between array with functions and array with pointers.

Ans:

Array with functions:

1D:

1. The function must be called by passing only the name of the array

2. In the function definition, the formal parameter must be an array type; the size of the

array does not need to be specified.

3. The function prototype must show that the argument is an array.

2D:

1. The function must be called by passing only the array name.

2. In the function definition, we must indicate that the array has two dimension by

including two sets of brackets.

3. The size of the second dimension must be specified.

4. The prototype declaration should be similar to the function header

Array with pointer:

1. Declare a pointer variable.

2. Then make the pointer variable to point to the array by using assign operator (=).

3. We can access every value of the array x using increment operator on ht epointer

variable.

So to access the value of the 1D array pointer variable:

*(arry name + index no.)

And to access the address then

(arry name + index no.)

h. What is the difference between break and continue statement? Explain with the help of

examples.

Ans:

Break:

When a break statement is encountered inside a loop, the loop is immediately

exited and the program continues with the statement immediately following the loop. wne

the loops are nested, the break would only exit from the loop containing it, i.e., the break

will exit only a single loop.

Example of your own.

Continue:

Continue causes the loop to be continued with the next iteration after skipping any

statement after it.

In while and do while, continue causes the control to go directly to the test-

condition and then to continuethe iteration process.

In for loop, the increment section of the loop is executed before the test-condition is

evaluated.

Example of your own.

Page 8: BPUT MCA 2007_2009 Solved Questions

Previous Year MCA C Programs Solution Manual by Debesh Page 8

Written by Debesh Nayak

i. Read the following recursive program and answer the questions below:

int f(int k, int m)

{

if (k >= m) return 0;

else if (m % k == 0) return 1;

else return f(k+1, m);

}

What is the output on inputs k = 5 and m = 9?

Ans: Output: 0, because the recursion function work and reach a time when value of k = 9, m

= 9, at that point the “if” condition satisfy, and return 0 to the calling function and the else

part is not executed.

Page 9: BPUT MCA 2007_2009 Solved Questions

Previous Year MCA C Programs Solution Manual by Debesh Page 9

Written by Debesh Nayak

Full Mark – 70 Time – 3 hours

Only Short Types Questions

1. a. A computer system has eight bit addressing. What is the range of addresses? What is

the total number of memory cells it can address?

Ans: Computer system having 8-bit addressing capacity can addresses form 0000 0000 –

1111 1111 range of address..

And

The total number of memory cell it can address is: 28 = 256 bytes.

b. What is flow chart? List out symbols used in writing flowchart.

Ans: Defn: Flowchart is a graphical representation of an algorithm.

All symbols are connected among themselves to indicate the flow of informationand

processes.

The following are the symbols used in flow chart:

1. Terminal

2. Flow Direction

3. Connector

4. Input-Output

5. Decision making

6. Annotation

7. Process

8. Looping Predefined Process

c. What is the difference between these two declarations?

struct x1 {….};

teypedef struct {…} x2;

Ans:

In the first case after the structure definition, whenever we have to declare a

structure variable we have to use struct x1 <variable name>. Here we have to use

the struct tag repeatedly.

But in the second case, the repeatedly writing of struct tag is eliminated by using the

typedef keyword by x2 user-defineed structure type.

Here we can declare a structure variable in following manner:

x2 <variable name>;

d. What is meant by the equivalence of pointers and array in C?

Ans:

Equivalence of using pointer:

Pointer are more efficient in handling arrays and data tables.

Pointer can be used to return multiple values from a function via function

arguments.

Pointer permit references to function and thereby facilitating passing of functionsas

arguments to other functions.

PROGRAMMING IN ‘C’

Page 10: BPUT MCA 2007_2009 Solved Questions

Previous Year MCA C Programs Solution Manual by Debesh Page 10

Written by Debesh Nayak

The use of pointer arrays to character strings results in saving of data storage space

in memory.

Pointer allow C to support dynamic memory management.

It provide an efficient tool for manipulating dynamic data structures such as

o structures,

o linked lists,

o queues,

o stacks and

o trees

It reduces length and complexity of programs.

It increase the execution speed and thus reduce the program execution time.

Equivalence of using array:

Array provides a single name to represent a collection of items of the same type.

By the help of array we can efficiently store, access and manipulate data items of

same type.

e. What does extern mean in a function declaration?

Ans: The external declaration of a variable inside the functions informs the compiler that the

variable is defined somewhere else in the program.

The Extern declaration does not allocate storage space for variables.

f. What’s the difference between using a typedef or a #define for a user-defined type?

Ans:

typedef: It allows users to define an identifier that would represent an existing data type. We can

define using typedef keyword. The user-defined data type identifier can later be used to declare

variable.

Syntax: typedef <existing-data-type> <newtype>;

Example: typedef int unit;

unit n1, n2, n3 = 8;

#define: This directive defines an identifier and a string that will be substituted for the identifier

each time the identifier is encountered in the source file.

Here the identifier is called macro name and the replacement process is called as macro

substitution.

Syntax: #define <identifier name> <string>

g. You will do it.

h. You will do it.

i. What does it mean for a function parameter to be const? What do the two const’s meanmean in

the following statement:

int f(const * const p)

Ans:

If a variable is declared with const quantifier, then the value of the variable can’t be

changed by the program.

Any attempt to change the value of this variable in the program will result in an

error.

The const quantifier informs the compiler that the variable can be stored in read

only memory. (ROM)

Page 11: BPUT MCA 2007_2009 Solved Questions

Previous Year MCA C Programs Solution Manual by Debesh Page 11

Written by Debesh Nayak

A const variable can be given a vlue only through initialization or by some hardware

devices.

The value of a const variable can’t be modified by the program, but any external

event outside the program can change its value.

int f(const * const p)

Here the p passed from the other function is declared as a constant pointer to

constant integer.

We can neither change the pointer variable p nor the variable pointed by it.