Q 21- Show and describe six different relational operators supported by Java.
A - Java supports the following set of relational operators:
Operator Returns true if > Left operand is greater than right operand
>= Left operand is greater than or equal to right operand
< Left operand is less than right operand
<= Left operand is less than or equal to right operand
== Left operand is equal to right operand
!= Left operand is not equal to right operand
A - Java supports the following set of relational operators:
Operator Returns true if > Left operand is greater than right operand
>= Left operand is greater than or equal to right operand
< Left operand is less than right operand
<= Left operand is less than or equal to right operand
== Left operand is equal to right operand
!= Left operand is not equal to right operand
class prg2 { //define the controlling class
public static void main(String[] args){ //define main method
System.out.println("The relational 6<5>
System.out.println("The relational 6>5 is " + (6>5 ) );
}//end main
}//End class. Note no semicolon required
//End Java application
A - This program produces the following output:
The relational 6<5 is false
The relational 6>5 is true
A - The following three logical or conditional operators are supported by Java.
Operator Typical Use Returns true if
&& Left && Right Left and Right are both true
|| Left || Right Either Left or Right is true
! ! Right Right is false
A - An important characteristic of the behavior of the && and || operators in Java is that the expressions are evaluated from left to right, and the evaluation of the expression is terminated as soon as the result of evaluating the expression can be determined. For example, in the above expression, if the variable a is less than the variable b , there is no need to evaluate the right operand of the || to determine the value of the entire expression. Thus, evaluation will terminate as soon as it is determined that a is less than b.
A - The & operator in Java is a bitwise and and the | operator is a bitwise or.
A - The bitwise and operator is represented by the & symbol in Java.
A - The following table shows the seven bitwise operators supported by Java.
Operator Typical Use Operation >> OpLeft >> Dist Shift bits of OpLeft right by Dist bits (signed)
<< OpLeft << style=""> Shift bits of OpLeft left by Dist bits
>>> OpLeft >>> Dist Shift bits of OpLeft right by Dist bits (unsigned)
& OpLeft & OpRight Bitwise and of the two operands | OpLeft | OpRight Bitwise inclusive or of the two operands ^ OpLeft ^ OpRight Bitwise exclusive or (xor) of the two operands
~ ~ OpRight Bitwise complement of the right operand (unary)
A - False: In Java, the signed right shift operation populates the vacated bits with the sign bit, while the left shift and the unsigned right shift populate the vacated bits with zeros.
A - True: For both Java and C++, bits shifted off the right end are lost.
A - The answer is:
1 and 1 produces 1
1 and 0 produces 0
0 and 1 produces 0
0 and 0 produces 0
No comments:
Post a Comment