Amity C Question Paper
Amity C Question Paper
Amity C Question Paper
SUBMITTED BY:
Rishabh Shukla
A2305216665
3CSE3- Y
2016
Q1. What is the function of storage unit of a computer system? How many types of storage
are there in a storage unit? Justify the need of each storage.
Ans1. Storage Unit: The storage unit is used for storing data and instructions before and after
processing. There are two types of storage devices used with computers: a primary storage
device, such as RAM, and a secondary storage device, like a hard drive.
Q2. (a) Differentiate between structure and union?
Ans. Structure: To define a structure, you must use the struct statement. The struct statement
defines a new data type, with more than one member.
Union: To define a union, you must use the union statement in the same way as you did while
defining a structure. The union statement defines a new data type with more than one member
for your program.
(b) What are pre-processor directives and command line arguments?
Ans. Pre-processor directives are lines included in the code of programs preceded by a hash sign
(#). These lines are not program statements but directives for the pre-processor. The pre-
processor examines the code before actual compilation of code begins and resolves all these
directives before any code is actually generated by regular statements.
Command line argument is a parameter supplied to the program when it is invoked. It is mostly
used when you need to control your program from outside. Command line arguments are passed
to main() method.
Q3. Write a program to swap two numbers without using third number.
Ans. #include<stdio.h>
#include<conio.h>
main()
{
int a=10, b=20;
clrscr();
printf("Before swap a=%d b=%d",a,b);
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
printf("\nAfter swap a=%d b=%d",a,b);
getch(); }
Q4. What do you mean by Array? Explain multidimensional array.
Ans. An array is a data structure that contains a group of elements. Typically these elements are
all of the same data type, such as an integer or string.
C allows for arrays of two or more dimensions. A two-dimensional (2D) array is an array of
arrays. A three-dimensional (3D) array is an array of arrays of arrays is called multidimensional
array. A multidimensional array is declared using the following syntax:
type array_name[d1][d2][d3][d4][dn];
Q5. (a) What is the difference between if. Then and if. Then else statement.
Ans. The if-then statement is the most basic of all the control flow statements. It tells your
program to execute a certain section of code only if a particular test evaluates to true.
The if-then-else statement provides a secondary path of execution when an "if" clause evaluates
to false.
(b) Both DOWHILE and FOR are used for looping. Discuss the difference between
two.
Ans. Both for loop and do-while loop are used to execute one or more lines of code certain
number of times.
In for loop the initialization step is executed if it is there. It is never executed again. After this
condition expression is evaluated and if it is found true then body of loop is executed after this
control goes to third part i.e. increment or decrement if it is defined. Then again control goes to
check the condition again and if found true then body gets executed and this goes on until
condition becomes false.
The do while loop executes the content of the loop once before checking the condition of the
while.
Q6. Convert the following
(24.3)10 = (220.022)3
(110.101)2 = (6.625)10
(1AC) = (428)10
(42.25)10 = (101010.0100000)2
(4052)6 = (2420)7
7. (a) Explain the concept behind using pointers and give an example of array of pointers.
Ans. A pointer is a variable whose value is the address of another variable, i.e., direct address of
the memory location. Like any variable or constant, you must declare a pointer before using it to
store any variable address.
#include <stdio.h>
const int MAX = 3;
int main ()
int var[] = {10, 100, 200};
int i, *ptr[MAX];
for ( i = 0; i < MAX; i++) {
ptr[i] = &var[i]; /* assign the address of integer. */
}
for ( i = 0; i < MAX; i++) {
printf("Value of var[%d] = %d\n", i, *ptr[i] );
}
return 0;
}
(b) What is von- Neumann architecture? Explain the function of various units.
Ans. Von Neumann Architecture also known as the Von Neumann model, the computer
consisted of a CPU, memory and I/O devices. The program is stored in the memory. The CPU
fetches an instruction from the memory at a time and executes it.
The CPU- The CPU, or Central Processing Unit, is the name given to the component that
controls the computer and works on the data.
IAS- Immediate Access Store, where computer puts both programs and data. We often
commonly refer to this memory as RAM.
I/O- A computer needs peripherals for inputting and outputting data. It needs to be able to read
data into itself and send data out. It reads data in and sends data out through its I/O ports.
8. (a) Explain various operations performed on file and also the file opening modes. Write a
program to open a pre-existing file and information at the end of the file. Display the
contents of file before and after appending.
r Reading NULL
Create New
w Writing Over write on Existing
File
Create New
a Append
File
Create New
w+ Reading + Writing Over write on Existing
File
b. #include <stdio.h>
#include <stdlib.h>
main()
{
FILE *fsring1, *fsring2, *ftemp;
char ch, file1[20], file2[20], file3[20];
int factorial(int);
int main()
{
int num;
int result;
s1[i] = '\0';
printf("After concatenation: %s", s1);
return 0;
}
(ii) Differentiate between call by value and call by reference.
Ans.
In call by value, a copy of actual arguments is In call by reference, the location (address) of
passed to formal arguments of the called actual arguments is passed to formal
function and any change made to the formal arguments of the called function. This means
arguments in the called function have no effect by accessing the addresses of actual
on the values of actual arguments in the calling arguments we can alter them within from the
function. called function.
In call by value, actual arguments will remain In call by reference, alteration to actual
safe, they cannot be modified accidentally. arguments is possible within from called
function; therefore the code must handle
arguments carefully else you get unexpected
results.
int factorial(int);
int main()
{
int num;
int result;
To define Union, union keyword is used. At a time, only one member of union can be
accessed.
union [union name]
{
member definition;
member definition;
...
member definition;
};
5. Convert the following into given radix:
o (243)16 = (1103)8
o (462)8 = (100110010)2
o (43.3125)10 = (101011.01010000000000)2
o (984)10 = (3D8)16
6. Write a program to check whether that a given no. is prime or not. Also write its
algorithm.
Ans. #include <stdio.h>
int main()
{
int n, i, flag = 0;
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
Algo.
Step 1: Start
flag1
i2
flag0
Go to step 6
5.2 ii+1
Step 6: If flag=0
else
Display n is prime
Step 7: Stop
7. (a) What is an operating system? What are the functions of an operating system?
Name 3 operating systems.
Ans. An Operating System (OS) is an interface between a computer user and computer
hardware. An operating system is a software which performs all the basic tasks like file
management, memory management, process management, handling input and output, and
controlling peripheral devices such as disk drives and printers.
Functions.
Process Management
Memory Management
Extended Machine
3 operating systems. 1. Windows 2. MacOS 3. Linux
(b) Wap in C to print a Fibonacci series.
Ans. #include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
return 0;
}
9. What are Command Line arguments? Write a program to find some of n integer
and print it.
Ans. It is possible to pass some values from the command line to your C programs when
they are executed. These values are called command line arguments and many times they
are important for your program especially when you want to control your program from
outside instead of hard coding those values inside the code.
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Sum = %d",sum);
return 0;
}
10. (a) Write a program input string from keyboard and write them in file.
Ans. #include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char ch,file[10];
clrscr();
printf(Enter file name:);
scanf(%s,file);
fp=fopen(file,w);
if(fp==NULL)
{
printf(File could not open!!);
exit(0);
}
printf(Enter data(* to exit)n);
while(1)
{
ch=getche();
if(ch==*)
exit(0);
putc(ch,fp);
}
fclose(fp);
}
(b) Differentiate between the following:
I. Goto and exit.
Exit will cause you to exit from your shell script entirely.
A goto statement in C programming provides an unconditional jump from the 'goto' to a
labeled statement in the same function.
II. ROM and RAM
A ROM chip is used primarily in the start up process of a computer, whereas a RAM chip
is used in the normal operations of a computer after starting up and loading the operating
system.
RAM chips are also used in computers, as well as other devices, to store information and
run programs on the computer
III. Break and Continue
A break causes the switch or loop statements to terminate the moment it is executed.
Loop or switch ends abruptly when break is encountered.
A continue doesn't terminate the loop, it causes the loop to go to the next iteration. All
iterations of the loop are executed even if continue is encountered. The continue
statement is used to skip statements in the loop that appear after the continue
IV. Increment and Decrement operators.
Increment operators are used to increase the value of the variable by one.
Decrement operators are used to decrease the value of the variable by one.