Array questions with explanation in c


1.
What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    char arr[7]="Network";
    printf("%s",arr);
   
}
(A) Network
(B) N
(C) network
(D) Garbage value
(E) Compilation error


Explanation: **
2.
What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    char arr[11]="The African Queen";
    printf("%s",arr);
   
}
(A) The African Queen
(B) The
(C) Queen
(D) null
(E) Compilation error


Explanation: **
3.
What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    char arr[20]="MysticRiver";
    printf("%d",sizeof(arr));   
}
(A) 20
(B) 11
(C) 12
(D) 22
(E) 24


Explanation: **
4.
What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    int const SIZE=5;
    int expr;
    double value[SIZE]={2.0,4.0,6.0,8.0,10.0};
    expr=1|2|3|4;
    printf("%f",value[expr]);
}
(A) 2.000000
(B) 4.000000
(C) 6.000000
(D) 8.000000
(E) Compilation error


Explanation: **
5.
What will be output if you will execute following c code?
#include<stdio.h>
enum power{
    Dalai,
    Vladimir=3,
    Barack,
    Hillary
};
void main(){
    float leader[Dalai+Hillary]={1.f,2.f,3.f,4.f,5.f};
    enum power p=Barack;
    printf("%0.f",leader[p>>1+1]);  
}
(A) 1
(B) 2
(C) 3
(D) 5
(E) Compilation error


Explanation: **
6.
What will be output if you will execute following c code?
#include<stdio.h>
#define var 3
void main(){
    char *cricket[var+~0]={"clarke","kallis"};
    char *ptr=cricket[1+~0];
    printf("%c",*++ptr); 
}
(A) k
(B) a
(C) l
(D) i
(E) Compilation error


Explanation: **
7.
What will be output if you will execute following c code?
#include<stdio.h>
#define var 3
void main(){
    char data[2][3][2]={0,1,2,3,4,5,6,7,8,9,10,11};
    printf("%o",data[0][2][1]);
}
(A) 5
(B) 6
(C) 7
(D) 8
(E) Compilation error


Explanation: **
8.
What will be output if you will execute following c code?
#include<stdio.h>
#define var 3
void main(){
    short num[3][2]={3,6,9,12,15,18};
    printf("%d   %d",*(num+1)[1],**(num+2));
}
(A) 12  15
(B) 12  12
(C) 15  15
(D) 15  12
(E) Compilation error


Explanation: **
9.
What will be output if you will execute following c code?
#include<stdio.h>
#define var 3
void main(){
    char *ptr="cquestionbank";
    printf("%d",-3[ptr]);
}
(A) 100
(B) -100
(C) 101
(D) -101
(E) Compilation error


Explanation: **
10.
What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    long  myarr[2][4]={0l,1l,2l,3l,4l,5l,6l,7l};
    printf("%ld\t",myarr[1][2]);
    printf("%ld%ld\t",*(myarr[1]+3),3[myarr[1]]);
    printf("%ld%ld%ld\t" ,*(*(myarr+1)+2),*(1[myarr]+2),3[1[myarr]]);
   
}
(A) 6   66   777
(B) 6   77   667
(C) 5   66   777
(D) 7   77   666
(E) 6   67   667


Explanation: **
11.
What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    int array[2][3]={5,10,15,20,25,30};
    int (*ptr)[2][3]=&array;
    printf("%d\t",***ptr);
    printf("%d\t",***(ptr+1));
    printf("%d\t",**(*ptr+1));
    printf("%d\t",*(*(*ptr+1)+2));
}
(A) 5   Garbage value   20   30
(B)
5    15    20    25
(C)
10   20    30    30
(D) 5    15    20    30
(E)
Compilation error


Explanation: **
12.
What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    static int a=2,b=4,c=8;
    static int *arr1[2]={&a,&b};
    static int *arr2[2]={&b,&c};
    int* (*arr[2])[2]={&arr1,&arr2};
    printf("%d %d\t",*(*arr[0])[1],  *(*(**(arr+1)+1)));
}
(A) 2  4
(B) 4  2
(C) 4  8
(D) 8  4
(E) 8  2


Explanation: **
13.
What will be output if you will execute following c code?
#include<stdio.h>
#include<math.h>
double myfun(double);
void main(){
    double(*array[3])(double);
    array[0]=exp;
    array[1]=sqrt;
    array[2]=myfun;   printf("%.1f\t",(*array)((*array[2])((**(array+1))(4))));
   
}
double myfun(double d){
       d-=1;
       return d;
}
(A) 1.4
(B) 2.8
(C) 4.2
(D) 3.0
(E) 2.7


Explanation: **
14.
What will be output if you will execute following c code?
#include<stdio.h>
#include<math.h>
typedef struct{
    char *name;
    double salary;
}job;
void main(){
    static job a={"TCS",15000.0};
    static job b={"IBM",25000.0};
    static job c={"Google",35000.0};
    int x=5;
    job * arr[3]={&a,&b,&c};
    printf("%s  %f\t",(3,x>>5-4)[*arr]);  
}
double myfun(double d){
       d-=1;
       return d;
}
(A) Google 35000.000000
(B) TCS  15000.000000
(C) IBM  25000.000000
(D) null   15000.000000
(E) Google   null


Explanation: **
15.
What will be output if you will execute following c code?
#include<stdio.h>
union group{
    char xarr[2][2];
    char yarr[4];
};
void main(){
    union group x={'A','B','C','D'};
    printf("%c",x.xarr[x.yarr[2]-67][x.yarr[3]-67]);
}
(A) A
(B) B
(C) C
(D) D
(E) null


Explanation: **
16.
What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    int a=5,b=10,c=15;
    int *arr[3]={&a,&b,&c};
    printf("%d",*arr[*arr[1]-8]);
}
(A) 5
(B) 10
(C) 18
(D) Garbage value
(E)
Compilation error


Explanation: **
17.
What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    int arr[][3]={{1,2},{3,4,5},{5}};
    printf("%d %d %d",sizeof(arr),arr[0][2],arr[1][2]);
}
(A)
6  0 4
(B) 6  1 5
(C) 18 0 5
(D) 18 1 5
(E) Compilation error


Explanation: **
18.
What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    int xxx[10]={5};
    printf("%d %d",xxx[1],xxx[9]);
}
(A) 0  5
(B) 5  5
(C) 5  0
(D) 0  0
(E) Compilation error


Explanation: **
19.
What will be output if you will execute following c code?
#include<stdio.h>
#define WWW -1
enum {cat,rat};
void main(){
    int Dhoni[]={2,'b',0x3,01001,'\x1d','\111',rat,WWW};
    int i;
    for(i=0;i<8;i++)
         printf(" %d",Dhoni[i]);
}
(A) 2 98 3 513 29 73 0 -1
(B) 2 98 3 513 30 73 1 -1
(C) 2 99 3 513 29 73 1 -1
(D) 2 98 3 513 29 73 1 -1
(E) Compilation error


Explanation: **
20.
What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    long double a;
    signed char b;
    int arr[sizeof(!a+b)];
    printf("%d",sizeof(arr))
}


(A) 8
(B) 9
(C) 1
(D) 4
(E) Compilation error


Explanation: **

Array tutorial in c




17 comments:

Anonymous said...

answer to the first question is "Network" and not garbage value...pls correct it.

Priyanka kumari said...

Answer of first question is correct. It will garbage. You may get output as "Network" but not
always. I will publish explanation of all questions very soon. Please think little more why
output is not "Network" ?

sharad said...

plz give some explanation for question 8..
hoz these 2d n 3d array address location are given with respect to pointers.

vkey said...

in q1>
the actual size of array is 8 (arr[7])
string terminates with a \0, so it takes a garbage value.

Anonymous said...

Mainsh,please add the explanation of 2nd question.

Anonymous said...

Hey manish.....
your website is awesome yaar
it helps me to get comfort over the loops and much more....
keep rockin man.......

Anonymous said...

Here there are so many questions which i can not undeestand well please explain it with good method ...

Ok.

Hajvery University said...

Sir please provide some explanation about the questions.

Anonymous said...

Even though Q1 will compile correctly with "gcc -pedantic -Wall -ansi -Werror" and give the output as 'expected', i.e., Network, it is not correct because you are not reserving space for the '\0' null character to make it a string.

Anonymous said...

plz sir upload the explanation too.... need them

Unknown said...

hey where are the explanations

sachin nagar said...

can any1 help to solve que. no. 11.
printf("%d\t",***(ptr+1));
how this statment printing address.

Unknown said...

why garbage for the first question kindly explain

Unknown said...

Pls provide the explanations

Unknown said...

cause u are declaring an array of 7 characters whereas Network has 7 characters yes but u leave no place for the null character '\0' so the compiler doesnt know where to stop printing and prints out some characters more.
Btw sry for my english, gj for the site :)
best regards

Siddhartha rao said...

I think For 2nd question the answer should be compile time error...

Unknown said...

The answer of second question is wrong you should correct it. The output would be garbage value rather than null.