0% found this document useful (0 votes)
101 views

QUIZ With Solutions C Programming

This C programming quiz contains multiple choice questions about string handling functions in C like strcpy(), strcat(), strlen(), strcmp(), scanf(), printf(), gets() and puts(). The summary provides: 1) The questions test knowledge of how these string functions work and what they return. 2) Concepts examined include concatenating and copying strings, getting string lengths, comparing strings for equality, and input/output of strings. 3) Correct answers are provided for each question to illustrate the expected behavior of the string functions in different code examples.

Uploaded by

Mouad Kacemi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

QUIZ With Solutions C Programming

This C programming quiz contains multiple choice questions about string handling functions in C like strcpy(), strcat(), strlen(), strcmp(), scanf(), printf(), gets() and puts(). The summary provides: 1) The questions test knowledge of how these string functions work and what they return. 2) Concepts examined include concatenating and copying strings, getting string lengths, comparing strings for equality, and input/output of strings. 3) Correct answers are provided for each question to illustrate the expected behavior of the string functions in different code examples.

Uploaded by

Mouad Kacemi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Boumerdes University,IGEE (ex-INELEC)

C programming QUIZ N°1 (Solution) Student name:……………………………….


1. What will be the output of the following C code? 3. What will be the output of the following C code?
#include <stdio.h> #include <stdio.h>
int main() {
int matrix[3][3] = int main() {
{{1,2,3},{4,5,4},{7,9,8}};
int i,j; int matrix[3][3] ={{1,2,3},{4,5,6},{7,8,9}};
for(i=0; i<3; i++) {
for(j=0; j<3; j++) { int i,j;
if(i>=j)
printf("%d ", matrix[i][j]); for(i=0; i<3; i++) {
else
printf("%d ", 0); for(j=0; j<3; j++) {
}
printf("%d ", matrix[j][i]);
printf("\n");
}
}
return 0; Ans :
}
printf("\n"); 1 4 7
2 5 8
Ans : } 3 6 9
1 0 0
return 0; }
4 5 0 4. What will be the output of the following C code?
7 9 8
#include <stdio.h>
int main()
2. What will be the output of the following C code? {
char str[]={"F","L","O","W","E","R","\0"};
#include <stdio.h> printf("%s",str);
int main() { return 0;
int matrix[3][3] = }
{{1,2,3},{4,5,4},{7,9,8}};
int i,j; a) F
b) FLOWER
for(i=0; i<3; i++) { c) FLOWER\0
for(j=0; j<3; j++) { d) Compiler error
if(i<=j) Explanation:
printf("%d ", matrix[i][j]); Yes. You can not use Double Quotes " to represent a
else
single character. Correct way is 'C' not "C". You should
use Single Quotes around a single character constant.
printf("%d ", 0); 5. What will be the output of the following C code?
}
printf("\n"); #include <stdio.h>
} int main()
{ char str1[]="igee";
return 0;
char str2[20];
} str2= str1;
printf("%s",str2);
Ans: return 0; }
1 2 3
0 5 4
a) igee
0 0 8
b) i
c) igee\0
d) compiler error

Miss .FZ.Hamrioui 1
Boumerdes University,IGEE (ex-INELEC)
C programming QUIZ N°1 (Solution) Student name:……………………………….
6. What will be the output of the following C code? 9. What will be the output of the following C code?

#include <stdio.h>
int main() #include<stdio.h>
{ #include<string.h>
char str[2]; int main()
scanf("%s", str); {
printf("%s",str); printf("%d\n", strlen("123456789"));
return 0; return 0;
//Input: ALGERIA }

A) AL
B) ALGERIA a) 9
C) Compiler error b) 10
D) None of the above. c) 3
Explanation: d) 8
In C Arrays, Overflow or Out of Bounds is not checked Ans :a
properly. It is your responsibility to check. Explanation:
The function strlen returns the number of characters in the
7.If the two strings are identical,
given string.
then strcmp() function returns Therefore, strlen("123456789") returns 9.
Hence the output of the program is "9".
a) -1 10. What will be the output of the following C code?
b) 1
c) 0 #include<stdio.h>
#include<string.h>
Ans : c
int main()
8. What will be the output of the following C code? {
char str[] = "Stay\0positive\0";
#include<stdio.h> printf("%s\t", str);
#include<string.h> printf("%d\n",strlen(str));
return 0;
int main() }
{
char str1[20] = "Promo", str2[20] = " 2022";
printf("%s\n", strcpy(str2, strcat(str1, a) Stay 4
str2))); b) Stay\0positive 14
return 0; c) Stay positive 13
} d) positive 8
Ans: a
a) Promo 11. What will be the output of the following C code?
b) 2022
c) Promo 2022
d) 2022Promo #include<stdio.h>
Ans c: int main()
Explanation: {
printf("%s\n", strcpy(str2, strcat(str1, str2))); char ary[]="Think twice";
=> strcat(str1, str2)) it joins the strings str2 and str1 printf("%s",ary);
together . The result will be stored in str1. return 0; }
Therefore str1 contains "Promo 2022".
=> strcpy(str2, "Promo 2022") it copies the "Promo
2022" to the variable str2.
Hence it prints "Promo 2022"
Think twice

Miss .FZ.Hamrioui 1
Boumerdes University,IGEE (ex-INELEC)
C programming QUIZ N°1 (Solution) Student name:……………………………….
 15. Choose the correct C sentence about strings.
12. What will be the output of the following C code?  A. puts() is capable of printing a multi-word string.
#include<stdio.h>  B. gets() is capable of accepting a multi-word string.
int main()  C. printf() is capable of printing a multi-word string.
{  D. All of these
Ans :- D
char ary[20];
16. What will be the output of the following C code?
printf("Enter your text\n");
scanf("%s",ary);// Input :Think twice #include<stdio.h>
printf("%s",ary); int main()
return 0; } { printf("%d",strcmp("flower","flouer"));
return 0; }

13. What will be the output of the following C code?


Ans: 1

Think
17. What will be the output of the following C code?

#include<stdio.h> #include<stdio.h>
int main()
int main() { printf("%d",strcmp("abcdezzz","abcdf"));
return 0; }
{
char ary[20];
printf("Enter your text\n"); Ans: -1
gets(ary);// Input :Think twice
printf("%s",ary);
18. What will be the output of the following C code?
return 0; }
#include<stdio.h>
#include<string.h>
Think twice int main()
{int i;
char name[]="Germany";
14. What will be the output of the following C code? for(i=0;i<strlen(name);i+=2){
printf("%c",name[i]);
#include<stdio.h> }
#include<string.h>
return 0; }
int main()
{ char sentence[80];
int i; Ans: Gray
printf("Enter a line of text\n");
gets(sentence); 19. What will be the output of the following C code?
for(i=strlen(sentence)-1; i >=0; i--)
putchar(sentence[i]); #include<stdio.h>
return 0; #include<string.h>
}//Input: kniht uoy naht retrams dna mees int main()
uoy naht regnorts, eveileb uoy naht revarb {
char p[20],q[20];
era uoY
printf("please enter your name");
gets(p);
printf("%s",strrev(p));
return 0; }// Input : your name(example:inelec)
Ans:
You are braver than you believe, stronger
than you seem and smarter than you think
Ans: celeni

Miss .FZ.Hamrioui 1
Boumerdes University,IGEE (ex-INELEC)
C programming QUIZ N°1 (Solution) Student name:……………………………….
20. State true or false with reason:
The function call strcmp("pqr","PQR")returns positive 25. What will be the output of the following C code?
number. #include<stdio.h>
True, compared ASII values #include<string.h>
int main()
21. Find errors, if any in the following code segment: { char str[50]="Bac2022 ,Master 2027 ,yes we can";
gets(word1,word2); charstr2[50];
Ans: Error. For function gets() ,we use only one int i,j;
argument. for(i=0,j=0;str[i]!='\0';i++)
{
22. What will be the output of the following C code? if((str[i]>='a' && str[i]<='z') ||(str[i]>='A' &&
str[i]<='Z')||str[i]==' ')
#include <stdio.h>
{
#include <string.h>
str2[j]=str[i];
int main()
j++;
{
} }
char str[100]="igee boumerdes";
str2[j]='\0';
printf("%s",strupr(str));
return 0;
} printf("string is %s \n",str2);
}

Ans: string is Bac Master yes we can


Ans: IGEE BOUMERDES

23. What will be the output of the following C code?

#include <stdio.h>
#include <string.h>
int main()
{
char str[100]="HELLO SUMMER";
printf("%s",strlwr(str));
return 0;
}

Ans: hello summer


Ans: x1=160 y1=140

24. What will be the output of the following C code?

#include<stdio.h>
int main()
{ char str[]="students";
int x=1,z=2,y;
y=++x + z++;
printf("%c\n",str[++y]);
return 0; }

Ans: n
Ans x1=100 y1=200

Miss .FZ.Hamrioui 1

You might also like