Sunday, 1 January, 2012



Short Answers

1. What is an array? How many types of array and what are they?
An array is a collection of variables of the same type that are referenced by a common name.
Arrays are of two types:they are One dimensional and Multi dimensional
  • One dimensional: comprising of finite homogenous elements. Ex.
  • Multi dimensional: comprising of elements, each of which is itself a one – dimensional array.
2.Define one (or) single dimensional array?
They are comprising of finite homogenous elements
Data_type array_name [size];
  • The size of the array should always be positive
  • Each element of the array is accessed by the array name and the position of the element in the array.
Ex. int num_array [5];
3. State the following array declaration are valid or Invalid.
(a)int array [100.5]; (b)int a [10]; c)char name [15]; (d)const j = 15;double val[ j ];(e)int d[ ] ={ 1, 2, 3, 4, 5, 6, 7 }
(a) invalid (b)valid (c)valid (d)valid (e)valid
4. Write a statement for the following.
(a) Read 6th element (b)assigns the contents of the 4th element of the array to its 5th element
(c) increments the value stored as 5th element by 1
(a) cin >> n [5] (b) n [4] = n [3] (c) n [4] ++
5. What are strings? Give an example.
  • Strings are otherwise called as literals, which are treated as single dimension of characters.
  • A character array (string) should be terminated with a ‘\0’ (NULL) character.
Ex. (i) char name [10];
6. What character is used to terminate the character array.
7.How will you accept the string value with space? (or) Difference between cin and gets() (or) define cin ,gets and getline()
cin - treats white space or carriage return (enter key) as terminator for string.
Syntax cin>>Variable name;
gets() -To treat spaces as part of string literal, then one has to use gets ( ) defined in stdio.h
Syntax gets (char array_identifier) or
gets (char *)
getline ( ) - a member function of standard input stream
. Syntax
cin.getline (char*, no.of characters, delimiter);
8. Write the syntax of cin,gets() and getline .
9.What are the methods to display the contents of string?
There are two methods to display the contents of string.
1. cout << name - this is similar to any other variable.
2. cout.write (identifier , No.of.characters to be displayed);
Ex. cout.write(pincode, 7); Define
10.What is the use of write statement?
  • Write ( ) is a member function of standard output stream, i.e., ostream.
  • All member functions of a class, should be accessed through an object /instance of class.
  • The two parameters required for write () function are identifier string characters, and no. of characters to be displayed.
Ex. cout.write (pincode, 7)
11.What are the two parameters required for write function?
12.What is the syntax and purpose of the function strlen( )?
strlen ( )
Returns the number of characters stored in the array
13.Give the syntax and purpose of the function strcpy ( )?
strcpy ( )
Copies source string to target string.
Ex. strcpy(b,a);
14.Give the syntax and purpose of the function strcmp ( )?
strcmp(string 1,string2);
it is used to compares the two given strings and returns 0 if string1 = string2
if string1 > string2 it returns 1
if string1< string2 it returns -1
15.What is two-dimensional array? Given an example.
  • A two-dimensional array is an array in which each element is itself an array.
  • The number of elements in a 2-dimensional array is determined by multiplying the number of rows with number of columns
  • The subscripts always commence from zero
  • Syntax : type array_id [row][column];
Ex. int marks[3][4];
16.What the data types can be used in two dimensional arrays? How can be indicate the dimensions of an array?
The dimensions (rows/columns) of an array can be indicated
1. using integer constants
2. using constant identifier of integer or ordinal
4. using char constants
5. using enum identifiers.
17.What are row-major order and column-major order? How 2-darray store the data in memory blocks?
A 2-D array is stored in sequential memory blocks. The elements are stored either,
1. row wise manner (this method is called as row-major order).
2. column wise manner (this method is called as column-major order).
18.What is matrix?
  • A matrix is a set of m x n numbers arranged in the form of a rectangular array of m rows and n columns.
  • Matrices can be represented through 2-D arrays.
19.What is array of strings? Give example.
  • An array of strings is a two-dimensional character array.
  • The size of first index (rows) determines the number of strings and the size of second index determines maximum length of each string. Ex. char day[2] [10] ={“Sunday \0”,“Monday\0”};
20.What is sorting?
  • One can rearrange the data in a given array either in ascending or descending order.
  • This process is called SORTING.
21.Define Array parameters (or) How arrays passing the arguments to the function?
  • Arrays can be passed on as arguments to functions.
  • The actual parameter is passed only by the identifier, ignoring dimensions.
  • Array parameters by default behave like a reference parameter,
  • The array identifier unlike other identifiers, represents the base address of the array
22.How to calculate the size of the array
One dimension array size = memory required ( data type ) x No. of the elements in the array
Ex. int n[5] - 2 x 5 = 10 bytes ( one integer is 2 bytes)
Two dimension array = Number of elements ( Row x column ) x memory required
Ex. int n [2] [3] = (2 x 3) x 2 = 12 bytes

No comments:

Post a Comment