Strings Questions
Practice Strings MCQs with answers and explanations. Page 2 of 3.
Category
C# Programming
Topic
Strings
Page
2 / 3
Mode
Practice
Questions
Open any question to view the answer and explanation.
C#.NET — choose the valid ways to convert a Single (float) to a String.
Open
View answer
C#.NET strings — select the correct statement about strings.
Open
View answer
C#.NET strings — evaluate the output of:
String s1 = "ALL MEN ARE CREATED EQUAL";
String s2;
s2 = s1.Substring(12, 3);
Console.WriteLine(s2);
Open
View answer
C#.NET — Determine the correct output for the code:
String s1 = "Kicit";
Console.Write(s1.IndexOf('c') + " ");
Console.Write(s1.Length);
Open
View answer
C strings and the null terminator: what does this program print?
#include<stdio.h>
#include<string.h>
int main()
{
static char s[] = "Hello!";
printf("%d
", *(s + strlen(s)));
return 0;
}
Open
View answer
Nested standard-library calls: what does this program print after strcat then strcpy?
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20] = "Hello", str2[20] = " World";
printf("%s
", strcpy(str2, strcat(str1, str2)));
return 0;
}
Open
View answer
Comparing two distinct C arrays of identical contents: what does this program print?
#include<stdio.h>
int main()
{
char str1[] = "Hello";
char str2[] = "Hello";
if (str1 == str2)
printf("Equal
");
else
printf("Unequal
");
return 0;
}
Open
View answer
In C, what will be the exact output of this program (note the embedded nulls and how strlen works)?
#include<stdio.h>
#include<string.h>
int main()
{
char str[] = "India\0\CURIOUSTAB\0";
printf("%d
", strlen(str));
return 0;
}
Choose the correct value printed by strlen.
Open
View answer
In C, what does this program print with %s given embedded null bytes inside the array literal?
#include<stdio.h>
#include<string.h>
int main()
{
char str[] = "India\0\CURIOUSTAB\0";
printf("%s
", str);
return 0;
}
Select the exact output.
Open
View answer
In C, consider strcmp on two user-entered strings using gets (unsafe). What output should you expect from this program and why can it vary?
#include<stdio.h>
#include<string.h>
int main()
{
char str1[5], str2[5];
int i;
gets(str1);
gets(str2);
i = strcmp(str1, str2);
printf("%d
", i);
return 0;
}
Choose the most accurate description of the printed value.
Open
View answer
C printf with extra arguments: what will this program print when the format string contains no conversion specifiers?
#include<stdio.h>
int main()
{
printf("India", "CURIOUSTAB
");
return 0;
}
Select the exact output that appears.
Open
View answer
In C, swapping two pointers to string literals inside an array: what exactly does this program print?
#include<stdio.h>
int main()
{
char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"};
int i;
char *t;
t = names[3];
names[3] = names[4];
names[4] = t;
for (i = 0; i <= 4; i++)
printf("%s,", names[i]);
return 0;
}
Choose the correct comma-separated order.
Open
View answer
Indexing tricks and side effects in C: determine the precise characters printed by this code involving pre/post increment and the i[s] notation.
#include<stdio.h>
int main()
{
static char s[25] = "The cocaine man";
int i = 0;
char ch;
ch = s[++i]; printf("%c", ch);
ch = s[i++]; printf("%c", ch);
ch = i++[s]; printf("%c", ch);
ch = ++i[s]; printf("%c", ch);
return 0;
}
Choose the exact 4-character output.
Open
View answer
Recursive I/O with getchar and printing on unwinding: if the user inputs the characters a, b, c followed by Enter, what will this program print?
#include<stdio.h>
int main()
{
void fun();
fun();
printf("
");
return 0;
}
void fun()
{
char c;
if ((c = getchar()) != '
')
fun();
printf("%c", c);
}
Select the exact output sequence.
Open
View answer
String reversal using indexing: what does this C program output after reading a line with gets and then printing characters in reverse order?
#include<stdio.h>
#include<string.h>
int main()
{
char sentence[80];
int i;
printf("Enter a line of text
");
gets(sentence);
for (i = strlen(sentence) - 1; i >= 0; i--)
putchar(sentence[i]);
return 0;
}
Identify the correct behavior.
Open
View answer
Array indexing into a string literal in C: what character does this code print?
#include<stdio.h>
#include<string.h>
int main()
{
printf("%c
", "abcdefgh"[4]);
return 0;
}
Pick the exact character output.
Open
View answer
Pointer types and %s in printf: what happens when you pass &str + 2 (a pointer to beyond the array) to %s?
#include<stdio.h>
int main()
{
char str[25] = "CuriousTab";
printf("%s
", &str + 2);
return 0;
}
Select the most accurate outcome.
Open
View answer
C strings and strcat on string literals (Turbo C, 16-bit DOS): what will the following program print or do?
#include<stdio.h>
#include<string.h>
int main()
{
char *str1 = 'India';
char *str2 = 'CURIOUSTAB';
char *str3;
str3 = strcat(str1, str2); // concatenating into a string literal
printf('%s %s
', str3, str1);
return 0;
}
Assume a Turbo C compiler on a 16-bit DOS platform, as originally intended.
Open
View answer
C printf return value and empty strings: what does this program print?
#include<stdio.h>
int main()
{
int i;
char a[] = '\0'; // empty C string
if (printf('%s', a))
printf('The string is not empty
');
else
printf('The string is empty
');
return 0;
}
Open
View answer
Addresses of string literals and %s printing: given that the memory address of the literal 'Hello1' is 1022 (Turbo C, 16-bit DOS), what will this print?
#include<stdio.h>
int main()
{
printf('%u %s
', &'Hello1', &'Hello2');
return 0;
}
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.