core java interview questions
Q 1What do we call an operator that operates on only one
operand?
A - An operator that operates on only one operand is called
a unary operator.
Q 2- What do we call an operator that operates on two
operands?
A - An operator that operates on two operands is called a
binary operator.
Q 3- Is the minus sign a unary or a binary operator, or
both? Explain your answer.
A - Both. As a binary operator, the minus sign causes its
right operand to be subtracted from its left operand. As a unary operator, the
minus sign causes the algebraic sign of the right operand to be changed.
Q 4- Describe operator overloading.
A - For those languages that support it (such as C++)
operator overloading means that the programmer can
redefine the behavior of an operator with respect to objects
of a new type defined by that program.
Q5- Java programmers may overload operators: True or False?
A 6- False: Unfortunately, Java does not support operator
overloading.
Q7 - Show the symbols used for the following operators in
Java: assignment, not equal, addition,cast.
A - The above listed operators in order are: = != + (type)
Q8 - Is any operator automatically overloaded in Java? If
so, identify it and describe its overloaded
behavior.
A - The plus sign (+) is automatically overloaded in Java.
The plus sign can be used to perform arithmetic
addition. It can also be used to concatenate strings.
However, the plus sign does more than concatenate strings. It also performs a
conversion to String type. When the plus sign is used to concatenate strings,
the operand on the right is automatically converted to a character string
before being concatenated with the operand on the left. This assumes that the
compiler knows enough about the operand on the right to be able to successfully
perform the conversion. It has that knowledge for all of the primitive types
and most or all of the built-in reference types.
Q 9- What is the purpose of the cast operator?
A - The cast operator is used to purposely convert from one
type to another.
Q10 - The increment operator is a binary operator: True or
False?
A - False: The increment operator is a unary operator.
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."
great
ReplyDelete