C Strings Set 1

Ques: 1 What will be output of the following program?
#include<stdio.h>
int main()
{
    char name[] = "Hello";
    char *ptr;
    ptr = name;
    while(*ptr){
     printf("%c",name[ptr++ - name]);
    }
    ptr = name;
    printf("\t");
    while(*ptr){
     printf("%c",ptr[ptr++ - name]);
    }
    return 0;
}
A. Hello Hello
B. garbage Hello 
C. Hello garbage
D. Break
E. garbage garbage
Description
Answer: C
First loop is quite clear but in second one let’s see it step by step.
Lets say base address of Hello is 1000 so value in ptr will be 1000. 
Now in expression inside the printf subscript operator [] have higher precedence 
so in first iteration result will be like 1001[0] which will result “e� because postpix operator will work after - operator because it is inside the [] operator.
Now current value of ptr will be 1001. 
Now similarly in second iteration result of expression will be like 1002[1] which will result “l� after that values are not there so it will print some garbage values

Ques: 2 What will be output of the following program?
#include<stdio.h>
#define A 10
int main()
{
    char name[] = "Geeksbix";
    while(name++)
     printf("%c",name[0]);
    return 0;
}
A. Compile time Error
B. Geeksbix 
C. Runtime Error
D. None of these
Description
Answer: A
l value required Error. Increment in base address of array is not allowed.

Ques: 3 What will be output of the following program?
#include<stdio.h>
int main()
{
    printf(5+"GeeksBix");
    return 0;   
}
A. Compile time Error
B. GeeksBix 
C. Runtime Error 
D. None of these
Description
Answer: D
Output will be "Bix".Reason is simply printf() function will pass the address after adding 5 to it so it will print the string from that base address only.

Ques: 4 What will be output of the following program?
#include<stdio.h>
int main()
{
    printf("%s","abcdefg"[4]);
    return 0;	
}
A. Compile time Error
B. efg 
C. Runtime Error
D. undeclared identifier
Description
Answer: C

It will generate Runtime error because as we have used "%s" here so printf() is expecting address of a string but we are giving some value to it.

Let's see this:-
	printf("%c"abcdefg[4]);
it will simply print e because we are passing what printf() is expecting because of "%c".

Ques: 5 What will be output of the following program?
#include<stdio.h>
int main()
{
    char str[7] = "Geeksbix";
    printf("%s",str);
    return 0;	
}
A. Compile time Error
B. Geeksbi 
C. Warning: Array size exceeds
D. None of these
Description
Answer: D
It will give warning as well as output Geeksbi on DevC++.

Ques: 6 What will be output of the following program?
#include<stdio.h>
int main()
{
    char name[3][10]={
                    "Geeskbix",
                    "isthe",
                    "best",
                    };
    char **ptr;
    ptr = name;
    printf("%c",ptr[0][0]);
    return 0;	
}
A. G
B. Geeksbix
C. Runtime Error
D. None of these
Description
Answer: C

Ques: 7 What will be output of the following program?
#include<stdio.h>
#include<math.h>
double myfun(double);
int main(){
    double(*array[3])(double);
    array[0]=exp;
    array[1]=sqrt;
    array[2]=myfun;
    printf("%.1f\t",(*array)((*array[2])((**(array+1))(4))));   
}
double myfun(double d){
    d-=1;
    return d;
}
A. 3.5
B. 7.0
C. 2.7
D. Compilation Error
Description
Answer: C

Ques: 8 What will be output of the following program?
#include<stdio.h>
int main()
{
    char *str = "hello ";
    char *str1 = "hello";
    if(strcmp(str, str1))
        printf("Yes");
    else
        printf("No");
}
A. Yes 
B. No
C. Compilation Error
D. None
Description
Answer: A
strcmp() will return 0 if strings are equal.

Ques: 9 What will be output of the following program?
#include<stdio.h>
int main()
{
    char str[10] = "wello";
    char *str1 = "horld";
    char temp = str1[0];
    str1[0] = str[0];
    str[0] = temp;
    printf("%s %s",str,str1);
}
A. helloworld
B. hello
C. world
D. Error
Description
Answer: D
It will give an runtime error because str1 cannot be modified;

Ques: 10 What will be output of the following program?
#include<stdio.h>
int main()
{
    char str[] = "geeksbix";
    char *str1 = "geeksbix";
    printf("%d %d %d",sizeof(str), sizeof(str1), sizeof(*str1));
    return 0;
}
A. 8 8 1
B. 9 9 1 
C. 8 4 1
D. 9 4 1
Description
Answer: D
sizeof() operator always counts '\0' also which is automatically putted at the end of all such initialized strings.

Ques: 11 What will be output of the following program?
#include<stdio.h>
int main()
{
    char *s1="Geeksbix";
    char *s2;
    s2=(char*)malloc(30);
    memset(s2,0,30);
    while(*s2++= *s1++);
    printf("%s",s2);
}
A. No output
B. Geeksbix 
C. Infinite loop
D. G
Description
Answer: A

The C library function void *memset(void *str, int c, size_t n) copies the character c (an unsigned char) to the first n characters of the string pointed to by the argument str.

In while(*s2++ = *s1++); we are just coping s1 to s2, but s2 has already been initialize with null

so after the increament in the pointer s2 will point to a location whilch will hold null hence it will give no output.



comments powered by Disqus