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
Question 6
How many times "practicepaper" will be printed in the output of the following program?
#include < stdio.h > 
int main()
{
    int i=0;
    while(5,4,3,2,1,0)
    {
        if(i++<10)
        break;
        printf("practicepaper");
    }
    return 0;
}
A
5
B
10
C
0
D
Compiler Error
   C Programming
Question 6 Explanation: 
Here, condition part of while loop has expression 5,4,3,2,1,0. This expression has operator (,) which returns the right most value. Here it, retun 0 which will be considerd as false in C compiler. Hence, body of while loop will not executed.

OUTPUT: 0

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

Click Here to Practice ALL Previous MOCK TEST FREE
Question 7
How many times "practicepaper" will be printed in the output of the following program?
#include < stdio.h > 
int main(){
    int i;
    for(i=0;i<10;i++);
    printf("practicepaper");
    return 0;
}
A
10
B
11
C
1
D
Compiler Error
   C Programming
Question 7 Explanation: 
In this program for loop statement is end with (;) which indicates the empty body of for loop. So compiler evaluate the program as
#include < stdio.h > 
int main(){
    int i;
    for(i=0;i<10;i++)
    {
    }
    printf("practicepaper");
    return 0;
}
When i=10, condition part of for loop become false. So, program control will come out of the for loop. Last printf statement print practicepaper.

OUTPUT: 1

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

Click Here to Practice ALL Previous MOCK TEST FREE
Question 8
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 (i=0;i<5;i++)
         {
            printf("practicepaper");
         }
    }
    return 0;
}
A
25
B
5
C
6
D
infinite time
   C Programming
Question 8 Explanation: 
In this program, outer for loop start with i=0, then condition i<5 is true.
So, it executes the body of loop which is inner for loop.
Inner for loop uses same variable i,
it makes i=0, then condition i<5 is true.
so it executes body of loop and print "practicepaper".
this will continue five times. After that for i=5, condition i<5 become false.

Inner loop gives control to outer loop and it executes the increament part of loop as i++, then check the condition i<5 which becomes false. So, total five 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
Question 9
What is the output of the following program?
#include < stdio.h > 
int main(){
    int i=0;
    for(;i+=2,i<5 ;i++)
   {
         printf("%d ",i);
    }
return 0;        
}
A
0 3
B
2 5
C
2
D
Compiler Error
   C Programming
Question 9 Explanation: 
First iteration:
i = 0
i+=2,i<5 i.e. 2 , 2<5 i.e. 2,1 i.e. 1
loop condition is true.
Hence printf statement will print: 2
Then, compiler execute the increment part of for loop
i++ i.e i=3

Second iteration:
i = 3
i+=2,i<5 i.e. 5, 5<5 i.e. 5, 0 i.e. 0
loop condition is false.
Hence, program control will come out of the for loop.

OUTPUT: 2

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

Click Here to Practice ALL Previous MOCK TEST FREE
Question 10
How many time "practicepaper" will be printed in the output of following program?
#include < stdio.h > 
int main()
{
    int i,j;
    for(i=0,j=0;i<10,j<20;i++,j++)
    {
        printf("practicepaper\n");
    }
    return 0;
}
A
10
B
20
C
200
D
Compiler Error
   C Programming
Question 10 Explanation: 
This question is based on the concept of comma operator.

Condition of for loop i<10,j<20 is evaluted as Ex. x,y return y.
Therefore, i<10,j<20 is evaluted as true till j<20.

So This program print Hello 20 times.

OUTPUT: 20 times

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

Click Here to Practice ALL Previous MOCK TEST FREE
There are 10 questions to complete.

Leave a Comment