Professional Documents
Culture Documents
}
Output: 10 5
3) What is and why array in c?
Explanation:
An array is derived data type in c programming language which can store
similar type of data in continuous memory location. Data may be primitive
type (int, char, float, double), address of union, structure, pointer,
function or another array.
Example of array declaration:
int arr[5];
char arr[5];
float arr[5];
long double arr[5];
char * arr[5];
int (arr[])();
double ** arr[5];
Array is useful when:
(a) We have to store large number of data of similar type. If we have large
number of similar kind of variable then it is very difficult to remember
name of all variables and write the program. For example:
//PROCESS ONE
int main(){
int ax=1;
int b=2;
int cg=5;
int dff=7;
int am=8;
int raja=0;
int rani=11;
int xxx=5;
int yyy=90;
int p;
int q;
int r;
int avg;
avg=(ax+b+cg+dff+am+raja+rani+xxx+yyy+p+q+r)/12;
printf("%d",avg);
return 0;
}
If we will use array then above program can be written as:
//PROCESS TWO
int main(){
int arr[]={1,2,5,7,8,0,11,5,50};
int i,avg;
for(int i=0;i<12;i++){
avg=avg+arr[i];
}
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
printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
printf("\n");
}
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.
13. In C, malloc() and calloc() Functions 13.In C++, new and delete
are used for Memory Allocation and
operators are used for Memory
free() function for memory Deallocating. Allocating and Deallocating.
14. It supports built-in and primitive
data types.
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
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:
possibly (but not always) an increase in the size of the generated binary
executables
Encapsulation
Inheritance
Polymorphism
53) What is the difference between compile time and run time
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
{
public:
void display()
{ cout<<"Content of second derived class.\n"; }
};
int main()
{
B *b;
D1 d1;
D2 d2;
/* 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.
Primary doesn't allow NULL values whereas unique key can allow one
NULL value.
Primary Key: Primary key is used for unique identification of each row in
a table. Unique and not null values only are accepted to persist in primary
key.
Foreign Key: Foreign Key identifies a column with the primary key
column of another table (parent table) for storing data. The values in the
foreign key must be the values of primary key of other columns. Thus it
ensures the data integrity correct data.
70) Define Trigger.
Triggers are similar to stored procedure except it is executed
automatically when any operations are occurred on the table. A Trigger is
a code that associated with insert, update or delete operations. The code
is executed automatically whenever the associated query is executed on a
table. Triggers can be useful to maintain integrity in database.
71) Explain the types of relationships in database.
There are four relationships in database.
- One to One: One entity is associated with another entity. For Ex: Each
employee is associated with one department
- One to Many: One entity is associated with many other entities. For Ex: A
company is associated with all working employees in one
branch/office/country.
- Many to One: Many entities are associated with only one entity. For Ex:
Many employees are associated with one project.
- Many to Many: Many entities are associated with many other entities. For
Ex: In a company many employees are associated with multiple
projects(completed/existing), and at the same time, projects are
associated with multiple employees.
72) What is normalization?
Normalization is the way of organizing data in a database by removing
redundancy and inconsistent dependency.
Database normalization has the rules to be followed while creating
databases.
Each rule is called as normal form.
First Normal form states - Remove duplicate column and identify each set
of related data with a primary key.
Second normal form - Create relationship between master and master
detail tables using foreign key.
Third normal form - Remove the fields that do not depend on the primary
key.
1. Outer Join: Outer join has two categories left outer join and right outer
join. In this join functionality, two or more tables are joined by including
blank rows in the specified side, once the criterion satisfies.
The combination of left outer join and right outer join is simple outer join.
2. Inner Join: In this join functionality, two or more tables are joined by
leaving the blank rows for checking.
Other than these two joins, there areNatural Join: This join is the Cartesian product.
Equi Join: A condition includes an operator
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.
ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties
that guarantee that database transactions are processed reliably.
79) What are the functions of operating system?
The operating system controls and coordinates the use of hardware
among the different processes and applications. It provides the various
functionalities to the users. The following are the main job of operating
system.
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.