C Programming Strings Online Test
Finish Quiz
0 of 30 questions completed
Questions:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
Information
C Programming Strings Online Test. C Programming Question and Answers in English. C Programming Strings Online mock test paper is free for all students and Very Helpful for Exam Preparation. C Programming Strings Online Quiz. C Programming Online Mock test for Strings Topic. Here we are providing C Programming Strings Online Test Series in English. Check C Programming Mock Test Series 2024-2024.
This paper has 30 questions.
Time allowed is 30 minutes.
The C Programming online Mock Test Exam is Very helpful for all students. Now Scroll down below n click on “Start Quiz” or “Start Test” and Test yourself.
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Results
0 of 30 questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 points, (0)
Average score |
|
Your score |
|
Categories
- Not categorized 0%
Pos. | Name | Entered on | Points | Result |
---|---|---|---|---|
Table is loading | ||||
No data available | ||||
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- Answered
- Review
-
Question 1 of 30
1. Question
Which of the following function sets first n characters of a string to a given character?
Correct
char *strnset(char *s, int ch, size_t n); Sets the first n characters of s to ch
#include <stdio.h> #include <string.h> int main(void) { char *string = "abcdefghijklmnopqrstuvwxyz"; char letter = 'x'; printf("string before strnset: %s\n", string); strnset(string, letter, 13); printf("string after strnset: %s\n", string); return 0; }
Output:
string before strnset: abcdefghijklmnopqrstuvwxyz
string after strnset: xxxxxxxxxxxxxnopqrstuvwxyz
Incorrect
char *strnset(char *s, int ch, size_t n); Sets the first n characters of s to ch
#include <stdio.h> #include <string.h> int main(void) { char *string = "abcdefghijklmnopqrstuvwxyz"; char letter = 'x'; printf("string before strnset: %s\n", string); strnset(string, letter, 13); printf("string after strnset: %s\n", string); return 0; }
Output:
string before strnset: abcdefghijklmnopqrstuvwxyz
string after strnset: xxxxxxxxxxxxxnopqrstuvwxyz
-
Question 2 of 30
2. Question
If the two strings are identical, then strcmp() function returns
Correct
Declaration: strcmp(const char *s1, const char*s2);
The strcmp return an int value that is
if s1 < s2 returns a value < 0
if s1 == s2 returns 0
if s1 > s2 returns a value > 0
Incorrect
Declaration: strcmp(const char *s1, const char*s2);
The strcmp return an int value that is
if s1 < s2 returns a value < 0
if s1 == s2 returns 0
if s1 > s2 returns a value > 0
-
Question 3 of 30
3. Question
How will you print \n on the screen?
Correct
The statement printf(“\\n”); prints ‘\n’ on the screen.
Incorrect
The statement printf(“\\n”); prints ‘\n’ on the screen.
-
Question 4 of 30
4. Question
The library function used to find the last occurrence of a character in a string is
Correct
Declaration: char *strrchr(const char *s, int c);
It scans a string s in the reverse direction, looking for a specific character c.
Example:
#include <string.h> #include <stdio.h> int main(void) { char text[] = "I learn through IndiaBIX.com"; char *ptr, c = 'i'; ptr = strrchr(text, c); if (ptr) printf("The position of '%c' is: %d\n", c, ptr-text); else printf("The character was not found\n"); return 0; }
Output:
The position of ‘i’ is: 19
Incorrect
Declaration: char *strrchr(const char *s, int c);
It scans a string s in the reverse direction, looking for a specific character c.
Example:
#include <string.h> #include <stdio.h> int main(void) { char text[] = "I learn through IndiaBIX.com"; char *ptr, c = 'i'; ptr = strrchr(text, c); if (ptr) printf("The position of '%c' is: %d\n", c, ptr-text); else printf("The character was not found\n"); return 0; }
Output:
The position of ‘i’ is: 19
-
Question 5 of 30
5. Question
Which of the following function is used to find the first occurrence of a given string in another string?
Correct
The function strstr() Finds the first occurrence of a substring in another string
Declaration: char *strstr(const char *s1, const char *s2);
Return Value:
On success, strstr returns a pointer to the element in s1 where s2 begins (points to s2 in s1).
On error (if s2 does not occur in s1), strstr returns null.Example:
#include <stdio.h> #include <string.h> int main(void) { char *str1 = "IndiaBIX", *str2 = "ia", *ptr; ptr = strstr(str1, str2); printf("The substring is: %s\n", ptr); return 0; }
Output: The substring is: iaBIX
Incorrect
The function strstr() Finds the first occurrence of a substring in another string
Declaration: char *strstr(const char *s1, const char *s2);
Return Value:
On success, strstr returns a pointer to the element in s1 where s2 begins (points to s2 in s1).
On error (if s2 does not occur in s1), strstr returns null.Example:
#include <stdio.h> #include <string.h> int main(void) { char *str1 = "IndiaBIX", *str2 = "ia", *ptr; ptr = strstr(str1, str2); printf("The substring is: %s\n", ptr); return 0; }
Output: The substring is: iaBIX
-
Question 6 of 30
6. Question
Which of the following function is more appropriate for reading in a multi-word string?
Correct
gets(); collects a string of characters terminated by a new line from the standard input stream stdin
#include <stdio.h> int main(void) { char string[80]; printf("Enter a string:"); gets(string); printf("The string input was: %s\n", string); return 0; }
Output:
Enter a string: IndiaBIX
The string input was: IndiaBIX
Incorrect
gets(); collects a string of characters terminated by a new line from the standard input stream stdin
#include <stdio.h> int main(void) { char string[80]; printf("Enter a string:"); gets(string); printf("The string input was: %s\n", string); return 0; }
Output:
Enter a string: IndiaBIX
The string input was: IndiaBIX
-
Question 7 of 30
7. Question
What will be the output of the program ?
#include<stdio.h> #include<string.h> int main() { char str1[20] = "Hello", str2[20] = " World"; printf("%s\n", strcpy(str2, strcat(str1, str2))); return 0; }
Correct
Step 1: char str1[20] = “Hello”, str2[20] = ” World”; The variable str1 and str2 is declared as an array of characters and initialized with value “Hello” and ” World” respectively.
Step 2: printf(“%s\n”, strcpy(str2, strcat(str1, str2)));
=> strcat(str1, str2)) it append the string str2 to str1. The result will be stored in str1. Therefore str1 contains “Hello World”.
=> strcpy(str2, “Hello World”) it copies the “Hello World” to the variable str2.
Hence it prints “Hello World”.
Incorrect
Step 1: char str1[20] = “Hello”, str2[20] = ” World”; The variable str1 and str2 is declared as an array of characters and initialized with value “Hello” and ” World” respectively.
Step 2: printf(“%s\n”, strcpy(str2, strcat(str1, str2)));
=> strcat(str1, str2)) it append the string str2 to str1. The result will be stored in str1. Therefore str1 contains “Hello World”.
=> strcpy(str2, “Hello World”) it copies the “Hello World” to the variable str2.
Hence it prints “Hello World”.
-
Question 8 of 30
8. Question
What will be the output of the program ?
#include<stdio.h> int main() { char p[] = "%d\n"; p[1] = 'c'; printf(p, 65); return 0; }
Correct
Step 1: char p[] = “%d\n”; The variable p is declared as an array of characters and initialized with string “%d”.
Step 2: p[1] = ‘c’; Here, we overwrite the second element of array p by ‘c’. So array p becomes “%c”.
Step 3: printf(p, 65); becomes printf(“%c”, 65);
Therefore it prints the ASCII value of 65. The output is ‘A’.
Incorrect
Step 1: char p[] = “%d\n”; The variable p is declared as an array of characters and initialized with string “%d”.
Step 2: p[1] = ‘c’; Here, we overwrite the second element of array p by ‘c’. So array p becomes “%c”.
Step 3: printf(p, 65); becomes printf(“%c”, 65);
Therefore it prints the ASCII value of 65. The output is ‘A’.
-
Question 9 of 30
9. Question
What will be the output of the program ?
#include<stdio.h> #include<string.h> int main() { printf("%d\n", strlen("123456")); return 0; }
Correct
The function strlen returns the number of characters in the given string.
Therefore, strlen(“123456”) returns 6.
Hence the output of the program is “6”.
Incorrect
The function strlen returns the number of characters in the given string.
Therefore, strlen(“123456”) returns 6.
Hence the output of the program is “6”.
-
Question 10 of 30
10. Question
What will be the output of the program ?
#include<stdio.h> int main() { printf(5+"Good Morning\n"); return 0; }
Correct
printf(5+”Good Morning\n”); It skips the 5 characters and prints the given string.
Hence the output is “Morning”
Incorrect
printf(5+”Good Morning\n”); It skips the 5 characters and prints the given string.
Hence the output is “Morning”
-
Question 11 of 30
11. Question
What will be the output of the program ?
#include<stdio.h> #include<string.h> int main() { char str[] = "India\0\BIX\0"; printf("%s\n", str); return 0; }
Correct
A string is a collection of characters terminated by ‘\0’.
Step 1: char str[] = “India\0\BIX\0”; The variable str is declared as an array of characters and initialized with value “India”
Step 2: printf(“%s\n”, str); It prints the value of the str.
The output of the program is “India”.
Incorrect
A string is a collection of characters terminated by ‘\0’.
Step 1: char str[] = “India\0\BIX\0”; The variable str is declared as an array of characters and initialized with value “India”
Step 2: printf(“%s\n”, str); It prints the value of the str.
The output of the program is “India”.
-
Question 12 of 30
12. Question
What will be the output of the program If characters ‘a’, ‘b’ and ‘c’ enter are supplied as input?
#include<stdio.h> int main() { void fun(); fun(); printf("\n"); return 0; } void fun() { char c; if((c = getchar())!= '\n') fun(); printf("%c", c); }
Correct
Step 1: void fun(); This is the prototype for the function fun().
Step 2: fun(); The function fun() is called here.
The function fun() gets a character input and the input is terminated by an enter key(New line character). It prints the given character in the reverse order.
The given input characters are “abc”
Output: cba
Incorrect
Step 1: void fun(); This is the prototype for the function fun().
Step 2: fun(); The function fun() is called here.
The function fun() gets a character input and the input is terminated by an enter key(New line character). It prints the given character in the reverse order.
The given input characters are “abc”
Output: cba
-
Question 13 of 30
13. Question
What will be the output of the program ?
#include<stdio.h> int main() { printf("India", "BIX\n"); return 0; }
Correct
printf(“India”, “BIX\n”); It prints “India”. Because ,(comma) operator has Left to Right associativity. After printing “India”, the statement got terminated.
Incorrect
printf(“India”, “BIX\n”); It prints “India”. Because ,(comma) operator has Left to Right associativity. After printing “India”, the statement got terminated.
-
Question 14 of 30
14. Question
What will be the output of the program ?
#include<stdio.h> int main() { char str[7] = "IndiaBIX"; printf("%s\n", str); return 0; }
Correct
Here str[] has declared as 7 character array and into a 8 character is stored. This will result in overwriting of the byte beyond 7 byte reserved for ‘\0’.
Incorrect
Here str[] has declared as 7 character array and into a 8 character is stored. This will result in overwriting of the byte beyond 7 byte reserved for ‘\0’.
-
Question 15 of 30
15. Question
What will be the output of the program ?
#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; }
Correct
Step 1: char *names[] = { “Suresh”, “Siva”, “Sona”, “Baiju”, “Ritu”}; The variable names is declared as an pointer to a array of strings.
Step 2: int i; The variable i is declared as an integer type.
Step 3: char *t; The variable t is declared as pointer to a string.
Step 4: t = names[3]; names[3] = names[4]; names[4] = t; These statements the swaps the 4 and 5 element of the array names.
Step 5: for(i=0; i<=4; i++) printf(“%s,”, names[i]); These statement prints the all the value of the array names.
Hence the output of the program is “Suresh, Siva, Sona, Ritu, Baiju”.
Incorrect
Step 1: char *names[] = { “Suresh”, “Siva”, “Sona”, “Baiju”, “Ritu”}; The variable names is declared as an pointer to a array of strings.
Step 2: int i; The variable i is declared as an integer type.
Step 3: char *t; The variable t is declared as pointer to a string.
Step 4: t = names[3]; names[3] = names[4]; names[4] = t; These statements the swaps the 4 and 5 element of the array names.
Step 5: for(i=0; i<=4; i++) printf(“%s,”, names[i]); These statement prints the all the value of the array names.
Hence the output of the program is “Suresh, Siva, Sona, Ritu, Baiju”.
-
Question 16 of 30
16. Question
What will be the output of the program ?
#include<stdio.h> #include<string.h> int main() { char str[] = "India\0\BIX\0"; printf("%d\n", strlen(str)); return 0; }
Correct
The function strlen returns the number of characters int the given string.
Therefore, strlen(str) becomes strlen(“India”) contains 5 characters. A string is a collection of characters terminated by ‘\0’.
The output of the program is “5”.
Incorrect
The function strlen returns the number of characters int the given string.
Therefore, strlen(str) becomes strlen(“India”) contains 5 characters. A string is a collection of characters terminated by ‘\0’.
The output of the program is “5”.
-
Question 17 of 30
17. Question
What will be the output of the program ?
#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\n", i); return 0; }
Correct
Incorrect
-
Question 18 of 30
18. Question
What will be the output of the program ?
#include<stdio.h> #include<string.h> int main() { static char s[] = "Hello!"; printf("%d\n", *(s+strlen(s))); return 0; }
Correct
Incorrect
-
Question 19 of 30
19. Question
What will be the output of the program ?
#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; }
Correct
Incorrect
-
Question 20 of 30
20. Question
What will be the output of the program in 16-bit platform (Turbo C under DOS) ?
#include<stdio.h> int main() { printf("%d, %d, %d", sizeof(3.0f), sizeof('3'), sizeof(3.0)); return 0; }
Correct
Step 1:
printf(“%d, %d, %d”, sizeof(3.0f), sizeof(‘3’), sizeof(3.0));
The sizeof function returns the size of the given expression.
sizeof(3.0f) is a floating point constant. The size of float is 4 bytes
sizeof(‘3’) It converts ‘3’ in to ASCII value.. The size of int is 2 bytes
sizeof(3.0) is a double constant. The size of double is 8 bytes
Hence the output of the program is 4,2,8
Note: The above program may produce different output in other platform due to the platform dependency of C compiler.
In Turbo C, 4 2 8. But in GCC, the output will be 4 4 8.
Incorrect
Step 1:
printf(“%d, %d, %d”, sizeof(3.0f), sizeof(‘3’), sizeof(3.0));
The sizeof function returns the size of the given expression.
sizeof(3.0f) is a floating point constant. The size of float is 4 bytes
sizeof(‘3’) It converts ‘3’ in to ASCII value.. The size of int is 2 bytes
sizeof(3.0) is a double constant. The size of double is 8 bytes
Hence the output of the program is 4,2,8
Note: The above program may produce different output in other platform due to the platform dependency of C compiler.
In Turbo C, 4 2 8. But in GCC, the output will be 4 4 8.
-
Question 21 of 30
21. Question
What will be the output of the program ?
#include<stdio.h> int main() { int i; char a[] = "\0"; if(printf("%s", a)) printf("The string is empty\n"); else printf("The string is not empty\n"); return 0; }
Correct
The function printf() returns the number of charecters printed on the console.
Step 1: char a[] = “\0”; The variable a is declared as an array of characters and it initialized with “\0”. It denotes that the string is empty.
Step 2: if(printf(“%s”, a)) The printf() statement does not print anything, so it returns ‘0’(zero). Hence the if condition is failed.
In the else part it prints “The string is not empty”.
Incorrect
The function printf() returns the number of charecters printed on the console.
Step 1: char a[] = “\0”; The variable a is declared as an array of characters and it initialized with “\0”. It denotes that the string is empty.
Step 2: if(printf(“%s”, a)) The printf() statement does not print anything, so it returns ‘0’(zero). Hence the if condition is failed.
In the else part it prints “The string is not empty”.
-
Question 22 of 30
22. Question
If char=1, int=4, and float=4 bytes size, What will be the output of the program ?
#include<stdio.h> int main() { char ch = 'A'; printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f)); return 0; }
Correct
Step 1: char ch = ‘A’; The variable ch is declared as an character type and initialized with value ‘A’.
Step 2:
printf(“%d, %d, %d”, sizeof(ch), sizeof(‘A’), sizeof(3.14));
The sizeof function returns the size of the given expression.
sizeof(ch) becomes sizeof(char). The size of char is 1 byte.
sizeof(‘A’) becomes sizeof(65). The size of int is 4 bytes (as mentioned in the question).
sizeof(3.14f). The size of float is 4 bytes.
Hence the output of the program is 1, 4, 4
Incorrect
Step 1: char ch = ‘A’; The variable ch is declared as an character type and initialized with value ‘A’.
Step 2:
printf(“%d, %d, %d”, sizeof(ch), sizeof(‘A’), sizeof(3.14));
The sizeof function returns the size of the given expression.
sizeof(ch) becomes sizeof(char). The size of char is 1 byte.
sizeof(‘A’) becomes sizeof(65). The size of int is 4 bytes (as mentioned in the question).
sizeof(3.14f). The size of float is 4 bytes.
Hence the output of the program is 1, 4, 4
-
Question 23 of 30
23. Question
If the size of pointer is 32 bits What will be the output of the program ?
#include<stdio.h> int main() { char a[] = "Visual C++"; char *b = "Visual C++"; printf("%d, %d\n", sizeof(a), sizeof(b)); printf("%d, %d", sizeof(*a), sizeof(*b)); return 0; }
Correct
Incorrect
-
Question 24 of 30
24. Question
What will be the output of the program ?
#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\n", *(mess[2]+9), *(*(mess+2)+9)); return 0; }
Correct
Incorrect
-
Question 25 of 30
25. Question
What will be the output of the program ?
#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\n", str2); return 0; }
Correct
Incorrect
-
Question 26 of 30
26. Question
What will be the output of the program ?
#include<stdio.h> int main() { char str[] = "India\0BIX\0"; printf("%d\n", sizeof(str)); return 0; }
Correct
The following examples may help you understand this problem:
1. sizeof(“”) returns 1 (1*).
2. sizeof(“India”) returns 6 (5 + 1*).
3. sizeof(“BIX”) returns 4 (3 + 1*).
4. sizeof(“India\0BIX”) returns 10 (5 + 1 + 3 + 1*).
Here ‘\0’ is considered as 1 char by sizeof() function.5. sizeof(“India\0BIX\0”) returns 11 (5 + 1 + 3 + 1 + 1*).
Here ‘\0’ is considered as 1 char by sizeof() function.Incorrect
The following examples may help you understand this problem:
1. sizeof(“”) returns 1 (1*).
2. sizeof(“India”) returns 6 (5 + 1*).
3. sizeof(“BIX”) returns 4 (3 + 1*).
4. sizeof(“India\0BIX”) returns 10 (5 + 1 + 3 + 1*).
Here ‘\0’ is considered as 1 char by sizeof() function.5. sizeof(“India\0BIX\0”) returns 11 (5 + 1 + 3 + 1 + 1*).
Here ‘\0’ is considered as 1 char by sizeof() function. -
Question 27 of 30
27. Question
What will be the output of the program ?
#include<stdio.h> int main() { char str[25] = "IndiaBIX"; printf("%s\n", &str+2); return 0; }
Correct
Step 1: char str[25] = “IndiaBIX”; The variable str is declared as an array of characteres and initialized with a string “IndiaBIX”.
Step 2: printf(“%s\n”, &str+2);
=> In the printf statement %s is string format specifier tells the compiler to print the string in the memory of &str+2
=> &str is a location of string “IndiaBIX”. Therefore &str+2 is another memory location.
Hence it prints the Garbage value.
Incorrect
Step 1: char str[25] = “IndiaBIX”; The variable str is declared as an array of characteres and initialized with a string “IndiaBIX”.
Step 2: printf(“%s\n”, &str+2);
=> In the printf statement %s is string format specifier tells the compiler to print the string in the memory of &str+2
=> &str is a location of string “IndiaBIX”. Therefore &str+2 is another memory location.
Hence it prints the Garbage value.
-
Question 28 of 30
28. Question
What will be the output of the program ?
#include<stdio.h> int main() { char str = "IndiaBIX"; printf("%s\n", str); return 0; }
Correct
The line char str = “IndiaBIX”; generates “Non portable pointer conversion” error.
To eliminate the error, we have to change the above line to
char *str = “IndiaBIX”; (or) char str[] = “IndiaBIX”;
Then it prints “IndiaBIX”.
Incorrect
The line char str = “IndiaBIX”; generates “Non portable pointer conversion” error.
To eliminate the error, we have to change the above line to
char *str = “IndiaBIX”; (or) char str[] = “IndiaBIX”;
Then it prints “IndiaBIX”.
-
Question 29 of 30
29. Question
What will be the output of the program ?
#include<stdio.h> int main() { char str[] = "Nagpur"; str[0]='K'; printf("%s, ", str); str = "Kanpur"; printf("%s", str+1); return 0; }
Correct
The statement str = “Kanpur”; generates the LVALUE required error. We have to use strcpy function to copy a string.
To remove error we have to change this statement str = “Kanpur”; to strcpy(str, “Kanpur”);
The program prints the string “anpur”
Incorrect
The statement str = “Kanpur”; generates the LVALUE required error. We have to use strcpy function to copy a string.
To remove error we have to change this statement str = “Kanpur”; to strcpy(str, “Kanpur”);
The program prints the string “anpur”
-
Question 30 of 30
30. Question
What will be the output of the program ?
#include<stdio.h> int main() { printf(5+"IndiaBIX\n"); return 0; }
Correct
printf(5+”IndiaBIX\n”); In the printf statement, it skips the first 5 characters and it prints “BIX”
Incorrect
printf(5+”IndiaBIX\n”); In the printf statement, it skips the first 5 characters and it prints “BIX”