Sie sind auf Seite 1von 5

What is a function in C? What is the need of using functions?


A function is a block of code that has a name and is reusable i.e. it can be executed from as
many different points in a C Program as required. We can divide a long C program into small
blocks which can perform a certain task. Using functions, 1)we can make the code much
more readable 2)remove repeated coding. For example, consider the following code.

int convertHHMMSS_To_Seconds(int hours, int mins, int seconds) {


int totalSeconds = 0;
totalSeconds = hours*60*60 + mins*60 + seconds;
return totalSeconds;
}

We can use the above function as many times as we want without repeating the code inside
the function. Another advantage of using functions is that, if we need to change the code
after sometime, we need to make the edit at only one place.
 What is the difference between malloc and calloc?

The differences between malloc and calloc are as follows,


(i). Malloc allocate bytes of memory, whereas calloc allocates block of memory.
(ii). Malloc allocates memory as a single contiguous block, whereas calloc allocates memory
which may/may not be contiguous. (iii). If a single contiguous block cannot be allocated
then malloc would fail, whereas using calloc if memory is allocated in non-contiguous blocks
when a single contiguous block cannot be allocated.
(iv). The values stored in the allocated memory space by calloc is zero by default, whereas
with malloc the allocated memory could have any value.
(v). Malloc requires one argument - the number of bytes you want to allocate dynamically.
Calloc requires two arguments. The first is the number of variables you'd like to allocate
memory for. The second is the size of each variable.
 How can you swap two variables without using a third variable?

Method One : (Using arithmetic operators)


int a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;

Method Two : (Using bitwise operators)


int a = 10, b = 20;
a = a ^ b;
b = a ^ b;a = a ^ b;
 What is recursion? Give an example for recursion.

Recursion is a technique involving in which a function calls itself and has a termination
condition so that successive repetitions are processed up to the critical step where the
condition is met at which time the rest of each repetition is processed from the last one
called to the first. One simple example is the idea of building a wall that is ten feet high; if I
want to build a ten foot high wall, then I will first build a 9 foot high wall, and then add an
extra foot of bricks. Conceptually, this is like saying the "build wall" function takes a height
and if that height is greater than one, first calls itself to build a lower wall, and then adds
one a foot of bricks.

The most common example for recursion is the find the factorial of a number
long long int factorial(int n) {
if (n <= 1) return 1;
else return (n * factorial(n-1));
}

 Given the following program, remove all if else and replace it with switch case

if (a == 10) {
b = 20;
}
else if (a == 50) {
b = 25;
}
else if (a == 40) {
b = 160;
}
else {
b = 16;
}

switch(a) {
case 10:
b=20;
case 50:
b=25;
case 40:
b=60;
default:
b=16;
 What is a linked list?

A linked list is a data structure that consists of a sequence of data records such that in each
record there is a field that contains a reference (i.e., a link) to the next record in the
sequence. The chain of records are called nodes. Each node has at least two members, one
of which points to the next item or node in the list. These are defined as Single Linked Lists
because they only point to the next item, and not the previous. Those that do point to both
are called Doubly Linked Lists.

 What is Virtual Memory?

Virtual memory is a computer system technique which gives an application program the
impression that it has contiguous working memory (an address space), while in fact it may
be physically fragmented and may even overflow on to disk storage. In other words, it is an
imaginary memory area supported by some operating systems. You can think of virtual
memory as an alternate set of memory addresses. Programs use these virtual addresses
rather than real addresses to store instructions and data. When the program is actually
executed, the virtual addresses are converted into real memory addresses. The purpose of
virtual memory is to enlarge the address space, the set of addresses a program can utilize.
For example, virtual memory might contain twice as many addresses as main memory. A
program using all of virtual memory, therefore, would not be able to fit in main memory all
at once. Nevertheless, the computer could execute such a program by copying into main
memory those portions of the program needed at any given point during execution.

Checkout http://en.wikipedia.org/wiki/Virtual_memory and


http://www.howstuffworks.com/virtual-memory.htm to know more about virtual memory.

 What is DDL and DML? What is the difference DDL and DML?

Data Definition Language (DDL) statements are used to define the database structure or
schema. Data Manipulation Language (DML) statements are used for managing data within
schema objects. Examples of DDL are CREATE (create objects in the database), ALTER
(alters the structure of the database), DROP (delete objects from the database), RENAME
(rename an object) etc. Examples of DML are SELECT (retrieve data from the a database),
INSERT (insert data into a table), UPDATE (updates existing data within a table), DELETE
(deletes records from a table) etc. DML commands can usually be rolled back (if the
database supports rollback), but DDL commands can not be rolled back.

 What is the differece between TRUNCATE and DROP query?

TRUNCATE removes all rows from a table. The DROP command removes a table from the
database. All the tables' rows, indexes and privileges will also be removed. If a table is
dropped, all the relationships with other tables will no longer be valid, the integrity
constraints will be dropped, grant or access privileges on the table will also be dropped, if
want use the table again it has to be recreated with the integrity constraints, access
privileges and the relationships with other tables should be established again. But, if a table
is truncated, the table structure remains the same, therefore any of the above problems will
not exist.

 What is the difference between VIEW and TABLE?

A table is where you store your data. The table actually occupies space on disk. A view is a
stored query. A view is different from a regular table in that it does not store data, but
executes a stored SELECT statement each time it is accessed. Views are used primarily to
store a common query in the database. Without the view, you might have the same
complex query stored in multiple locations in your application code. If you need to make a
change, you would have to change the query in all locations. However, if that query were in
a view, you would only have to change it in one location. The other common reason for a
view is for security purposes.

 Write a query to extract only the duplicate records from the table?

Let us assume a table called users, which has a column named email_address

SELECT `email_address` FROM `users` GROUP BY (`email_address`) HAVING


( COUNT(`email_address`) > 1 )

 What is the difference between a primary key and a foreign key?

A primary key is an attribute (or combination of attributes) that uniquely identifies each row
in a relation. The primary key of an entity set allows us to distinguish among the various
entities of the set. A foreign key is an attribute in a relation of database that serves as the
primary key of another relation in the same database. Primary keys enforce entity integrity
by uniquely identifying entity instances. Foreign keys enforce referential integrity by
completing an association between two entities. Primary key is a unique key but foriegn key
usually refers to primary key.

 Explain function overloading and function overriding.

Function overloading is the process of defining the same function name with different
arguments,number of arguments ,or ordinal positions of arguments. Function overriding is
the process of defining the Base class function in the derived class with different code
implementation.

Example for function overloading.


int area(int side);
int area(int length, int breadth);
int area(int length, int breadth, int height);

Example for function overriding.


class Parent {
virtual int area(int side){ return side*side;}
}
class Child : public Parent {
int area(int side){ return side*side+20; }
}

 What is a complete binary tree?

A complete binary tree is a binary tree in which every level, except possibly the deepest, is
completely filled. At depth n, the height of the tree, all nodes must be as far left as possible.
In other words, a complete binary tree is a special case of a binary tree, in which all the
levels, except perhaps the last, are full; while on the last level, any missing nodes are to the
right of all the nodes that are present.

 What is a binary search tree? Why is searching easier in a binary search tree?

A binary search tree is a binary tree with the following properties,


(i) The left subtree of a node contains only nodes with keys less than the node's key.
(ii) The right subtree of a node contains only nodes with keys greater than the node's key.
(iii) Both the left and right subtrees must also be binary search trees.

The most common operations performed on a binary search tree is searching for a key
stored in the tree. These operations run in O(h) time where h is the height of the tree i.e., h
is the number of links root node to the deepest node.

Das könnte Ihnen auch gefallen