Search Placementyogi.com
 

Tough Difficult Tricky Operators in C Objective Questions and Answers

  :: Topics ::

1

What is the output of the following program?

#include<stdio.h>

void main()
{
        int x=10,y=20;
        x=((x>9) && y=30 ? printf("\nTRUE"): printf("\nFALSE"));
        printf("\ny=%d",y);
}
A.

FALSE

y=20

B.

TRUE

y=30

C.

Compilation Error

D.

None of the Above

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

Doubt: Are statements allowed in (?:)conditional operator ? Well, Yes Ofcourse! but without the semi-colon at the end!

Now, We might think that x>9 is TRUE and y=30 is TRUE Hence TRUE && TRUE becomes TRUE and TRUE is printed on screen, but we encounter

compile time error and the Error we get is :

error: lvalue required as left operand of assignment  

lvalue: Any Variable that can be assigned a value and hence are permitted on the LHS of assignment operator.

rvalue: Any variable or constant whose value can be used in an expression or to assign value to lvalue.

It happens because the associativity of "and" operator "&&" is higher than Assignment Operator "=".

This converts the above statement in to  

((x>9) && y) =30

i.e. TRUE && y = 30 // as y's current value is 20 , a non zero number,so

=> TRUE && TRUE =30

=> TRUE = 30 //  TRUE=1 by default so the expression becomes,

=> 1=30 

And of course, A constant is not allowed on the Left hand side of the assignment operator as it is an rvalue!
To get correct output use parenthesis appropriately:
x=((x>9) && (y=30) ? printf("\nTRUE"): printf("\nFALSE"));

 
2
int x=8,y=1;
switch (x--,y++)
{
    case 1: x *= 8;
    case 2:y *= x /= 2;
    case 3:
    case 4:y--;
    default: x += 5;
}
printf("%d %d", x, y);
A.

64 2

B.

64 1

C.

33 55

D.

33 56

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

"," operator evaluates its left operand, evaluates its right operand and then returns the value of its right operand. So, (x--, y++) will evaluate x-- first then y++ and at last will return the value of y-- i.e. 1(post increment operator).

Latter steps are easy to calculate.

 
 

Banner to LogicGuns.com

Hide Page Information

Operators in C Tough Difficult Tricky objective Questions and Answers

Operators in C Tough Difficult Tricky objective Questions and Answers

Here you can find Operators in C interview questions with answers and explanation. Common Operators are: Arithmetic operators, assignment operator, increment operator,++ operator operator,decrement operator,--,==,||(Logical OR), && (Logical AND), >> Right Shift Operator, << Left Shift Operator, ?: Conditional operator, % modulus operator.

Why Operators in C?

In this section you can learn and practice Operators in C (Questions with Answers) to improve your skills in order to face the interview, competitive examination and various entrance test (CAT, GATE, GRE, MAT, Bank Exam, Railway Exam etc.) with full confidence.

Where can I get Operators in C Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved Operators in C questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand. All students, freshers can download Operators in C quiz questions with answers as PDF files and eBooks.

Where can I get Operators in C Interview Questions and Answers ( Tough Difficult Tricky objective type, multiple choice)?

Here you can find Tough Difficult Tricky objective type Operators in C questions and answers for interview and entrance examination. Multiple choice and true or false type questions are also provided.

How to solve Operators in C problems?

You can easily solve all kind of questions based on Operators in C by practicing the exercises (including shortcut methods to solve problems) given below.