Looping C Programming Mock Test – 3


Question 1
What is the output of the following program?
#include < stdio.h > 
#define pt printf
int main(){
    int i=0;
    for(pt("%d ",++i); pt("%d ",i++); pt("%d ",++i))
    {
         pt("%d ",i++);
         if(i>8)
         break;
    }
    return 0;
}
A
1 1 3 3 4 6 6 7 9 9
B
1 2 2 4 5 5 6 7 8
C
1 1 2 4 4 5 7 7 8
D
1 1 3 3 4 4 5 7 8 8
   C Programming
Question 1 Explanation: 
As per for loop syntax, sequesnce in which all four component executes are as follow:


First iteration:
Initialization : printft("%d ",++i) print 1
Condition : printft("%d ",i++) print 1 and then i becomes 2.
printf function return 1 which is evaluated true by compiler.
Body of loop: printft("%d ",i++) print 2 and i becomes 3. If condition becomes false.
So, next
Increament : printft("%d ",++i) print 4 and value of i is 4.

Second iteration:
Condition : printft("%d ",i++) print 4 and then i becomes 5.
printf function return 1 which is evaluated true by compiler.
Body of loop: printft("%d ",i++) print 5 and i becomes 6. If condition becomes false.
So, next
Increament : printft("%d ",++i) print 7 and value of i is 7.

Third iteration:
Condition : printft("%d ",i++) print 7 and then i becomes 8.
printf function return 1 which is evaluated true by compiler.
Body of loop: printft("%d ",i++) print 8 and i becomes 9. If condition becomes true.
So, due to break statement, control goes out of for loop.

OUTPUT: 1 1 2 4 4 5 7 7 8

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE
Question 2
How many time "practicepaper" will be printed in the output of following program?
#include < stdio.h > 
int main(){
    int i=0;
    while(sizeof(++i))
    {
        if(i>5)
        break;
        printf("practicepaper");
    }
    return 0;
}
A
5
B
4
C
1
D
infinite time
   C Programming
Question 2 Explanation: 
Concept: sizeof() operator only evaluate the memory size required based on data type of the input parameter paased but not execute it.
Therefore, in the above program, sizeof(++i) only evaluate the resultant data type of ++i. It will not executes the statement ++i.
Therefore, value of i never change and due to that program print "practicepaper" infinite times.

OUTPUT: infinite

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE


Question 3
What is the output of the following program?
#include < stdio.h > 
int main(){
    int a=2,b=2;
    while(a+1?--a:b++)
         printf("%d",a);
    return 0;
}
A
2 1
B
2 0
C
3 2
D
2
   C Programming
Question 3 Explanation: 
Consider the while loop condition: a + 1 ? --a : ++b
In first iteration:
a + 1 = 3 (True)
So ternary operator will return --a i.e. 1
In C program 1 means true. So, while condition is true. Hence printf statement will print 1

In second iteration:
a+ 1 = 2 (True)
So ternary operator will return --a i.e. 0
In C program zero means false so while condition is false. Hence, program control will come out of the while loop.

OUTPUT: 1

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE
Question 4
What is the output of the following program?
#include < stdio.h > 
int main(){
    int n=6,i;
    for(i=0; i < n; i+=3){
         printf("Mock ");
         continue;
         printf("Test ");
    }
    return 0;
}
A
Mock Mock Test
B
Mock Test Mock Test
C
Mock Mock
D
Mock Test test
   C Programming
Question 4 Explanation: 
First iteration:
i = 0
i < n i.e. 0 < 6 i.e. if loop condition is true.
Hence printf statement will print: Mock
Due to continue keyword program control will come at the beginning of the for loop and value of variable i will be:
i += 3
i = i + 3 = 3

Second iteration:
i = 3
i < n i.e. 3 < 6 i.e. if loop condition is true.
Hence printf statement will print: Mock
Due to continue keyword program control will come at the beginning of the for loop and value of variable i will be:
i += 3
i = i + 3 = 6

Third iteration:
i = 6
i < n i.e. 6 < 6 i.e. if loop condition is false.
Hence program control will come out of the for loop.

OUTPUT: Mock Mock

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE
Question 5
How many times "practicepaper" will be printed in the output of the following program?
#include < stdio.h > 
int main(){
    int i,j;
    for(i=0;i<5;i++)
    {
        for (j=0;j<5;j++)
         {
            printf("practicepaper");
            break;
         }
    }
    return 0;
}
A
25
B
5
C
1
D
6
   C Programming
Question 5 Explanation: 
break keyword breaks only current loop only. So inner for loop with variable j will executes only one time and the due to break statement it will transfer control out of that loop. Keep in mod that it will not affect to the outer loop with variable i.
Therefore, total 5 times practicepaper will be printed.

OUTPUT: 5

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE


There are 5 questions to complete.

Leave a Comment