• +91 9971497814
• info@interviewmaterial.com

# Chapter 8- Arrays Interview Questions Answers

### Related Subjects

Question 1 :
Observe the following C++ code and find out, which out of the given options (i) to (iv) are the expected correct output. Also assign the maximum and minimum value that can be assigned to the variable ‘Go’.

void main()
{ int X [4] = {100, 75, 10, 125};
int Go = random(2) + 2;
for (int i = Go; i<4; i ++)
cout<
}
(i) 100\$\$75
(ii) 75\$\$10\$\$125\$\$
(iii) 75\$\$10\$\$
(iv) 10\$\$125\$

(iv) is correct option
Max. Value = 3
Min. Value = 2

Question 2 :
Write the difintion of a function grace_score (int score [], int size) in C ++,which should check all the elements of the array and give an increase of 5 to those scores which are less than 40.
Example: if an array of seven integers is as follows:
45,35,85,80,33,27,90
After executing the function, the array content should be changed as follows :
45,40,85,80,38,32,90

void grace_score(int score[].int size)
{
for(int i=0;i
{
if (score [i]<40)
score [i]=score[i]+5;
cout< }
}

Question 3 :
Find the output of the following C+ + program:
#include void repch(char s[]) { for(int i=0;s[i]!=’\0′;i++) { if(((i%2)!=0) &&(s[i]!=s[i + 1])) { S[i]=’@’; cout<<“Hello”; } else if (s[i]==s [i + 1]) { s[i+1]=” ! ” ; i++ ; } } } void main() { char str[]=”SUCCESS”; cout<<“Original String”<

Original String SUCCESS
Changed String S@C!ES!

Question 4 :
Find the output of the following:

#include
void switchover (int A[],int N, int split)
{
for(int K=0; K
if(K
A [K] + = K;
else
A[K] *=K; }
void display(int A [],int N)
{
for (in K=0; K
(K%2==0)? Cout<
cout<
}
void main()
{ int H [ ] = {30, 40, 50, 20, 10, 5};
switchover(H, 6, 3);
display(H, 6);
}

30%41
52% 60
40% 25

Question 5 :
An integer array A [30] [40] is stored along the column in the memory. If the element A[20][25] is stored at 50000, find out the location of A[25] [30].

A[i] [j] = B+W x [No. of rows x (I-Lr) + (J-Lc)]
A[20] [25] = B + 2x [30x (20-0) + (25-0)]
50000 = B+2x[30x(20-0)+(25-0)]
B = 48750
A[7] [10] = 48750+ 2x[30x(7-0)+(10-0)]
= 49190

Question 6 :
An array P[30][20] is stored along the column in the memory with each element requiring 2 bytes of storage. If the base address of the array P is 26500, find out the location of P[20][10].

Total number of rows = 30
Total size = 2 bytes
Assuming Lower Bound of Row(LBR)=0
Lower Bound of Column(LBC)=0
Total number of Rows(R)=30
Size of each element(W)=2
LOC(P[20][10]) = 26500 + ((20-0)+(10-0)*30)*2
LOC(P[20][10]) = 26500 + 640
LOC(P[20][10]) = 27140

Question 7 :
Write a function to sort any array of n elements using insertion sort. Array should be passed as argument to the function.

void insertsort (int a[],int n)
{
int p,ptr;
//Assuming a[0]=int_min i.e. smallest integer
for(p = 1; p<=n;p++)
{
temp=a[p];
ptr=p-1;
while (temp
{
a [ptr+1]=a[ptr] ; //Move Element
Forward
ptr--;
}
a[ptr+1]=temp; //Insert Element in Proper Place
}

Question 8 :
Write a function NewMAT(int A[][],int r, int c) in C++, which accepts a 2d array of integer and its size as parameters divide all those array elements by 6 which are not in the range 60 to 600(both values inclusive) in the 2d Array.

void NewMAT(int A[] [] , int r,int c)
{
for (int i = 0; i
for (j=0; j=60)&&(A[1] [j)<=600))
A[i][j]/=6
OR

A[i] [j] =A[i] [j] /6;
}

Question 9 :
A two dimensional array P[20] [50] is stored in the memory along the row with each of its element occupying 4 bytes, find the address of the element P[10] [30], if the element P[5] [5] is stored at the memory location 15000.

Loc (P [I] [J] along the row =BaseAddress+W [(I-LBR)*C+ (J-LBC)]
(where C is the number of columns, LBR = LBC =0)
LOC (P [5] [5]) = BaseAddress + W* [I*C + J]
15000 = BaseAddress + 4 * [5 * 50 + 5]
= BaseAddress + 4 * [250 + 5]
BaseAddress = 15000 -1020 = 13980
LOC (P [10] [30]) = 13980 + 4* [10 * 50 + 30]
= 13980 + 4 * 530
= 13980 + 2120
= 16100
OR LOC (P [10] [30])
= Loc (P[5] [5]) + W[ (I-LBR) * C+ (J-LBC)]
= 15000 + 4 [(10 – 5) * 50 + (30 – 5)]
= 15000 + 4 [5 * 50 + 25]
= 15000 + 4 *275
= 15000 + 1100
= 16100
OR
(where C is the number of columns and LBR = LBC = 1)
LOC (P [5] [5])
15000 = BaseAddress + W [(I-1) *C + (J-1)]
= BaseAddress + 4 [4*50 + 4]
= BaseAddress + 4 [200 + 4]
= BaseAddress + 4 * 204
BaseAddress = 15000 – 816 = 14184
LOC (P [10] [30])
= 14184 + 4 [(10 -1) * 50 + (30 -1)]
= 14184 + 4 [9*50 + 29]
= 14184 + 4*479
= 14184 + 1916
= 16100

Question 10 :
Write a user-defined function DisTen (int A[] [4], int N, int M) in C++ to find and display all the numbers, which are divisible by 10. For example if the content of array is :

12 20 13
2 10 30
The output should be
20 10 30

void DisTen (int A[] [4] , int N, int M)
{
int i, j;
for (i=0; i
{
for (j = 0 ; j
{
if (A[i][j]%10==0)
cout << A [i] [ j] ;
}
}
}

krishan