You are on page 1of 21

C Operators; Questions/ Answers

1. What will be the output of the program? #include<stdio.h> int main() { int i=-3, j=2, k=0, m; m = ++i && ++j && ++k; printf("%d, %d, %d, %d\n", i, j, k, m); return 0; } A.-2, 3, 1, 1 C. 1, 2, 3, 1 Answer & Explanation Answer: Option A Explanation: Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively. Step 2: m = ++i && ++j && ++k; becomes m = -2 && 3 && 1; becomes m = TRUE && TRUE; Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1. Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of i,j,k are increemented by '1'(one). Hence the output is "-2, 3, 1, 1". B. 2, 3, 1, 2 D.3, 3, 1, 2

2. Assuming, integer is 2 byte, What will be the output of the program? #include<stdio.h>

int main() { printf("%x\n", -2<<2); return 0; } A.ffff C. fff8 Answer & Explanation Answer: Option C Explanation: The integer value 2 is represented as 00000000 00000010 in binary system. Negative numbers are represented in 2's complement method. 1's complement of 00000000 00000010 is 11111111 11111101 (Change all 0s to 1 and 1s to 0). 2's complement of 00000000 00000010 is 11111111 11111110 (Add 1 to 1's complement to obtain the 2's complement value). Therefore, in binary we represent -2 as: 11111111 11111110. After left shifting it by 2 bits we obtain: 11111111 11111000, and it is equal to "fff8" in hexadecimal system. B. 0 D.Error

3. What will be the output of the program? #include<stdio.h>

int main() { int i=-3, j=2, k=0, m; m = ++i || ++j && ++k; printf("%d, %d, %d, %d\n", i, j, k, m); return 0; } A.2, 2, 0, 1 C. -2, 2, 0, 0 Answer & Explanation Answer: Option D Explanation: Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively. Step 2: m = ++i || ++j && ++k; here (++j && ++k;) this code will not get executed because ++i has nonzero value. becomesm = -2 || ++j && ++k; becomes m = TRUE || ++j && ++k; Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1. Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of variable 'i' only increemented by '1'(one). The variable j,k are not increemented. Hence the output is "-2, 2, 0, 1". B. 1, 2, 1, 0 D.-2, 2, 0, 1

4. What will be the output of the program? #include<stdio.h> int main()

{ int x=12, y=7, z; z = x!=4 || y == 2; printf("z=%d\n", z); return 0; } A.z=0 C. z=4 Answer & Explanation Answer: Option B Explanation: Step 1: int x=12, y=7, z; here variable x, y and z are declared as an integer and variable x and y are initialized to 12, 7 respectively. Step 2: z = x!=4 || y == 2; becomes z = 12!=4 || 7 == 2; then z = (condition true) || (condition false); Hence it returns 1. So the value of z=1. Step 3: printf("z=%d\n", z); Hence the output of the program is "z=1". B. z=1 D.z=2

5. What will be the output of the program? #include<stdio.h> int main() { static int a[20]; int i = 0; a[i] = i ;

printf("%d, %d, %d\n", a[0], a[1], i); return 0; } A.1, 0, 1 C. 0, 0, 0 Answer & Explanation Answer: Option C Explanation: Step 1: static inta[20]; here variable a is declared as an integer type and static. If a variable is declared as static and it willne automatically initialized to value '0'(zero). Step 2: int i = 0; here vaiableiis declared as an integer type and initialized to '0'(zero). Step 3: a[i] = i ; becomes a[0] = 0; Step 4: printf("%d, %d, %d\n", a[0], a[1], i); Here a[0] = 0, a[1] = 0(because all staic variables are initialized to '0') and i = 0. Step 4: Hence the output is "0, 0, 0". What will be the output of the program? #include<stdio.h> int main() { int i=4, j=-1, k=0, w, x, y, z; w = i || j || k; x = i && j && k; y = i || j &&k; z = i && j || k; printf("%d, %d, %d, %d\n", w, x, y, z); return 0; } B. 1, 1, 1 D.0, 1, 0

A.1, 1, 1, 1 C. 1, 0, 0, 1 Answer & Explanation Answer: Option D Explanation:

B. 1, 1, 0, 1 D.1, 0, 1, 1

Step 1: int i=4, j=-1, k=0, w, x, y, z; here variable i, j, k, w, x, y, z are declared as an integer type and the variable i, j, k are initialized to 4, -1, 0 respectively. Step 2: w = i || j || k; becomes w = 4 || -1 || 0;. Hence it returns TRUE. So, w=1 Step 3: x = i && j && k; becomes x = 4 && -1 && 0; Hence it returns FALSE. So, x=0 Step 4: y = i || j &&k; becomes y = 4 || -1 && 0; Hence it returns TRUE. So, y=1 Step 5: z = i && j || k; becomes z = 4 && -1 || 0; Hence it returns TRUE. So, z=1. Step 6: printf("%d, %d, %d, %d\n", w, x, y, z); Hence the output is "1, 0, 1, 1".

7. What will be the output of the program? #include<stdio.h> int main() { int i=-3, j=2, k=0, m; m = ++i && ++j || ++k; printf("%d, %d, %d, %d\n", i, j, k, m); return 0; } A.1, 2, 0, 1 C. -2, 3, 0, 1 B. -3, 2, 0, 1 D.2, 3, 1, 1

Answer & Explanation Answer: Option C Explanation: Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, kare initialized to -3, 2, 0 respectively. Step 2: m = ++i && ++j || ++k; becomes m = (-2 && 3) || ++k; becomes m = TRUE || ++k;. (++k) is not executed because (-2 && 3) alone return TRUE. Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1. Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of i,j are increemented by '1'(one). Hence the output is "-2, 3, 0, 1".

8. What will be the output of the program? #include<stdio.h> int main() { int x=4, y, z; y = --x; z = x--; printf("%d, %d, %d\n", x, y, z); return 0; } A.4, 3, 3 C. 3, 3, 2 B. 4, 3, 2 D.2, 3, 3

Answer & Explanation Answer: Option D Explanation: Step 1: int x=4, y, z; here variable x, y, z are declared as an integer type and variable x is initialized to 4. Step 2: y = --x; becomes y = 3; because (--x) is pre-increement operator. Step 3: z = x--; becomes z = 3;. In the next step variable x becomes 2, because (x--) is post-increement operator. Step 4: printf("%d, %d, %d\n", x, y, z); Hence it prints "2, 3, 3".

9. What will be the output of the program? #include<stdio.h> int main() { int i=3; i = i++; printf("%d\n", i); return 0; } A.3 C. 5 Answer & Explanation Answer: Option B Explanation: No answer description available for this question. Let us discuss. B. 4 D.6

10. What will be the output of the program? #include<stdio.h> int main() { int a=100, b=200, c; c = (a == 100 || b > 200); printf("c=%d\n", c); return 0; } A.c=100 C. c=1 Answer & Explanation Answer: Option C Explanation: Step 1: int a=100, b=200, c; Step 2: c = (a == 100 || b > 200); becomes c = (100 == 100 || 200 > 200); becomes c = (TRUE || FALSE); becomes c = (TRUE);(ie. c = 1) Step 3: printf("c=%d\n", c); It prints the value of variable i=1 Hence the output of the program is '1'(one). 11. What will be the output of the program? #include<stdio.h> int main() { int x=55; B. c=200 D.c=300

printf("%d, %d, %d\n", x<=55, x=40, x>=10); return 0; } A.1, 40, 1 C. 1, 55, 0 Answer & Explanation Answer: Option A Explanation: Step 1: int x=55; here variable x is declared as an integer type and initialized to '55'. Step 2: printf("%d, %d, %d\n", x<=55, x=40, x>=10); here x<=55 returns TRUE hence it prints '1'. x=40 here x is assigned to 40 Hence it prints '40'. x>=10 returns TRUE. hence it prints '1'. Step 3: Hence the output is "1, 40, 1". B. 1, 55, 1 D.1, 1, 1

12. What will be the output of the program? #include<stdio.h> int main() { int i=2; printf("%d, %d\n", ++i, ++i); return 0; } A. 3, 4 B. 4, 3 C. 4, 4

D.Output may vary from compiler to compiler Answer & Explanation Answer: Option D Explanation: The order of evaluation of arguments passed to a function call is unspecified. Anyhow, we consider ++i, ++i are Right-to-Left associativity. The output of the program is 4, 3. In TurboC, the output will be 4, 3. In GCC, the output will be 4, 4.

13. What will be the output of the program? #include<stdio.h> int main() { int k, num=30; k = (num>5 ? (num<=10 ? 100 : 200): 500); printf("%d\n", num); return 0; } A.200 C. 100 Answer & Explanation Answer: Option B Explanation: Step 1: int k, num=30; here variable k and num are declared as an integer type and variable num is initialized to '30'. B. 30 D.500

Step 2: k = (num>5 ? (num<=10 ? 100 : 200): 500); This statement does not affect the output of the program. Because we are going to print the variable num in the next statement. So, we skip this statement. Step 3: printf("%d\n", num); It prints the value of variable num '30' Step 3: Hence the output of the program is '30'

14. What will be the output of the program? #include<stdio.h> int main() { char ch; ch = 'A'; printf("The letter is"); printf("%c", ch>= 'A' &&ch<= 'Z' ? ch + 'a' - 'A':ch); printf("Now the letter is"); printf("%c\n", ch>= 'A' &&ch<= 'Z' ? ch : ch + 'a' - 'A'); return 0; } The letter is a A. Now the letter is A C. Error Answer & Explanation Answer: Option A Explanation: Step 1: char ch; ch = 'A'; here variable ch is declared as an character type an initialized to 'A'. Step 2: printf("The letter is"); It prints "The letter is". B. The letter is A Now the letter is a

D.None of above

Step 3: printf("%c", ch>= 'A' &&ch<= 'Z' ? ch + 'a' - 'A':ch); The ASCII value of 'A' is 65 and 'a' is 97. Here => ('A' >= 'A' && 'A' <= 'Z') ? (A + 'a' - 'A'):('A') => (TRUE && TRUE) ? (65 + 97 - 65) : ('A') => (TRUE) ? (97): ('A') In printf the format specifier is '%c'. Hence prints 97 as 'a'. Step 4: printf("Now the letter is"); It prints "Now the letter is". Step 5: printf("%c\n", ch>= 'A' &&ch<= 'Z' ? ch : ch + 'a' - 'A'); Here => ('A' >= 'A' && 'A' <= 'Z') ? ('A') : (A + 'a' - 'A') => (TRUE && TRUE) ? ('A') :(65 + 97 - 65) => (TRUE) ? ('A') : (97) It prints 'A' Hence the output is The letter is a Now the letter is A

15. What will be the output of the program? #include<stdio.h> int main() { int i=2; int j = i + (1, 2, 3, 4, 5); printf("%d\n", j);

return 0; } A.4 C. 6 Answer & Explanation Answer: Option B Explanation: Because, comma operator used in the expression i (1, 2, 3, 4, 5). The comma operator has left-right associativity. The left operand is always evaluated first, and the result of evaluation is discarded before the right operand is evaluated. In this expression 5 is the right most operand, hence after evaluating expression (1, 2, 3, 4, 5) the result is 5, which on adding to i results into 7. B. 7 D.5

True/ False Questions 1. Associativity has no role to play unless the precedence of operator is same. A.True Answer & Explanation Answer: Option A Explanation: Associativity is only needed when the operators in an expression have the same precedence. Usually + and - have the same precedence. Consider the expression 7 - 4 + 2. The result could be either (7 - 4) + 2 = 5 or 7 - (4 + 2) = 1. The former result corresponds to the case when + and - are left-associative, the latter to when + and - are rightassociative. Usually the addition, subtraction, multiplication, and division operators are left-associative, while the exponentiation, assignment and conditional operators are right-associative. To prevent cases where operands would be associated with two operators, or no operator at all, operators with the same precedence must have the same associativity. B.False

2. The expression of the right hand side of || operators doesn't get evaluated if the left hand side determines the outcome. A.True Answer & Explanation Answer: Option A Explanation: Because, if a is non-zero then b will not be evaluated in the expression (a || b) B.False

3. In the expression a=b=5 the order of Assignment is NOT decided by Associativity of operators A.True Answer & Explanation Answer: Option B Explanation: The equal to = operator has Right-to-Left Associativity. So it assigns b=5 then a=b. B.False

4. Associativity of an operator is either Left to Right or Right to Left. A.True Answer & Explanation Answer: Option A Explanation: Yes, the associativity of an operator is either Left to Right or Right to Left. B.False

YES/ NO Questions 1. Are the following two statement same? 1. a <= 20 ? b = 30: c = 30; 2. (a <=20) ? b : c = 30; A.Yes Answer & Explanation Answer: Option B Explanation: No, the expressions 1 and 2 are not same. 1. a <= 20 ? b = 30: c = 30; This statement can be rewritten as, B.No

if(a <= 20) { b = 30; } else { c = 30; } 2. (a <=20) ? b : c = 30; This statement can be rewritten as,

if(a <= 20) { //Nothing here }

else { c = 30; }

2. Two different operators would always have different Associativity. A.Yes Answer & Explanation Answer: Option B Explanation: No, Two different operators may have same associativity. Example: Arithmetic operators like ++, -- having Right-to-Left associativity. Relational operators like >, >= also have Left-to-Right associativity. B.No

3. Will the expression *p = p be disallowed by the compiler? A.Yes Answer & Explanation Answer: Option B Explanation: Because, here even though the value of p is accessed twice it is used to modify two different objects p and *p B.No

4. Every operator has an Associativity A.Yes Answer & Explanation Answer: Option A Explanation: Yes, Each and every operator has an associativity. The associativity (or fixity) of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses. Operators may be left-associative, rightassociative or non-associative. B.No

General Questions 1. Which of the following is the correct order of evaluation for the below expression? z=x+y*z/4%2-1 A.* / % + - = C. / * % - + = Answer & Explanation Answer: Option A Explanation: C uses left associativity for evaluating expressions to break a tie between two operators having same precedence. View Answer Online Compiler Report Discuss in Forum B. = * / % + D.* % / - + =

2. Which of the following correctly shows the hierarchy of arithmetic operations in C? A./ + * C. + - / * Answer & Explanation Answer: Option D B. * - / + D./ * + -

Explanation: Simply called as BODMAS (Bracket of Division, Multiplication, Addition and Subtraction). How Do I Remember ? BODMAS ! B - Brackets first O - Orders (ie Powers and Square Roots, etc.) DM - Division and Multiplication (left-to-right) AS - Addition and Subtraction (left-to-right) View Answer Online Compiler Report Discuss in Forum

3. Which of the following is the correct usage of conditional operators used in C? A.a>b ? c=30 : c=40; C. max = a>b ? a>c?a:c:b>c?b:c Answer & Explanation Answer: Option C Explanation: Option A: assignment statements are always return in paranthesis in the case of conditional operator. It should be a>b? (c=30):(c=40); Option B: it is syntatically wrong. Option D: syntatically wrong, it should be return(a>b ? a:b); Option C: it uses nested conditional operator, this is logic for finding greatest number out of three numbers. View Answer Online Compiler Report Discuss in Forum B. a>b ? c=30; D.return (a>b)?(a:b)

4. Which of the following is the correct order if calling functions in the below code? a = f1(23, 14) * f2(12/4) + f3(); A. f1, f2, f3

B. f3, f2, f1 C. Order may vary from compiler to compiler D.None of above Answer & Explanation Answer: Option C Explanation: Here, Multiplication will happen before the addition, but in which order the functions would be called is undefined. In an arithmetic expression the parenthesis tell the compiler which operands go with which operators but do not force the compiler to evaluate everything within the parenthesis first. View Answer Online Compiler Report Discuss in Forum

5. Which of the following are unary operators in C? 1. ! 2. sizeof 3. ~ 4. && A.1, 2 C. 2, 4 Answer & Explanation Answer: Option D Explanation: An operation with only one operand is called unary operation. Unary operators: ! Logical NOT operator. ~ bitwise NOT operator. sizeof Size-of operator. && Logical AND is a logical operator. B. 1, 3 D.1, 2, 3

Therefore, 1, 2, 3 are unary operators.

6. In which order do the following gets evaluated 1. Relational 2. Arithmetic 3. Logical 4. Assignment A.2134 C. 4321 Answer & Explanation Answer: Option A Explanation: 2. Arithmetic operators: *, /, %, +, 1. Relational operators: >, <, >=, <=, ==, != 3. Logical operators : !, &&, || 4. Assignment operators: = B. 1234 D.3214

You might also like