August 28, 2007

Java Questions - 2

Q11 - Show the symbol for the increment operator.
A - The symbol for the increment operator is two plus signs with nothing between them (++).
Q 12- Describe the appearance and the behavior of the increment operator with both prefix and
postfix notation. Show example, possibly incomplete, code fragments illustrating both notational
forms.
A - The increment operator may be used with both prefix and postfix notation. Basically, the increment operator causes the value of the variable to which it is applied to be increased by one. With prefix notation, the operand appears to the right of the operator ( ++X), while with postfix notation, the operand appears to the left of the operator (X++).
The difference in behavior has to do with the point in time that the increment actually occurs if the operator and its operand appear as part of a larger overall expression. With the prefix version, the variable is incremented before it is used to evaluate the larger overall expression. With the postfix version, the variable is used to evaluate the larger overall expression and then it is incremented.
Q13 - Show the output that would be produced by the following Java application.
class prg1 { //define the controlling class

public static void main(String[] args){ //define main method
int x = 5, X = 5, y = 5, Y = 5;
System.out.println("x = " + x );
System.out.println("X = " + X );
System.out.println("x + X++ = " + (x + X++) );
System.out.println("X = " + X );
System.out.println();
System.out.println("y = " + y );
System.out.println("Y = " + Y );
System.out.println("y + ++Y = " + (y + ++Y) );
System.out.println("Y = " + Y );
}//end main

}//End class. Note no semicolon required
//End Java application
A - The output from this Java application follows:
x = 5
X = 5
x + X++ = 10
X = 6
y = 5
Y = 5
y + ++Y = 11
Y = 6
Q 14 - Binary operators use outfix notation: True or False? If your answer is False, explain why.
A - False: Binary operators use infix notation, which means that the operator appears between its operands.
Q15 - In practice, what does it mean to say that an operator that has performed an action returns a value (or evaluates to a value) of a given type?
A - As a result of performing the specified action, an operator can be said to return a value (or evaluate to a value) of a given type. The type depends on the operator and the type of the operands. To evaluate to a value means that after the action is performed, the operator and its operands are effectively replaced in the expression by the value that is returned.
Q 16 - What are the four categories of operators described in Baldwin's Java tutorial on operators? Do you agree with this categorization? If not, explain why not.
A - Some authors divide Java's operators into the following categories:
arithmetic
relational and conditional (typically called relational and logical in C++)
bitwise and logical
assignment
Q17 - Show and describe at least five of the binary arithmetic operators supported by Java
(Clarification: binary operators does not mean bitwise operators).
A - Java support various arithmetic operators on floating point and integer numbers. The following table lists five of the binary arithmetic operators supported by Java.
Operator Description + Adds its operands - Subtracts the right operand from the left operand
* Multiplies the operands / Divides the left operand by the right operand
% Remainder of dividing the left operand by the right operand
Q18 - In addition to arithmetic addition, what is another use for the plus operator (+)? Show an
example code fragment to illustrate your answer. The code fragment need not be a complete
statement.
A - The plus operator (+) is also used to concatenate strings : "IMR " + " global Ltd."
Q19 - When the plus operator (+) is used as a concatenation operator, what is the nature of its
behavior if its right operand is not of type String? If the right operand is a variable that is not of typeString, what is the impact of this behavior on that variable.
A - In this case, the operator also coerces the value of the right operand to a string representation for use in the expression only. If the right operand is a variable, the value stored in the variable is not modified in any way.
Q 20- Show and describe four unary arithmetic operators supported by Java.
A - Java supports the following four unary arithmetic operators.
Operator Description + Indicates a positive value - Negates, or changes algebraic sign ++ Adds one to the operand, both prefix and postfix
-- Subtracts one from operand, prefix and postfix

No comments:

Post a Comment