C 2marks With Ans
C 2marks With Ans
UNIT-I
1. What are the various types of operators?
C supports the following types of operators
Unary
Unary Plus +
Unary Minus -
Increment ++
Decrement --
Binary
Arithmetic +,-,*,/,%
Relational <,>,<=,>=,==,!=
Logical &&,||,!
Bitwise &,|,^,<<,>>
Assignment =
Shorthand assignment +=, -=,*=,/=,%=
Ternary
?:
Special
Sizeof(), *. ->
UNIT- II
1. Define Array
Array is a collection of similar type of values
All values are stored in continuous memory locations
All values share a common name
Linear data structure. The elements are organized in a sequential order.
9. Define Sorting
Sorting is a process of arranging the elements either in ascending order or descending order.
10. Sort the following elements using selection sort method. 23,55,16,78,2
Step1:Find smallest element in the list & exchange the element with first element of the list
2,55,16,78,23
Step2: Find second smallest value & exchange it with the second element of the list
2,16,55,78,23
Step 3: Continue the process until all the elements are arranged in the order
2,16,23,78,55
Step 4: 2,16,23,55,78
UNIT-III
1. What is a function?
Function is a set of instructions
Self contained block
Performs a specific task
Used to avoid redundancy of code
2. What is the need for functions?
To reduce the complexity of large programs
To increase the readability
To achieve reusability
To avoid redundancy of code
To save Memory
4. Define recursion
Recursion is a process by which a function calls itself. Recursive function is a function that calls itself.
Pass-by-value Pass-by-reference
Copy of actual arguments are passed Address of actual arguments
are passed
Changes to formal arguments doesn’t Changes to formal arguments
reflect to actual argument reflect to actual argument
Need more memory Saves memory space
2002
The pointer holds the address 2000. This value is added with 1. The data type size of the constant is
added with the address.
p= 2000+(2*1)=2002
UNIT-IV
STRUCTRES
1. What do you mean by structure?
Structure is a user defines data type. It is a collection of different types of values.
It is a heterogeneous data type. C allows the programmer to create a new data type using structure.
Different types of vales stored under a common name. The values in a structure are called structure
member. These members are accessed by structure variable.
2. Define self referential structure?
A structure is said to be self referential structure if it contains a pointer to its same structure type. This
self referential structure is used to create linked list nodes.
Example:
struct node
{
int value;
struct node * next;
};
struct structname
{
Members;
};
void main()
{
struct structname structvar={values};
}
Example:
struct employee
{
int empid;
char empname[40];
float salary;
}e={101,abc,60000};
1. Define File
File is a collection of related records. File is used to store the data. A collection of data which is
stored on a secondary device like a hard disk is known as a file. A file is generally
used as real-life applications that contain a large amount of data.
“w” It opens a new file for writing. If the filename does not exist
it will be created and if the file already exists then its
contents are deleted.
“a” It appends the existing file. If the filename does not exist it
will be created.
“w+ It opens a new file for reading and writing. If a file with the
” current filename exists then it is destroyed and a new file
name is created.
6. What are the functions used to read & write a file content?
fgetc() : fgetc(fp) - reads a single character from a file
fputc() : fputc(ch,fp) - writes single character into a file
fgets(): fgets(char array,no. of bytes,fp) : reads a string from a file
fputs() : fputs(char array,fp) ; writes a string into a file
fscanf() : fscanf(fp,”format string”,address of variables): reads different types of values form file
fprintf(); fprintf(fp,”format string”,variables): writes different types of value into a file
fread() : fread(ptr,size of record,no.of records,fp) : reads record from a file
fwrite(): fwrite(ptr,size of record,no.of records,fp): writes record into a file
reflect as soon as the file is opened Can easily get corrupted, even a single
and can easily be undone. bit change may corrupt the file.
Can store only plain text in a file. (image, audio, text) in a single file.
Widely used file format and can be and may not be understood by other
Mostly .txt and .rtf are used as Can have any application defined