Arithmetic Operator Mock Test -1


Question 1
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int a,b;
    a=1,2,3,4;
    b=(1,2,3,4);
    printf("%d %d",a,b);
    return 0;
}
A
1 1
B
1 4
C
4 4
D
Compiler Error
   C Programming
Question 1 Explanation: 
First evaluate a=1,2,3,4;
In this statement total 2 types of operator are there(= and ,).
Precedence of (=) operator is higher than (,).
Hence, first a=1 gets evaluated by compiler and 1 is assigned to a.
Now, a=1 statement return 1
Therefore, statement become 1,2,3,4.
(,) operator return the right side operand as answer but there is no variable to store this value.

In second statement b=(1,2,3,4)
First (1,2,3,4) evaluted due to brackets and return 4 which gets stored in b.

So answer is 1 4.

OUTPUT: 1 4

Click to Join Our Telegram Group for Latest Update of MOCK TEST
Question 2
How many time Hello 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("Hello\n");
    }
    return 0;
}
A
200
B
10
C
20
D
Compiler Error
   C Programming
Question 2 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 is true.

So This program print Hello 20 times.

OUTPUT: 20 times

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


Question 3
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int i;
    if(i=0,2,4)
        printf("Comment Yes");
    else
        printf("Comment No");
    printf("%d",i);
    return 0;
}
A
Comment Yes0
B
Comment Yes4
C
Comment No0
D
Comment No4
   C Programming
Question 3 Explanation: 
Here condition part of if is i=0,2,4

In i=0,2,4 arithmatic expression (=) has higher priority compared to (,)
So, i=0 gets evaluted first and 0 is stored in i.
After that remaining expression becomes 0,2,4 (i=0 return 0 to original expression)
0,2,4 evaluted using (,) operator as Ex. x,y return y. So 0,2,4 evaluted as 4.
Hence, final condition part of if can be written as if(4).
Any non-zero value is considered as true in C. Hence, condition of if statement is true and as a result Comment Yes0 will be printed by program.

OUTPUT: Comment Yes0

Click to Join Our Telegram Group for Latest Update of MOCK TEST
Question 4
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int a=(printf("practicepaper"),3);
    printf("%d",a);
    return 0;
}
A
practicepaper13
B
practicepaper0
C
practicepaper3
D
Compiler Error
   C Programming
Question 4 Explanation: 
In this program first (printf("practicepaper"),3) gets evaluted by compiler as x,y return y.
NOTE: (,) operator evaluted by compiler with x,y
first execute x then y and finally retun y.
Therefore, first printf function print "practicepaper" and then (printf("practicepaper"),3) return 3 which will be stored in a.

So final outout of program is practicepaper3.

OUTPUT: practicepaper3

Click to Join Our Telegram Group for Latest Update of MOCK TEST
Question 5
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int i=0,j=1,k;
     k=(++i,++j);
    printf("%d%d%d ",i,j,k);
    k=(i++,j++);
    printf("%d%d%d",i,j,k);
    return 0;
}
A
022 033
B
122 333
C
112 223
D
122 232
   C Programming
Question 5 Explanation: 

NOTE: (,) operator evaluted by compiler with x,y
first execute x then y and finally retun y.

In first expression k=(++i,++j);
i become 1, then j become 2 and finaly (++i,++j) return 2 which is stored in k. So k become 2.
Output of first printf is 122

In second expression k=(i++,j++);
i remain 1, then j remain 2 and finaly (i++,j++) return 2 which is stored in k. So k become 2. After that due to post increment on i and j, value of i become 2 and value of j become 3.
Output of second printf is 232

So final output by program is 122 232.

OUTPUT: 122 232

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


There are 5 questions to complete.

2 thoughts on “Arithmetic Operator Mock Test -1”

Leave a Comment