100% found this document useful (1 vote)
42 views13 pages

6.arrays - 37

The document contains a series of programming questions and answers related to C language concepts, including array manipulation, pointer arithmetic, and function declarations. Each question presents a code snippet or a theoretical scenario, followed by multiple-choice answers with the correct answer indicated. The content is structured as a quiz format, testing knowledge on various aspects of C programming.

Uploaded by

harshacsds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
42 views13 pages

6.arrays - 37

The document contains a series of programming questions and answers related to C language concepts, including array manipulation, pointer arithmetic, and function declarations. Each question presents a code snippet or a theoretical scenario, followed by multiple-choice answers with the correct answer indicated. The content is structured as a quiz format, testing knowledge on various aspects of C programming.

Uploaded by

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

Question 1

Predict the output of below program:


#include <stdio.h>

int main()
{
int arr[5];

// Assume that base address of arr is 2000 and size of integer


// is 32 bit
arr++;
printf("%u", arr);

return 0;
}

A 2002
B 2004
C 2020
D lvalue required

ANS :-D

Question 2
Predict the output of below program:
#include <stdio.h>

int main()
{
int arr[5];
// Assume base address of arr is 2000 and size of integer is 32 bit
printf("%u %u", arr + 1, &arr + 1);

return 0;
}

A 2004 2020
B 2004 2004
C 2004 Garbage value
D The program fails to compile because Address-of operator cannot be
used with array name

ANS :- A

Question 3
What is output?
# include <stdio.h>

void print(int arr[])


{
int n = sizeof(arr)/sizeof(arr[0]);
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
}

int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
print(arr);
return 0;
}

A 1, 2, 3, 4, 5, 6, 7, 8
B Compiler Error
C 1 2
D Run Time Error

ANS :- C

Question 4
Output of following program?
#include<stdio.h>

int main()
{
int a[] = {1, 2, 3, 4, 5, 6};
int *ptr = (int*)(&a+1);
printf("%d ", *(ptr-1) );
return 0;
}

A 1
B 2
C 6
D Runtime Error

ANS :- C

Question 5
Consider the following C-function in which a[n] and b[m] are two sorted
integer arrays and c[n + m] be another integer array.
void xyz(int a[], int b [], int c[])
{
int i, j, k;
i = j = k = O;
while ((i<n) && (j<m))
if (a[i] < b[j]) c[k++] = a[i++];
else c[k++] = b[j++];
}

Which of the following condition(s) hold(s) after the termination of the


while loop? (GATE CS 2006)
(i) j < m, k = n+j-1, and a[n-1] < b[j] if i = n (ii) i < n, k = m+i-1, and
b[m-1] <= a[i] if j = m

A only (i)
B only (ii)
C either (i) or (ii) but not both
D neither (i) nor (ii)

ANS :- C

Question 6
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 (GATE CS 2003)?

A I, II, and IV only


B II, III, and IV only
C II and IV only
D IV only

ANS :- A

Question 7
Consider the following declaration of a �two-dimensional array in C:
char a[100][100];

Assuming that the main memory is byte-addressable and that the array is
stored starting from memory address 0, the address of a[40][50] is (GATE CS 2002)

A 4040
B 4050
C 5040
D 5050

ANS :- B

Question 8
Which of the following is true about arrays in C.
A For every type T, there can be an array of T.
B For every type T except void and function type, there can be an array
of T.
C When an array is passed to a function, C compiler creates a copy of
array.
D 2D arrays are stored in column major form

ANS :- B

Question 9
Predict the output of the below program:
#include <stdio.h>
#define SIZE(arr) sizeof(arr) / sizeof(*arr);
void fun(int* arr, int n)
{
int i;
*arr += *(arr + n - 1) += 10;
}

void printArr(int* arr, int n)


{
int i;
for(i = 0; i < n; ++i)
printf("%d ", arr[i]);
}

int main()
{
int arr[] = {10, 20, 30};
int size = SIZE(arr);
fun(arr, size);
printArr(arr, size);
return 0;
}

A 20 30 40
B 20 20 40
C 50 20 40
D Compile-time error

ANS :- C

Question 10
Predict output of following program
int main()
{
int i;
int arr[5] = {1};
for (i = 0; i < 5; i++)
printf("%d ", arr[i]);
return 0;
}

A 1 followed by four garbage values


B 1 0 0 0 0
C 1 1 1 1 1
D 0 0 0 0 0

ANS :- B

Question 11
Does C perform array out of bound checking? What is the output of the
following program?
int main()
{
int i;
int arr[5] = {0};
for (i = 0; i <= 5; i++)
printf("%d ", arr[i]);
return 0;
}

A Compiler Error: Array index out of bound.


B The always prints 0 five times followed by garbage value
C The program always crashes.
D The program may print 0 five times followed by garbage value, or may
crash if address (arr+5) is invalid.

ANS :- D

Question 12
#include <stdio.h>

int main()
{
int a[][] = {{1,2},{3,4}};
int i, j;
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
printf("%d ", a[i][j]);
return 0;
}
A 1 2 3 4
B Compiler Error in line " int a[][] = {{1,2},{3,4}};"
C 4 garbage values
D 4 3 2 1

ANS :- B

Question 13
#include<stdio.h>
int main()
{
int a[10][20][30] = {0};
a[5][2][1] = 2;
return 0;
}

Which of the following will print the value 2 for the above code?

A printf("%d",*(((a+5)+2)+1));
B printf("%d",***((a+5)+2)+1);
C printf("%d",*(*(*(a+5)+2)+1));
D None of these

ANS :- C

Question 14
#include <stdio.h>
int main()
{
char p;
char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};
p = (buf + 1)[5];
printf("%dn", p);
return 0;
}

A 5
B 6
C 9
D None of the above

ANS :- C

Question 15
For a C program accessing X[i][j][k], the following intermediate code is
generated by a compiler. Assume that the size of an integer is 32 bits and the size
of a character is 8 bits.
t0 = i * 1024
t1 = j * 32
t2 = k * 4
t3 = t1 + t0
t4 = t3 + t2
t5 = X[t4]
Which one of the following statements about the source code for the C program
is CORRECT?

A X is declared as �int X[32][32][8]�.


B X is declared as �int X[4][1024][32]�.
C X is declared as �char X[4][32][8]�.
D X is declared as �char X[32][16][2]�.

ANS :- A

Question 16
What�s the meaning of following declaration in C language?
int (*p)[5];

A It will result in compile error because there shouldn't be any


parenthesis i.e. �int *p[5]� is valid.
B p is a pointer to 5 integers.
C p is a pointer to integer array.
D p is an array of 5 pointers to integers.
E p is a pointer to an array of 5 integers

ANS :- E

Question 17
For the following declaration of a function in C, pick the best statement
int [] fun(void (*fptr)(int *));

A It will result in compile error.


B No compile error. fun is a function which takes a function pointer
fptr as argument and return an array of int.
C No compile error. fun is a function which takes a function pointer
fptr as argument and returns an array of int.
Also, fptr is a function pointer which takes int pointer as argument
and returns void.
D No compile error. fun is a function which takes a function pointer
fptr as argument and returns an array of int.
The array of int depends on the body of fun i.e. what size array is
returned. Also, fptr is a function pointer which takes int pointer as argument and
returns void.

ANS :- A

Question 18
In a C file (say sourcefile1.c), an array is defined as follows. Here, we
don�t need to mention arrary arr size explicitly in [] because the size would be
determined by the number of elements used in the initialization.
int arr[] = {1,2,3,4,5};

In another C file (say sourcefile2.c), the same array is declared for usage
as follows:
extern int arr[];

In sourcefile2.c, we can use sizeof() on arr to find out the actual size of
arr.

A TRUE
B FALSE

ANS :- B

Question 19
Find out the correct statement for the following program.
#include "stdio.h"
int * arrPtr[5];

int main()
{
if(*(arrPtr+2) == *(arrPtr+4))
{
printf("Equal!");
}
else
{
printf("Not Equal");
}
return 0;
}

A Compile Error
B It�ll always print Equal.
C It�ll always print Not Equal.
D Since elements of arrPtr aren�t initialized in the program, it�ll
print either Equal or Not Equal.

ANS :- B

Question 20
In C, 1D array of int can be defined as follows and both are correct.
int array1D[4] = {1,2,3,4};
int array1D[] = {1,2,3,4};
Run on IDE
But given the following definitions (along-with initialization) of 2D arrays
int array2D[2][4] = {1,2,3,4,5,6,7,8}; /* (i) */
int array2D[][4] = {1,2,3,4,5,6,7,8}; /* (ii) */
int array2D[2][] = {1,2,3,4,5,6,7,8}; /* (iii) */
int array2D[][] = {1,2,3,4,5,6,7,8}; /* (iv) */

Pick the correct statements.

A Only (i) is correct.


B Only (i) and (ii) are correct.
C Only (i), (ii) and (iii) are correct.
D All (i), (ii), (iii) and (iv) are correct.

ANS :- B

Question 21
Pick the best statement for the below:
int arr[50] = {0,1,2,[47]=47,48,49};

A This isn�t allowed in C and it�ll give compile error


B This is allowed in C as per standard. Basically, it�ll initialize
arr[0], arr[1], arr[2], arr[47], arr[48] and arr[49] to 0,1,2,47,48 and 49
respectively.
The remaining elements of the array would be initialized to 0.

ANS :- B

Question 22
Pick the best statement for the below program:
#include "stdio.h"
void fun(int n)
{
int idx;
int arr1[n] = {0};
int arr2[n];

for (idx=0; idx<n; idx++)


arr2[idx] = 0;
}

int main()
{
fun(4);
return 0;
}

A Definition of both arr1 and arr2 is incorrect because variable is


used to specify the size of array. That�s why compile error.
B Apart from definition of arr1 arr2, initialization of arr1 is also
incorrect. arr1 can�t be initialized due to its size being specified as variable.
That�s why compile error.
C Initialization of arr1 is incorrect. arr1 can�t be initialized due to
its size being specified as variable. That�s why compile error.
D No compile error. The program would define and initializes both
arrays to ZERO.

ANS :- C

Question 23
Pick the best statement for the below program:
#include "stdio.h"

int size = 4;
int arr[size];

int main()
{
if(arr[0])
printf("Initialized to ZERO");
else
printf("Not initialized to ZERO");

return 0;
}

A No compile error and it�ll print �Initialized to ZERO�.


B No compile error and it�ll print �Not initialized to ZERO�.
C Compile error because size of arr has been defined using variable
outside any function.
D No compile error and it�ll print either �Initialized to ZERO� or �Not
initialized to ZERO� depending on what value is present at arr[0] at a particular
run of the program.

ANS :- C

Question 24
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.
i = 0;
j = 1;
while (j < n )
{
if (E) j++;
else if (a[j] - a[i] == S) break;
else i++;
}
if (j < n)
printf("yes")
else
printf ("no");

Choose the correct expression for E.

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

Question 25
Let a and b be two sorted arrays containing n integers each, in non-
decreasing order.
Let c be a sorted array containing 2n integers obtained by merging the two
arrays a and b. Assuming the arrays are indexed starting from 0, consider the
following four statements
a[i] = b [i] => c[2i] = a [i]
a[i] = b [i] => c[2i] = b [i]
a[i] = b [i] => c[2i] = a [i]
a[i] = b [i] => c[2i] = b [i]
Which of the following is TRUE?

A only I and II
B only I and IV
C only II and III
D only III and IV

ANS :- C

Question 26
The following function computes the maximum value contained in an integer
array p[] of size n (n >= 1)
int max(int *p, int n)
{
int a=0, b=n-1;
while (__________)
{
if (p[a] <= p[b])
{
a = a+1;
}
else
{
b = b-1;
}
}
return p[a];
}

`The missing loop condition is

A a != n
B b != 0
C b > (a + 1)
D b != a

ANS :- D

Question 27
Consider the C program given below :
#include <stdio.h>
int main () {
int sum = 0, maxsum = 0, i, n = 6;
int a [] = {2, -2, -1, 3, 4, 2};
for (i = 0; i < n; i++) {
if (i == 0 || a [i] < 0 || a [i] < a [i - 1]) {
if (sum > maxsum) maxsum = sum;
sum = (a [i] > 0) ? a [i] : 0;
}
else sum += a [i];
}
if (sum > maxsum) maxsum = sum ;
printf ("%dn", maxsum);

What is the value printed out when this program is executed?

A 9
B 8
C 7
D 6

ANS :- C

Question 28
What is the output printed by the following C code?
# include <stdio.h>
int main ()
{
char a [6] = "world";
int i, j;
for (i = 0, j = 5; i < j; a [i++] = a [j--]);
printf ("%sn", a);
}
/* Add code here. Remove these lines if not writing code */

A dlrow
B Null String
C dlrld
D worow

ANS :- B
Question 29
Consider the C program given below. What does it print?
#include <stdio.h>
int main ()
{
int i, j;
int a [8] = {1, 2, 3, 4, 5, 6, 7, 8};
for(i = 0; i < 3; i++) {
a[i] = a[i] + 1;
i++;
}
i--;
for (j = 7; j > 4; j--) {
int i = j/2;
a[i] = a[i] - 1;
}
printf ("%d, %d", i, a[i]);
}
/* Add code here. Remove these lines if not writing code */

A 2, 3
B 2, 4
C 3, 2
D 3, 3

ANS :- C

Question 30
C program is given below:
# include <stdio.h>
int main ()
{
int i, j;
char a [2] [3] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}};
char b [3] [2];
char *p = *b;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
*(p + 2*j + i) = a [i] [j];
}
}
}
/* Add code here. Remove these lines if not writing code */

What should be the contents of the array b at the end of the program?
A
a b
c d
e f
B
a d
b e
c f
C
a c
e b
d f
D
a e
d c
b f

ANS :- B
Question 31
Consider the array A[]= {6,4,8,1,3} apply the insertion sort to sort the
array . Consider the cost associated with each sort is 25 rupees ,
what is the total cost of the insertion sort when element 1 reaches the
first position of the array ?

A 50
B 25
C 75
D 100

ANS :- A

Question 32
You are given an array A[] having n random bits and a function OR(i,j) which
will take two indexes from an array as parameters and will return
the result of ( A[i] OR A[j] ) i.e bitwise OR. What is the minimum no of OR
calls required so as to determine all the bits inside the array i.e.
to determine every index of A[] whether it has 0 or 1 ?

A N-1
B N*(N-1)/2
C N
D Not possible to determine the bit array

ANS :- C

Question 33
Let A be a two dimensional array declared as follows:
A: array [1 ... 10] [1 ... 15] of integer;
Assuming that each integer takes one memory location, the array is stored in
row-major order and the first element of the array is stored at location 100,
what is the address of the element a[i][j] ?

A 15i+ j+ 84
B 15j+ i+ 84
C 10i+ j+ 89
D 10j+ i+ 89

ANS :- A

Question 34
An array A contains n=1 positive integers in the locations A[1], A[2],...
A[n]. The following program fragment prints the length of a shortest sequence of
consecutive elements of A, A[i], A[i+1],...A[j] such that the sum of their
values is =M, a given positive number. It prints �n+1� if no such sequence exists.
Complete the program by filling in the boxes. In each case use the simplest
possible expression. Write only the line number and the contents of the box.

REMAIN QUESTION IS WITH IMAGE IN FOLDER BY NAME 6.ARRAY 34 Q IMAGE

Question 35
Let A(1:8, -5:5, -10:5) be a three dimensional array. How many elements are
there in the array A?
A 1200
B 1408
C 33
D 1050

ANS :- B

Question 36
In an array of 2N elements that is both 2-ordered and 3-ordered, what is the
maximum number of positions that an element can be from its position if the
array were 1-ordered?

A 1
B 2
C N/2
D 2N-1

ANS :- A

Question 37
A one dimensional array A has indices 1....75. Each element is a string and
takes up three memory words. The array is stored at location 1120 decimal.
The starting address of A[49] is

A 1267
B 1164
C 1264
D 1169

ANS :- C

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy