C Programming :: Arrays

Home > Technical Aptitude > C Programming > Arrays > General Questions

NA
SHSTTON
6
Solv. Corr.
10
Solv. In. Corr.
16
Attempted
0 M:0 S
Avg. Time

1 / 55

What is the output of the following problem ?
#include<stdio.h>
int main()
{
func(1);
return 0;
}
func(int  i){
static char *str[] = {"One","Two", "Three", "Four"};
printf("%s\n",str[i++]);
return;
}

AOne

BTwo

CThree

Dwo

ESegmentation fault

Answer: Option B

Explanation:

Here, in main() first func(1) will be called, since char is static in func(int i) after first increment it goes to another index i.e. "Two" so, it will get printed.

Workspace

NA
SHSTTON
9
Solv. Corr.
10
Solv. In. Corr.
19
Attempted
0 M:0 S
Avg. Time

2 / 55

What will be the output of the program ?
#include<stdio.h>
int  main()
{
char s[ ]="man";
int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
return 0;
}

Asegmentation falult

Bmmmm aaaa nnnn

CCompiler Error

DNone of these

Answer: Option B

Explanation:

s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, in directing it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i].

Workspace

NA
SHSTTON
6
Solv. Corr.
11
Solv. In. Corr.
17
Attempted
0 M:0 S
Avg. Time

3 / 55

What will be the output of the program ?
#include<stdio.h>
int main()
{
char *str1="abcd";
char str2[]="abcd";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
return 0;
}

A2 5 5

B2 4 4

C2 5 4

D2 4 5

ENone of these

Answer: Option A

Explanation:

In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable.
In second sizeof the name str2 indicates the name of the array whose size is 5 (including the '\0' termination character).
The third sizeof is similar to the second one.

Workspace

NA
SHSTTON
2
Solv. Corr.
10
Solv. In. Corr.
12
Attempted
0 M:0 S
Avg. Time

4 / 55

What will be the output of the program ?
#include<stdio.h>
int main() { 
   char s[]={'a','b','c','\n','c','\0'};
   char *p,*str,*str1;
   p=&s[3];  
   str=p;  
   str1=s; 
   printf("%d",++*p + ++*str1-32); 
   return 0; 
}

A88

B76

C77

D98

ENone of these

 View Answer |  Discuss in Forum |  Workspace | Asked In Societe Generale |

Answer: Option C

Explanation:

p is pointing to character '\n'. str1 is pointing to character 'a' ++*p. "p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10, which is then incremented to 11.
The value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it becomes
'b'. ASCII value of 'b' is 98.
Now performing (11 + 98 - 32), we get 77("M"); So we get the output 77 :: "M" (Ascii is 77).

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

5 / 55

What is the output of the following C Program?
#include<stdio.h>
#define max 5
#define int arr1[max]
int main()
{
typedef char arr2[max]; 
arr1 list={0,1,2,3,4}; 
arr2 name="name";
printf("%d %s",list[0],name);
return 0;
}

A0 name

BGarbage Value

C1 name

DCompilation error

Answer: Option A

Explanation:

#defines are used for textual replacement whereas typedefs are used for declaring new types.

Workspace

NA
SHSTTON
3
Solv. Corr.
5
Solv. In. Corr.
8
Attempted
0 M:0 S
Avg. Time

6 / 55

What is the output of the following C Program?
#include<stdio.h>
int main()
{
static char names[5][20]={"pascal","ada","cobol","fortran","perl"};
int i; char *t; t=names[3];
names[3]=names[4];
names[4]=t;
for (i=0;i<=4;i++)
printf("%s",names[i]);
return 0;
}

Apascal ada cobol fortran perl

Bpascal ada cobol

CCompiler error: incompatible types in assignment

DNone of these

Answer: Option C

Explanation:

Array names are pointer constants. So it cannot be modified.

Workspace

NA
SHSTTON
8
Solv. Corr.
16
Solv. In. Corr.
24
Attempted
0 M:8 S
Avg. Time

7 / 55

What is the output of the following C Program?
#include<stdio.h>
void main()
{
int a[10];
printf("%d",*a+1-*a+3); 
}

A2

B4

C5

D1

ECompilation Error

Answer: Option B

Explanation:

*a and -*a cancels out. The result is as simple as 1 + 3 = 4 !

Workspace

NA
SHSTTON
2
Solv. Corr.
6
Solv. In. Corr.
8
Attempted
0 M:0 S
Avg. Time

8 / 55

What will be output of the following "c" code?
#include<stdio.h>
int main(){
int arr[]={6,12,18,24};
int x=0;
x=arr[1]+(arr[1]=2);
 printf("%d",x); return 0;
}

A4

B8

C14

D14

ECompilation error

 View Answer |  Discuss in Forum |  Workspace | Asked In Societe Generale |

Answer: Option A

Explanation:

x=arr[1]+(arr[1]=2); arr[1] gets modified here itself.

So, 4 will be printed.

Workspace

NA
SHSTTON
2
Solv. Corr.
7
Solv. In. Corr.
9
Attempted
0 M:0 S
Avg. Time

9 / 55

What will be output of the following "c" code?
#include<stdio.h>
int main(){
int arr[3]={10,20,30};
int x=0;
x=++arr[++x]+ ++x+arr[--x];
printf("%d ",x);
return 0;
}

A23

B43

C22

D44

ECompilation error

Answer: Option B

Explanation:

44 is correct 

Workspace

NA
SHSTTON
7
Solv. Corr.
3
Solv. In. Corr.
10
Attempted
0 M:0 S
Avg. Time

10 / 55

What will be output of the following "c" code?
#include<stdio.h>
int main(){
int a[]={10,20,30,40};
int i=3,x;
x=1*a[--i]+2*a[--i]+3*a[--i];
printf("%d",x);
return 0;
}

A60

B50

C40

D30

ECompilation error

 View Answer |  Discuss in Forum |  Workspace | Asked In Societe Generale |

Answer: Option A

Explanation:

I think so the ans should 100

Workspace