Strings Questions
Practice Strings MCQs with answers and explanations. Page 3 of 3.
Category
C# Programming
Topic
Strings
Page
3 / 3
Mode
Practice
Questions
Open any question to view the answer and explanation.
sizeof an array of pointers and strlen of the first word: assume pointer size is 4 bytes. What does this print?
#include<stdio.h>
#include<string.h>
int main()
{
char str[] = {'Frogs', 'Do', 'Not', 'Die', 'They', 'Croak!'};
printf('%d, %d', sizeof(str), strlen(str[0]));
return 0;
}
Open
View answer
Modifying a char array then attempting to reassign it: what does this program do?
#include<stdio.h>
int main()
{
char str[] = 'Nagpur'; // writable array
str[0] = 'K'; // modify first character → 'Kagpur'
printf('%s, ', str);
str = 'Kanpur'; // attempt to reassign an array variable
printf('%s', str+1);
return 0;
}
Open
View answer
Editing a printf format string in a writable array: what is printed?
#include<stdio.h>
int main()
{
char p[] = '%d
'; // writable copy of the format
p[1] = 'c'; // change '%d' to '%c'
printf(p, 65);
return 0;
}
Open
View answer
Pointer arithmetic on string literals with printf: what is printed?
#include<stdio.h>
int main()
{
printf(5 + 'Good Morning
');
return 0;
}
Open
View answer
Copying a C string using pointer assignment in the while condition and again in the body: what final string is printed?
#include<stdio.h>
int main()
{
char str1[] = 'Hello';
char str2[10];
char *t, *s;
s = str1;
t = str2;
while (*t = *s)
*t++ = *s++;
printf('%s
', str2);
return 0;
}
Open
View answer
Type mismatch: attempting to store a string literal in a single char variable. What happens?
#include<stdio.h>
int main()
{
char str = 'CuriousTab'; // invalid: char cannot hold a whole string
printf('%s
', str);
return 0;
}
Open
View answer
Pointer arithmetic on string literals with printf: determine the output.
#include<stdio.h>
int main()
{
printf(5 + 'CuriousTab
');
return 0;
}
Open
View answer
In C, what is the output of this string-library expression (concatenation + copy + compare)?
#include<stdio.h>
#include<string.h>
int main()
{
static char str1[] = "dills";
static char str2[20];
static char str3[] = "Daffo";
int i;
i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills");
printf("%d
", i);
return 0;
}
Open
View answer
In C on a typical 32-bit model (char=1 byte, int=4 bytes, float=4 bytes), what does sizeof report?
#include<stdio.h>
int main()
{
char ch = 'A';
printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f));
return 0;
}
Open
View answer
In C string-pointer assignment sequencing, what prints?
#include<stdio.h>
int main()
{
char t; /* unused */
char *p1 = "India", p2;
p2 = p1; / p2 points to "India" /
p1 = "CURIOUSTAB";/ p1 now points elsewhere */
printf("%s %s
", p1, p2);
return 0;
}
Open
View answer
In C, what is sizeof for this compound string with embedded nulls?
#include<stdio.h>
int main()
{
char str[] = "India\0CURIOUSTAB\0";
printf("%d
", sizeof(str));
return 0;
}
Open
View answer
In C, printf return value in an if-condition with an empty C-string literal element.
#include<stdio.h>
int main()
{
int i;
char a[] = "\0"; /* a[0] == 0, array is a valid empty string */
if (printf("%s", a))
printf("The string is empty
");
else
printf("The string is not empty
");
return 0;
}
Open
View answer
In C, basic strlen on a digit-only literal: what is printed?
#include<stdio.h>
#include<string.h>
int main()
{
printf("%d
", strlen("123456"));
return 0;
}
Open
View answer
In Turbo C (16-bit), what happens when you assign a string literal to a single char element?
#include<stdio.h>
int main()
{
char str[10] = "India";
str[6] = "CURIOUSTAB"; /* invalid: assigning char* to char /
printf("%s
", str);
return 0;
}
Open
View answer
In C, swapping function parameters passed by value (pointers to string literals): what prints?
#include<stdio.h>
void swap(char *, char *);
int main()
{
char *pstr[2] = {"Hello", "CuriousTab"};
swap(pstr[0], pstr[1]);
printf("%s
%s", pstr[0], pstr[1]);
return 0;
}
void swap(char *t1, char *t2)
{
char *t;
t = t1;
t1 = t2;
t2 = t;
}
Open
View answer
In C, 2D array-of-strings indexing vs pointer arithmetic: what characters print?
#include<stdio.h>
int main()
{
static char mess[6][30] = {
"Don't walk in front of me...",
"I may not follow;",
"Don't walk behind me...",
"Just walk beside me...",
"And be my friend."
};
printf("%c, %c
", *(mess[2] + 9), ((mess + 2) + 9));
return 0;
}
Open
View answer
On a 16-bit Turbo C/DOS platform (int=2 bytes), what does sizeof report for these literals?
#include<stdio.h>
int main()
{
printf("%d, %d, %d", sizeof(3.0f), sizeof('3'), sizeof(3.0));
return 0;
}
Open
View answer
In C programming on a 32-bit system (pointer size = 4 bytes), what will this program print for sizeof results of an array versus a pointer, and of the pointed elements?
#include <stdio.h>
int main()
{
char a[] = "Visual C++"; /* array holds characters plus the '\0' terminator */
char b = "Visual C++"; / pointer to string literal */
printf("%d, %d
", sizeof(a), sizeof(b));
printf("%d, %d", sizeof(*a), sizeof(*b));
return 0;
}
Open
View answer
In C programming, consider the fixed-size array below. Will this program compile and what would be printed if it did?
#include <stdio.h>
int main()
{
char str[7] = "CuriousTab"; /* literal length exceeds array capacity */
printf("%s
", str);
return 0;
}
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.