You are on page 1of 21

Introduction to Computer

Programming
Quiz 2
Date: 27/09/2023

Prepared by :
Sajal Mukhopadhyay
Naren Debnath
NIT Durgapur (CSE)
Loop – while()
• What will be the output of the following program?
#include<stdio.h>
void main ()
{
while()
{
printf("hello Javatpoint");
}
}
Output:
In function 'main':/tmp/RBsv1egmEr.c:
4:11: error: expected expression before ')' token
while()
Loop – while()
• What will be the output of the following program?

#include<stdio.h>
void main()
{
int i=1;
while(i<=5)
{
if(i==3)
break;
printf("hello Javatpoint");
i++;
}
}
Output:
hello Javatpoint
hello Javatpoint
Loop – do-while()
• What will be the output of the following program?

#include<stdio.h>
int main()
{
int i=1;
do
{
printf("%d \n",i);
i++;
}while(i<=10);
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
Loop – do-while()
• What will be the output of the following program?

#include<stdio.h>
int main()
{
int i=1;
do
{
printf("%d \n",i);
i++;
}while(i>10);
return 0;
}
Output:
1
Array
• What will be the output of the following program?

int main(){
int a[] = {1, 2, 3, 4, 5};
int sum = 0;
for(int i = 0; i < 5; i++) {
if(i % 2 == 0) {
sum += *(a + i); }
else {
sum -= *(a + i);}
}
printf("%d", sum);
}
Output:
3
Array
• What will be the output of the following program?

int main(){
char ch[10] = "abcdefghij";
int ans = 0;
for(int i = 0; i < 10; i++) {
for(int j=0;j<=i;j++)
printf("%c",ch[j]);
printf("\n");}
return 0;
}
Output:
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
abcdefghij
Switch-Case
• What will be the output of the following program?

int main() {
int ch = 2;
switch(ch) {
case 1: printf("1 ");
case 2: printf("2 ");
case 3: printf("3 ");
default: printf("None");
return 0;
}
Output:
2 3 None
Array
• What will be the output of the following program?

int main(){
int arr[]={1,2,3,4,5,6,7,8,9,10};
int loc[]={0,1,2,3,4,5,6,7,8,9};
for(int i=0;i<10;i++)
printf("%d\n",arr[loc[i]]);
}
Output:
1
2
3
4
5
6
7
8
9
10
Array
• What will be the output of the following program?

int main(){
int arr[]={1,2,3,4,5,6,7,8,9,10};
int temp;
for(int i=0,j=9;i<10/2;i++,j--){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
for(int i=0;i<10;i++)
printf("%d\n",arr[i]);
return 0;
}
Output:
10
9
8
7
6
5
4
3
2
1
Array
• What will be the output of the following program?

int main()
{
int r=4,c=1,i,j=1;
for(i=1;i<=r;i++){
for(j=1;j<=I;j++)
printf("%d",j);
printf("\n“);
}
return 0;
}
Output:
1
12
123
1234

You might also like