100% found this document useful (1 vote)
40 views6 pages

1-D Array Code Challenges

The document contains 11 coding questions related to 1-D arrays and pointers in C programming. Each question is followed by multiple choice options and the correct answer. The questions cover concepts like sizeof operator, pointer arithmetic, passing arrays to functions, string manipulation using pointers, and recursion.

Uploaded by

Kavita Badgujar
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)
40 views6 pages

1-D Array Code Challenges

The document contains 11 coding questions related to 1-D arrays and pointers in C programming. Each question is followed by multiple choice options and the correct answer. The questions cover concepts like sizeof operator, pointer arithmetic, passing arrays to functions, string manipulation using pointers, and recursion.

Uploaded by

Kavita Badgujar
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

***** 1-D Array Twisters *****

1)
#include<stdio.h>
int main()
{
int a[] = { 1, 2, 3};
printf("%d, %d," , sizeof(a), sizeof (a[-1]));
print_size(a);
return 0;
}

int print_size(int a[])


{
printf("%d, %d," , sizeof(a), sizeof (a[-1]));
return 0;
}

Options:
1) 6, 2, 6, 2
2) Compile Error
3) 12, 4, 4, 4
4) 6, 1, 6, 4

Answer: 3

***********************************************************************************
**************
2)
#include<stdio.h>
int main()
{

int a[ ] = { 1, 2, 3};
++a;
printf("%d", a[2]);
return 0;
}

Options:
1) Compile Error
2) 3
3) 2
4) Linker Error

Answer: 1

***********************************************************************************
***************
3)#include<stdio.h>
int main()
{
int a[ ] = { 1, 2, 3};
printf("%d", a[a[1]]);
return 0;
}

Options:
1) Compile Error
2) 3
3) 2
4) Linker Error

Answer: 2
***********************************************************************************
****************
4)
#include<stdio.h>
int main()
{
int a[ ] = { 1, 2, 3};
f(a);
return 0;
}
int f(int a [ ])
{
++a; printf("%d", a[-1]);
//*(a-1)
return 0;
}

Options:
1) 1
2) Compile Error
3) 0
4) 2

Answer: 1

***********************************************************************************
****************************
5)
#include<stdio.h>
#include<string.h>

int main()
{
char *s = "SunBeam"; int i; char *p = s;
for(i=0; i <strlen(s); ++i, ++p)
printf("%c", p[-i]);

return 0;
}

Options:
1) maeBnuS
2) SSSSSSS
3) SunBeam
4) Snem

Answer: 2
***********************************************************************************
********************************
6)
#include<stdio.h>
#include<string.h>
int main()
{
char *s = "SunBeam"; int i; char *p = s;
// 0<7
// 1<5
// 2<3
// 3<1
for(i=0; i <strlen(p); ++i, ++p)
printf("%c", *p++);

return 0;
}
Sne
S u n B e a m
100 101 102
p
|102|
500

SunBeam\0

Options:
1) SunBeam
2) Sne
3) Snem
4) SunB

Answer: 2

***********************************************************************************
*********************************
7)
#include<stdio.h>
int main()
{
char *s = "SunBeam"; char p[7] = "SunBeam";
printf("%d %d, %d, %d ", sizeof(s), sizeof(p), sizeof(*p),
sizeof("SunBeam"));
return 0;
}

Options:
1) 7, 7, 7, 7
2) 7, 7, 1, 8
3) 6, 7, 1, 8
4) 4, 7, 1, 8

Answer: 4

***********************************************************************************
***************************************
8)
#include<stdio.h>

int main(int argv, char *argc[])


{

printf("%d %d, %d ", *argc[argv], *argv[argc], *(argc + argv),


*(argv + argc));
return 0;
}

Options:
1) 0, 0, 0, 0
2) compile error
3) none
4) depends or arguments

Answer: 3

***********************************************************************************
**************************************
9)
#include<stdio.h>

int main(int argv, char *argc[])


{
char *arr[] = {"SunBeam", "DAC", "WiMC", "Pune", "Karad"};
char **ptr = arr + 2;
printf("%s ", ++ptr[arr-ptr] - 1);
return 0;
}
arr

100
500 [0] -> S u n B e a m \0
100
300
504 [1] -> D A C \0
300
800
508 [2] -> W I M C
800

500
ptr
900 ptr[-8] --> *(ptr-8)

Options:
1) Karad
2) SunBeam
3) DAC
4) WiMC

Answer: 2

***********************************************************************************
***********************************
10)
#include<stdio.h>

int main()
{
char *arr[5] = {"SunBeam"}; // partial init
char **ptr = arr + 2;
printf("%d ", *ptr);
return 0;
}

Options:
1) garbage
2) 117
3) 0
4) 110

Answer: 3

***********************************************************************************
********************************
11)
#include<stdio.h>

int main()
{
static char *s = "SunBeam"; char ch = *s;
if (*s) {++s; main(); printf("%c ", ch);}
return 0;
}

Options:
1) SunBeam
2) maeBnuS
3) Compile error
4) SSSSSSS

Answer: 2

***********************************************************************************
*******************************

#include<stdio.h>

S u n B e a M \0
100

101
s

You might also like