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
Question 6
What is the output of the following program?
#include < stdio.h > 
int main()
{
  int a = 1;
  int b ;
  b = (a=a+4, a%5);
  printf("%d",b);
  return 0;
}
A
5
B
1
C
0
D
Compiler Error
   C Programming
Question 6 Explanation: 
Given expression from left a=a+4=5 gives a=5. Now, 2nd expression a%5=5%5=0. Finally b=0.

OUTPUT: 0

Click to Join Our Telegram Group for Latest Update of MOCK TEST
Question 7
What is the output of the following program?
#include < stdio.h > 
int main()
{
  int a = 0;
  int b;
  b = (a=a+1, a=a+2, a=a+3);
  printf("%d",b);
  return 0;
}
A
0
B
1
C
3
D
6
   C Programming
Question 7 Explanation: 
Given expressions evaluated from left to right, a=a+1 gives a=1; 2nd expression a=a+2 gives a=3; 3rd expression a=a+3 gives a=6.
So finally b=(1,3,6) transfer 6 into variable b.

OUTPUT: 6

Click to Join Our Telegram Group for Latest Update of MOCK TEST
There are 7 questions to complete.

1 thought on “Arithmetic Operator Mock Test -1”

Leave a Comment

Like this FREE website? Please share it among all your friends and join the campaign of FREE Education to ALL.