Sail E0 Webinar

MCQs

Total Questions : 65 | Page 6 of 7 pages
Question 51.


What will be the output of the program?


class Equals
{
public static void main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y); /* Line 7 */
System.out.println(b);
}
}
  1.    true
  2.    false
  3.    Compilation fails
  4.    An exception is thrown at runtime
 Discuss Question
Answer: Option C. -> Compilation fails

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.

Question 52.


What will be the output of the program?


class BitShift
{
public static void main(String [] args)
{
int x = 0x80000000;
System.out.print(x + " and ");
x = x >>> 31;
System.out.println(x);
}
}
  1.    -2147483648 and 1
  2.    0x80000000 and 0x00000001
  3.    -2147483648 and -1
  4.    1 and -2147483648
 Discuss Question
Answer: Option A. -> -2147483648 and 1

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.


Question 53.

Which right shift operator preserves the sign of the value?


 Discuss Question
Answer: Option A. -> -2147483648 and 1

None.


Question 54.


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";
}
}
  1.    slip stream
  2.    slipstream stream
  3.    stream slip stream
  4.    slipstream slip stream
 Discuss Question
Answer: Option D. -> slipstream slip 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".


Question 55.

What is the value stored in x in following lines of code?
int x, y, z;
x = 0;
y = 1;
x = y = z = 8;


  1.    0
  2.    1
  3.    9
  4.    8
 Discuss Question
Answer: Option D. -> 8

None.


Question 56.

With x = 0, which of the following are legal lines of Java code for changing the value of x to 1?
1. x++;
2. x = x + 1;
3. x += 1;
4. x =+ 1;


  1.    1, 2 & 3
  2.    1 & 4
  3.    1, 2, 3 & 4
  4.    3 & 2
 Discuss Question
Answer: Option C. -> 1, 2, 3 & 4

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.



Question 57.
On applying Left shift operator, <<, on an integer bits are lost one they are shifted past which
position bit
  1.    1
  2.    32
  3.    33
  4.    31
 Discuss Question
Answer: Option D. -> 31

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.


Question 58.

What should be expression1 evaluate to in using ternary operator as in this line?
expression1 ? expression2 : expression3


  1.    Integer
  2.    Floating - point numbers
  3.    Boolean
  4.    None of the mentioned
 Discuss Question
Answer: Option C. -> Boolean

The controlling condition of ternary operator must evaluate to boolean.


Question 59.

Which of these have highest precedence?


  1.    ()
  2.    ++
  3.    *
  4.    >>
 Discuss Question
Answer: Option A. -> ()

Order of precedence is (highest to lowest) a -> b -> c -> d.


Question 60.

Which operator is used to invert all the digits in binary representation of a number?


  1.    ~
  2.    >>>
  3.    ^
 Discuss Question
Answer: Option A. -> ~

Unary not operator, ~, inverts all of the bits of its operand in binary representation.


Latest Videos

Latest Test Papers