Wednesday, 30 January 2019

Operator and Expression (Multiple choice type question)

Multiple choice type question
Q.1)
main
{
int x=7 ,y=5;
x=y++ +x++;
y= ++y + ++x;
printf(“%d%d”,x,y)
}
Output:

A) 12 14 B) 9 7 C) 12 20 D) 12 19

Q.2)
#include<stdio.h>

int main()
{
   int x=5,y=15;
   x= x++ + ++y;
   y = ++x + ++y;
   printf("%d %d",x,y);
   return 0;
}
Output:

A) 23 40 B) 22 43 C) 23 42 D) 24 40


Q.3)

#include<stdio.h>

int main ()
{
   int x=2, y=4;
   int z=x+x+x+x+(x++);
   printf("x=%d \n y=%d \n z=%d",x,y,z);
   return 0;
}
OUTPUT
A) x=3, y=4, z=19 B) x=3, y=5, z=10 C) x=2, y=4, z=10 D) x=3, y=4, z=10
Q.4) which one of the following is conditional operator?
A) ?: B)if C)< D)&&

Q.5) What will be the value of a and M after execution of the following code?

int i=5,m ;
m=I++;

A) 5&6 B)5&5 C)6&5 D)6&6


Q.6) Which of the following is a ternary conditional operator? The <<operator is used for
A) && B) if C)<= D) ?


Q.7) The <<operator is used for

A) One return statements B)Two return statements C)More than two return statements D) Left Shift


Q. 8) “C”  allow a three-way transfer of control with the help of

A) Relational operator B) unary operator C) Comparison operators D) Ternary operator


Q.9)The output of
int i=5;
printf("%d%d%d", i,i++,++i);

A) 5 5 7 B) 5 6 7 C)7 6 5 D) 7 6 6


Q.10) Which one of the following is a Bitwise operator?

A) < B) >= C)<< D)&&


Q.11) Which one is the right output?

int x=9;
if(10)
printf("%d",++x);
else
printf("%d",x++);

A) 9 B) 10 C)11 D) 12


Q.12) Which one is the right output?

#define int char
main()
{
int i=65;
printf("sizeof(i)=%d", sizeof(i));
}

A) sizeof(i)=1 B) sizeof(i)=2 C) sizeof(i)= 4 D) sizeof(i)=8


Q.13) Which one is the right output?
main()
{
int i=5, j=6,z;
printf("%d",i+ ++j);
}

A) 12 B) 10 C)11 D)13


Q.14) Which among the following is a special operator?

A) << B) ++ C)sizeof(i) D) ?:


Operators and Expression(Part-5)

1.a) Differentiate post increment and pre increment operator. {WBUT 2014, 2017}
b)Using ternary operator write a macro to find out the absolute value of a number{WBUT}
c)Write A program to find out the absolute value of a number using ternary operator

2. what is the Type casting? what is the automatic type conversion? {WBUT }

Answer: 1.a)



Pre incrementPost increment
1
Pre increment operator is used to increment variable value by 1 before assigning the value to the variable.
Post increment operator is used toincrement variable value by 1 after assigning the value to the variable.
2
Syntax:
a = ++x;
Syntax:
a = x++;
3
Value of i is incremented before assigning it to variable i.
Value of i is incremented after assigning it to variable i.
4

5
Example Pre-increment unary operator:
  • ++i; //it is equivalents to the i=i+1; or i+=1 ;
Programming Code 1:
#include <stdio.h>
int main()
{
int i=5;
printf(“%d\n”,++i);
return 0;
}
Output: 6
Example Post-increment unary operator:
  • ++i;
Programming Code 1:
#include <stdio.h>
int main()
{
int i=5;
printf(“%d\n”,i++);
return 0;
}
Output: 5
6

Answer: 1.b)

#include<stdio.h>
#include<conio.h>

void main()
{
int num,r;
clrscr();
printf(" Enter a number");
scanf("%d",&num);
r=(num>0)?(num):(-num);
printf("The absolute value of %d is %d",num,r);
getch();
}

Output:
Enter a number 9
The absolute value of 9 is 9

Enter a number -9
The absolute value of -9 is 9  

Answer: 1.c)

#include<stdio.h>
#include<conio.h>
#define ABSOLUTE(x)X<0?abs(x):y
int main (void)
{
int x,y,result;
printf(“Enter the value of x”);
scanf(“%d”,&x);
result=ABSOLUTE(x);
printf(“The absolute number is %d”, result);
return EXIT_SUCCESS;
}

Answer: 2)

Typecasting is the way to convert a variable from one datatype to another the datatype.

provide to the facility of mixing different types of variable and constant in expression
in this type of  operation data type of  offer of one operand is converted into data type of another offer and. this is known as type conversion or Type casting.

The mainly two types of Type casting

1) implicity type conversion

char will be converted into int
float Will be converted into int
explicite type conversion
(type)(Expression)
(float)(x+y /2)

2) Automatic type conversion
  • In automatic unary conversion all operands of type char and short will be converted into int Before any operation.
  • Some compilers convert all float operants to double Before any operation


Automatic binary conversion
  •  The rules for automatic binary conversion are as follow
  •  If one operand is long double than the other will be converted to long double and the result     will be long double.
  •  Therwise if one operand is double than the other will be converted into double and the result   will be double.
  •  Otherwise, if one operand is float  then other will be converted to float and the result will be float
  •  Otherwise,  if one operand is unsigned  long int then other will be converted to unsigned long int  and the result will be unsigned lo ng int
  • Otherwise, if one operand is long int and other is unsigned int then
  • If long int can represent all the value of unsigned int then unsigned int will be converted into long int and result will be long int
  • Elss,  both the  operand will be converted to unsigned long int and the result will be unsigned long int. 


Basic Electronics Engineering (EC101)

Basic Electronics Engineering (EC101)