0% found this document useful (0 votes)
16 views10 pages

Ch#5 Array & StringsComputer.s (HSSC-II)

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

Ch#5 Array & StringsComputer.s (HSSC-II)

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

By the name of Allah

Computer Science (HSSC-II)

Arrays and Strings


MCQ.s
 Select the correct option:
I. An array is a collection of ______ of elements stored in contiguous memory locations.
A. Same type B. Different types C. Both A & B D. None
II. A two dimensional array uses a _____ variable name to represent a collection of same
type of data that is in the form of a table or matrix.
A. Single B. Double C. Triple D. All
III. The cin.get( ) function is used to read a ____ from the keyboard that may contain blank
spaces.
A. Character B. Variable C. String D. None
IV. A function that is used to copy contents of a string variable?
A. stry( ) B. strcpy( ) C. srpy( ) D. strlen( )
V. Which of the following identifies the last element of array declared as int b[5][5];?
A. b[3][3] B. b[4][4] C. b[5][5] D. b[6][6]
VI. What is the index number of the last element of an array with 50 elements?
A. 0 B. 40 C. 49 D. 50
VII. Which of the following is correct declaration of marks array?
A. int m; B. int m{15}; C. int m[15]; D. int m(15);
VIII. Which of the following is correct declaration of array?
A. int arr; B. int arr{5}; C. int arr[5]; D. int arr(5);
IX. Which function is used to return the length of string?
A. strcpy( ) B. strcmp( ) C. Both A & B D. strlen( )
X. A function that compares two strings and return as an integer value based on the result
of comparison?
A. strcpy( ) B. strcmp( ) C. Both A & B D. strlen( )

Answer Keys:
MCQ.s

I. A II. A III. C IV. B V. B VI. C

VII. C VIII. C IX. D X. B

Prepared By:
Mr. Usman (SS)
Army College Mailsi Campus
Cell # 0301-2156545 , 0349-3036699
Ch # 5
Array & Strings
Short Questions & Answers
Exercise

1. Define array and give its advantages in programming.


Ans:
An array is a collection of same type of elements stored in contiguous memory
locations.
Advantages in programming
Array allows programmer to use single variable name to represent a collection of same
type of data. This reduce the program size provides an easy way of handling list of
numbers or strings and makes a computer programming task simple and easy.

2. Differential B\w One dimensional and two dimensional array.


Ans:
One dimensional array Two dimensional array

1. Store single list of elements of 1. Store list of lists or array of arrays or


similar data type. array of one dimensional arrays.

2. Datatype arrayname[arraysize]; 2. Datatype arrayname[rowsize][colsize];

3. It can be received in a pointer 3. Parameter receiving it must define the


sized array or an un-sized array. rightmost dimensional of an array.

3. What is the purpose of sizeof( ) function? Give one example.


Ans:
The sizeof( ) function provides the number of bytes occupied to store values for data
type named within the parenthesis. It is used to determine the amount of storage reserved for
int, float, double, char, etc. data types.
Example:
# include <iostream>
using namespace std;
int main( )
{
cout<< “size of char” << sizeof (char) << “bytes” << endl;
cout<< “size of int” << sizeof (int) << “bytes” << endl;
cout<< “size of float” << sizeof (float) << “bytes” << endl;
}

4. Declare an array named x, that has 3 rows and 5 columns and assign it values from 1
to 15 in the declaration statement.
Ans:
# include <iostream>
using namespace std;
int main( )
{
int x[3][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
cout<< “Values are:” << endl;
for( int i=0; i<3; i++)
{
for( int j=0; j<5; j++)
{
cout<< “x*“ <<i<<”+*“<<j<<”+” ;
cout<< x[i][j]<<endl;
}
}

5. Define string and explain how it is stored in computer memory.


Ans:
String
String is sequence of characters. In C++ character string is stored in a one
dimensional array of char data type. Each element of character string holds one
character.
Storage of string in computer memory:
The following diagram shows how the string variable weekday is represented in
computer memory.

Index 0 1 2 3 4 5 6 7 8 9
Variable
S u n d a y \0
The index start from zero. The compiler automatically places the null character (\0) after the
last character. The remaining three characters are not defined.

6. What is the advantages of using cin.get( ) function over cin statement for reading a
string?
Ans:
The advantage of cin.get( ) function over cin statement for reading a string is
that cin.get( ) function reads a whole line then consumes the end-of-line character so
that repeating this will read the next line. Whereas, cin statement consider a space as
terminating character. The cin statement reads strings that consist of a single word.
Anything typed after a space is ignored.

7. Differentiate B/w strcpy( ) and strcmp( ) functions.


Ans:

Strcpy( ) Strcmp( )

The strcpy( ) functions is used to copy The strcmp( ) function compares two
contents of a string variable or string strings and returns an integer value based
constant to another string variable. on the result of comparison. This
comparison is based on ASCII codes of
characters.

8. Diffe.. .B/w strlen( ) and strcat( ) functions.


Ans.

Strlen( ) Function Strcat( ) Function

The strlen( ) function is used to return the


The strcat( ) function is used for
length(the number of characters) of a string.
concatenation or joining of two strings.

End Exercise
Prepared By: Mr. Usman (SS) Army College Mailsi Campus
Cell # 0301-2156545 , 0349-3036699
Additional Short Questions & Answers
1. Why array used in program?
Ans: Array allows programmer to use a single variable name to represent a collection of same
type of data. This reduces the program size, provides an easy way of handling list of numbers or
strinns and makes computer-programming task simple and easy way.

2. Define array.
Ans: An array is a collection of same type of elements stored in contiguous memory locations.

3. What is the structure of array?


Ans: Array consists of contiguous memory locations and each cell represents an element of the
array.

4. What do you know about index? OR Index of array?


Ans: The number within the square brackets is called index and it is used to access a specific
element of the array. The first element of the array always has the index 0.

5. What is required to declare an array?


Ans: To declare an array in C++, the type of the elements, the name of array and number of
elements it is required to store need to be mentioned in the declaration statement.

6. State the syntax to declare an array.


Ans: The following is the general form of declaration of array:
data type arrayname [arraysize];

7. How we initialize an array?


Ans: An array can be initialized in declaration statement. The following statement declares the
marks array as integer of size 6 and assigns marks to each element of the array.
int marks[6] = {55, 25, 30, 32, 11, 42 };
Second Method
The above statement can also be written as
int marks[ ] = {55, 25, 30, 32, 11, 42 };

8. Define sizeof( ) Function.


Ans: The sizeof( ) function provides the number of bytes occupied to store values for data type
named within the parenthesis. It is used to determine the amount of storage reserved for int,
float, double, char, etc. data types.

9. Define two dimensional array.


Ans: A two dimensional array uses a single variable name to represent a collection of same type
of data that is in the form of a table or matrix. It has two dimensions i.e. vertical and horizontal
dimensions. Vertical dimensions represent rows and horizontal dimension represent columns.
Two dimensional array provides an easy way of handling data that is stored in the form of a
table.

10. How we declared two-dimensional array?


Ans: The general form of declaration of two dimensional array.

Syntax:
datatype arrayname[rowsize][columnsize];

11. Define string.


Ans: String is a sequence of characters. In C++, character string is stored in a one-dimensional
array of char data type. Each element of character string holds one character.

12. What do you know about null character? OR Briefly describe null character.
Ans: All the strings end with a special character, known as null character and it is represented by
'\0'. The null character is automatically appended at the end of string.

13. How we declare a string?


Ans: To declare a string in C++, the data type char, the name of string and the number of
characters it is required to store is mentioned in the declaration statement.

14. State the general form of declaration of string.


Ans: The following is the general form of declaration of string.
char stringname[stringsize];

15. How we initialize a string in C++?


Ans:
String str = (“gfg”);
> Strings are initialized in either of the following two forms.
char name*4+ = , ‘R’, ‘A’, ‘M’, ‘\0’-;
char name* + = , ‘R’, ‘A’, ‘M’, ‘\0’-;
OR
char name*4+ = “RAM”;
char name* + = “RAM”;
> When we initialize a character array by listing its elements, the null terminator or the size of
the array must be provided explicitly.

R A M \0

Name[0] Name[1] Name[2] Name[3]

16. State the second method to initialize string variable.


Ans: Second method that is used to initialize a string variable is to type the contents within the
curly brackets but without mentioning its size by leaving the square brackets empty.
char city* + = “Karachi”:

17. Which header file is used for string?


Ans: To use strings in computer programs, it is essential to learn how string functions are used.
The header file<string.h> is used when string function are used in program. C++ supports a large
number of string handling functions in the standard library<string.h>.

18. Name some commonly used string functions.


Ans: The most commonly used string functions are described as follows.
(1) cin.get( ) Function (2) strcpy( ) Function (3) strcat( ) Function
(4) strcmp( ) Function (5) strlen( ) Function

19. State the use of cin.get function.


Ans: cin.get( ) function is used to read a string from the keyboard that may contain blank
spaces.

20. State the general form of cin.get( ) function.


Ans: The general form of cin.get( ) function is:
cin.get(strvar, strsize);

21. Why we use strcpy( ) function.


Ans: The strcpy( ) functions is used to copy contents of a string variable or string constant to
another string variable.

22. State the general form of strcpy( ) function.


Ans: The general form of strcpy( ) function is:
Syntax:
strcpy(string2, string1);

23. State the use of strcat( ) function.


Ans: The strcat( ) function is used for concatenation or joining of two strings.

24. Write the general form of strcat( ) function.


Ans: The general form of strcat( ) function is:
Syntax:
strcat(string1, string2);

25. Why we use strlen( ) function?


Ans: The strlen( ) function is used to return the length (the number of characters) of a string.

26. Write the general form of strlen( ) function.


Ans:
Syntax:
strlen(string);
27. Write down the use of strcmp( ) function.
Ans: The strcmp( ) function compares two strings and returns an integer value based on the
result of comparison. This comparison is based on ASCII codes of characters.

28. State the general form of strcmp( ) function.


Ans: The general form of strcmp() function is:
Syntax:
strcmp(string1, string2);

29. Write a program in C++, which accepts the user's first and last name and print them in reverse
order with a space between them.
Ans: Program:

# include<iostream>
# include<string>
using namespace std;
int main( )
{
char fname[30], lname[30];
cout<<" Input first name: " ;
cin>> fname;
cout<<" Input last name: " ;
cin>> lname;
cout<< " Name in reverse order is: " << lname << " " << fname<< endl;
cout<< endl;
retutn 0;
}

30. Write a C++ program to concatenate two strings.


Ans: Program:
# include<iostream>
# include<cstring>
using namespace std;
int main( )
{
char s1[50], s2[50], result[100];
cout<< " Enter string s1: " ;
cin.getline(s1 , 50); // Reading first string from user
cout<< " Enter string s2: " ;
cin.getline(s2, 50) ; // Reading second string from user
strcat( s1, s2); // strcat is used to concatenates two strings
cout<< " String obtained on concatenation is: " << s1 << endl;
return 0;
}

31. Write a program that find sting length in C++.


Ans: Program

# include<iostream>
using namespace std;
int main( )
{
string str = " Tech study" ;
// You can also use str.length( )
cout<< "" String length = " << str.size( );
return 0 ;
}

32. Write a C++ program to compare two strings strcmp.


Ans: Program

# include<iostream>
# include<string.h>
using namespace std;
int main( )
{
char str1[1000], str2[1000];
cout<< " Enter the first string" << endl;
cin>> str1;
cout<< " Enter the second string" << endl;
cin>> str2;

if (strcmp(str1, str2) == 0)

cout<< " Entered strings are equal" <<endl;

else

cout<<" Entered strings are not equal"<< endl;


return 0;
}

Prepared By:
Mr. Usman (SS)
Army College Mailsi Campus
Cell # 0301-2156545 , 0349-3036699

You might also like