Here is a listing of C Objective Questions on “Character Pointers and Functions” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
char *str = "hello, worldn";
-
char *strc = "good morningn";
-
strcpy(strc, str);
-
printf("%sn", strc);
-
return 0;
-
}
a) hello, world
b) Crash/segmentation fault
c) Undefined behaviour
d) Run time error
Answer: b
Clarification: None.
2. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
char *str = "hello world";
-
char strc[] = "good morning indian";
-
strcpy(strc, str);
-
printf("%sn", strc);
-
return 0;
-
}
a) hello world
b) hello worldg india
c) Compile time error
d) Undefined behaviour
Answer: a
Clarification: None.
3. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
char *str = "hello, world!!n";
-
char strc[] = "good morningn";
-
strcpy(strc, str);
-
printf("%sn", strc);
-
return 0;
-
}
a) hello, world!!
b) Compile time error
c) Undefined behaviour
d) Segmenation fault
Answer: c
Clarification: None.
4. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
char *str = "hello, worldn";
-
str[5] = '.';
-
printf("%sn", str);
-
return 0;
-
}
a) hello. world
b) hello, world
c) Compile error
d) Segmentation fault
Answer: d
Clarification: None.
5. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
char str[] = "hello, world";
-
str[5] = '.';
-
printf("%sn", str);
-
return 0;
-
}
a) hello. world
b) hello, world
c) Compile error
d) Segmentation fault
Answer: a
Clarification: None.
6. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
char *str = "hello world";
-
char strary[] = "hello world";
-
printf("%d %dn", sizeof(str), sizeof(strary));
-
return 0;
-
}
a) 11 11
b) 12 12
c) 4 12
d) 4 11
Answer: c
Clarification: None.
7. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
char *str = "hello world";
-
char strary[] = "hello world";
-
printf("%d %dn", strlen(str), strlen(strary));
-
return 0;
-
}
a) 11 11
b) 12 11
c) 11 12
d) x 11 where x can be any positive integer.
Answer: a
Clarification: None.
8. What will be the output of the following C code?
-
#include
-
void f(char *k)
-
{
-
k++;
-
k[2] = 'm';
-
printf("%cn", *k);
-
}
-
void main()
-
{
-
char s[] = "hello";
-
f(s);
-
}
a) l
b) e
c) h
d) o
Answer: b
Clarification: None.
9. What will be the output of the following C code?
-
#include
-
void fun(char *k)
-
{
-
printf("%s", k);
-
}
-
void main()
-
{
-
char s[] = "hello";
-
fun(s);
-
}
a) hello
b) Run time error
c) Nothing
d) h
Answer: a
Clarification: None.