August 28, 2007

Java Questions - 3

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
Q 22- Show the output that would be produced by the following Java application:
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
Q23 - Show and describe three operators (frequently referred to as conditional operators in Java and logical operators in C++) which are often combined with relational operators to construct more complex expressions (often called conditional expressions). Hint: The && operator returns true if the left and right operands are both true. What are the other two and how do they behave?
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
Q24 - Describe the special behavior of the || operator in the following expression for the case where the value of the variable a is less than the value of the variable b. (a <>
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.
Q25 - Show the symbols used for the bitwise and operator and the or operator.
A - The & operator in Java is a bitwise and and the | operator is a bitwise or.
Q26 - The logical and operator is represented in Java by the && symbol. What is the representation of the bitwise and operator in Java?
A - The bitwise and operator is represented by the & symbol in Java.
Q27 - Show and describe five operators in Java that perform actions on the operands one bit at a time(bitwise operators).
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)
Q28 - In Java, the signed right shift operation populates the vacated bits with the zeros, while the left shift and the unsigned right shift populate the vacated bits with the sign bit: True or False. If youranswer is False, explain why.
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.
Q29 - In a signed right-shift operation in Java, the bits shifted off the right end are lost: True or False.If your answer is False, explain why.
A - True: For both Java and C++, bits shifted off the right end are lost.
Q30 - Using the symbols 1 and 0 construct a table showing the four possible combinations of 1 and 0.Using a 1 or a 0, show the result of the bitwise and operation on these four combinations of 1 and 0.
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