geek-quiz-Operators--A
Discuss it
Discuss it
Discuss it
What is the output of the above program?
Discuss it
Discuss it
Assume size of an integer as 4 bytes. What is the output of above program?
Discuss it
Discuss it
An expression doesn't get evaluated inside sizeof operator. GeeksQuiz will not be printed.printf returns the number of characters to be printed i.e. 9 which is an integer value. sizeofoperator returns sizeof(int).
Discuss it
if --a than answer will be 0,1,1,0 . but due to post fix nature if get exicuted late
Discuss it
Discuss it
Discuss it
Discuss it
Discuss it
Discuss it
Discuss it
Discuss it
Question 2
WRONG
|
#include <stdio.h> int main() { int i = 1, 2, 3; printf ( "%d" , i); return 0; } |
1
| |
3
| |
Garbage value
| |
Compile time error
|
Discuss it
Question 2 Explanation:
Comma acts as a separator here. The compiler creates an integer variable and initializes it with 1. The compiler fails to create integer variable 2 because 2 is not a valid identifier.
Question 3
CORRECT
|
#include <stdio.h> int main() { int i = (1, 2, 3); printf ( "%d" , i); return 0; } |
1
| |
3
| |
Garbage value
| |
Compile time error
|
Discuss it
Question 3 Explanation:
The bracket operator has higher precedence than assignment operator. The expression within bracket operator is evaluated from left to right but it is always the result of the last expression which gets assigned.
Question 4
WRONG
|
#include <stdio.h> int main() { int i; i = 1, 2, 3; printf ( "%d" , i); return 0; } |
1
| |
3
| |
Garbage value
| |
Compile time error
|
Discuss it
Question 4 Explanation:
Comma acts as an operator. The assignment operator has higher precedence than comma operator. So, the expression is considered as (i = 1), 2, 3 and 1 gets assigned to variable i.
Question 5
WRONG
|
#include <stdio.h> int main() { int i = 3; printf ( "%d" , (++i)++); return 0; } |
3
| |
4
| |
5
| |
Compile-time error
|
Discuss it
Question 5 Explanation:
In C, prefix and postfix operators need l-value to perform operation and return r-value. The expression (++i)++ when executed increments the value of variable i(i is a l-value) and returns r-value. The compiler generates the error(l-value required) when it tries to post-incremeny the value of a r-value.
Question 6
WRONG
|
What is the output of below program?
#include <stdio.h> int foo( int * a, int * b) { int sum = *a + *b; *b = *a; return *a = sum - *b; } int main() { int i = 0, j = 1, k = 2, l; l = i++ || foo(&j, &k); printf ( "%d %d %d %d" , i, j, k, l); return 0; } |
1 2 1 1
| |
1 1 2 1
| |
1 2 2 1
| |
1 2 2 2
|
Discuss it
Question 6 Explanation:
The control in the logical OR goes to the second expression only if the first expression results in FALSE. The function foo() is called because i++ returns 0(post-increment) after incrementing the value of i to 1. The foo() function actually swaps the values of two variables and returns the value of second parameter. So, values of variables j and k gets exchanged and OR expression evaluates to be TRUE.
Question 7
WRONG
|
#include <stdio.h> int main() { int i = 5, j = 10, k = 15; printf ( "%d " , sizeof (k /= i + j)); printf ( "%d" , k); return 0; } |
4 1
| |
4 15
| |
2 1
| |
Compile-time error
|
Discuss it
Question 7 Explanation:
The main theme of the program lies here: sizeof(k /= i + j). An expression doesn't get evaluated inside sizeof operator. sizeof operator returns sizeof(int) because the result of expression will be an integer. As the expression is not evaluated, value of k will not be changed.
Question 8
CORRECT
|
#include <stdio.h> int main() { //Assume sizeof character is 1 byte and sizeof integer is 4 bytes printf ( "%d" , sizeof ( printf ( "GeeksQuiz" ))); return 0; } |
GeeksQuiz4
| |
4GeeksQuiz
| |
GeeksQuiz9
| |
4
| |
Compile-time error
|
Discuss it
Question 8 Explanation:
Question 9
WRONG
|
Output of following program?
#include <stdio.h> int f1() { printf ( "Geeks" ); return 1;} int f2() { printf ( "Quiz" ); return 1;} int main() { int p = f1() + f2(); return 0; } |
GeeksQuiz
| |
QuizGeeks
| |
Compiler Dependent
| |
Compiler Error
|
Discuss it
Question 9 Explanation:
The operator ‘+’ doesn't have a standard defined order of evaluation for its operands. Either f1() or f2() may be executed first. So a compiler may choose to output either “GeeksQuiz” or “QuizGeeks”.
#include <stdio.h> int main() { int a = 1; int b = 1; int c = a || --b; int d = a-- && --b; printf ( "a = %d, b = %d, c = %d, d = %d" , a, b, c, d); return 0; } |
a = 0, b = 1, c = 1, d = 0
| |
a = 0, b = 0, c = 1, d = 0
| |
a = 1, b = 1, c = 1, d = 1
| |
a = 0, b = 0, c = 0, d = 0
|
Question 10 Explanation:
Let us understand the execution line by line. Initial values of a and b are 1.
// Since a is 1, the expression --b is not executed because // of the short-circuit property of logical or operator // So c becomes 1, a and b remain 1 int c = a || --b; // The post decrement operator -- returns the old value in current expression // and then updates the value. So the value of expression --a is 1. Since the // first operand of logical and is 1, shortcircuiting doesn't happen here. So // the expression --b is executed and --b returns 0 because it is pre-increment. // The values of a and b become 0, and the value of d also becomes 0. int d = a-- && --b;
uestion 11
CORRECT
|
#include <stdio.h> int main() { int a = 10, b = 20, c = 30; if (c > b > a) printf ( "TRUE" ); else printf ( "FALSE" ); return 0; } |
TRUE
| |
FALSE
| |
Compiler Error
| |
Output is compiler dependent
|
since > is left associative
Question 12
WRONG
|
#include<stdio.h> int main() { char *s[] = { "knowledge" , "is" , "power" }; char **p; p = s; printf ( "%s " , ++*p); printf ( "%s " , *p++); printf ( "%s " , ++*p); return 0; } |
is power
| |
nowledge nowledge s
| |
is ower
| |
nowledge knowledge is
|
Discuss it
Question 12 Explanation:
Let us consider the expression ++*p in first printf(). Since precedence of prefix ++ and * is same, associativity comes into picture. *p is evaluated first because both prefix ++ and * are right to left associative. When we increment *p by 1, it starts pointing to second character of “knowledge”. Therefore, the first printf statement prints “nowledge”. Let us consider the expression *p++ in second printf() . Since precedence of postfix ++ is higher than *, p++ is evaluated first. And since it’s a psotfix ++, old value of p is used in this expression. Therefore, second printf statement prints “nowledge”. In third printf statement, the new value of p (updated by second printf) is used, and third printf() prints “s”.
Predict the output of the below program:
#include <stdio.h> int main() { printf ( "%d" , 1 << 2 + 3 << 4); return 0; } |
112
| |
52
| |
512
| |
0
|
Discuss it
Question 16 Explanation:
The main logic behind the program is the precedence and associativity of the operators. The addition(+) operator has higher precedence than shift(<<) operator. So, the expression boils down to 1 << (2 + 3) << 4 which in turn reduces to (1 << 5) << 4 as the shift operator has left-to-right associativity.
Question 17
WRONG
|
Which of the following can have different meaning in different contexts?
&
| |
*
| |
Both of the above
| |
There are no such operators in C
|
Discuss it
Question 17 Explanation:
'&' can be used to get address of a variable and can also be used to do bitwise and operation. Similarly '*' can be used to get value at an address and can also be used to multiplication.
In C, two integers can be swapped using minimum
0 extra variable
| |
1 extra variable
| |
2 extra variable
|
Discuss it
Question 18 Explanation:
We can swap two variables without any extra variable using bitwise XOR operator '^'. Let X and Y be two variables to be swapped. Following steps swap X and Y.
X = X ^ Y; Y = X ^ Y; X = X ^ Y;
Question 19
WRONG
|
What does the following statement do?
x = x | 1 << n; |
Sets x as 2n
| |
Sets (n+1)th bit of x
| |
Toggles (n+1)th bit of x
| |
Unsets (n+1)th bit of x
|
Discuss it
Question 19 Explanation:
Let n be 3, the value of expression 1 << n would be 8 (In binary 00001000). If we do bitwise or of binary 00001000 with any number x would set its (n+1)th bit from left side.
Question 24
CORRECT
|
#include <stdio.h> int main() { int x = 10; int y = (x++, x++, x++); printf ( "%d %d\n" , x, y); return 0; } |
13 12
| |
13 13
| |
10 10
| |
Compiler Dependent
|
Discuss it
Question 24 Explanation:
The comma operator defines a sequence point, so the option (d) is not correct. All expressions are executed from left to right and the value of rightmost expression is returned by the comma operator.
Question 25
CORRECT
|
#include <stdio.h> int main() { int y = 0; int x = (~y == 1); printf ( "%d" , x); return 0; } |
0
| |
1
| |
A bog negative Number
| |
Compiler Error
|
Discuss it
Question 25 Explanation:
The important thing to note here is ~ is a bitwise not operator. So the value of ~0 would be all 1s in binary representation which means decimal value of ~0 is not 1. Therefore the result of comparison operator becomes 0.
Question 27
WRONG
|
#include <stdio.h> #include <stdlib.h> int top=0; int fun1() { char a[]= { 'a' , 'b' , 'c' , '(' , 'd' }; return a[top++]; } int main() { char b[10]; char ch2; int i = 0; while (ch2 = fun1() != '(' ) { b[i++] = ch2; } printf ( "%s" ,b); return 0; } |
abc(
| |
abc
| |
3 special characters with ASCII value 1
| |
Empty String
|
Discuss it
Question 27 Explanation:
Precedence of '!=' is higher than '='. So the expression "ch2 = fun1() != '('" is treated as "ch2 = (fun1() != '(' )". So result of "fun1() != '(' " is assigned to ch2. The result is 1 fir first three characters. Smile character has ASCII value 1. Since the condition is true for first three characters, you get three smilies. If we, put a bracket in while statement, we get "abc". 1 This modified program prints "abc"
sdlkaskldaskdsa
No comments:
Post a Comment