0% found this document useful (0 votes)
31 views7 pages

Question Bank UNIT III

FOC

Uploaded by

VISHNUKUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views7 pages

Question Bank UNIT III

FOC

Uploaded by

VISHNUKUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Unit 3 Question Bank

Part A
1. What is an array?
An array is a group of similar data types stored under a common name. An array is used to
store a collection of data, but it is often more useful to think of an array as a collection of
variables of the same type.
Example:
int a[10];
Here a[10] is an array with 10 values.
2. What are the main elements of an array declaration?
1. Array name
2. Type
3. Size
3. How to initialize an array?
You can initialize array in C either one by one or using a single statement as follows:
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
The number of values between braces { } cannot be larger than the number of
elements that we declare for the array between square brackets [ ]. Following is an example
to assign a single element of the array:
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. The size is required to allocate the
required space. Thus, the size must be mentioned.

5. What is the difference between an array and pointer?

6. List the characteristics of Arrays.


All elements of an array share the same name, and they are distinguished form one
another
with help of an element number. Any particular element of an array can be modified
separately without disturbing other elements.

7. What are the types of Arrays?


1. One-Dimensional Array
2. Two-Dimensional Array
3. Multi-Dimensional Array

8. Write the features of array. (Jan 2013)


a. An array is a derived data type. It is used to represent a correction of elements of the
same data type.
b. The elements can be accessed with base address and the subscript defined for the
position of elements.
c. The elements are stored in continuous memory location
d. The starting memory location is represented by the array name and it is known as
the base address of the array.
9. Define One dimensional Array.
One dimensional array can be defined as the collection of data item that can be stored
under a one variable name using only one subscript. Ex. int a[10];

8. Define Two Dimensional Arrays.


Two dimensional arrays can be defined as an array with two subscripts. A two
dimensional array enables us to store multiple row of elements. Ex. int a[10][10];
9. Give example for array initialization.
An array can be initialized at the time of declaration is known as compile time
initialization. Ex. int a[5]={5,3,5,4,1};
10. Why do you need to use array? (Jan 2012)
In many cases, we need to declare a set of variables that are of the same data type.
Instead of declaring each variable separately, we can declare all variable collectively
in the format of an array. Each variable, as an element of the array, can be accessed
either through the array
elements references or through a pointer that references the array.
11. List out the disadvantages of an array.
a. The elements in the array must be same data types.
b. The size of an array is fixed.
c. If we need more space at run time, it Is not possible to extend array.
d. The insertion and deletion an operation is an array require shifting of elements
which takes times.
12. What are the limitation of one-dimensional and two dimensional arrays?
The limitations of one dimensional array are
a. There is no easy method to initialize large number of array elements
b. It is difficult to initialize selected array element
The limitations of two dimensional arrays are
a. Deletion of any element from an array is not possible
b. Wastage of memory when it is specified in large array size
13. Why array elements must be initialized?
After declaration array elements must be initialized otherwise it holds garbage value.
There
are two types of initialization
a. compile time initialization(initialization at the time of
declaration) int a[5]={12,34,23,56,12};
b. run time initialization (when the array has large number of elements it can be
initialized at run time)
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);

14. What is the value of b in the following program?


main()
{int a[5]={1,3,6,7,0}; Output: 6
int *b;
b=&a[2];
printf(“%d”,*b);
}
OUTPUT: 6
15. Write a C program to find the number of elements in an array.

#include <stdio.h>
int main() Output
{
int array[] = {15, 50, 34, 20, 10, 79, 100}; Size of the given array is 7
int n;
n = sizeof(array);
printf("Size of the given array is %d\n",n/sizeof(int));
return 0;
}
OUTPUT:
Size of the given array is 7

16. Define Strings.


Strings:
The group of characters, digit and symbols enclosed within quotes is called as Stirng
(or) character
Arrays. Strings are always terminated with ‘\0’ (NULL) character. The compiler
automatically adds ‘\0’ at the end of the strings.
Example:
char name[]={‘C’,’O’,’L’,’L’,’E’,’G’,’E’,’E’,’\0’};
17. Write syntax and example for one dimensional array in c?
Synatx: data-type arr_name[array_size];

Array declaration, initialization and Int Array Example Character Array Example
Accessing
Array declaration syntax: Integer array example: char str[10];
data_type arr_name [arr_size]; int age [5];

Array initialization syntax:


data_type arr_name [arr_size]=(value1, int age[5]={0, 1, 2, 3, char str[10]={‘H’,‘a’,‘i’};
value2, value3,....); 4}; (or)
char str[0] = ‘H’;
char str[1] = ‘a’;
char str[2] = ‘i;

Array accessing syntax:


arr_name[index]; X=age[3]; s=str[2];

18. Write syntax and example for two dimensional array in c?


Synatx: data-type arr_name[num_of_rows][num_of_col];
Array declaration, initialization and Accessing Example
Array declaration syntax: Integer array example:
data_type arr_name [num_of_rows][num_of_column]; int arr[2][2];

Array initialization syntax: int arr[2][2] = {1,2, 3, 4};


data_type arr_name[2][2] = {{0,0},{0,1},{1,0},{1,1}};
Array accessing syntax: arr [0] [0] = 1;
arr_name[index]; arr [0] ]1] = 2;
arr [1][0] = 3;
arr [1] [1] = 4;

19. What is the use of ‘\0’ character?


When declaring character arrays (strings), “\0” (NULL) character is automatically added at
end. The “\0‟ character acts as an end of character array.
20. Define string.h.
String.h is a header file which includes the declarations, functions, constants of string
handling utilities. Ex.: strlen(), strrev(), strncpy().
21. How strings are represented in C language
Strings in C are represented by arrays of characters. The end of the string is marked with a
special character, the null character, which is simply the character with the value 0. (The null
character has no relation except in name to the null pointer. In the ASCII character set, the
null character is named NUL.) Ex.: char string1[] = "Hello, world!";
22. How strings are declared and initialized in C?
Strings are declared as a array
Strings are declared as a array of characters Initialization of strings
Syntax: char string_name[size]; Char name[6]=”RAMKI”;
Example: char text[30]; Char city[]={„B‟,‟O‟,‟M‟,‟B‟,‟A‟,‟Y‟,‟\0‟};

23. Write important string handling functions in C.


Some of the important strings handling functions in C are as follows
a. strcat()-concatenates two strings
b. strcmp()-compares two strings
c. strcpy()-copies one string over other
d. strlen()-finds length of a string
e. strrev()-reverse the string
24. What is the difference between a string and an array?

25. What is the use of strlen()?


This function is used to count and return the number of character present in a string
Syntax: len=strlen(string);
26. What is the use of strcat()?
This function is used to concatenate or combine to strings together then forms a new
string. Syntax: strcat(str1,str2);
27. What is the use of strrev()?
This function is used to reverse a string. This function takes and return only one
argument Syntax: strrev(str);
28. What is the use of strcmp()?
This function which compares two string to find whether they are same or different. If two
strings are equal means it return a zero otherwise numeric difference between the non
matching character
Syntax: strcmp(string1,string2);
29. What does strncpy()do?
strncpy()copies portion of one string to another string. Ex : strncpy(str1 , str2 ,4)
It copies first 4 characters of str2 to str1.
30. Write definition of function. Indicate types of functions available in C.
A function is a self-contained block or a sub-program of one or more statements that
performs a special task when called.
 Without arguments or return values. Eg. abc()
 With arguments but without return values. Eg. abc (int x)
 With arguments and return values. Eg. int abc(int x)
 Without argument but with return values. Eg. int abc().
31. Write the difference between pre-defined (library) function and user defined
function.

32. Write the Syntax of function call.


Syntax of function call
functionName(argument1, argument2, ...);
function call is made using functionName(n1,n2); statement inside the main().

33. Write a program to swap two strings?


#include <stdio.h>
#include <string.h>
void main()
{
char s1[10]="ABCD", s2[10]="XYZ", temp[10];
printf("Before swapping string1 = %s and string2 = %s\n",s1,s2);
strcpy(temp,s1);
strcpy(s1,s2);
strcpy(s2,temp);
printf("After swapping string1 = %s and string2 = %s",s1,s2);
}

Part B
1. Write in detail about the array in C with suitable example.
2. Write about the various operations applied on strings.
3. Write a program to arrange n names in ascending order.
4. What is a function. Describe in detail about the function with suitable example.
5. List and explain various function representations in C with suitable example.
6. Write a program to arrange the numbers in descending order.
7. Try the Hacker rank problem for your practice.

https://www.hackerrank.com/challenges/1d-arrays-in-c/problem
Create an array of size dynamically, and read the values from stdin. Iterate the
array calculating the sum of all elements. Print the sum and free the memory
where the array is stored. While it is true that you can sum the elements as they
are read, without first storing them to an array, but you will not get the experience
working with an array. Efficiency will be required later.

Input Format
The first line contains an integer,
The next line contains space-separated integers.
Sample Input 0
6
16 13 7 2 1 12
Sample Output 0
51

8. Create an array of size dynamically and read the values from stdin. Reverse the array and
print the result.
9. Two set of lists are given. The first list is an original one. The second one has numbers
where some numbers are missed out of the original list. Write a C program to find and
print the missing numbers of the second list?

For Example, the original list a = [12, 10, 4, 11, 16, 5, 21, 30] and the missing list,
b = [12, 4, 16, 5, 30], here, the missing numbers are 10, 11 and 21.

10. Write a C program to calculate the power of a number.


#include <stdio.h>
int main()
{
printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter an exponent: ");
scanf("%d", &exponent);
while (exponent != 0)
{
result *= base;
--exponent;
}
printf("Answer = %lld", result);
return 0;
}

OUTPUT:
Enter a base number: 3 int base, exponent;
Enter an exponent: 4 long long result = 1;
Answer = 81

11. Write a C program to generate Pascal triangle.


#include <stdio.h>
void main()
{
int array[30], temp[30], i, j, k, l, num; //using 2 arrays
printf("Enter the number of lines to be printed: ");
scanf("%d", &num);
temp[0] = 1;
array[0] = 1;
for (j = 0; j < num; j++)
{
printf(" ");
}
printf(" 1\n");
for (i = 1; i < num; i++)
{
for (j = 0; j < i; j++)
printf(" ");
for (k = 1; k < num; k++)
{
array[k] = temp[k - 1] + temp[k];
}
array[i] = 1;
for (l = 0; l <= i; l++)
{
printf("%3d", array[l]);
temp[l] = array[l];
}
printf("\n");
}
}

12. Write a C program for


a. Calculator using switch
b. To check prime number
13. Write a C program for find the reverse of a number and palindrome checking.
14. Write a C program for
a. Printing Fibonacci series
b. Check Armstrong number
15. Write a C program for the following
a. Roots of quadratic equation
b. Sum of n natural numbers

You might also like