QUIZ With Solutions C Programming
QUIZ With Solutions C Programming
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; }
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);
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[100]="HELLO SUMMER";
printf("%s",strlwr(str));
return 0;
}
#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