Font size
WorksheetsQuiz on C Strings
Total questions: 91
Worksheet time: 1hrs 2mins
What is the output of the following C code?
#include
Hello world
Compilation Error
Hello World
Undefined Behavior
Assuming a 64-bit system where a pointer takes 8 bytes, what is the output of this code?
#include
5 4
8 4
5 5
4 4
What is the output of the following code snippet?
#include
StringTest
Test
ingTest
ngTest
Which line, if uncommented, would cause a compilation error?
#include
Line A
Line B
Line C
Line D
What is the most likely outcome of running this code?
#include
It prints Buffer contains: 9876
It prints Buffer contains: 98765
It results in a Compilation Error.
It results in Undefined Behavior.
What is the return value of the following statement if it is placed in C program? strcmp(“ABC”,”ABC”);
33
-1
1
0
Which built in function is used to convert string to lowercase
strlen()
strlwr()
strcmp()
strrev()
Which is not a built in function
string()
strlen()
strcat()
strrev()
String is an array of characters with …….. character as the last element of array.
Null
Empty
0
1
What is the format specifier used to print or read a character in an array in Printf or Scanf function?
%C
%f
%w
%c
Which of the following function is more appropriate for reading in a multi-word string?
scanf()
gets()
printf()
puts()
The library function used to copy a string to another is
strncpy()
strcmp()
strcpy()
strchr()
String is initialized with datatype
float
char
int
double
Strcat() function adds null character
Only if there is space
Always
Depends on the standard
Depends on the compiler
What will be the output of the below program
#include <stdio.h>
#include <string.h>
int main( )
{
char source[ ] = " fresh2refresh" ;
char target[ ]= " C tutorial" ;
strcat ( target, source ) ;
printf ( "\nTarget string after strcat( ) = %s", target ) ;
}
Target string after strcat( ) = fresh2refresh
Target string after strcat( ) = C tutorial
Target string after strcat( ) =
Target string after strcat( ) = C tutorial fresh2refresh
What is the output of this program?
#include <stdio.h>
int main()
{
char str[] = "hello, world";
printf("%d", strlen(str));
}
Compilation error
10
13
12
What is the output of
#include <stdio.h>
int main()
{
char str[8]="IncludeHelp";
printf("%s",str);
return 0;
}
IncludeHelp
IncludeH
Error
No Output
What is the output of this
#include <stdio.h>
#include <string.h>
int main()
{
char s1[10] = "Hello";
char s2[10] = "World";
strncat(s1,s2, 3);
printf("Concatenation using strncat: %s", s1);
return 0;
}
Concatenation using strncat: Hel
Concatenation using strncat: HelloWorld
Concatenation using strncat: HelloWor
None of the above
What is the output of
#include <stdio.h>
#include <string.h>
int main()
{
char s1[30] = "string 1";
char s2[30] = "string 2: Am using strncpy now";
strncpy(s1,s2, 12);
printf("String s1 is: %s", s1);
return 0;
}
String s1 is: string 2: Am
String s1 is: string 2: A
String s1 is: string 2:
None of the above
What is the output of the below program
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "abcd", str2[] = "abcd";
int result;
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
return 0;
}
0
1
Invalid
None of the above
What is the output of the below program
#include <stdio.h>
#include <ctype.h>
void strupr(char s[])
{
int i = 0;
while(s[i] != '\0')
{
printf("%c",toupper(s[i]));
i++;
}
printf("\n");
}
int main()
{
strupr("Hello Shivamogga");
return 0;
}
Hello Shivamogga
HELLO Shivamogga
Error
HELLO SHIVAMOGGA
What is the output of the below program
#include <stdio.h>
int main()
{
char str[ ] = "Hello";
int i;
for( i=2; i<4; i++)
{
printf("%c", str[i]);
}
return 0;
}
He
ll
el
lo
What will be the output of the below program
#include <stdio.h>
int main()
{
int a[5]={5,1,15,20,25};
int i,j,m;
i=++a[1];
j=a[1]++;
m=a[i++];
printf("%d,%d,%d",i,j,m);
}
3,2,15
2,3,20
2,1,15
1,2,5
What is the output of the below program
#include <stdio.h>
int main()
{
char str[]="JNNCE", ch='N';
int i, frequency = 0;
for(i = 0; str[i] != '\0'; ++i)
{
if(ch == str[i])
++frequency;
}
printf("Frequency of %c = %d", ch, frequency);
return 0;
}
Frequency of N = 0
Frequency of N = -1
Frequency of N = 2
None of the above
The library function used to calculate the length of the string is
strstr()
strrev()
strlen()
strcat()
What is the proper declaration for an array that has 2 rows and 3 columns?
type arr[2][2];
arr[2][3] type;
type arr[2][3];
type arr[4][3];
How can I store the value 10 into the second row, first column of my array that I have declared as int numbers[10][10]?
int numbers[1][2] = 10;
numbers[1][2] = 10;
numbers[2][3] = 10;
numbers[1][0] = 10
(a)
Compilation Error
14
13
Garbage Value
321004
3210
01234
400123
432100
1 2 3 4 5 5
1 2 3 4 5 0
1 2 3 4 5 Garbage value
Error
int A[5][4];
What will be the size of above array element ?
(a)
int A[2][2][2];
What will be the size of above array element ?
(a)
Pick all correct statements.
int A[][]={1,2,3,4};
int A[2][]={1,2,3,4};
int A[][2]={1,2,3,4};
int A[2][2]={1,2,3,4};
if two strings are identical then function strcmp() returns
0
-1
true
none of these
what will be the output of the program?
#include <stdio.h>
#include<string .h>
void main()
{
char str1[20]="Hello",str2="world";
strcat(str1,str2);
puts(str1);
return 0;
}
Hello
world
Helloworld
none of the above
str1="6/4";
printf("str1");
6/4
str1
None
Which of the following function is more appropriate for reading in a multi-word string?
scanf()
printf()
puts()
gets()
int main()
{
char str[25];
scanf("%s", str);
printf("%s",str);
return 0;
}
//input: South Africa
South
South Africa
S
Compiler error
What is the output of C program with strings.?
int main()
{
char str1[]="SMART";
char str2[10];
str2 = str1;
printf("%s",str2);
return 0;
}
SMART
SMART followed by 5 spaces
SMART followed by 4 spaces
can not assign one string to another
What will the output of the following program?
#include <stdio.h>
int main()
{
char str1[] = "College";
char str2[] = "University";
strcpy(str1, str2);
printf("%s", str1);
}
College
University
College University
University College
Given the code, what is in values[2][1] ?
7.9
9.2
7.3
0.5
Given the code, what is the value of things.length ?
4
3
12
7
Given the following:
double [][] stuff ;
Which of the following statements constructs an array with 5 rows of 7 columns each and assign its reference to stuff ?
stuff = new stuff[5][7] ;
stuff = new double[5][7] ;
new stuff = double[5][7] ;
stuff = double new[5][7] ;
Given the code, which of the following fragments prints out every element of items?
Each piece of data in an array is BEST described as a what?
Integer and string
Item
Element
Elephant
Which BEST describes an array?
A data structure - Stores a collection of values under one name
A data collection - Stores a structure of values under one name
A list - Stores a list of numbers
A list - Stores a list of strings
Why are arrays useful?
It saves storage space storing under one name
You can store integers and strings together
You can store thing related on different topics
You may have lots of related data that you don't want to store separately
Which of the following statements correctly declares a two-dimensional integer array?
int Matrix[ ] = new int[5,4];
int Matrix[ ]; Matrix = new int[5,4];
int Matrix[ ][ ] = new int[5][4];
int Matrix[ ][ ] = int[5][4];
How would you get the value "6" out of the following array?
a [0][3]
a [1][3]
c) a[0][2]
d) a[2][0]
e) a[3][1]
What would the statement a[1].length return?
3
4
2
1
An array elements are always stored in ________ memory locations.
sequential
Random
Sequential and Random
None of the above
What is the correct way to declare a string in C?
char myString[] = "Hello";
char myString[6] = 'Hello';
string myString = 'Hello';
char myString = 'Hello';
Which function is used to find the length of a string in C?
strlength()
strlen()
stringlength()
len()
How do you concatenate two strings in C?
strcat(string1, string2)
concat(string1, string2)
strncat(string1, string2, n)
append(string1, string2)
Which function is used to compare two strings in C?
strcmp()
add()
multiply()
divide()
What is the correct way to input a string in C?
input()
print()
cin >>
scanf()
Which function is used to find the first occurrence of a character in a string in C?
findchar()
strrchr()
strchr()
index()
What is the correct way to copy one string to another in C?
moveString(destination, source)
copyString(destination, source)
duplicateString(destination, source)
strcpy(destination, source)
Which function is used to reverse a string in C?
strrev
string_reverse
str_reverse
reversestr
if two strings are identical then function strcmp() returns
0
-1
true
none of these
what will be the output of the program?
#include <stdio.h>
#include<string .h>
void main()
{
char str1[20]="Hello",str2="world";
strcat(str1,str2);
puts(str1);
return 0;
}
Hello
world
Helloworld
none of the above
Which is the correct example of declaring functions?
int add(int [],int);
int add(int, int);
int add(int a, int b);
All of these
Strcmp() is used to ___________.
Find the length of the string.
Copies one string over another.
Compare two strings.
Concatenates two strings.
What is the value of "z" in this code?
The value of z is 10
The value of z is 15
The value of z is 15.6
The value of z is 16
What will be the output of this code?
OK
KO
Depends on compiler
Depends on standard
How to capture the string in C? (declared as- char str[100]; )
scanf( "%c", str);
scanf( "%s", str);
scanf( "%c", &str);
scanf( "%s", &str);
What is the output of this code?
1 1
5 1
1 5
1 13465
What is a String in C Language.?
String is a new Data Type in C
String is an array of Characters with null character as the last element of array.
String is an array of Characters with null character as the first element of array
String is an array of Integers with 0 as the last element of array
What is the Format specifier used to print a String or Character array in C Printf or Scanf function.?
%d
%f
%s
%c
What is the output?
int main()
{
char str[]={'B','M','E','\0'};
printf("%s",str);
return 0;
}
BME
BME with garbage value at the end
BME\0
error
What is the output of C program with strings.?
int main()
{
char str1[]="SMART";
char str2[10];
str2 = str1;
printf("%s",str2);
return 0;
}
SMART
SMART followed by 5 spaces
SMART followed by 4 spaces
can not assign one string to another
int main()
{
char str[25];
scanf("%s", str);
printf("%s",str);
return 0;
}
//input: South Africa
South
South Africa
S
Compiler error
How do you accept a Multi Word Input in C Language.?
scanf()
gets()
getc()
finds()
Any function working with String knowns the String has ended when it encounters
null character
Empty space
"\1"
pointer
What is the output of C Program with Strings.?
int main()
{
char ary[]="Discovery Channel";
printf("%s",ary);
return 0;
}
D
Discovery Channel
Discovery
Compiler error
Size of the array need not be specified, when
Initialization is a part of definition
It is a formal parameter
It is a declaration
All of the above
An array Index starts with.?
-1
0
1
2
Which of the following expressions will result in “crying”?
Let sentence = "There's no crying in baseball"
print(sentence[10:16])
print(sentence[11:17])
print(sentence[11:16])
print(sentence[10:17])
Which of the following expressions will print “phone”?
Let sentence = "E.T. phone home"
print(sentence[4:9])
print(sentence[4:10])
print(sentence[5:9])
print(sentence[5:10])
Which of the following expressions will print “N”?
word = "MONEY"
print(word[3])
print(word[2])
print(word[4])
print(word[1])
Which of the following string operation is illegal? Assume that word = "goose".
word[0] = "M"
word = word[2] + word[-1]
word = "moose"
word = word + "aloose"
What is the output of the program?
This program will cause an error.
Which of the following if statements checks if the string variable quote contains the word “big”?
Which of the functions below will return a string that is in alternating caps? For example, alt_case("beatles") should return “bEaTlEs”.
What is the value of index after this function call?
7
C
5
-1
What is the value of index after this function call?
-1
0
7
15
