Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

Q1: Consider an array X that contains n positive integers. A subarray of X is defined to be a sequence of array locations with consecutive indices.
The C code snippet given below has been written to compute the length of the longest subarray of X that contains at most two distinct integers. The code has two missing expressions labelled (P) and (Q).
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Which one of the following options gives the CORRECT missing expressions?   (2024 SET-2)
(Hint: At the end of the i-th iteration, the value of len1 is the length of the longest subarray ending with X[i] that contains all equal values, and len2 is the length of the longest subarray ending with X[i] that contains at most two distinct values.)
(a) (P) len1+1 (Q) len2+1
(b) (P) 1 (Q) len1+1
(c) (P) 1 (Q) len2+1
(d) (P) len2+1 (Q) len1+1
Ans: 
(b)
Sol: 
In the given program, we keep track of the at most two distinct values allowed in the subarray using the variables first & second.
The variable first stores the latest (or the second) distinct value in the subarray,
while the variable second stores the second latest (or the first) distinct value we see in the subarray.
The code with explanation,
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
∴ Ans = B

Q2: What is the output of the following C program?  (2024 SET-2)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 4,8
(b) 1,5
(c) 8,5
(d) 1,8
Ans: (b)
Sol:
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
int(q-p) is pointer subtraction which subtracts the address of the pointer whereas int(*q-*p) is also performs subtraction operation but it subtracts the values which are hold by pointer *p, *q
In the printf statement, the format specifier is %d which returns output in an integer from
So
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
int(*q-*p) = int(25.0 - 20.0) = int(5.0) = 5
Option (B) is correct.

Q3: Consider the following C function definition.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Which of the following statements is/are TRUE?  (2024 SET-2)
(a) The function call fX("abcd") will always return a value
(b) Assuming a character array c is declared as char c[] = "abcd" in main(), the function call fX(c)will always return a value
(d) The code of the function will not compile
(d) Assuming a character pointer c is declared as char *c = "abcd" in main(), the function call fX(c)will always return a value
Ans:
(a, b and d)
Sol: 
We are modifying the local variable inside the function.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)

Q4: What is printed by the following ANSI C program?  (2022)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(a)Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(b)Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(c)Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(d)Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Ans: (a)
Sol:

a is 3D array with size [3][3][3]
Therefore each 2D array contains 9 elements, we have 3 such arrays
0th 2D array have {1, 2, 3, 4, 5, 6, 7, 8, 9}
1st 2D array have (10, 11, 12, 13, 14, 15, 16, 17,18)
2nd 2D array have (19, 20, 21, 22, 23, 24, 25, 26, 27)
each 2D array is collection of 1D array. we have 3 one dimensional arrays in one 2D
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
when i = 0, j = 0, output = {1,2,3}
when i = 1, j = 0, output = {10,11,12)
when i = 2, j = 0, output = {19,20,21}

Q5: What is printed by the following ANSI C program?  (2022)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 1, 10, 11
(b) 1, 10, 14
(c) 10, 14, 11
(d) 10, 10,14
Ans: 
(d)
Sol: 
Given that x=1, z[0]=10 and z[1]=11
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
By this code, they are accessing x and changing it's value to 10. Therefore x = 10 after the above code executed.
*(&z[0]+1) += 3; 
[As per precedence rule, z[0], &z[0], &z[0]+1, *(&z[0]+1) and *(&z[0]+1) +=3; ev
&z[0] --- address of z[0]
&z[0] + 1 --- address of z[1]
address of z[1]
[because z[0] and z[1] are contiguously allocated in the memory]
So, we can rewrite the above statement as * (address of z[1] ) + = 3
which is nothing but z[1] + = 3 ==> z[1] = z[1] + 3
Therefore z[1] = 14
Notice that, z[0] not changed.

Q6: Consider the following ANSI C code segment:
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Assume that the variable y points to a struct (allocated on the heap) containing two fields f1 and f2, and the local variables x, y, z, p, q, and i are allotted registers. Common sub-expression elimination (CSE) optimization is applied on the code. The number of addition and the dereference operations (of the form y -> f1 or y -> f2) in the optimized code, respectively, are:  (2021 SET-2)
(a) 403 and 102
(b) 203 and 2
(c) 303 and 102
(d) 303 and 2

Ans: (d)
Sol: 

Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Whether we take if or else block we get 2 additions, the loop runs exactly 200/2 = 100 times, so from loop we get 2 × 100 = 200 additions plus 100 additions for incrementing the value of i, before loop we had perform 3 additions, so total additions 303.
We only do two de-reference outside the for loop, so total de-references = 2.
Option D.

Q7: Consider the following ANSI C program.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
What is the output of the above program?  (2021 SET-2)
(a) 14
(b) 20
(c) 24
(d) 30
Ans:
(c)
Sol: 
arr[1] + 9
arr[1] is a pointer to 1D array of 5 integers and so the above expression involves pointer arithmetic.
For a pointer value p and integer value d,

  • p + d ⇒ p + sizeof(*p) + d

So,
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Now, C language follows row major ordering for arrays which means that when a multi dimensional array gets linearized in memory the lower dimensions get arranged contiguously.
For the 2D array arr[4][5] it'll be
5 elements of arr[0] [5] followed by 5 elements of arr[1] [5] followed by 5 elements of arr[2][5] and so on.
So, the 14th element will be at row number [14/5] = 2 and column number 14%5 = 4,
which is arr[2][4] = 10 × 2 + 4 = 24.

Q8: Following declaration of an array of struct, assumes size of byte, short, int and long are 1,2,3 and 4 respectively. Alignment rule stipulates that n byte field must be located at an address divisible by n, the fields in the struct are not rearranged, padding is used to ensure alignment. All elements of array should be of same size.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Assuming C is located at an address divisible by 8, what is the total size of C, in bytes?  (2020)
(a)150
(b) 160
(c) 200
(d) 240
Ans: 
(b)
Sol: 
I believe the options are incorrect
Alignment rule stipulates that n−byte field must be located at an address divisible by n.
So,

  • Short must be in an address divisible by 2
  • Byte must be in an address divisible by 1
  • Long must be in an address divisible by 4
  • Int must be in an address divisible by 3

Following the same (assuming Base Address to be 0),
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
So the data type is 12 bytes after padding. Since it's an array of 10 elements, total size = 120 bytes

Q9: What is output of the following 'C' code assuming it runs on a byte addressed little endian machine?  (2020)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 622
(b) 311
(c) 22
(d) 110
Ans:
(d)
Sol: 
Big-endian : 
It is the order in which the "big end" (most significant value in the sequence (MSB) is stored first at the lowest storage address.
Little-endian :
It is an order in which the "Little end" (LSB) is stored first. Now the associativity of the comma (,) is from left to right.
Also, the precedence of the comma is lower than the equal to "=" operator.
So, x will store only 622
Now, Binary value of  622 is given byPrevious Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
∴ Little-endian will store only lower bytes.
So, 110 (decimal value) will only be stored in x.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
() ∴ (d) is the correct option. 

Q10: What is the output of the code given below?  (2020)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 100
(b) 110
(c) 40
(d) 44
Ans: 
(b)
Sol: 
The above char array name can be written as below:
char[] name = {'s','a','t','e','l','l','i','t','e','s', '\0' };
strlen gives no of characters in a string without the null in the end = 10.
sizeof gives the no of characters(no of bytes specifically) in a string including the null at the end = 11
So 11*10= 110.

Q11: Consider the following C program.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
The output of the program is _______.  (2020)
(a) 7
(b) 10
(c) 18
(d) 19
Ans: 
(d)
Sol: 
'a' is a two dimensional array.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)

  • a = address of 0th index of 2-D array which means address of 1-D array
  • *a = address of 0th index element of 0th index 1-D array
  • **a = value at 0th index element of 0th index 1-D array
    • **a = 1
    • **a + 2 = 1 + 2 = 3
  • a + 3 = address of 3rd index 1-D array
  • *(a + 3) = address of 0th index element of 3rd index 1-D array
  • *(a + 3) + 3 = address of 3rd index element of 3rd index 1-D array
  • *(*(a + 3) + 3) = value at 3rd index element of 3rd index 1-D array = 19
    Correct Answer: 19.

Q12: Consider the following C program:
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
The output of above C program is __________ . Note: This was Numerical Type question.  (2019)
(a) 30
(b) 10
(c) 12
(d) 18
Ans: 
(b)
Sol: 
sum = 0, * b = a + 4 i.e.pointing to 10
sum = sum + (*b - i) - *(b - i)
i = 0
sum = 0 + (10 - 0) - (10) = 0
i = 1
sum = 0 + (10 - 1) - (8) = 1
i = 2
sum = 1 + (10 - 2) - (6) = 3
i = 3
sum = 3 + (10 - 3) - (4) = 6
i = 4
sum = 6 + (10 - 4) - (2) = 10

Q13: Consider the following C program:
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
The number that will be displayed on execution of the program is _________ .  (2019)
(a) 2
(b) 5
(c) 4
(d) 6
Ans: 
(d)
Sol: 
int arr[]={1,2,3,4,5,6,7,8,9,0,1,2,5} , *ip = arr+4;
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
printf("%d\n",ip[1]);
ip is an integer pointer that is currently holding the address where 5 is stored.(i.e. a[4])
ip[1] = *(ip+1) = value present at the next address i.e. 6. so it will print 6.

Q14: Consider the following C program. 
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
The output of this program is:  (2018)
(a) 0, c
(b) 0, a+2
(c) '0', 'a+2'
(d) '0', 'c'
Ans: 
(a)
Sol: 
char for + 2
i.e. x =='c'
So, p ={'1','0','c'}
*((char*)q + 1) == p[1]
*((char*)q + 2) == p[2]
printf("%c, %c", *((char*)q+1), *((char*)q+2));
Output: 0, c
Correct Answer: A

Q15: Consider the following C Program.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE) 
The output of the program is _______________.  (2017 SET-2)
(a) 2
(b) 3
(c) 4
(d) 5
Ans: 
(a)
Sol: 

Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
2[p] = *(2 + p) = p[2]
6[p] = *(6 + p) = p[6]
c + 2[p] - 6[p]-1 = c + 'T'-'I' -1 = c + 11 - 1 = c + 10 (In any character coding all alphabet letters are assigned consecutive int values as per C)
printf will print 2 which is the length of "17".

Q16: Consider the following snippet of a C program. Assume that swap (&x, &y) exchanges the contents of x and y.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
The output of the program is ____________.  (2017 SET-2)
(a) 2
(b) 3
(c) 4
(d) 5
Ans: 
(b)
Sol: 
Well, the above program is sorting the array in descending order.
Initially, while loop starts execution by evaluating the iniatial condition
while(done==0)
For the first time the first for loop will be executed completey, the content of array will be as follows:
5, 3, 4, 6, 2, 1
After the second for executed completey the content of array will be as follows:
6,5, 3, 4, 2, 1
But the value variable done is still 0 so while loop will execute again, so now the content of array after executing the first for loop will be 6, 5, 4, 3, 2, 1 and no change in second for loop but still the done variable is 0.
So, while loop execute again, now done variable is modified to 1 and there will be no change in done variable because inside first and second for loop no if condition will satisfied.
Finally, the while condition is evaluated false and value of array[3] will be printed which is 3.

Q17: Consider the following function implemented in C:
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)

The output of invoking printxy (1,1) is  (2017 SET-2)
(a) 0,0
(b) 0,1
(c) 1,0
(d) 1,1
Ans:
(c)
Sol: 
At first in loop we are giving x = 0 then ptr is pointing to X.
So, *ptr = 0
Now, we copying the value of ptr to y, so Y = 0
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Now, value of ptr is changed to 1. so the location of X itself got modified
*ptr=1;
As it is pointing to x so x will also be changed to 1
So, 1, 0 will be the value
C is correct answer here.

Q18: Consider the following C program.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)

Recall that strlen is defined in string. h as returning a value of type size_t, which is an unsigned int. The output of the program is _____________.  (2017 SET-1)
(a) 5
(b) 3
(c) 4
(d) 6
Ans: 
(b)
Sol: 
(strlen(s) - strlen(t)) = 3 - 5 = -2
But in C, when we do operation with two unsigned integers, result is also unsigned. (strlen returns size_t which is unsigned in most systems). So, this result "-2" is treated as unsigned and its value is INT_MAX - 2 (not sure about all systems, but at least on systems using 2's complement representation). Now, the comparison is between this large number and another unsigned number c which is 0. So, the comparison return TRUE here.
Even if 'c' is declared as "int", while doing an operation with an unsigned int, it gets promoted to unsigned int and we get the same result.
Hence (strlen(s) - strlen(t)) > 0 will return 1 on execution thus the conditional operator will return the true statement which is strlen(abc) = 3.
Ans. should be 3.

Q19: Consider the following C code:
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
The code suffers from which one of the following problems:  (2017 SET-1)
(a) compiler error as the return of malloc is not typecast appropriately.
(b) compiler error because the comparison should be made as x==NULL and not as shown.
(c) compiles successfully but execution may result in dangling pointer.
(d) compiles successfully but execution may result in memory leak.

Ans: (d)
Sol
Option A: In C++ we need to do typecasting. C does automatic implicit typecasting.
Option B: Null means address 0. if (a == 0) if (0 == a) There is no difference.
Option C: Do it step by step, always x is pointing to a valid memory location. Dangling Pointer means if it points to a memory location which is deleted(freed). So no dangling pointer.
Option D: x will loss the previous address it was pointing to. So it will result in memory leak.

Q20: Consider the following C program.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
The output of the program is ______________.  (2015 SET-3)
(a) 440
(b) 140
(c) 110
(d) 410
Ans: 
(b)
Sol: 

Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
ptr++;
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
**ptr = p[2] = *(a+3) = 40
printf("%d%d", ptr-p, **ptr);  // 140

Q21: Consider the following C program segment.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
What will be printed by the program?  (215 SET-3)
(a) 12
(b) 120400
(c) 1204
(d) 1034
Ans: 
(c)
Sol: 
p = s1 + 2;
Type of s1 is char[7] and sizeof *s1 is sizeof (char) = 1. So, s1 + 2 will return address in s1 + 2 * sizeof(char) = address in s1 + 2. So, p now points to the third element in s1.
*p = '0';
The third element in s1 is made 0. So, 1234 becomes 1204. C choice.

Q22: What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an integer requires four bytes of memory.  (2015 SET-1)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 2036, 2036, 2036

(b) 2012, 4, 2204
(c) 2036, 10, 10
(d) 2012, 4, 6
Ans: 
(a)
Sol: 
Address of z is 2000.
being a 2 D array,
x + 3 = x + 3 * sizeof its inner dimension
2000 + 3 * 3 * 4 (as inner dimension is 3 integers of size 4)
2000 + 36 = 2036.
*(x + 3) returns the value at address 2036. But since z is
2 - D array, one * will just return the 1 D array which is the starting address of it, which is 2036 only.
(x + 2) = 2000 + 2 * 3 * 4 = 2024
*(x + 2) + 3 = 2024 + 3 * 4 = 2036 (The changes the
data type from 2D to 1D and hence + 3 will add 3 * 4 and not 3 * 3 * 4)
So, A.

Q23: Consider the C function given below. Assume that the array listA contains n (> 0) elements, sorted in ascending order.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)

Which one of the following statements about the function Process Array is CORRECT?  (2014 SET-3)
(a) It will run into an infinite loop when x is not in listA
(b) It is an implementation of binary search
(c) It will always find the maximum element in listA.
(d) It will return - 1 even when x is present in listA.
Ans: 
(b)
Sol: 
This is an implementation of the Binary search algorithm.
Note that the loop will be terminated when we have found x. In that case both the if conditions will be true making condition inside the while as false i.e., i > j.
Correct Answer: B

Q24: Consider the following program in C language:
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Which one of the following statements is TRUE?  (2014 SET-1)
(a) Compilation fails.
(b) Execution results in a run-time error.
(c) On execution, the value printed is 5 more than the address of variable i.
(d) On execution, the value printed is 5 more than the integer value entered
Ans: 
(d)
Sol:

 Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
input = 3 ; output = 8
Option D is answer.

Q25: What does the following fragment of C-program print?  (2011)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(a) GATE2011
(b) E2011
(c) 2011
(d) 11
Ans: 
(c)
Sol: 
In C, there is a rule that whatever character code be used by the compiler, codes of all alphabets and digits must be in order. So, if character code of 'A' is x, then for 'B' it must be x + 1.
Now %s means printf takes and address and prints all bytes starting from that address as characters till any byte becomes the code for '\0'. Now, the passed value to printf here is
p + p[3] - p[1]
p is the starting address of array c. p[3] =' E' and p[1] =' A'. So, p[3] - p[1] = 4, and p + 4 will be pointing to the fifth position in the array c. So, printf starts printing from 2 and prints 2011.
(Here " WGATE2011"// is a string literal and by default a '\0' is added at the end of it by the compiler).
NB: In this question %s is not required.
 printf(p + p[3] - p[1]);
Also gives the same result as first argument to printf is a character pointer and only if we want to pass more arguments we need to use a format string.

Q26: C program is given below:
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
What should be the contents of the array b at the end of the program?  (2008)
(a)Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)

(b)Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(c)Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(d)Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Ans: (b)
Sol: 
The correct answer is option (B).
first integer type two variables declared i and j
then an integer type 2 - d array a[2] [3] is declared and initialized and 2 - d array b[3] [2] is created but not initialized. i.e.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
now the char type pointer is declared and the base address of  array b is put  in it. so p = 3000
now the for loop is started where i is initialized to 0, so
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
now the values in array b is
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Hence, the output will be (B) choice.
Note:
*(p + 2*j + i)
p+ size of inner dimension *j + i, hence is same as p[j][i]. Hence with this statement we can identify that the code is transposing the matrix a and storing in b using pointer p.

Q27: Consider the C program given below. What does it print?  (2008)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 2,3
(b) 2,4
(c) 3,2
(d) 3,3
Ans:
(c)
Sol: 
First 2 variable integer type declared named i and j
Then int type array a[8] declared and initialized.
a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4, a [4] = 5, a[5] = 6, a [6] = 7, a [7] = 8
Then for loop started
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Now another for loop started where in loop integer type variable named i declared
Block Scope: A Block is a set of statements enclosed within left and right braces ({ and } respectively). Blocks may be nested in C (a block may contain other blocks inside it). A variable declared in a block is accessible in the block and all inner blocks of that block, but not accessible outside the block.
What if the inner block itself has one variable with the same name?
If an inner block declares a variable with the same name as the variable declared by the outer block, then the visibility of the outer block variable ends at the point of declaration by inner block.
So here inner block int i has the scope in this block only and outer block int i visibility is not allowed in that block
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Now when the for loop ends its variable named i scope is also end and the outer block variable now visible. So, in printf outer variable i is used.
So, the output would be: 3, 2.

Q28: What is the output printed by the following C code?  (2008)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(a) dlrow
(b) Null string
(c) dlrld
(d) worow
Ans: 
(b)
Sol: 

Char a[6] =Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
After the loop executes for the first time,
a[0] = a[5]
a[0] = '\0'
Next two more iterations of the loop till i < j condition becomes false, are not important for the output as the first position is '\0';
printf("%s", a);
printf function for format specifier '%s' prints the characters from the corresponding parameter (which should be an address) until "\0" occurs. Here, first character at a is "\0" and hence it will print nothing.
So, option (B).

Q29: Consider the following C program that attempts to locate an element x in an array Y[] using binary search. The program is erroneous.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
The correction needed in the program to make it work properly is  (2008)
(a) Change line 6 to: if (Y[k] <x) i=k+1; else j=k-1;
(b) Change line 6 to: if (Y[k]<x) i=k-1; else j=k+1;
(c) Change line 6 to: if (Y[k] <x) i=k; else j=k;
(d) Change line 7 to: } while ((Y[k]==x) && (i<j));
Ans: 
(a)
Sol:

if( Y[k] < x) then i = k + 1;
if given element that  we are searching is greater, then searching will be continued in the upper half of array
otherwise j = k - 1;
in the lower half.
Take few case in consideration i.e.

  1.  All elements are same
  2.  Increasing order with no repetition
  3.  Increasing order with repetition.

Q30: Consider the following C program that attempts to locate an element x in an array Y[] using binary search. The program is erroneous.
On which of the following contents of Y and x does the program fail?  (2008)
(a) Y is [1 2 3 4 5 6 7 8 9 10] and x < 10
(b) Y is [1 3 5 7 9 11 13 15 17 19] and x < 1
(c) Y is [2 2 2 2 2 2 2 2 2 2] and x > 2
(d) Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is even
Ans: 
(c)
Sol: 
when it is option C the control will continue to iterate as i = 8 and j = 9;
again and again i will be assigned k which itself equals 8 asPrevious Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)being stored in an integer type variable, will evaluate to 8.
For option A, with x = 9, k will take the following values:

  • 4
  • 6
  • 7
  • 8 - y[8] = 9, x found

For option D, with x = 10, k will take the following values:

  •  4, y[4] = 10, x found

Q31: Consider the C program given below :
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
What is the value printed out when this program is executed?  (2007)
(a) 9
(b) 8
(c) 7
(d) 6
Ans: (c)
Sol:

Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
[End of for loop]
If (sum (i.e., 2) > maxsum (i.e., 7)) // No
maxsum = sum; // Not Executed
printf will output maxsum = 7

Q32: Which one of the choices given below would be printed when the following program is executed?  (2006)

Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 8, -12, 7, 23, 8
(b) 8, 8, 7, 23, 7
(c) -12, -12, 27, -31, 23
(d) -12, -12, 27, -31, 56
Ans:
(a)
Sol:

a = {a1, a2, a3};
a[0] is al. So, this will print a1 [2] = 8;
 printf("%d,", *a[2]);
a[2] is a3. So, this will print *a3 = a3[0] = -12( has greater precedence than *)
printf("%d,", *++a[0]);
a[0] which is a1 is incremented. a1 is a pointer to int (base address of an integer array) and so increment means adding sizeof(int) and hence a1 now points to the second element in the array. So, * + +a[0] prints second element of a1 which is 7 and now a1 starts from 7.
printf("%d,", *(++a)[0]);
+ + a will increment a, which being a pointer (In C, an array when passed to a function becomes a pointer) to pointer (to int) will add sizeof(pointer) to a. So, a now contains {a2, a3} and a[0] will be a2 and *a2
will be the first element in a2 which is 23
printf("%d\n", a[-1] [+1]);
a[-1] will subtract a size of pointer from the base address of a. Normally this results in invalid memory access, but since we have incremented a previously, a[-1] is valid and will point to a1. So, a[-1][+1] will be a1 [1] which has the value 8.
(al was incremented in 3rd printf and hence starts from 7 and not 6. +1 is same as 1, just given to create confusion)
Correct Answer: A

Q33: Which one of the choices given below would be printed when the following program is executed ?  (2006)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(a) jungle, n, 8, nclastor
(b) after, u, 6, ungle
(c) cetter, k, 6, jungle
(d) after, u, 8, ncestor
Ans: 
(b)
Sol: 

code :
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
We will assume few things:

  • Size of integer 4 Bytes.
  • Size of a pointer 4 Bytes.

Neglecting any alignment issues with the storage of this structure we will have 8 Bytes per structure.
And one precedence rule we need to use:
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Initial situation :
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
struct test *p = st;
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
p += 1;
We know that if ptr is a pointer then, ptr + x = ptr + x*sizeof(*ptr);
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
++p->c;
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
printf("%s,", p++->c);         // (p++)->c
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
printf("%c,", *++p->c);     // *(++(p->c))
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
printf("%d,", p[0].i);
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
printf("%s \n", p->c);
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Correct Answer: B

Q34: Let a be an array containing n integers in increasing order. The following algorithm determines whether there are two distinct numbers in the array whose difference is a specified number S > 0.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Choose the correct expression for E.  (2005)
(a) a[j] - a[i] > S
(b) a[j] - a[i] < S
(c) a[i] - a[j] < S
(d) a[i] - a[j] > S
Ans: 
(b)
Sol: 
For some 'i' if we find that difference of (A[j] - A[i] < S) we increment 'j' to make this difference wider so that it becomes equal to S.
If at times difference becomes greater than S we know that it wont reduce further for same 'i' and so we increment the 'i'.
We do it for each 'i' if not found in previous iteration. until i = n

Q35: The following C function takes two ASCII strings and determines whether one is an anagram of the other. An anagram of a string s is a string obtained by permuting the letters in s.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Choose the correct alternative for statements A and B.  (2005)
(a) A: count [a[j]]++ and B: count[b[j]]--
(b) A: count [a[j]]++ and B: count[b[j]]++
(c) A: count [a[j++]]++ and B: count[b[j]]--
(d) A: count [a[j]]++ and B: count[b[j++]]--
Ans: 
(d)
Sol:

Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)

Q36: Consider the following C program which is supposed to compute the transpose of a given 4 x 4 matrix M. Note that, there is an X in the program which indicates some missing statements. Choose the correct option to replace X in the program.  (2004)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)

(a)Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(b)Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(c)Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(d)Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Ans: 
(c)
Sol: 
look at the initial value of j, if j starts with 0, then double for loop will swap M[i][j] with M[j][i] and also M[j][i] and M[i][j] so the matrix M will remain unchanged, so to avoid this double swapping we need to initialize j = i and swap only upper triangular matrix with lower triangular matrix.
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)

Q37: Consider the following C program segment:
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
The output of the program is  (2004)
(a) gnirts
(b) string
(c) gnirt
(d) no output is printed
Ans:
(d)
Sol: 

Here,
p[0] = s[length] = '\0'; //compiler puts a '\0' at the end of all string literals
Now, for any string function in C, it checks till the first '\0' to identify the end of string. So, since the first char is '\0', printf %s, will print empty string. If we use printf("%s", p+1); we will get option (C) with some possible garbage until some memory location happens to contain "\0". For the given code, answer is (D).

Q38: In the following C program fragment, j, k, n and TwoLog_n are integer variables, and A is an array of integers. The variable n is initialized to an integer ≥ 3, and TwoLog_n is initialized to the value of 2 * [log2n]
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
The set of number printed by this program fragment is  (2003)
(a) {m | m ≤ n, (∃ i) [m = i!]} Here i! mean factorial of i
(b) {m | m ≤ n, (∃i) [m = i2]}
(c) {m | m ≤ n, m is prime)
(d) {}
Ans: 
(d)
Sol:

The nested loop takes all integers from 2 to 2 * logn, takes all their non-multiples before n, and makes the corresponding entry in A as 1. For example, for 2, and n = 10, A[3], A[5], A[7], and A[9] are made 1. Similarly for 3, 4,... till 2 * log n. So, if any entry A[p] is 0 means it must be a multiple of 2,3,.... 2 * log2 n, which is (2 log n)! and is greater than n. So, for no index p, A[p] will be 0. So, the
answer is D.
Suppose the line
A[j] = A[j] || (j%k);
is replaced with
A[j] = A[j] || !(j%k);
Now, the nested loop takes all integers from 2 to log2 n, takes all their multiples before n, and makes the corresponding entry in A as 1. For example, for 2, and n = 10, A[4], A[6], A[8] and A[10] are made 1. Similarly for 3, 4,... till 2 * log n. So, for all non-prime indices of A, we will have a 1, and for prime indices, we have a 0. And we print j if A[j] is 0 meaning j is prime.

Q39: Assume the following C variable declaration
int *A [10], B[10][10];
Of the following expressions
I A[2]
II A[2][3]
III B[1]
IV B[2][3]
which will not give compile-time errors if used as left hand sides of assignment statements in a C program?  (2003)
(a) I, II, and IV only
(b) II, III, and IV only
(c) II and IV only
(d) IV only
Ans: 
(a)
Sol: 

A is an array of pointers to int, and B is a 2-D array.

  •  A[2] = can take a pointer
  •  A[2][3] = can take an int
  •  B[1] = B[1] is the base address of the array and it cannot be changed as array in C is a

constant pointer.

  •  B[2][3] = can take an integer

So, (A) is the answer.

Q40: Consider the following C declaration:
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
Assume that the objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment consideration, is:  (2000)
(a) 22 bytes
(b) 14 bytes
(c) 18 bytes
(d) 10 bytes
Ans: 
(c)
Sol: 
Here, structure creates the memory for 'array and union', but union only creates the memory for only 'long z' which is the largest size data type inside it.
Hence,
short x [5] = 5 * 2 = 10 bytes [shorts take 2 bytes]
long z = 8 bytes
So, (10 + 8) = 18 bytes

Q41: Aliasing in the context of programming languages refers to  (2000)
(a) multiple variables having the same memory location
(b) multiple variables having the same value
(c) multiple variables having the same identifier
(d) multiple uses of the same variable

Ans: 
(a)
Sol: 
In computer programming, aliasing refers to the situation where the same memory location can be accessed using different names. For instance, if a function takes two pointers A and B which have the same value, then the name A aliases the name B.

Q42: The most appropriate matching for the following pairs is:  (2000)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
(a) X-1 Y-3 Z-2
(b) X-2 Y-1 Z-3
(c) X-3 Y-2 Z-1
(d) X-3 Y-1 Z-2
Ans:
(d)
Sol:  

X : m = NULL; makes the pointer m point to NULL. But the memory created using malloc is still there and but cannot be used as we don't have a link to it. Hence, lost memory
Y : n is freed and so pointer n is now pointing to an invalid memory making it a Dangling pointer.
Z : p is not initialized. p = malloc(sizeof(char)); should have been used before assigning 'a' to *p.

Q43: The following C declarations:  (2000)
Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)
define s to be:
(a) An array, each element of which is a pointer to a structure of type node

(b) A structure of 2 fields, each field being a pointer to an array of 10 elements
(c) A structure of 3 fields: an integer, a float, and an array of 10 elements

(d) An array, each element of which is a structure of type node
Ans: 
(a)
Sol: 
The reference id from *s[10] to the structure and not vice-versa, ruling out options B & C.
The main ambiguity lies among options A & D.
Declaration of the type struct node s[10] == option D.
*s[10] is an array of 10 pointers, each of type struct node. Hence, option A is the correct answer.
The precedence among operators * & [] can also be another determining factor.

 

The document Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Previous Year Questions: Array and Pointer - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is the difference between an array and a pointer in C programming?
Ans. In C programming, an array is a collection of elements of the same data type stored in contiguous memory locations, accessed using an index. A pointer, on the other hand, is a variable that stores the memory address of another variable.
2. How can you access elements of an array using pointers in C?
Ans. You can access elements of an array using pointers by dereferencing the pointer and then using array notation or pointer arithmetic to access the specific element.
3. Can an array name be used as a pointer in C programming?
Ans. Yes, in C programming, the name of an array can be used as a pointer to the first element of the array. This is because the name of an array represents the address of the first element in memory.
4. How do you pass an array to a function in C using pointers?
Ans. To pass an array to a function in C using pointers, you can simply pass the array name as an argument to the function. The function prototype should include a pointer parameter to receive the array.
5. What are the advantages of using pointers with arrays in C programming?
Ans. Using pointers with arrays in C programming allows for more efficient memory usage, easier manipulation of array elements, and the ability to pass arrays to functions without having to copy the entire array.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)

,

Objective type Questions

,

Sample Paper

,

Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)

,

shortcuts and tricks

,

pdf

,

mock tests for examination

,

past year papers

,

Previous Year Questions with Solutions

,

Summary

,

MCQs

,

ppt

,

Extra Questions

,

Exam

,

Free

,

practice quizzes

,

study material

,

Semester Notes

,

Viva Questions

,

video lectures

,

Previous Year Questions: Array and Pointer | Programming and Data Structures - Computer Science Engineering (CSE)

,

Important questions

;