corejava interview questions part 3
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 << Dist 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 your
answer 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
Q 31- Using the symbols 1 and 0 construct a truth table
showing the four possible combinations of 1
and 0. Using a 1 or a 0, show the result of the bitwise
inclusive or operation on these four
combinations on these four combinations of 1 and 0.
A - The answer for the inclusive or is:
1 or 1 produces 1
1 or 0 produces 1
0 or 1 produces 1
0 or 0 produces 0
Q 32- Using the symbols 1 and 0 construct a truth table
showing the four possible combinations of 1
and 0. Using a 1 or a 0, show the result of the bitwise
exclusive or operation on these four
combinations on these four combinations of 1 and 0.
A - The answer for the exclusive or is:
1 xor 1 produces 0
1 xor 0 produces 1
0 xor 1 produces 1
0 xor 0 produces 0
Q 33- For the exclusive or, if the two bits are different,
the result is a 1. If the two bits are the same,
the result is a 0. True or False? If your answer is False,
explain why.
A - True.
Q 34- Is the assignment operator a unary operator or a
binary operator. Select one or the other.
A - The assignment operator is a binary operator.
Q 35- In Java, when using the assignment operator, the value
stored in memory and represented by the
right operand is copied into the memory represented by the
left operand: True or False? If your
answer is False, explain why.
A - True.
Q 36- Show two of the shortcut assignment operators and
explain how they behave by comparing
them with the regular (nonshortcut) versions. Hint: The (^=)
operator is a shortcut assignment
operator.
A - Java supports the following list of shortcut assignment
operators. These operators allow you to perform an assignment and another
operation with a single operator. +=
-= *= /= %= &= |= ^= <<= >>= >>>=
For example, the two statements which follow perform the
same operation.
x += y; x = x + y;
The behavior of all the shortcut assignment operators
follows this same pattern.
Q 37 - Write a Java application illustrates the difference
between the prefix and the postfix
versions of the increment operator.
class prog3{
static public void
main(String[] args){
int x = 3;
int y = 3;
int z = 10;
System.out.println("Prefix version gives " + (z + ++x));
System.out.println("Postfix version gives " + (z + y++));
}//end main
}//end class
Q38 Write a Java
application that illustrates the use of the following relational
operators: < > <= >= == !=
class Prog4 { //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 ) );
System.out.println("The relational 5>=5 is " + (5>=5 )
);
System.out.println("The relational 5<=5 is " + (5<=5 )
);
System.out.println("The
relational 6==5 is " + (6==5 ) );
System.out.println("The relational 6!=5 is " + (6!=5 ) );
}//end main
}//End prog4
class. Note no semicolon required
Q39 - Write a Java application that illustrates the use of
the following logical or conditional
operators:
&& || !
class prg5 { //define
the controlling class
public static void
main(String[] args){ //define main method
System.out.println("true and true is " + (true &&
true) );
System.out.println("true and false is " + (true &&
false) );
System.out.println("true or true is " + (true || true) );
System.out.println("true or false is " + (true || false) );
System.out.println("false or false is " + (false || false) );
System.out.println("not true is " + (! true) );
System.out.println("not false is " + (! false) );
}//end main
}
Q40 - Java supports a constant type: True or False. If
false, explain why.
A - Java does not support a constant type. However, in Java,
it is possible to achieve the same result by
declaring and initializing a variable and making it final.
Q41 Provide a code fragment that illustrates the syntax for
creating a named constant in Java.
A - The syntax for creating a named constant in Java is as
follows: final float PI = 3.14159;
Q42 - What is the common method of controlling the order of
evaluation of expressions in Java?
A - you can control
the order of evaluation by the use of parentheses.
Q43 - What are the three actions normally involved in the
operation of a loop (in addition to executing
the code in the body of the loop)?
A - The operation of a loop normally involves the following
three actions in addition to executing the code in the body of the loop:
Initialize a
control variable.
Test the control
variable in a conditional expression.
Update the
control variable.
0 comments:
Post a Comment