corejava interview questions part 2
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 type
String, 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
Q20 - What is the type returned by relational operators in
Java?
A - Relational operators return the boolean type in Java.
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 is " + (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 < b) || (c < d)
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.
0 comments:
Post a Comment