wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Quiz on C Strings

Total questions: 91

Worksheet time: 1hrs 2mins

Name
Class
Date
1.

What is the output of the following C code? #include int main() { char *str = "Hello World"; *(str + 6) = 'w'; printf("%s\n", str); return 0; }

a)

Hello world

b)

Compilation Error

c)

Hello World

d)

Undefined Behavior

2.

Assuming a 64-bit system where a pointer takes 8 bytes, what is the output of this code? #include #include void find_size(char arr[]) { printf("%zu %zu\n", sizeof(arr), strlen(arr)); } int main() { char my_str[] = "Test"; // "Test" has 4 chars + '\0' find_size(my_str); return 0; }

a)

5 4

b)

8 4

c)

5 5

d)

4 4

3.

What is the output of the following code snippet? #include int main() { char s[] = "StringTest"; // Index: 0123456789 char *p = s; printf("%s\n", p + (s[1] > s[3] ? 3 : 5)); return 0; }

a)

StringTest

b)

Test

c)

ingTest

d)

ngTest

4.

Which line, if uncommented, would cause a compilation error? #include int main() { char str1[] = "alpha"; char str2[] = "beta"; char * const p = str1; // A) p[0] = 'A'; // B) *p = 'A'; // C) p = str2; // D) strcpy(p, "gamma"); printf("%s\n", p); return 0; }

a)

Line A

b)

Line B

c)

Line C

d)

Line D

5.

What is the most likely outcome of running this code? #include int main() { char buffer[5]; int val = 98765; sprintf(buffer, "%d", val); printf("Buffer contains: %s\n", buffer); return 0; }

a)

It prints Buffer contains: 9876

b)

It prints Buffer contains: 98765

c)

It results in a Compilation Error.

d)

It results in Undefined Behavior.

6.

What is the return value of the following statement if it is placed in C program? strcmp(“ABC”,”ABC”);

a)

33

b)

-1

c)

1

d)

0

7.

Which built in function is used to convert string to lowercase

a)

strlen()

b)

strlwr()

c)

strcmp()

d)

strrev()

8.

Which is not a built in function

a)

string()

b)

strlen()

c)

strcat()

d)

strrev()

9.

String is an array of characters with …….. character as the last element of array.

a)

Null

b)

Empty

c)

0

d)

1

10.

What is the format specifier used to print or read a character in an array in Printf or Scanf function?

a)

%C

b)

%f

c)

%w

d)

%c

11.

Which of the following function is more appropriate for reading in a multi-word string?

a)

scanf()

b)

gets()

c)

printf()

d)

puts()

12.

The library function used to copy a string to another is

a)

strncpy()

b)

strcmp()

c)

strcpy()

d)

strchr()

13.

String is initialized with datatype

a)

float

b)

char

c)

int

d)

double

14.

Strcat() function adds null character

a)

Only if there is space

b)

Always

c)

Depends on the standard

d)

Depends on the compiler

15.

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 ) ;

}

a)

Target string after strcat( ) =  fresh2refresh

b)

Target string after strcat( ) =  C tutorial

c)

Target string after strcat( ) =

d)

Target string after strcat( ) = C tutorial fresh2refresh

16.

What is the output of this program?

#include <stdio.h>

int main()

{

        char str[] = "hello, world";

        printf("%d", strlen(str));

        }

a)

Compilation error

b)

10

c)

13

d)

12

17.

What is the output of

#include <stdio.h>

int main()

{

    char str[8]="IncludeHelp";

    printf("%s",str);

    return 0;

}

a)

IncludeHelp

b)

IncludeH

c)

Error

d)

No Output

18.

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;

}

a)

Concatenation using strncat: Hel

b)

Concatenation using strncat: HelloWorld

c)

Concatenation using strncat: HelloWor

d)

None of the above

19.

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;

}

a)

String s1 is: string 2: Am

b)

String s1 is: string 2: A

c)

String s1 is: string  2:

d)

None of the above

20.

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;

}

a)

0

b)

1

c)

Invalid

d)

None of the above

21.

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;

}

a)

Hello Shivamogga

b)

HELLO Shivamogga

c)

Error

d)

HELLO SHIVAMOGGA

22.

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;

}

a)

He

b)

ll

c)

el

d)

lo

23.

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);

}

a)

3,2,15

b)

2,3,20

c)

2,1,15

d)

1,2,5

24.

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;

}

a)

Frequency of N = 0

b)

Frequency of N = -1

c)

Frequency of N = 2

d)

None of the above

25.

The library function used to calculate the length of the string is

a)

strstr()

b)

strrev()

c)

strlen()

d)

strcat()

26.

What is the proper declaration for an array that has 2 rows and 3 columns?

a)

type arr[2][2];

b)

arr[2][3] type;

c)

type arr[2][3];

d)

type arr[4][3];

27.

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]?

a)

int numbers[1][2] = 10;

b)

numbers[1][2] = 10;

c)

numbers[2][3] = 10;

d)

numbers[1][0] = 10

28.



(a)  

29.
a)

Compilation Error

b)

14

c)

13

d)

Garbage Value

30.
a)

321004

b)

3210

c)

01234

d)

400123

e)

432100

31.
a)

1 2 3 4 5 5

b)

1 2 3 4 5 0

c)

1 2 3 4 5 Garbage value

d)

Error

32.

int A[5][4];

What will be the size of above array element ?

(a)  

33.

int A[2][2][2];

What will be the size of above array element ?

(a)  

34.

Pick all correct statements.

a)

int A[][]={1,2,3,4};

b)

int A[2][]={1,2,3,4};

c)

int A[][2]={1,2,3,4};

d)

int A[2][2]={1,2,3,4};

35.

if two strings are identical then function strcmp() returns

a)

0

b)

-1

c)

true

d)

none of these

36.

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;

}

a)

Hello

b)

world

c)

Helloworld

d)

none of the above

37.

str1="6/4";

printf("str1");

a)

6/4

b)

str1

c)

None

38.

Which of the following function is more appropriate for reading in a multi-word string?

a)

scanf()

b)

printf()

c)

puts()

d)

gets()

39.

int main()

{

char str[25];

scanf("%s", str);

printf("%s",str);

return 0;

}

//input: South Africa

a)

South

b)

South Africa

c)

S

d)

Compiler error

40.

What is the output of C program with strings.?

int main()

{

char str1[]="SMART";

char str2[10];

str2 = str1;

printf("%s",str2);

return 0;

}

a)

SMART

b)

SMART followed by 5 spaces

c)

SMART followed by 4 spaces

d)

can not assign one string to another

41.

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);

}

a)

College

b)

University

c)

College University

d)

University College

42.

Given the code, what is in values[2][1] ?

a)

7.9

b)

9.2

c)

7.3

d)

0.5

43.

Given the code, what is the value of things.length ?

a)

4

b)

3

c)

12

d)

7

44.

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 ?

a)

stuff = new stuff[5][7] ;

b)

stuff = new double[5][7] ;

c)

new stuff = double[5][7] ;

d)

stuff = double new[5][7] ;

45.

Given the code, which of the following fragments prints out every element of items?

a)
b)
c)
d)
46.

Each piece of data in an array is BEST described as a what?

a)

Integer and string

b)

Item

c)

Element

d)

Elephant

47.

Which BEST describes an array?

a)

A data structure - Stores a collection of values under one name

b)

A data collection - Stores a structure of values under one name

c)

A list - Stores a list of numbers

d)

A list - Stores a list of strings

48.

Why are arrays useful?

a)

It saves storage space storing under one name

b)

You can store integers and strings together

c)

You can store thing related on different topics

d)

You may have lots of related data that you don't want to store separately

49.

Which of the following statements correctly declares a two-dimensional integer array?

a)

int Matrix[ ] = new int[5,4];

b)

int Matrix[ ]; Matrix = new int[5,4];

c)

int Matrix[ ][ ] = new int[5][4];

d)

int Matrix[ ][ ] = int[5][4];

50.

How would you get the value "6" out of the following array?

a)

a [0][3]

b)

a [1][3]

c)

c) a[0][2]

d)

d) a[2][0]

e)

e) a[3][1]

51.

What would the statement a[1].length return?

a)

3

b)

4

c)

2

d)

1

52.
What value is at index 1 in this array?  String[] names = {"Mack", "Dennis", "Dee", "Charlie"};
a)
Mack
b)
Dennis
c)
Dee
d)
Charlie
53.
True or False:  An array can be store different types of data (like store both doubles and ints).
a)
True
b)
False
54.
A two-dimensional array is a ___________ that contains multiple fields of data that are interrelated to a single record.  It field is referenced by x and y coordinates, often referred to as rows and columns.
a)
list
b)
cube
c)
table
d)
none of the above
55.
An array declaration contains _______________________ after its array name.
a)
an assignment operator
b)
keyword new
c)
a data type and rectangle brackets with a numeric size inside it
d)
all of the above
56.

An array elements are always stored in ________ memory locations.

a)

sequential

b)

Random

c)

Sequential and Random

d)

None of the above

57.

What is the correct way to declare a string in C?

a)

char myString[] = "Hello";

b)

char myString[6] = 'Hello';

c)

string myString = 'Hello';

d)

char myString = 'Hello';

58.

Which function is used to find the length of a string in C?

a)

strlength()

b)

strlen()

c)

stringlength()

d)

len()

59.

How do you concatenate two strings in C?

a)

strcat(string1, string2)

b)

concat(string1, string2)

c)

strncat(string1, string2, n)

d)

append(string1, string2)

60.

Which function is used to compare two strings in C?

a)

strcmp()

b)

add()

c)

multiply()

d)

divide()

61.

What is the correct way to input a string in C?

a)

input()

b)

print()

c)

cin >>

d)

scanf()

62.

Which function is used to find the first occurrence of a character in a string in C?

a)

findchar()

b)

strrchr()

c)

strchr()

d)

index()

63.

What is the correct way to copy one string to another in C?

a)

moveString(destination, source)

b)

copyString(destination, source)

c)

duplicateString(destination, source)

d)

strcpy(destination, source)

64.

Which function is used to reverse a string in C?

a)

strrev

b)

string_reverse

c)

str_reverse

d)

reversestr

65.

if two strings are identical then function strcmp() returns

a)

0

b)

-1

c)

true

d)

none of these

66.

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;

}

a)

Hello

b)

world

c)

Helloworld

d)

none of the above

67.

Which is the correct example of declaring functions?

a)

int add(int [],int);

b)

int add(int, int);

c)

int add(int a, int b);

d)

All of these

68.

Strcmp() is used to ___________.

a)

Find the length of the string.

b)

Copies one string over another.

c)

Compare two strings.

d)

Concatenates two strings.

69.

What is the value of "z" in this code?

a)

The value of z is 10

b)

The value of z is 15

c)

The value of z is 15.6

d)

The value of z is 16

70.

What will be the output of this code?

a)

OK

b)

KO

c)

Depends on compiler

d)

Depends on standard

71.

How to capture the string in C? (declared as- char str[100]; )

a)

scanf( "%c", str);

b)

scanf( "%s", str);

c)

scanf( "%c", &str);

d)

scanf( "%s", &str);

72.

What is the output of this code?

a)

1 1

b)

5 1

c)

1 5

d)

1 13465

73.

What is a String in C Language.?

a)

String is a new Data Type in C

b)

String is an array of Characters with null character as the last element of array.

c)

String is an array of Characters with null character as the first element of array

d)

String is an array of Integers with 0 as the last element of array

74.

What is the Format specifier used to print a String or Character array in C Printf or Scanf function.?

a)

%d

b)

%f

c)

%s

d)

%c

75.

What is the output?

int main()

{

char str[]={'B','M','E','\0'};

printf("%s",str);

return 0;

}

a)

BME

b)

BME with garbage value at the end

c)

BME\0

d)

error

76.

What is the output of C program with strings.?

int main()

{

char str1[]="SMART";

char str2[10];

str2 = str1;

printf("%s",str2);

return 0;

}

a)

SMART

b)

SMART followed by 5 spaces

c)

SMART followed by 4 spaces

d)

can not assign one string to another

77.

int main()

{

char str[25];

scanf("%s", str);

printf("%s",str);

return 0;

}

//input: South Africa

a)

South

b)

South Africa

c)

S

d)

Compiler error

78.

How do you accept a Multi Word Input in C Language.?

a)

scanf()

b)

gets()

c)

getc()

d)

finds()

79.

Any function working with String knowns the String has ended when it encounters

a)

null character

b)

Empty space

c)

"\1"

d)

pointer

80.

What is the output of C Program with Strings.?


int main()

{

char ary[]="Discovery Channel";

printf("%s",ary);

return 0;

}

a)

D

b)

Discovery Channel

c)

Discovery

d)

Compiler error

81.

Size of the array need not be specified, when

a)

Initialization is a part of definition

b)

It is a formal parameter

c)

It is a declaration

d)

All of the above

82.

An array Index starts with.?

a)

-1

b)

0

c)

1

d)

2

83.

Which of the following expressions will result in “crying”?

Let sentence = "There's no crying in baseball"

a)

print(sentence[10:16])

b)

print(sentence[11:17])

c)

print(sentence[11:16])

d)

print(sentence[10:17])

84.

Which of the following expressions will print “phone”?

Let sentence = "E.T. phone home"

a)

print(sentence[4:9])

b)

print(sentence[4:10])

c)

print(sentence[5:9])

d)

print(sentence[5:10])

85.

Which of the following expressions will print “N”?

word = "MONEY"

a)

print(word[3])

b)

print(word[2])

c)

print(word[4])

d)

print(word[1])

86.

Which of the following string operation is illegal? Assume that word = "goose".


a)

word[0] = "M"

b)

word = word[2] + word[-1]

c)

word = "moose"

d)

word = word + "aloose"

87.

What is the output of the program?

a)

b)

c)

d)

This program will cause an error.

88.

Which of the following if statements checks if the string variable quote contains the word “big”?

a)

b)

c)

d)

89.

Which of the functions below will return a string that is in alternating caps? For example, alt_case("beatles") should return “bEaTlEs”.

a)

b)

c)

d)

90.

What is the value of index after this function call?

a)

7

b)

C

c)

5

d)

-1

91.

What is the value of index after this function call?

a)

-1

b)

0

c)

7

d)

15