C String

Last Updated :
Discuss
Comments

Question 1

Consider the following C program.

C
#include <stdio.h>
struct Ournode {
  char x, y, z;
};

int main() {
  struct Ournode p = {'1', '0', 'a' + 2};
  struct Ournode *q = &p;
  printf("%c, %c", *((char *)q + 1), *((char *)q + 2));
  return 0;
}

The output of this program is:

  • 0, c

  • 0, a+2

  • '0', 'a+2'

  • '0', 'c'

Question 2

Output of following program C
#include <stdio.h>
int fun(char *p)
{
    if (p == NULL || *p == '\0') return 0;
    int current = 1, i = 1;
    while (*(p+current))
    {
        if (p[current] != p[current-1])
        {
            p[i] = p[current];
            i++;
        }
        current++;
    }
    *(p+i)='\0';
    return i;
}

int main()
{
    char str[] = "geeksskeeg";
    fun(str);
    puts(str);
    return 0;
}
  • gekskeg
  • geeksskeeg
  • geeks
  • Garbage Values

Question 3

Consider the following C program segment. 
 

C
# include <stdio.h>
int main( )
{
    char s1[7] = "1234", *p;
    p = s1 + 2;
    *p = '0' ;
    printf ("%s", s1);
}

What will be printed by the program?
 

  • 12 

  • 120400

  • 1204

  • 1034

Question 4

In below program, what would you put in place of “?” to print “Quiz”?
 

C
#include <stdio.h>
int main() 
{ 
  char arr[] = "GeeksQuiz"; 
  printf("%s", ?); 
  return 0; 
}
  • arr

  • (arr+5)

  • (arr+4)

  • Not possible

Question 5

A language with string manipulation facilities uses the following operations. 

head(s)- returns the first character of the string s 
tail(s)- returns all but the first character of the string s 
concat(sl, s2)- concatenates string s1 with s2. 

The output of concat(head(s), head(tail(tail(s)))), where s is acbc is

  • ab

  • ba

  • ac

  • as

Question 6

Predict the output of following program, assume that a character takes 1 byte and pointer takes 4 bytes.
 

C
#include <stdio.h>
int main()
{
    char *str1 = "GeeksQuiz";
    char str2[] = "GeeksQuiz";

    printf("sizeof(str1) = %d, sizeof(str2) = %d",
           sizeof(str1), sizeof(str2));

    return 0;
}
  • sizeof(str1) = 10, sizeof(str2) = 10 

  • sizeof(str1) = 4, sizeof(str2) = 10

  • sizeof(str1) = 4, sizeof(str2) = 4

  • sizeof(str1) = 10, sizeof(str2) = 4

Question 7

Assume that a character takes 1 byte. Output of following program? 
 

C
#include<stdio.h>
int main()
{
    char str[20] = "GeeksQuiz";
    printf ("%d", sizeof(str));
    return 0;
}
  • 9

  • 10

  • 20

  • Garbage Value

Question 8

Consider the following function written in the C programming language. The output of the above function on input “ABCD EFGH” is

C
void foo (char *a)
{
  if (*a && *a != ` `)
  {
     foo(a+1);
     putchar(*a);
  }
}
  • ABCD EFGH

  • ABCD

  • HGFE DCBA

  • DCBA

Question 9

The output of following C program is C
#include <stdio.h>
char str1[100];

char *fun(char str[])
{
    static int i = 0;
    if (*str)
    {
        fun(str+1);
        str1[i] = *str;
        i++;
    }
    return str1;
}

int main()
{
    char str[] = "GATE CS 2015 Mock Test";
    printf("%s", fun(str));
    return 0;
}
  • GATE CS 2015 Mock Test
  • tseT kcoM 5102 SC ETAG
  • Nothing is printed on screen
  • Segmentation Fault

Question 10

Output of following C program? Assume that all necessary header files are included 

C
int main()
{
    char *s1 = (char *)malloc(50);
    char *s2 = (char *)malloc(50);
    strcpy(s1, "Geeks");
    strcpy(s2, "Quiz");
    strcat(s1, s2);
    printf("%s", s1);
    return 0;
}
  • GeeksQuiz

  • Geeks

  • Geeks Quiz

  • Quiz

Tags:

There are 21 questions to complete.

Take a part in the ongoing discussion