Q11 - Show the symbol for the increment operator.
A - The symbol for the increment operator is two plus signs with nothing between them (++).
A - The symbol for the increment operator is two plus signs with nothing between them (++).
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.
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++ = " + (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
A - False: Binary operators use infix notation, which means that the operator appears between its operands.
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.
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
(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
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."
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.
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