Simple Array Questions in C
Yes/No Array Questions
-------------------------------------------------------------------------------------------------------
1.
Predict the output?
3, 2, 3, 2
4, 4, 4, 4
3, 4, 3, 4
4, 2, 4, 2
2, 2, 2, 2
3, 3, 3, 3
3, 4, 1, 2
4, 1, 2, 3
Yes/No Array Questions
-------------------------------------------------------------------------------------------------------
1.
Predict the output?
public class Main {
public static void main(String args[]) {
int arr[] = {10, 20, 30, 40, 50};
for(int i=0; i < arr.length; i++)
{
System.out.print(" " + arr[i]);
}
}
}
A. 10 20 30 40 50
B. Compiler Error
C. 10 20 30 40
D. None
Answer: A
------------------------------------------------------------------------------------------------------------------------
2.
class Test {
public static void main(String args[]) {
int arr[] = new int[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
A. 0
0
B. Garbage value
Garbage value
C. Compiler
D.Exception
Answer: A
-----------------------------------------------------------------------------------------------------------------
3.
public class Main {
public static void main(String args[]) {
int arr[][] = new int[4][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
arr[3] = new int[4];
int i, j, k = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
arr[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
System.out.print(" " + arr[i][j]);
k++;
}
System.out.println();
}
}
}
A. 0
1 2
3 4 5
6 7 8 9
B. Compiler Error
C. 0
0 0
0 0 0
0 0 0 0
D. 9
7 8
4 5 6
0 1 2 3
Answer: A
-----------------------------------------------------------------------------------------------------------------
4.
Output of following Java program?
int main()
{
static int a[2][2] = {1, 2, 3, 4};
int i, j;
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
for(i=0; i<2 font="" i="">2>
{
for(j=0; j<2 font="" j="">2>
{
printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i),
*(*(i+p)+j), *(*(p+j)+i));
}
}
return 0;
}
A. 1, 1, 1, 1
2, 3, 2, 33, 2, 3, 2
4, 4, 4, 4
B. 1, 2, 1, 2
2, 3, 2, 33, 4, 3, 4
4, 2, 4, 2
C. 1, 1, 1, 1
2, 2, 2, 22, 2, 2, 2
3, 3, 3, 3
D. 1, 2, 3, 4
2, 3, 4, 13, 4, 1, 2
4, 1, 2, 3
Answer: C
-----------------------------------------------------------------------------------------------------------
5.
What will be the output of the program ?
#include
void fun(int **p);
int main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
int *ptr;
ptr = &a[0][0];
fun(&ptr);
return 0;
}
void fun(int **p)
{
printf("%d\n", **p);
}
A. 1
B. 2
C. 3
D. 4
Answer: A
------------------------------------------------------------------------------------------------------------
More Array Topics: