A B+a B A-B A A-B 2) : 1) Swap Two Variables Without Using Third Variable
A B+a B A-B A A-B 2) : 1) Swap Two Variables Without Using Third Variable
a=b+a;
b=a-b;
a=a-b;
2) What is difference between pass by value and pass by
reference?
In c we can pass the parameters in a function in two different ways.
(a)Pass by value: In this approach we pass copy of actual variables in
function as a parameter. Hence any modification on parameters inside the
function will not reflect in the actual variable. For example:
#include<stdio.h>
int main(){
int a=5,b=10;
swap(a,b);
printf("%d
%d",a,b);
return 0;
}
void swap(int a,int b){
int temp;
temp =a;
a=b;
b=temp;
}
Output: 5 10
(b)Pass by reference: In this approach we pass memory address actual
variables in function as a parameter. Hence any modification on
parameters inside the function will reflect in the actual variable. For
example:
#incude<stdio.h>
int main(){
int a=5,b=10;
swap(&a,&b);
printf("%d %d",a,b);
return 0;
}
void swap(int *a,int *b){
int *temp;
*temp =*a;
*a=*b;
*b=*temp;
}
Output: 10 5
}
printf("%d",avg/12);
return 0;
}
Question: Write a C program to find out average of 200 integer
number using process one and two.
(b) We want to store large number of data in continuous memory location.
Array always stores data in continuous memory location.
What will be output when you will execute the following program?
int main(){
int arr[]={0,10,20,30,40};
char *ptr=arr;
arr=arr+2;
printf("%d",*arr);
return 0;
}
Advantage of using array:
1. An array provides singe name .So it easy to remember the name of all
element of an array.
2. Array name gives base address of an array .So with the help increment
operator we can visit one by one all the element of an array.
4) How to add two numbers without using the plus operator in c
#include<stdio.h>
int main(){
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
//sum = a - (-b);
sum = a - ~b -1;
printf("Sum of two integers: %d",sum);
return 0;
}
Sample output:
Enter any two integers: 5 10
Sum of two integers: 15
Algorithm:
In c ~ is 1's complement operator. This is equivalent to:
~a = -b + 1
So, a - ~b -1
= a-(-b + 1) + 1
=a+b1+1
=a+b
5) Bubble sorting algorithm
for(i=s-2;i>=0;i--){
for(j=0;j<=i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
return 0;
}
6) Fibonacci series:
printf("Enter the number range:");
scanf("%d",&r);
printf("FIBONACCI SERIES: ");
printf("%ld %ld",i,j); //printing firts two values.
for(k=2;k<r;k++){
f=i+j;
i=j;
j=f;
printf(" %ld",j);
}
return 0;
}
Sample output:
Enter the number range: 15
FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Output:
Enter total numbers of elements: 5
Enter 5 elements: 6 2 0 11 9
After sorting: 0 2 6 9 11
7) differences between structures and arrays
The following are the differences between structures and arrays:
- Array elements are homogeneous. Structure elements are of different
data type.
- Array allocates static memory and uses index / subscript for accessing
elements of the array. Structures allocate dynamic memory and uses (.)
operator for accessing the member of a structure.
- Array is a pointer to the first element of it. Structure is not a pointer
- Array element access takes less time in comparison with structures.
8) Explain the meaning of "Segmentation violation".
Segmentation violation usually occurs at the time of a programs attempt
for accessing memory location, which is not allowed to access. The
following code should create segmentation violation.
main() {
char *hello = Hello World;
*hello = h;
}
At the time of compiling the program, the string hello world is placed in
the binary mark of the program which is read-only marked. When loading,
the compiler places the string along with other constants in the read-only
segment of the memory. While executing a variable *hello is set to point
the character h , is attempted to write the character h into the memory,
which cause the segmentation violation. And, compiler does not check for
assigning read only locations at compile time.
9) Floyds Triangle
#include<stdio.h>
int main(){
int i,j,r,k=1;
printf("Enter the range: ");
scanf("%d",&r);
printf("FLOYD'S TRIANGLE\n\n");
for(i=1;i<=r;i++){
for(j=1;j<=i;j++,k++)
printf(" %d",k);
printf("\n");
}
return 0;
}
Sample output:
Enter the range: 10
FLOYD'S TRIANGLE
1
23
456
7 8 9 10
11 12 13
16 17 18
22 23 24
29 30 31
37 38 39
46 47 48
14
19
25
32
40
49
15
20
26
33
41
50
21
27
34
42
51
28
35 36
43 44 45
52 53 54 55
return 0;
}
long fact(int num){
long f=1;
int i=1;
while(i<=num){
f=f*i;
i++;
}
return f;
}
Sample output:
Enter the no. of lines: 8
1
11
121
1331
14641
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
11) What are the different storage class specifiers in C?
There are 4 storage class specifiers in C, out of which auto and static act
as storage classes as well. So, the storage class specifiers are:
Auto
Register
Static
Extern
C++
1. C is Procedural Language.
8. In C
scanf() Function used for Input.
8. In C++
Cin>> Function used for
Input.
20) Differentiate between a for loop and a while loop? What are it
uses?
For executing a set of statements fixed number of times we use for loop
while when the number of
iterations to be performed is not known in advance we use while loop
hash table where this key can be placed. Collision Resolution technique
can be classified as:
1) Open Addressing (Closed Hashing)
a) Linear Probing
b) Quadratic Probing
c) Double Hashing
2) Separate Chaining (Open Hashing)
24) Describe binary tree and its property.
In a binary tree a node can have maximum two children, or in other words
we can say a node can have 0,1, or 2 children.
Properties of binary tree.
1) The maximum number of nodes on any level i is 2i where i>=0.
2) The maximum number of nodes possible in a binary tree of height h is
2h-1.
3) The minimum number of nodes possible in a binary tree of height h is
equal to h.
4) If a binary tree contains n nodes then its maximum possible height is n
and minimum height possible is log2 (n+1).
5) If n is the total no of nodes and e is the total no of edges then e=n1.The tree must be non-empty binary tree.
6) If n0 is the number of nodes with no child and n2 is the number of nodes
with 2 children, then n0=n2+1.
25) What are different application of stack.
Some of the applications of stack are as follows:
-
Function calls.
Reversal of a string.
Checking validity of an expression containing nested parenthesis.
Conversion of infix expression to postfix.
- These data elements are called members. They can have different types
and different lengths.
- Some of them store the data of same type while others store different
types of data.
27) What is a queue ?
- A Queue refers to a sequential organization of data.
- It is a FIFO type data structure in which an element is always inserted at
the last position and any element is always removed from the first
position
29) Quick sort employs the divide and conquer concept by dividing the
list of elements into two sub elements
The process is as follows:
1. Select an element, pivot, from the list.
2. Rearrange the elements in the list, so that all elements those are less
than the pivot are arranged before the pivot and all elements those are
greater than the pivot are arranged after the pivot. Now the pivot is in its
position.
3. Sort the both sub lists sub list of the elements which are less than the
pivot and the list of elements which are more than the pivot recursively.
Merge Sort: A comparison based sorting algorithm. The input order is
preserved in the sorted output. Run time is T(n log n).
Merge Sort algorithm is as follows:
1. The length of the list is 0 or 1, and then it is considered as
sorted.
2. Other wise, divide the unsorted list into 2 lists each about
half the size.
3. Sort each sub list recursively. Implement the step 2 until the
two sub lists are sorted.
4. As a final step, combine (merge) both the lists back into one
sorted list.
{
if(head==0)
return;
if(head->next==0)
return;
if(head->next==tail)
{
head->next = 0;
tail->next = head;
}
else
{
node* pre = head;
node* cur = head->next;
node* curnext = cur->next;
head->next = 0;
cur-> next = head;
for(; curnext!=0; )
{
cur->next = pre;
pre = cur;
cur = curnext;
curnext = curnext->next;
}
curnext->next = cur;
}
}
Encapsulation
Inheritance
Polymorphism
Classes and objects are separate but related concepts. Every object
belongs to a class and every class contains one or more related objects.
A Class is static. All of the attributes of a class are fixed before, during,
and after the execution of a program. The attributes of a class dont
change.
53) What is the difference between compile time and run time
polymorphism?
Function overloading, operator overloading,and parametric types
(templates in C++ or generics in Java) are done at compile time.
Dynamic binding (virtual functions) is runtime polymorphism.
By default, the members ot structures are public while that tor class
is private.
Structures doesn't provide something like data hiding which is
provided by the classes.
Structures contains only data while class bind both data and
member func
Single inheritance
Multiple inheritance
Hierarchical inheritance
Hybrid inheritance
/* b->display(); // You cannot use this code here because the function of
base class is virtual. */
b = &d1;
b->display(); /* calls display() of class derived D1 */
b = &d2;
b->display(); /* calls display() of class derived D2 */
return 0;
}
Output
Content of first derived class.
Content of second derived class.
After the function of base class is made virtual, code b->display( ) will call
the display( ) of the derived class depending upon the content of pointer.
In this program, display( ) function of two different classes are called with
same code which is one of the example of polymorphism in C++
programming using virtual functions.
NonEqui Join: They are conditional joins which does not use their
conditions.
76) What are the restrictions applicable while creating views?
The following are the restrictions for creating views in RDBMS;
- Views can be created only in the current database.
- A computed value can not be changed in a view
- INSERT and DELETE statements may be used subject to the integrity
constraints
Views can be created referencing tables and views only in the current
database.
A view name must not be the same as any table owned by that user.
You can build views on other views and on procedures that reference
views.
Rules or DEFAULT definitions can't be associated with views.
Only INSTEAD OF triggers can be associated with views.
The query that defines the view cannot include the ORDER BY, COMPUTE,
or COMPUTE BY clauses or the INTO keyword.
You cannot define full-text index definitions for views.
You cannot create temporary views
You cannot create views on temporary tables.
77) What are the uses of view?
1. Views can represent a subset of the data contained in a table;
consequently, a view can limit the degree of exposure of the underlying
tables to the outer world: a given user may have permission to query the
view, while denied access to the rest of the base table.
2. Views can join and simplify multiple tables into a single virtual table
3. Views can act as aggregated tables, where the database engine
aggregates data (sum, average etc.) and presents the calculated results
as part of the data
4. Views can hide the complexity of data; for example a view could appear
as Sales2000 or Sales2001, transparently partitioning the actual
underlying table
5. Views take very little space to store; the database contains only the
definition of a view, not a copy of all the data which it presentsv.
6. Depending on the SQL engine used, views can provide extra security
78) What is a transaction? What are ACID properties?
Ans: A Database Transaction is a set of database operations that must be
treated as whole, means either all operations are executed or none of
them.
An example can be bank transaction from one account to another
account. Either both debit and credit operations must be executed or none
of them.
Resource utilization
Resource allocation
Process management
Memory management
File management
I/O management
Device management
Mutex
Semaphores
Monitors
Condition variables
Critical regions
Read/ Write locks
91) What is the basic difference between pre-emptive and nonpre-emptive scheduling.
Pre-emptive scheduling allows interruption of a process while it is
executing and taking the CPU to another process while non-pre-emptive
scheduling ensures that a process keeps the CPU under control until it has
completed execution.
Page frames are the fixed size contiguous areas into which the main
memory is divided by the virtual memory.
-Pages are same sized pieces of logical memory of a program. Usually
they range from 4 KB to 8 KB depending on the addressing hardware of
the machine.
- Pages improve the overall system performance and reduces requirement
of physical storage as the data is read in 'page' units.
95) Differentiate between logical and physical address.
- Physical addresses are actual addresses used for fetching and storing
data in main memory when the process is under execution.
- Logical addresses are generated by user programs. During process
loading, they are converted by the loader into physical address.