[go: up one dir, main page]

0% found this document useful (0 votes)
33K views96 pages

Operators

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 96

Which operators are used to compare the values

of operands to produce logical value in C


language?
A Logical operator
B Relational operator
C Assignment operator
D None of the above
Answer: Option [B]
Select the relevant output for the following set of code:
static void Main(string[] args)
{
int a = 4;
int b = 5;
int c = 6;
int d = 8;
if (((a * b / c) + d) >= ((b * c + d ) / a))
{
Console.WriteLine("Line 1 - a is greater to b");
Console.WriteLine((a * b / c) + d);
}
else
{
Console.WriteLine("Line 1 - a is not greater to b");
Console.WriteLine((b * c + d )/ a);
}
}
Options
• a) “Line 1 – a is greater to b”11
b) “Line 1 – a is not greater to b”9
c) Both are equal
d) None of the mentioned
Answer: a
Explanation: Now, here in ‘if’ condition both conditions of parenthesis and
hence evaluating operators based on parenthesis are tested.
for expression :
((a * b / c) + d)
Step 1 : (a*b/c) (Evaluating as 4*5/6 = 3)
Step 2 : ( (a*b/c) + d ) (Evaluating (3 + 8 = 11))
Result : 11
For expression : (b * c + d )/ a
Step 1 : (b*c + d) (Evaluating as 5*6 +8 = 38)
Step 2: (b*c + d) / a (Evaluating as 38 / 4 = 9)
Result : 9
• The relational operator “>=” between both expressions check for largest
figure and hence consecutively executes the if condition.
Output:
• Line 1 - a is greater to b. 11
Select the relevant output for the set of code :
m = 5; int y; 1. y = m++; 2. y = ++m;
a) y = 5, m = 6 ; y = 5, m = 5
b) y = 6, m = 6; y = 7, m = 6
c) y = 5, m = 6; y = 7, m = 7
d) y = 5, m = 6; y = 7, m = 8
Answer: c
Explanation:
step 1 : m = 5, y = m++ i.e y =5 ,m =6.
step 2 : y = ++m ,
Since m = 6 .So, m = 7 on ++m and hence y = 7.
Output : y = 5, m = 6; y =7 , m = 7.
Are logical operators sequence points?
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
• Answer: a
Explanation: None.
• Result of a logical or relational expression in C
is
a) True or False
b) 0 or 1
c) 0 if expression is false and any positive
number if expression is true
d) None of the mentioned
• Answer: b
Explanation: None.
What will be the value of d in the following program?
#include <stdio.h>
int main()
{
int a = 10, b = 5, c = 5;
int d;
d = b + c == a;
printf("%d", d);
}
a) Syntax error
b) 1
c) 5
d) 10
Answer: b
What is the output of this C code?
#include <stdio.h>
int main()
{
int a = 10, b = 5, c = 3;
b != !a;
c = !!a;
printf("%d\t%d", b, c);
}
a) 5 1
b) 0 3
c) 5 3
d) 1 1
• Answer: a
Which among the following is NOT a logical or
relational operator?

a) !=
b) ==
c) ||
d) =
• Answer: d
• Relational operators cannot be used on:
a) structure
b) long
c) strings
d) float
Answer: a
Arithmetic Operators
What is the output of this C code?
#include <stdio.h>
int main()
{
int i = -3;
int k = i % 2;
printf("%d\n", k);
}
a) Compile time error
b) -1
c) 1
d) Implementation define
• Answer: b
What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 3;
int l = i / -2;
int k = i % -2;
printf("%d %d\n", l, k);
return 0;
}
a) Compile time error
b) -1 1
c) 1 -1
d) Implementation defined
• Answer: b
What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 5;
i = i / 3;
printf("%d\n", i);
return 0;
}
a) Implementation defined
b) 1
c) 3
d) Compile time error
• Answer: b
What is the output of this C code?
#include <stdio.h>
int main()
{
int i = -5;
i = i / 3;
printf("%d\n", i);
return 0;
}
a) Implementation defined
b) -1
c) -3
d) Compile time error
• Answer: b
What is the value of x in this C code?
#include <stdio.h>
void main()
{
int x = 5 * 9 / 3 + 9;
}
a) 3.75
b) Depends on compiler
c) 24
d) 3
• Answer: c
What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 5.3 % 2;
printf("Value of x is %d", x);
}
a) Value of x is 2.3
b) Value of x is 1
c) Value of x is 0.3
d) Compile time erro
• Answer: d
What is the output of this C code?
#include <stdio.h>
void main()
{
int y = 3;
int x = 5 % 2 * 3 / 2;
printf("Value of x is %d", x);
}
a) Value of x is 1
b) Value of x is 2
c) Value of x is 3
d) Compile time error
• Answer: a
Logical Operator
 Are logical operators sequence points?
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
View Answer
• Answer: a
• Does logical operators in C language are
evaluated with short circuit?
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
• Answer: a
• Result of a logical or relational expression in C
is
a) True or False
b) 0 or 1
c) 0 if expression is false and any positive
number if expression is true
d) None of the mentioned
• Answer: b
What will be the value of d in the following program?
#include <stdio.h>
int main()
{
int a = 10, b = 5, c = 5;
int d;
d = b + c == a;
printf("%d", d);
}
a) Syntax error
b) 1
c) 5
d) 10
• Answer: b
What is the output of this C code?

    int main()
    {
        int i = -5;
        int k = i %4;
        printf("%d\n", k);
    }
A. Compile time error
B. -1
C. 1
D. None
• Answer: Option B
• What is the output of this C code?

    int main()
    {
        int i = 7;
        i = i / 4;
        printf("%d\n", i);
       return 0;
    }
• A. Run time error
• B. 1
• C. 3
• D. Compile time error
• Answer: Option B
What is the output of this C code?
#include <stdio.h>
int main()
{
int a = 1, b = 1, d = 1;
printf("%d, %d, %d", ++a + ++a+a++, a++ + ++b, ++d + d++
+ a++);
}
a) 15, 4, 5
b) 9, 6, 9
c) 9, 3, 5
d) Undefined (Compiler Dependent)
• Answer: d
• For which of the following, “PI++;” code will
fail?
a) #define PI 3.14
b) char *PI = “A”;
c) float PI = 3.14;
d) none of the Mentioned
• Answer: a
What is the output of this C code?
#include <stdio.h>
int main()
{
int a = 10, b = 10;
if (a = 5)
b--;
printf("%d, %d", a, b--);
}
a) a = 10, b = 9
b) a = 10, b = 8
c) a = 5, b = 9
d) a = 5, b = 8
• Answer: c
What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 0;
int j = i++ + i;
printf("%d\n", j);
}
a) 0
b) 1
c) 2
d) Compile time error
• Answer: a
What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 2;
int j = ++i + i;
printf("%d\n", j);
}
a) 6
b) 5
c) 4
d) Compile time error
• Answer: a
Comment on the output of this C code?
#include <stdio.h>
int main()
{
int i = 2;
int i = i++ + i;
printf("%d\n", i);
}
a) = operator is not a sequence point
b) ++ operator may return value with or without side
effects
c) it can be evaluated as (i++)+i or i+(++i)
d) = operator is a sequence point
• Answer: a
• The precedence of arithmetic operators is
(from highest to lowest)
a) %, *, /, +, –
b) %, +, /, *, –
c) +, -, %, *, /
d) %, +, -, *, /
• Answer: a
• . Which of the following is not an arithmetic
operation?
a) a *= 10;
b) a /= 10;
c) a != 10;
d) a %= 10;
• Answer: c
• Which of the following data type will throw an
error on modulus operation(%)?
a) char
b) short
c) int
d) float
• Answer: d
What is the output of this C code?
#include <stdio.h>
int main()
{
int a = 10;
double b = 5.6;
int c;
c = a + b;
printf("%d", c);
}
a) 15
b) 16
c) 15.6
d) 10
• Answer: a
What is the output of this C code?
#include <stdio.h>
int main()
{
int a = 10, b = 5, c = 5;
int d;
d = a == (b + c);
printf("%d", d);
}
a) Syntax error
b) 1
c) 10
d) 5
• Answer: b
Assignment Opertor
What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 0;
if (x = 0)
printf("Its zero\n");
else
printf("Its not zero\n");
}
a) Its not zero
b) Its zero
c) Run time error
d) None
• Answer: a
What is the output of this C code?
#include <stdio.h>
void main()
{
int k = 8;
int x = 0 == 1 && k++;
printf("%d%d\n", x, k);
}
a) 0 9
b) 0 8
c) 1 8
d) 1 9
• Answer: b
What is the output of this C code?
#include <stdio.h>
void main()
{
char a = 'a';
int x = (a % 10)++;
printf("%d\n", x);
}
a) 6
b) Junk value
c) Compile time error
d) 7
• Answer: c
What is the output of this C code?
#include <stdio.h>
void main()
{
1 < 2 ? return 1: return 2;
}
a) returns 1
b) returns 2
c) Varies
d) Compile time error
• Answer: d
What is the output of this C code?
#include <stdio.h>
void main()
{
unsigned int x = -5;
printf("%d", x);
}
a) Run time error
b) Aries
c) -5
d) 5
• Answer: c
 What is the output of this C code?
#include <stdio.h>
int main()
{
int x = 2, y = 1;
x *= x + y;
printf("%d\n", x);
return 0;
}
a) 5
b) 6
c) Undefined behaviour
d) Compile time error
• Answer: b
What is the output of this C code?
#include <stdio.h>
int main()
{
int x = 2, y = 2;
x /= x / y;
printf("%d\n", x);
return 0;
}
a) 2
b) 1
c) 0.5
d) Undefined behaviour
• Answer: a
What is the output of this C code?
#include <stdio.h>
int main()
{
int x = 1, y = 0;
x &&= y;
printf("%d\n", x);
}
a) Compile time error
b) 1
c) 0
d) Undefined behaviour
• Answer: a
What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 0;
int x = i++, y = ++i;
printf("%d % d\n", x, y);
return 0;
}
a) 0, 2
b) 0, 1
c) 1, 2
d) Undefined
• Answer: a
What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 10;
int *p = &i;
printf("%d\n", *p++);
}
a) 10
b) 11
c) Garbage value
d) Address of i
• Answer: a
What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 97;
int y = sizeof(x++);
printf("X is %d", x);
}
a) X is 97
b) X is 98
c) X is 99
d) Run time error
• Answer: a
 What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 4, y, z;
y = --x;
z = x--;
printf("%d%d%d", x, y, z);
}
a) 3 2 3
b) 2 3 3
c) 3 2 2
d) 2 3 4
• Answer: b
 What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 4;
int *p = &x;
int *k = p++;
int r = p - k;
printf("%d", r);
}
a) 4
b) 8
c) 1
d) Run time error
• Answer: c
What is the output of this C code?
#include <stdio.h>
void main()
{
int a = 5, b = -7, c = 0, d;
d = ++a && ++b || ++c;
printf("\n%d%d%d%d", a, b, c, d);
}
a) 6 -6 0 0
b) 6 -5 0 1
c) -6 -6 0 1
d) 6 -6 0 1
• Answer: d

You might also like