Sie sind auf Seite 1von 8

ST.

JOSEPHS COLLEGE OF ENGINEERING


ST. JOSEPHS INSTITUTE OF TECHNOLOGY
DEPARTMENT OF CSE/IT
CYCLE TEST-II Questions
UNIT III
1. What is an array? (JAN 2013) (JAN 2014)
Array is data structure
It stores objects/variables of same data types in contiguous memory locations
Array elements can be accessed using index/subscript numbers
Example: int arr[10];
2. What are the main elements of an array declaration? (or) Give the general form of
array declaration. (Nov/Dec 2006)
Data type of array object
Name of array object
Size of the array
Syntax: datatype arrayName [ arraySize ];
Example: int arr[10];
3. What is dynamic memory allocation?
Allocating the memory at run time is called as dynamic memory allocation. C has 4 dynamic memory
allocation functions
Function
Use of Function
malloc() Allocates requested size of bytes and returns a pointer to first byte of allocated
space
calloc()
Allocates space for an array elements, initializes to zero and then returns a pointer
to memory
free()
dellocate the previously allocated space
realloc() Change the size of previously allocated space
4. Why is it necessary to give the size of an array in an array declaration?
When an array is declared, the compiler allocates a base address and reserves enough space in the
memory for all the elements of the array at compile time based on the array size specified. Therefore size
must be specified in array declaration inorder to allocate the required space.
5. Declare a float array of size 5 and assign 5 values to it. [DEC 2014]
float arr[5];
arr[0]=0.1;
arr[1]=0.2;
arr[2]=0.3;
arr[3]=0.4;
arr[4]=0.5;
6. Write the features of array.
Array contains data elements of same data type.
Array stores fixed number of data elements.
The elements in the array can be accessed by the index of the array.
The elements are stored in contiguous location in an array
7 Give an example of initialization of string array. [DEC 2014]
There are different ways of initializing String in C Programming:

Initializing Unsized Array of Character


char name [] = {'P','R','I','T','E','S','H','\0'};
Initializing String Directly
char name [ ] = "PRITESH";
Initializing String Using Character Pointer
char *name = "PRITESH";
8. What will happen when you access the array more than its dimension?
When you access the array more than its dimensions(size),no error will be caused but garbage values
will be displayed for those locations
Eg:
void main()
{
int a[2];
a[0]=1;
a[1]=2;
a[2]=3;
for(i=0;i<5;i++)
printf(%d\t,a[i]);
}
Output
1
2
3
0
32743
(here 0 and 32743 are garbage values)

9. Define String.
A string in C is merely an array of characters. The length of a string is determined by a
terminating null character: '\0'. So, a string with the contents, say, "abc" has four characters: a, b, c,
and the terminating null character. Eg: char name [ ] = {a,b,c,\0}; or char name[ ]=abc;
10. What is the use of strcat() function?
The strcat() function joins two strings. Its a library function, and has its declaration in string.h header
file. Eg: strcat(s1,s2) It shall append a copy of the string pointed to by s2 to the end of the string pointed
to by s1.
#include <stdio.h>
#include <string.h>
int main()
{
char a[10], b[10];
printf("\n Enter the first string: ");
gets(a);
printf("\n Enter the second string: ");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
}
Output:
Enter the first string: hello
Enter the second string: world
String obtained on concatenation is helloworld
11. How does array variable differ from an ordinary variable?
Array variable can store group of elements of the same data type under a common name where
as ordinary can be able to store only one value at a time.
Example: int a; // variable a can store single integer value
int a[5]; // here in a we can save 5 integer values

12. Give the declaration for the string welcome in c


Syntax: char array_name[size] = String;
Example:
char mes[ ]= { w,e,l,c,o,m,e,\0}; or
char mes[ ] =Welcome
13. Write example code to declare two dimensional array? (MAY-2014)
Syntax: <datatype> Arrayname [row size][column size];
Example:
#include<stdio.h>
voidmain()
{
int a[3][3]; //2-d Arraywith 3 row and 3 column
}
14. What is a null character? What is its use in a string?
A null character is specified as \0. It is used as terminator in the string. It avoids storage of garbage
values in character arrays.
15. Write a C Program to length of the string.
void main()
{
int n;
printf("\n Enter the string");
gets(a);
n=strlen(a);
printf("\nThe length of the string is %d",n);
}
16. Name any four library functions used for string handling.(JAN-2014)(MAY-2014)
Function
Work of Function
Calculates the length of string
strlen()
Copies a string to another string
strcpy()
Concatenates(joins) two strings
strcat()
Compares two string
strcmp()
Converts string to lowercase
strlwr()
Converts string to uppercase
strupr()
17. Write a C Program to copy the string one array to another array.
void main()
{
char a[15],b[15];
printf("\n Enter the string");
gets(a);
strcpy(b,a);
printf("\nThe copied string :");
puts(b);
}
18. Write a C Program to find length of a String without using Library functions.
#include<stdio.h>
int main()

{
char str[100];
int length;
printf("\nEnter the String : ");
gets(str);
length = 0; // Initial Length
while (str[length] != '\0')
length++;
printf("\nLength of the String is : %d", length);
return(0);
}
19. What is meant by Sorting?What are its types?
Sorting refers to ordering data in an increasing or decreasing fashion according to some linear relationship
among the data items. Sorting can be done on names, numbers and records.
Types of sorting available in C:
Insertion sort.
Merge Sort.
Quick Sort.
Radix Sort.
Heap Sort
Selection sort
Bubble sort
20. Define Searching.
Searching for data is one of the fundamental fields of computing. Search is an operation in which a given
list is searched for a particular value.the location of the searched element is informed.
Various types of searching techniques in C are
Linear search
Binary search
21. What is Binary Search?
A search algorithm which repeatedly divides an ordered search array in half and compared the required
search value with the middle element of the array.
If the search value is equal to middle element then it returns the middle element.
If the search value is less than the middle element it Searches only from low to mid-1.
If the search value is greater than the middle element it Searches only from mid+1 to high.

UNIT-IV

1. Define Recursion? (MAY-2014)


A function that calls itself is known as recursive function and the process of calling function itself
is known as recursion in C programming.
Example:
void rec( )
{
rec( );
}
void main( )
{
rec( );
}
2. What is a Pointer? How a variable is declared to the pointer?
Pointer is a variable which holds the address of another variable.
x
Pointer declaration:
datatype *variable-name;
2000
Example:
int *x, c=5; x=&c;
Addr: 1000

c
5
2000

3. What are the uses of Pointers? (JAN2014)


Pointers are used to return more than one value to the function
Pointers are more efficient in handling the data in arrays
Pointers reduce the length and complexity of the program
They increase the execution speed
The pointers saves data storage space in memory
4. What are * and & operators means? (DEC 2014)
* operator - value at the address[indirection/dereferencing operator]. The indirection
operator (*) accesses a value indirectly, through a pointer. The operand must be a pointer
value. The result of the operation is the value addressed by the operand; that is, the value at
the address to which its operand points. The type of the result is the type that the operand
addresses.
& operator - address of operator [referencing operator]. Its a pointer address operator is
denoted by & symbol. When we use ampersand symbol as a prefix to a variable name &,
it gives the address of that variable.
5. How can you return more than one value from a function?
A function returns only one value. By using pointer we can return more than one value. If
we want the function to return multiple values of same data types, we could return the pointer to
array of that data types.
6. What is the difference between an array and pointer?
Array
Pointer
Its a data structure that stores elements of same Its a variable that stores address of another
data type in contiguous memory locations
variable
Array declaration: int a[6];
Pointer declaration: int *a;
Memory allocation is static
Memory allocation can be dynamic
Array can be initialized at definition. Example
Pointers cant be initialized at definition
int num[] = { 2, 4, 5}
Array elements are accessed using subscripts
Pointers variables can be accessed using
indirection operator(*)

7. What is dangling pointer?


In C, a pointer may be used to hold the address of dynamically allocated memory .
In some time this memory is freed with the free() function, the pointer itself will still contain
the address of the released block. This is referred to as a dangling pointer.
Using the pointer in this state is a serious programming error. Pointer should be assigned
NULL after freeing memory to avoid this bug.
8. Is using exit() the same as using return?
No. The exit() function is used to exit your program and return control to the operating system.
The return statement is used to return from a function and return control to the calling function. If you
issue a return from the main() function, you are essentially returning control to the calling function, which
is the operating system. In this case, the return statement and exit() function are similar. Here is an
example of a program that uses the exit() function and return statement:
9. What is the need for function?(JAN-2014)
Modularization: Divide complex problems to simple sub problems
Reusability of Code: resuse code rather than developing it from scratch.
Remove Redundancy: reduce usage of same set of code repeatedly
10.Can math operations be performed on a void pointer?
No. Pointer addition and subtraction are based on advancing the pointer by a number of elements. By
definition, if you have a void pointer, you don't know what it's pointing to, so you don't know the size of
what it's pointing to. If you want pointer arithmetic to work on raw addresses, use character pointers.
11. What is a void pointer?
Pointers created to point to nothing/void are called as void pointers.
It has type void*
Example: void* ptr
Void pointer is a generic pointer, it can point to any type of data: char, int, float or any type
2000

3000
ptr

2000

12. What is the necessity of a function prototype?


A function prototype tells the compiler what kind of arguments a function is looking to receive
and what kind of return value a function is going to give back.
This approach helps the compiler ensure that calls to a function are made correctly and that no
erroneous type conversions are taking place.
13. How do you use a pointer to a function?
The format of a function pointer goes like this: return_type (*pointer_name)(parameter_list);
#include<stdio.h>
int func (int a)
{
printf("\n a = %d\n",a);
return 0;
}
int main(void)
{
int(*fptr)(int); // Function pointer

fptr = func; // Assign address to function pointer


func(2);
return 0;
}
14. What is the difference between call by value and call by reference?(MAY-2014)
Call by Value
Call by Reference
The value of variables are passed as parameters The address of the variables are passed as
in the function call
parameters in the function call
Changes made in the formal parameters (in called Changes made in the formal parameters (in called
function) will not affect the actual arguments in function) will affect the actual arguments in the
the calling function
calling function
15. When is a null pointer used?
Null pointer does not point anywhere and does not hold address of any object or function
It has numerical value 0
Eg: int *p=0 or int *p=NULL
When referring to computer memory, a null pointer is used to direct a software program or
operating system to an empty location in the computer memory. Commonly, the null pointer is used to
denote the end of a memory search or processing event.
16. What is a function? (DEC 2014)
Function is a group of statements that together performs a task.
Every C program has atleast one function called main( )
Functions can be classified into 2 types:
User defined functions: functions defined by user
Library functions: inbuilt functions in C
17. What is return statement
A return statement returns result of computations performed in called function and transfers the
program control back to the calling function, assigns returned value to the variable in the left side of the
calling function. If a function does not return a value, the return type in the function definition and
declaration is specified as void.
Two forms:
return;
return expression;
18. What is fixed argument functions and variable argument functions?
A function that accepts a fixed number of arguments is called a fixed argument function
Example: pow(2,5)pow function expects 2 arguments only
Giving arguments lesser or greater than the fixed size will result in error
A function that accepts a variable number of arguments is called a variable argument function
Example: printf(%d%d,a,b) function expects 2 arguments
Example: printf(%d,a) function expects 1arguments
Thus printf() function can accept any number of arguments
19. Write a C program to find sum of N numbers using function
#include<stdio.h>

#include<conio.h>
int sum(int n);
void main()
{
int n;
printf("How many terms ?:");
scanf("%d",&n);
printf("Sum of First %d Natural Number=%d",n,sum(n));
getch();
}
int sum(int n)
{
int i,sum=0;
for(i=1; i<=n; i++)
sum+=i;
return sum;
}
Output
How many terms ?:10
Sum of First 10 Natural Number=55
20. What are the rules to be followed for performing pointer operations?
One can perform different arithmetic operations on pointer such as increment, decrement but still we have
some more arithmetic operations that cannot be performed on pointer as follows:
Addition of two addresses.
Multiplying two addresses.
Division of two addresses.
Modulo operation on pointer.
Cannot perform bitwise AND,OR,XOR operations on pointer.
Cannot perform NOT operation or negation operation.

Das könnte Ihnen auch gefallen