Operator
Question 1 |
Which of the following ‘C’ language arithmetic expressions has logical error?
-13 % -5 + 3; | |
4 / (-10 % -2) / 3; | |
3 / (-13 % -5) / 3; | |
-5 % 3 / 13; |
Question 1 Explanation:
→In the option B, -10%-2 gives remainder of “0” and then expression becomes 4/0/3.
→4/0 gives divide by zero error.
→4/0 gives divide by zero error.
Question 2 |
Which of the following operators can be used if a portion of a given bit patterns needs to be copied to a new word, while the remainder of the new word is filled with 0’s?
Logical AND | |
Bitwise AND | |
Bitwise OR | |
Bitwise XOR |
Question 2 Explanation:
Bitwise AND is a binary operator that operates on two equal-length bit patterns. If both bits in the compared position of the bit patterns are 1, the bit in the resulting bit pattern is 1, otherwise 0.
Bitwise OR is also a binary operator that operates on two equal-length bit patterns, similar to bitwise AND. If both bits in the compared position of the bit patterns are 0, the bit in the resulting bit pattern is 0, otherwise 1.
Bitwise XOR also takes two equal-length bit patterns. If both bits in the compared position of the bit patterns are 0 or 1, the bit in the resulting bit pattern is 0, otherwise 1.
Bitwise OR is also a binary operator that operates on two equal-length bit patterns, similar to bitwise AND. If both bits in the compared position of the bit patterns are 0, the bit in the resulting bit pattern is 0, otherwise 1.
Bitwise XOR also takes two equal-length bit patterns. If both bits in the compared position of the bit patterns are 0 or 1, the bit in the resulting bit pattern is 0, otherwise 1.
Question 3 |
In C language, x - = y + 1; means
x=x-y+1 | |
x=-x-y-1 | |
x=-x+y+1 | |
x=x-y-1 |
Question 3 Explanation:
In general, a-=b means a=a-b. So from this we can conclude that correct option is A.
Question 4 |
main( ) { int x; x=3*4%5; printf(“x=%d”,x); }
The output is
x=2 | |
x=2.3
| |
x=5 | |
x=1.9
|
Question 4 Explanation:
‘*’ and ‘%’ operator have the same precedence but the associativity is from left to write. So x = 3*4%5 = 12%5 = 2.
Question 5 |
What is the output of the following C program
#include int main() { int i=5; printf("%d%d%d", i++, i ,++i); return 0; }
7 6 6
| |
6 7 8 | |
6 7 7 | |
5 6 7
|
Question 5 Explanation:
int i=5;
printf("%d%d%d", i++, i ,++i);
In printf() the firstly the calculation is done from right to left and then printing is done from left to right.
So first ++i will increase the value of i by 1 and make it 6. Now the i will be read simply for second calculation.And now i++ will be calculated which will increment the value of i by 1 and make it 7, but since it is post increment so first 6 will be printed and then increment will be done and then for rest two 7 and 7 will be printed .Hence the output will be 6,7,7.
printf("%d%d%d", i++, i ,++i);
In printf() the firstly the calculation is done from right to left and then printing is done from left to right.
So first ++i will increase the value of i by 1 and make it 6. Now the i will be read simply for second calculation.And now i++ will be calculated which will increment the value of i by 1 and make it 7, but since it is post increment so first 6 will be printed and then increment will be done and then for rest two 7 and 7 will be printed .Hence the output will be 6,7,7.
Question 6 |
Consider the following conditional code, which returns a Boolean values
if ((x>25) && (y>100))
return 'false';
else if((x<=25) && (y<=100)
return 'true';
else if((x>25) && (y<=100)
return 'false';
else
return 'true';
simplify it by filling in the following blank with a single Boolean expression without changing the behaviour of the conditional code.
if( ...........)
return 'true';
else
return 'false';
if ((x>25) && (y>100))
return 'false';
else if((x<=25) && (y<=100)
return 'true';
else if((x>25) && (y<=100)
return 'false';
else
return 'true';
simplify it by filling in the following blank with a single Boolean expression without changing the behaviour of the conditional code.
if( ...........)
return 'true';
else
return 'false';
x>25 | |
x<=25 | |
y>100 | |
y<=100 |
Question 6 Explanation:
According to given conditions, if x value is < = 25 means it will return true and for other values it will return false.
Alone y<=100 won't return true, it should be along with x<=25
Alone y<=100 won't return true, it should be along with x<=25
Question 7 |
The condition num!=65 cannot be replaced by
num > 65 || num<65 | |
!(num==65) | |
num-65 | |
!(num-65) |
Question 7 Explanation:
Except !(num-65) can be replaced by num!=65
Question 8 |
Of the following, which is NOT a logical error?
Using the '=', instead of '==' to determine if two values are equal | |
Divide by zero | |
Failing to initialize counter and total variables before the body of loop. | |
Using commas instead of two required semicolon in a for loop header |
Question 9 |
Given i= 0, j = 1, k = –1, x = 0.5, y = 0.0 What is the output of given ‘C’ expression ?
x * 3 && 3 || j | k
x * 3 && 3 || j | k
-1 | |
0 | |
1 | |
2 |
Question 9 Explanation:
Step-1: Evaluate x * 3 because multiplication has more priority than remaining operators x * 3→ 1.5
Step-2: && is logical AND. Both the statements are TRUE, then it returns 1 otherwise 0.
1.5 && 3 is TRUE. So, it returns 1.
Step-3: j | k is bitwise OR operator. It returns -1.
Step-4: ((x * 3) && 3) || (j | k) islogical OR operator. It returns 1
Note: The precedence is ((x * 3) && 3) || (j | k)
Step-2: && is logical AND. Both the statements are TRUE, then it returns 1 otherwise 0.
1.5 && 3 is TRUE. So, it returns 1.
Step-3: j | k is bitwise OR operator. It returns -1.
Step-4: ((x * 3) && 3) || (j | k) islogical OR operator. It returns 1
Note: The precedence is ((x * 3) && 3) || (j | k)
Question 10 |
Find the output of the following “C” code :
main ( )
{
int x=20, y=35;
x= y++ + x++;
y= ++y + ++x;
printf (“%d, %d\n”, x,y);
}
main ( )
{
int x=20, y=35;
x= y++ + x++;
y= ++y + ++x;
printf (“%d, %d\n”, x,y);
}
55, 93 | |
53, 97 | |
56, 95 | |
57, 94 | |
56, 93 |
Question 10 Explanation:
Step-1: Here, we are using post increment for both x and y. In post increment the value assign first and update after assigning.
x= 35 + 20
y= 37 + 56 (In this statement, ‘y’ will become 36 before performing pre increment)
Step-2: Final x=56 and y=93
Note: They given wrong options. We are added correct option.
Excluded for evaluation.
x= 35 + 20
y= 37 + 56 (In this statement, ‘y’ will become 36 before performing pre increment)
Step-2: Final x=56 and y=93
Note: They given wrong options. We are added correct option.
Excluded for evaluation.
Question 11 |
main( )
{
char *str = “abcde”;
printf (“%c”, *str);
printf (“%c”, *str++);
printf (“%c”, *(str++));
printf (“%s”, str);
}
The output of the above ‘C’ code will be :
{
char *str = “abcde”;
printf (“%c”, *str);
printf (“%c”, *str++);
printf (“%c”, *(str++));
printf (“%s”, str);
}
The output of the above ‘C’ code will be :
a a c b c d e | |
a a c c c d e | |
a a b c d e | |
None of these |
Question 11 Explanation:
The above program we are using post increment operators. In post increment the value assign first and update after assigning.
main( )
{
char *str = “abcde”;
printf (“%c”, *str); /* It returns character ‘a’ */
printf (“%c”, *str++); /* It returns character ‘a’ because it is used post increment*/
printf (“%c”, *(str++)); /* It returns character ‘b’ because the previous increment is update here and present also they given post increment, so it only returns ‘b’ */
printf (“%s”, str); /* It returns aabcde */
}
main( )
{
char *str = “abcde”;
printf (“%c”, *str); /* It returns character ‘a’ */
printf (“%c”, *str++); /* It returns character ‘a’ because it is used post increment*/
printf (“%c”, *(str++)); /* It returns character ‘b’ because the previous increment is update here and present also they given post increment, so it only returns ‘b’ */
printf (“%s”, str); /* It returns aabcde */
}