C Programming Questions and Answers – Initialization of Pointer Arrays
C Programming Questions and Answers – Initialization of Pointer Arrays
1.To declare a 3 dimension array using pointers, which of the following is the correct syntax:
a) char *a[][];
b) char **a[];
c) char ***a;
d) All of the mentioned
View Answer
Answer:a
2.Comment on the output of this C code?
#include <stdio.h>
int main()
{
char *a = {“p”, “r”, “o”, “g”, “r”, “a”, “m”};
printf(“%s”, a);
}
a) Output will be program
b) Output will be p
c) No output
d) Compile-time error
View Answer
Answer:b
3.An array of strings can be initialized by:
a) char *a[] = {“Hello”, “World”};
b) char *a[] = {“Hello”, “Worlds”};
c) char *b = “Hello”;
char *c = “World”;
char *a[] = {b, c};
d) All of the mentioned.
View Answer
Answer:d
4.What is the output of this C code?
#include <stdio.h>
void main()
{
char *a[10] = {“hi”, “hello”, “how”};
int i = 0;
for (i = 0;i < 10; i++)
printf(“%s”, *(a[i]));
}
a) Segmentation fault
b) hi hello how followed by 7 null values
c) 10 null values
d) depends on compiler
View Answer
Answer:a
5.What is the output of this C code?
#include <stdio.h>
void main()
{
char *a[10] = {“hi”, “hello”, “how”};
int i = 0, j = 0;
a[0] = “hey”;
for (i = 0;i < 10; i++)
printf(“%s\n”, a[i]);
}
a) hi hello how Segmentation fault
b) hi hello how followed by 7 null values
c) hey hello how Segmentation fault
d) Depends on compiler
View Answer
Answer:c
6.What is the output of this C code?
#include <stdio.h>
void main()
{
char *a[10] = {“hi”, “hello”, “how”};
printf(“%d\n”, sizeof(a));
}
a) 10
b) 13
c) Run time error
d) 40
View Answer
Answer:d
7.What is the output of this C code?
#include <stdio.h>
void main()
{
char *a[10] = {“hi”, “hello”, “how”};
printf(“%d\n”, sizeof(a[1]));
}
a) 6
b) 4
c) 5
d) 3
View Answer
Answer:b
8.What is the output of this C code?
#include <stdio.h>
void main()
{
char *a[10] = {“hi”, “hello”, “how”};
int i = 0;
for (i = 0;i < 10; i++)
printf(“%s”, a[i]);
}
a) hi hello how Segmentation fault
b) hi hello how null
c) hey hello how Segmentation fault
d) hi hello how followed by 7 nulls
View Answer
Answer:d
9.What is the output of this C code?
#include <stdio.h>
int main()
{
char *p[1] = {“hello”};
printf(“%s”, (p)[0]);
return 0;
}
a) Compile time error
b) Undefined behaviour
c) hello
d) None of the mentioned
View Answer
Answer:c
10.What is the output of this C code?
#include <stdio.h>
int main()
{
char **p = {“hello”, “hi”, “bye”};
printf(“%s”, (p)[0]);
return 0;
}
a) Compile time error
b) Undefined behaviour
c) hello
d) Address of hello
View Answer
Answer:b
11.What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 0, j = 1;
int *a[] = {&i, &j};
printf(“%d”, (*a)[0]);
return 0;
}
a) Compile time error
b) Undefined behaviour
c) 0
d) Some garbage value
View Answer
Answer:c
12.What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 0, j = 1;
int *a[] = {&i, &j};
printf(“%d”, *a[0]);
return 0;
}
a) Compile time error
b) Undefined behaviour
c) 0
d) Some garbage value
View Answer
Answer:c
13.What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 0, j = 1;
int *a[] = {&i, &j};
printf(“%d”, (*a)[1]);
return 0;
}
a) Compile time error
b) Undefined behaviour
c) 1
d) Some garbage value
View Answer
Answer:d
14.Which of the following are generated from char pointer?
a) char *string = “Hello.”;
b) char *string;
scanf(“%s”, string);
c) char string[] = “Hello.”;
d) Both (a) and (c).
View Answer
Answer:a
15.Which of the following declaration are illegal?
a) int a[][] = {{1, 2, 3}, {2, 3, 4, 5}};
b) int *a[] = {{1, 2, 3}, {2, 3, 4, 5}};
c) int a[4][4] = {{1, 2, 3}, {2, 3, 4, 5}};
d) Both (a) and (b).
View Answer
Answer:a