1.
main()
{
char c[] = “SCABHOPAL”;
char *p =c;
printf(“%s”, p + p[4] – p[3]) ;
}

(a) PAL
(b) HOPAL
(c) BHOPAL
(d) SCABHOPAL
Answer
(a)
char c[] = “SCABHOPAL”;
p now has the base address string “SCABHOPAL”
char *p = c;
p[4] is ‘H’ and p[3] is ‘B’.
p[4] – p[3] = ASCII value of ‘H’ – ASCII value of ‘B’ = 6
So the expression p + p[4] – p[3] becomes p + 6 which is
base address of string “PAL”
printf(“%s”, p + p[4] – p[3]); // prints PAL

2.
#include “stdio.h”
int main()
{
char str[] = “ILoveYou”;
printf(“%s %s %s\n”, &str[5], &5[str], str+5);
printf(“%c %c %c\n”, *(str+6), str[6], 6[str]);
return 0;
}
(a) Runtime Error
(b) Compiler Error
(c) you you you
(d) you you you
o o o
Answer

(d) The program has no error. All of the following expressions mean same thing
&str[5] &5[str] str+5
Since compiler converts the array operation in pointers before accessing the array elements, all above result in same address.
Similarly, all of the following expressions mean same thing.
*(str+6)
str[6] 6[str]

3.
In below program, what would you put in place of “?” to print “BHOPAL”?
#include “stdio.h”
int main()
{
char arr[] = “SCABHOPAL”;
printf(“%s”, ?);
return 0;
}
(a) arr
(b) (arr+3)
(c) (arr+4)
(d) Not possible

Answer
(b) Since %s is used, the printf statement will print everything starting from arr+3 until it finds ‘\0’

4.
int main()
{
char a[2][3][3] = {‘S’,’C’,’A’,’B’,’H’,’O’,’P’,’A’,’L’};
printf(“%s “, **a);
return 0;
}
(a) Compiler Error
(b) SCABHOPAL followed by garbage characters
(c) SCABHOPAL
(d) Runtime Error
Answer

(c) We have created a 3D array that should have 2*3*3 (= 18) elements, but we are initializing only 9 of them. In C, when we initialize less no of elements in an array all uninitialized elements become ‘\0′ in case of char and 0 in case of integers.

5.
#include “stdio.h”
int main()
{
char str[] = “%d %c”, arr[] = “SCABHOPAL”;
printf(str, 0[arr], 2[arr + 43);
return 0;
}
(a) S O
(b) 83 91
(c) 83 O
(d) Compile-time error

Answer
(c)
The statement printf(str, 0[arr], 2[arr + 3]); boils down to:
printf(“%d %c, 0[“SCABHOPAL”], 2[“SCABHOPAL” + 3]);
Which is further interpreted as:
printf(“%d %c, *(0 + “SCABHOPAL”), *(2 + “SCABHOPAL” + 3));
Which prints the ascii value of ‘S’ and character ‘O’.

6.
#include “stdio.h”
int main()
{
char str[20] = “SCABHOPAL”;
printf (“%d”, sizeof(str));
return 0;
}
(a) 9
(b) 10
(c) 20
(d) Garbage Value

Answer
(c) Note that the sizeof() operator would return size of array.

7.
# include “stdio.h”
int main( )
{
char s1[7] = “ABCD”, *p;
p = s1 + 2;
*p = ‘0’ ;
printf (“%s”, s1);
}
(a) AB
(b) AB0D00
(c) AB0D
(d) A0CD

Answer
(c) char s1[7] = “ABCD”, *p;
p = s1 + 2; // p holds address of character c
*p = ‘0’ ; // memory at s1 + 3 now becomes 0
printf (“%s”, s1); // All characters are printed

8.
main()
{
char str[] =”SCA BHOPAL”;
fun(str);
}
void fun (char *a)
{
if (*a && *a != ‘ ‘)
{
fun(a+1);
putchar(*a);
}
}
(a) SCA BHOPAL
(b) SCA
(c) LAPOHB ACS
(d) ACS

Answer
(d) The program prints all characters before ‘ ‘ or ‘\0′ (whichever comes first) in reverse order.

9.
Output of following C program? Assume that all necessary header files are included
int main()
{
char *s1 = (char *)malloc(50);
char *s2 = (char *)malloc(50);
strcpy(s1, “Sca”);
strcpy(s2, “Bhopal”);
strcat(s1, s2);
printf(“%s”, s1);
return 0;
}
(a) ScaBhopal
(b) Sca
(c) Sca Bhopal
(d) Bhopal

Answer
(a) strcpy puts \0 at the end.
strcat starts from \0, concatenates string and puts \0 at the end.

10.
int main()
{
char p[] = “ScaBhopal”;
char t;
int i, j;
for(i=0,j=strlen(p); i<j; i++)
{
t = p[i];
p[i] = p[j-i];
p[j-i] = t;
}
printf(“%s”, p);
return 0;
}
(a) zlapohBacS
(b) Nothing is printed on the screen
(c) ScaBhopal
(d) ssssssssss

Answer
(b) The string termination character ‘\0′ is assigned to first element of array p[]

Strings in C