InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
Give one example showing initialization of a two-dimensional array. |
|
Answer» Example of array initialization with declaration: int matrix [3] [3] = { 91,96,90,94,99,95,87,92,98}; |
|
| 2. |
How an element in the two-dimensional array is accessed? |
|
Answer» An element in the two-dimensional array is accessed using two index/subscript numbers. The first number indicates the row index and the second number indicates the column index. |
|
| 3. |
Declare an array to store the 6 subject marks of 500 students. |
|
Answer» Int marks [500] [6]; |
|
| 4. |
What is index or subscript of an Array? |
|
Answer» Subscript is used to identify/locate the position of array elements. |
|
| 5. |
What is zero-based indexing? |
|
Answer» If ‘i’ is the element, then its index number would be i-1 and it is called zero-based indexing. |
|
| 6. |
Define multidimensional array. |
|
Answer» A collection of similar elements structured in N dimensions where N>=1. Each element is accessed by N indices. |
|
| 7. |
Mention the different types of arrays. |
|
Answer» There are three types of arrays, namely 1. One dimensional array, 2. Two-dimensional array and 3. Multidimensional array |
|
| 8. |
What is an Array? |
|
Answer» Array is a group of similar elements that share a common name. |
|
| 9. |
What is the use of arrays? |
|
Answer» The use of arrays reduces the creation and use of more number of individual variables in a program. It also helps to reduce the program length. |
|
| 10. |
Explain any two uses of arrays. |
|
Answer» It reduces the creation and use of more number of individual variables in a program. Confusion and complexity in identifying the variables can be avoided. |
|
| 11. |
What is the subscript of the first element of an array? |
|
Answer» The first element i = 1, then subscript is 0 because of i-1 i.e., 1-1 |
|
| 12. |
Write any two features of an array. |
|
Answer» Array can be of any data type available in ‘C++’, i.e., integer array, float array, character array, etc., All the elements of an array will contain the same type of data. |
|
| 13. |
Mention any one feature of subscript or index of an array. |
|
Answer» Subscript or index of an array is always a positive whole number. |
|
| 14. |
What are the characteristics of index/subscript of any array? |
|
Answer» It is always a positive whole number. It may be an integer, constant or expression and cannot be a negative integer or floating value. The first element of an array always starts with the value 0. |
|
| 15. |
Give an example of, how a two-dimensional array is declared? |
|
Answer» int marks[5][5]; |
|
| 16. |
Define a one-dimensional array. |
|
Answer» One dimensional array is a collection of similar elements that share a common name and is structured in one dimension only. |
|
| 17. |
How can we input the values into a one-dimensional array? |
|
Answer» Using ‘for’ loop: int age [10], i; for(i=0; i<10;i++) cin>>age[i]; } |
|
| 18. |
Give an explanation of the declaration of one-dimensional array with an example. |
|
Answer» int marks[6]; Name of the array is marks. Type of the array is integer. Size of the array is 6. i.e., we can store up to 6 integer values in the array. |
|
| 19. |
Write the declaration syntax of a two-dimensional array. |
|
Answer» <data-type><array-name>[<sizq1>][<size>]; |
|
| 20. |
Give the declaration syntax of a one-dimensional array. |
|
Answer» datatype array name [size]; |
|
| 21. |
Define a two-dimensional array. |
|
Answer» Two-dimensional array is a collection of elements of similar type that share a common name, structured in two dimensions. Each element is accessed by two index values. |
|
| 22. |
How are elements of one-dimensional array displayed? |
|
Answer» for(i=0;i<10;i++) cout<<age[i]; |
|
| 23. |
Give an example showing initialization of one-dimensional array. |
|
Answer» int marks[6] = { 91,96,90,94,99,93}; |
|
| 24. |
What is meant by initialization of an array? |
|
Answer» Initialization means assigning values to the declared variables or arrays. To initialize an array, each and every element of the array has to be referred and then value assigned to it. |
|
| 25. |
Give an explanation of the declaration of a two-dimensional array with an example. |
|
Answer» int marks[5][5]; 1. Name of the array is marks. 2. Type of the array is integer. 3. Size of the array is 5 rows and 5 columns, i.e., we can store up to 25 integer values in the array (row x column) 5 x 5 = 25. |
|
| 26. |
How can we input the values into a two-dimensional array? |
|
Answer» int mat[3][5], row, col; for (row = 0; row < 3; row++) for (col = 0; col < 5; col++) cin >> mat[row] [col]; |
|
| 27. |
Write a C++ program to read 10 integer values and find the largest number among them using array. |
|
Answer» # include using namespace std; int main() { int n[10],i, big; cout<<"enter 10 integers:"; for(i=0;i<10;i++) cin>>n[i]; big=n[0]; for(i=0;i>=10;i++) { if(n[i]>big) big=n[i]; } cout<<"largest mark is"<<big; } |
|
| 28. |
Consider the following array declaration. Write statements to count how many numbers are greater than zero. int p[ ] = {-5, 6, -7, 0, 8, 9}; |
|
Answer» # include using namespace std; int main() { int p[]={-5, 6, -7, 0, 8, 9},i, count =0; for(i=5;i>=0;i--) if(p[i]>0) count++; cout<<"There are "<<count<<"numbers that are greater than zero"; } |
|
| 29. |
Consider the following C++ statements char str[] = “NO/nSMOKING”;cout<<str;1. What is the output of the above code? 2. How many bytes will be allocated in the memory for the variable str? |
|
Answer» 1. NO SMOKING. The output is in 2 lines. 2. A total of 11 bytes is used to store this string. 1 byte for \n. 1 byte for \0(The null character that is automatically appended) and 9 bytes for the remaining characters (N, 0, S, M, 0, K, l, N AND G). |
|
| 30. |
Write a C++ program to read 6 marks of a student and find total and average mark. |
|
Answer» # include using namespace std; int main() { int mark[6],i,total=0; float avg; for(i=0;i<6;i++) { cout<<"Enter mark"<<i+1<<":"; cin>>mark[i]; total=total+mark[i]; } avg=total/6.0; cout<<"\n The marks are given below\n"; for(i=0;i<6;i++) cout<<mark[i]<<endl; cout<<"The total mark is "<<total<<endl; cout<<"The average mark is"<<avg; } |
|
| 31. |
Consider the following C++ statements1. char word[20]; cin>>word; cout<>word;2. char word[20]; gets(word); puts(word);If the string entered is “HAPPY NEW YEAR”. Predict the output in both cases and justify your answers. |
|
Answer» 1. HAPPY. When we use cin to accept string then space is the delimiter. The string after space is truncated. 2. HAPPY NEW YEAR. gets() reads all the characters (including space) upto the user presses the enter key. |
|
| 32. |
Write a C++ program to accept a sentence and count the number of times the letter ‘s’ occurs in it. For example if the sentence is This is my school’, the output should be 3. |
|
Answer» # include # include using namespace std; int main() { char str[80], ch='s'; int i,n=0,pos; cout<<"Enter a string"; gets(str); for(i=0;str[i]!='\0';i++) if(str[i]==ch) { n++; pos=i; } if(n==1) cout<<"The character s is present in the position"<<pos+1; else if(n>1) cout<<"The character s is present"<<n<<"times"; else cout<<"The character s is not present in the string"; } |
|