Operators

Question 1
#include "stdio.h"
int main() 
{ 
  int x, y = 5, z = 5; 
  x = y == z; 
  printf("%d", x); 
  
  getchar(); 
  return 0; 
}
A
0
B
1
C
5
D
Compiler Error
Operators    
Discuss it


Question 1 Explanation: 
The crux of the question lies in the statement x = y==z. The operator == is executed before = because precedence of comparison operators (<=, >= and ==) is higher than assignment operator =. The result of a comparison operator is either 0 or 1 based on the comparison result. Since y is equal to z, value of the expression y == z becomes 1 and the value is assigned to x via the assignment operator.
Question 2
 

#include <stdio.h>

int main()
{
    int i = 1, 2, 3;
    
    printf("%d", i);
    
    return 0;
}


A
1
B
3
C
Garbage value
D
Compile time error
Operators    
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
#include <stdio.h>

int main()
{
    int i = (1, 2, 3);
    
    printf("%d", i);
    
    return 0;
}

A
1
B
3
C
Garbage value
D
Compile time error
Operators    
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
 
#include <stdio.h>

int main()
{
    int i;
    
    i = 1, 2, 3;
    printf("%d", i);
    
    return 0;
}
A
1
B
3
C
Garbage value
D
Compile time error
Operators    
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
#include <stdio.h>
int main()
{
    int i = 3;
    printf("%d", (++i)++);
    return 0;
}
What is the output of the above program?
A
3
B
4
C
5
D
Compile-time error
Operators    
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
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;
}
A
1 2 1 1
B
1 1 2 1
C
1 2 2 1
D
1 2 2 2
Operators    
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
#include <stdio.h>
int main()
{
    int i = 5, j = 10, k = 15;
    printf("%d ", sizeof(k /= i + j));
    printf("%d", k);
    return 0;
}
Assume size of an integer as 4 bytes. What is the output of above program?
A
4 1
B
4 15
C
2 1
D
Compile-time error
Operators    
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
#include <stdio.h>
int main()
{
    //Assume sizeof character is 1 byte and sizeof integer is 4 bytes
    printf("%d", sizeof(printf("GeeksQuiz")));
    return 0;
}
A
GeeksQuiz4
B
4GeeksQuiz
C
GeeksQuiz9
D
4
E
Compile-time error
Operators    
Discuss it


Question 8 Explanation: 
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. sizeof operator returns sizeof(int).
Question 9
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;
}
A
GeeksQuiz
B
QuizGeeks
C
Compiler Dependent
D
Compiler Error
Operators    
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”.
Question 10
What is the output of following program?
#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
a = 0, b = 1, c = 1, d = 0
B
a = 0, b = 0, c = 1, d = 0
C
a = 1, b = 1, c = 1, d = 1
D
a = 0, b = 0, c = 0, d = 0
Operators    
Discuss it


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;
There are 31 questions to complete.

Company Wise Coding Practice    Topic Wise Coding Practice