MCQs
The code will not compile because in line 7, the line will work only if we use (x==y)
in the line. The == operator compares values to produce a boolean, whereas the =
operator assigns a value to variables.
Option A, B, and D are incorrect because the code does not get as far as compiling.
If we corrected this code, the output would be false.
Option A is correct. The >>> operator moves all bits to the right, zero filling the left bits.
The bit transformation looks like this:
Before: 1000 0000 0000 0000 0000 0000 0000 0000
After: 0000 0000 0000 0000 0000 0000 0000 0001
Option C is incorrect because the >>> operator zero fills the left bits, which in this case
changes the sign of x, as shown.
Option B is incorrect because the output method print() always displays integers in base 10.
Option D is incorrect because this is the reverse order of the two output numbers.
None.
What will be the output of the program?
class PassS
{
public static void main(String [] args)
{
PassS p = new PassS();
p.start();
}
void start()
{
String s1 = "slip";
String s2 = fix(s1);
System.out.println(s1 + " " + s2);
}
String fix(String s1)
{
s1 = s1 + "stream";
System.out.print(s1 + " ");
return "stream";
}
}
When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both
refer to the same String object (with a value of "slip"). Fix()'s s1 is reassigned to a new
object that is created when the concatenation occurs (this second String object has a
value of "slipstream"). When the program returns to start(), another String object is created,
referred to by s2 and with a value of "stream".
None.
Operator ++ increases value of variable by 1. x = x + 1 can also be written in shorthand form as x += 1. Also x =+ 1 will set the value of x to 1.
The left shift operator shifts all of the bite in a value to the left
specified number of times. For each
shift left, the high order bit is
shifted out and lost, zero is brought in from right. When a left shift
is
applied to an integer operand, bits are lost once they are shifted
past the bit position 31.
The controlling condition of ternary operator must evaluate to boolean.
Order of precedence is (highest to lowest) a -> b -> c -> d.
Unary not operator, ~, inverts all of the bits of its operand in binary representation.