Followers

Sunday, 16 September 2012

Operators

Question 1

class EBH019 {
  public static void main (String args[]) {
    int i1 = 0xffffffff, i2 = i1 << 1;
    int i3 = i1 >> 1, i4 = i1 >>> 1;
    System.out.print(Integer.toHexString(i2) + ",");
    System.out.print(Integer.toHexString(i3) + ",");
    System.out.print(Integer.toHexString(i4));
}}
What is the result of attempting to compile and run the program?
a.   Prints: ffffffff,ffffffff,ffffffff
b.  Prints: ffffffff,ffffffff,7fffffff
c.   Prints: ffffffff,7fffffff,ffffffff
d.  Prints: ffffffff,7ffffffe,7ffffffe
e.   Prints: fffffffe,ffffffff,ffffffff
f.   Prints: fffffffe,ffffffff,7fffffff
g.  Prints: fffffffe,7fffffff,ffffffff
h.  Prints: fffffffe,7fffffff,7fffffff
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 2

class EBH011 {
  public static void main (String[] args) {
    float a = Float.POSITIVE_INFINITY;
    double b = Double.POSITIVE_INFINITY;
    double c = Double.NaN;
    System.out.print((a == b)+","+(c == c)+","+(c != c));
}}
What is the result of attempting to compile and run the program?
a.   Prints: false,false,false
b.  Prints: false,false,true
c.   Prints: false,true,false
d.  Prints: false,true,true
e.   Prints: true,false,false
f.   Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 3

class EBH012 {
  public static void main (String[] args) {
    byte x = 3, y = 5;
    System.out.print((-x == ~x + 1)+","+(-y == ~y + 1));
}}
What is the result of attempting to compile and run the program?
a.   Prints: false,false
b.  Prints: false,true
c.   Prints: true,false
d.  Prints: true,true
e.   Run-time error
f.   Compile-time error
g.  None of the above

Question 4

class EBH013 {
  public static void main (String[] args) {
    byte x = 3, y = 5;
    System.out.print((~x == -x - 1)+","+(~y == -y - 1));
}}
What is the result of attempting to compile and run the program?
a.   Prints: false,false
b.  Prints: false,true
c.   Prints: true,false
d.  Prints: true,true
e.   Run-time error
f.   Compile-time error
g.  None of the above

Question 5

class EBH014 {
  public static void main (String[] args) {
    byte x = 3, y = 5;
    System.out.print((y % x) + ",");
    System.out.print(y == ((y/x)*x + (y%x)));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 1,true
b.  Prints: 2,true
c.   Prints: 1,false
d.  Prints: 2,false
e.   Run-time error
f.   Compile-time error
g.  None of the above

Question 6

class Color {}
class Red extends Color {}
class Blue extends Color {}
class A {
  public static void main (String[] args) {
    Color color1 = new Red(); Red color2 = new Red();
    boolean b1 = color1 instanceof Color;
    boolean b2 = color1 instanceof Blue;
    boolean b3 = color2 instanceof Blue;
    System.out.print(b1+","+b2+","+b3);
}}
What is the result of attempting to compile and run the program?
a.   false,false,false
b.  false,false,true
c.   false,true,false
d.  false,true,true
e.   true,false,false
f.   true,false,true
g.  true,true,false
h.  true,true,true
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 7

class EBH020 {
  public static void main (String[] args) {
    int a = 1 | 2 ^ 3 & 5;
    int b = ((1 | 2) ^ 3) & 5;
    int c = 1 | (2 ^ (3 & 5));
    System.out.print(a + "," + b + "," + c);
}}
What is the result of attempting to compile and run the program?
a.   Prints: 0,0,0
b.  Prints: 0,0,3
c.   Prints: 0,3,0
d.  Prints: 0,3,3
e.   Prints: 3,0,0
f.   Prints: 3,0,3
g.  Prints: 3,3,0
h.  Prints: 3,3,3
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 8

class EBH025 {
  public static void main (String args[]) {
    int i1 = 0xffffffff, i2 = i1 << 33;
    int i3 = i1 << (33 & 0x1f);
    System.out.print(Integer.toHexString(i2) + ",");
    System.out.print(Integer.toHexString(i3));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 0,0
b.  Prints: 0,fffffffe
c.   Prints: 0,ffffffff
d.  Prints: ffffffff,ffffffff
e.   Prints: ffffffff,fffffffe
f.   Prints: fffffffe,ffffffff
g.  Prints: fffffffe,fffffffe
h.  Run-time error
i.   Compile-time error
j.   None of the above

Question 9

class EBH001 {
  static int m(int i) {System.out.print(i + ", "); return i;}
  public static void main(String s[]) {
    m(m(1) - m(2) + m(3) * m(4));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 1, 2, 3, 4, 8,
b.  Prints: 1, 2, 3, 4, 11,
c.   Prints: 3, 4, 1, 2, 11,
d.  Run-time error
e.   Compile-time error
f.   None of the above

Question 10

class EBH002 {
  static int m(int i) {System.out.print(i + ", "); return i;}
  public static void main(String s[]) {
    m(m(1) + m(2) % m(3) * m(4));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 1, 2, 3, 4, 0,
b.  Prints: 1, 2, 3, 4, 3,
c.   Prints: 1, 2, 3, 4, 9,
d.  Prints: 1, 2, 3, 4, 12,
e.   Prints: 2, 3, 4, 1, 9,
f.   Prints: 2, 3, 4, 1, 3,
g.  Run-time error
h.  Compile-time error
i.   None of the above

Question 11

class EBH005 {
  public static void main (String[] s) {
    byte b = 127; b <<= 2;System.out.println(b);
}}
What is the result of attempting to compile and run the program?
a.   Prints: -4
b.  Prints: -3
c.   Prints: -2
d.  Prints: 0
e.   Prints: 1
f.   Prints: 127
g.  Prints: 508
h.  Run-time error
i.   Compile-time error
j.   None of the above

Question 12

class EBH007{
  public static void main (String[] s) {
    byte b = 5; System.out.println(b<<33);
}}
What is the result of attempting to compile and run the program?
a.   Prints: -1
b.  Prints: 0
c.   Prints: 1
d.  Prints: 5
e.   Prints: 10
f.   Run-time error
g.  Compile-time error
h.  None of the above

Question 13

class EBH015 {
  public static void main (String[] args) {
    System.out.print((new Object() instanceof Object)+",");
    System.out.print((new Object() instanceof String)+",");
    System.out.print((new String() instanceof Object));
}}
What is the result of attempting to compile and run the program?
a.   Prints: false,false,false
b.  Prints: false,false,true
c.   Prints: false,true,false
d.  Prints: false,true,true
e.   Prints: true,false,false
f.   Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 14

class EBH023 {
  static String m1(boolean b){return b?"T":"F";}
  public static void main(String [] args) {
    boolean b1 = false?false:true?false:true?false:true;
    boolean b2 = false?false:(true?false:(true?false:true));
    boolean b3 = ((false?false:true)?false:true)?false:true;
    System.out.println(m1(b1) + m1(b2) + m1(b3));
}}
What is the result of attempting to compile and run the program?
a.   Prints: FFF
b.  Prints: FFT
c.   Prints: FTF
d.  Prints: FTT
e.   Prints: TFF
f.   Prints: TFT
g.  Prints: TTF
h.  Prints: TTT
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 15

class EBH024 {
  public static void main(String[] args) {
    int i1 = 15;
    String b1 = (i1>30)?"Red":(i1>20)?"Green":(i1>10)?"Blue":"Violet";
    String b2 = (i1>30)?"Red":((i1>20)?"Green":((i1>10)?"Blue":"Violet"));
    System.out.println(b1 + "," + b2);
 
}}
What is the result of attempting to compile and run the program?
a.   Prints: Red,Red
b.  Prints: Green,Green
c.   Prints: Blue,Blue
d.  Prints: Violet,Violet
e.   Prints: Blue,Violet
f.   Prints: Violet,Blue
g.  Prints: Blue,Green
h.  Prints: Green,Blue
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 16

class EBH003 {
  static int m(int i) {System.out.print(i + ", "); return i;}
  public static void main(String s[]) {
    m(m(~1) + m(1|2) + m(1&2) + m(1^3) + m(1<<1));
}}
What is the result of attempting to compile and run the program?
a.    Prints: -2, 3, 0, 3, 0, 6,
b.   Prints: -2, 3, 0, 2, 1, 4,
c.    Prints: -2, 3, 0, 2, 2, 5,
d.   Prints: -2, 3, 0, 3, 2, 6,
e.    Prints: -1, 3, 0, 3, 2, 7,
f.    Prints: -2, 0, 3, 3, 0, 6,
g.   Prints: -1, 0, 3, 2, 1, 4,
h.   Prints: -2, 0, 3, 2, 2, 5,
i.    Prints: -2, 0, 3, 3, 2, 6,
j.    Prints: -1, 0, 3, 3, 2, 7,
k.   Run-time error
l.    Compile-time error

m.  None of the above


Question 17

class EBH021 {
  public static void main(String[] args) {
    System.out.print((-1 & 0x1f) + "," + (8 << -1));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 0,0
b.  Prints: -1,4
c.   Prints: 0x1f,8
d.  Prints: 31,16
e.   Run-time error
f.   Compile-time error
g.  None of the above




Answers



No.
Answer
Remark
1
Prints: fffffffe,ffffffff,7fffffff 
If the left-hand operand of a shift operator, <<, >> and >>>, is of type int, then the shift distance is always within the range of 0 to 31, inclusive; and is specified by the least significant 5 bits of the right hand operand. Similarly, if the left-hand operand of a shift operator is of type long, then the shift distance is always within the range of 0 to 63, inclusive; and is specified by the least significant 6 bits of the right hand operand. The left shift operator, <<, shifts each bit of the left operand to the left a distance specified by the shift distance. A number of bits that is equal to the shift distance are shifted out of the left-most bit position and are lost. A number of bits that is equal to the shift distance are shifted in at the right. The signed right shift operator, >>, shifts each bit of the left operand to the right a distance specified by the shift distance. A number of bits that is equal to the shift distance are shifted out of the right-most bit position and are lost. A number of bits that is equal to the shift distance are shifted in at the left. The value of each bit that is shifted in at the left is equal to the value of the sign bit. The signed right shift operator maintains the sign of the left operand. The unsigned right shift operator, >>>, is similar to the signed right shift operator except for the fact that each bit shifted in at the left is zero.  
2
Prints: true,false,true 
The positive infinity of type float is promoted to the positive infinity of type double. NaN is not equal to anything including itself.  
3
Prints: true,true 
The sign of an integral numeric type is changed by inverting all of the bits and by adding one.  
4
Prints: true,true 
The bitwise complement operator produces the same result as changing the sign and subtracting one. Please note that the operand must be an integral type. The bitwise complement operator can not be applied to a floating-point value.  
5
Prints: 2,true 
Suppose the left operand were divided by the right operand. The remainder operator returns the remainder of the division operation. For integral types, the identity, (y == ((y/x)*x+(y%x))), is always true.  
6
Compile-time error 
The type of the reference color2 is Red. Since Red is not a subclass or a superclass of Blue, the expression color2 instanceof Blue is rejected at compile-time. Please note: The expression, x instanceof T, produces a compile-time error whenever the cast expression (T)x produces a compile-time error. If the program had been able to compile and run, the expression color1 instanceof Color would evaluate to true at run-time. The reference color1 refers to an instance of type Red. Since Red is a subclass of Color, the expression color1 instanceof Color would evaluate to true at run-time. The expression, color1 instanceof Blue would evaluate to false at run-time. The reference, color1, is of type Color. Since Color is a superclass of Blue, the expression, color1 instanceof Blue, is accepted at compile-time. The type of the object instance referenced by color1 is Red. Since Red is not Blue or a subclass of Blue, the expression, color1 instanceof Blue, would be false at run-time.  
7
Prints: 3,0,3 
Java evaluates operands from left to right while respecting operator precedence. The order of operator precedence starting with the lowest is as follows: |, ^, &. Although complete memorization of the operator precedence chart is not necessary, it is a good idea to memorize the three levels that appear in this question.  
8
Prints: fffffffe,fffffffe 
For each of the three shift operators, <<, >> and >>>, the shift distance is specified by the right hand operand. If the left operand is of type int, then the shift distance is always within the range 0 to 31, inclusive; and the following expression is always true: (int1 << shift) == (int1 << (shift & 0x1f)). The hexadecimal representation of decimal 31 is 0x1f and the binary representation is 0001 1111. The hexadecimal representation of decimal 33 is 0x21 and the binary representation is 0010 0001. The expression i1 << (33 & 0x1f) is equivalent to (0xffffffff << (0x21 & 0x1f)). Evaluation of the right hand operand of the shift operator produces (0xffffffff << 1). The final result is 0xfffffffe. Similarly, if the left operand is of type long, then the shift distance is always within the range 0 to 63, inclusive; and the following expression is always true: (long1 << shift) == (long1 << (shift & 0x3f)).  
9
Prints: 1, 2, 3, 4, 11, 
The expression can be simplified as follows: j = 1 - 2 + 3 * 4 = 11. The original expression is as follows: m(m(1) - m(2) + m(3) * m(4)). Simplification step one. Evaluate each operand from left to right: m(1 - 2 + 3 * 4). Step two. Add parentheses to indicate operator precedence: m(1 - 2 + (3 * 4)). Step three. Evaluate the inner-most parentheses: m(1 - 2 + 12). Step four: Work through the expression from left to right. j = 11.  
10
Prints: 1, 2, 3, 4, 9, 
The expression can be simplified as follows: 1 + 2 % 3 * 4 = 9. The original expression is as follows: m(m(1) + m(2) % m(3) * m(4)). Simplification step one. Evaluate each operand from left to right: m(1 + 2 % 3 * 4). Step two. Add parentheses to indicate operator precedence and associativity: m(1 + ((2 % 3) * 4). Step three. Evaluate the inner-most parentheses: m(1 + (2 * 4)). Step four. Evaluate the inner-most parentheses: m(1 + 8). The result is 9.  
11
Prints: -4 
If the left-hand operand of the shift operator is of type byte, short, or char then the left operand is promoted to a 32 bit int and all four bytes are shifted. When a variable of type int with a value of 127 is shifted two bits to the left, the result is 508. The compound assignment operator includes an implicit cast to the type of the left-hand operand. The expression, E1 op= E2, is equivalent to E1=(T)((E1) op (E2)), where T is the type of the left hand operand. Therefore, when 508 is cast to an eight bit byte, the three most significant bytes (24 bits) are discarded leaving only the least significant byte (8 bits). The result is the binary value, 11111100, which is the two's complement representation of negative four.  
12
Prints: 10 
If the left-hand operand of the shift operator is of type byte, short, or char then the left operand is promoted to a 32 bit int and all four bytes are shifted. If the promoted type of the left-hand operand is of type int, then the shift distance is always within the range of 0 to 31, inclusive; and is specified by the least significant 5 bits of the right-hand operand. In this case, the shift distance is 33, and the five least significant bits are 00001; so the shift distance is one bit. Note: If the type of the left hand operand is long, then the least significant six bits of the right hand operand are used.  
13
Prints: true,false,true 
The left operand of the instanceof operator must be null or a reference to an instance of an Object or a subclass of Object. The right operand of the instanceof operator must be a class type, interface type or array type. If the left operand is a reference to an instance of the type specified by the right operand or if the left operand is a reference to an instance of a subclass of the type specified by the right operand, then instanceof returns true.  
14
Prints: FFT 
The expression used to assign variable b1 is equivalent to the expression used to assign variable b2. The results demonstrate that the conditional operator (?:) groups from right-to-left.  
15
Prints: Blue,Blue 
The expression used to assign variable b1 is equivalent to the expression used to assign variable b2. The results demonstrate that the conditional operator (?:) groups from right-to-left.  
16
Prints: -2, 3, 0, 2, 2, 5, 
The expression can be simplified as follows: -2+3+0+2+2=5. The original expression is as follows: m(m(~1) + m(1|2) + m(1&2) + m(1^3) + m(1<<1)). Expr 1: ~1 = -1 - 1 = -2. Expr 2: 1|2 = 0001 | 0010 = 0011 = 3. Expr 3: 1&2 = 0001 & 0010 = 0000. Expr 4: 1^3 = 0001 ^ 0011 = 0010 = 2. Expr 5: 1<<1 = 0001 << 1 = 0010 = 2. Note: The bitwise expressions were demonstrated using only four bits of the 32 bit int type values.  
17
None of the above 
Prints 31,0. The expression (-1 & 0x1f) is equal to (0xffffffff & 0x1f), and both are equal to the hex value 0x1f or decimal 31. The expression (8 << -1) is equivalent to (8 << 0xffffffff). If the left hand operand of a shift expression is of type int, then the right hand operand is implicitly masked with the value 0x1f. In other words, the expression (8 << -1) is equivalent to (8 << (-1 & 0x1f)). By replacing -1 with the hexadecimal representation we have (8 << (0xffffffff & 0x1f)). By evaluating the right hand operand we have (8 << 31). When 8 is shifted 31 bits to the left, the result is zero since the only non-zero bit is lost as it is shifted beyond the most significant bit of the int data type.  

�F=mP�0 �unt: 1'>  Prints: -1
b.  Prints: 0
c.   Prints: 1
d.  Prints: 5
e.   Prints: 10
f.   Run-time error
g.  Compile-time error
h.  None of the above

Question 13

class EBH015 {
  public static void main (String[] args) {
    System.out.print((new Object() instanceof Object)+",");
    System.out.print((new Object() instanceof String)+",");
    System.out.print((new String() instanceof Object));
}}
What is the result of attempting to compile and run the program?
a.   Prints: false,false,false
b.  Prints: false,false,true
c.   Prints: false,true,false
d.  Prints: false,true,true
e.   Prints: true,false,false
f.   Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 14

class EBH023 {
  static String m1(boolean b){return b?"T":"F";}
  public static void main(String [] args) {
    boolean b1 = false?false:true?false:true?false:true;
    boolean b2 = false?false:(true?false:(true?false:true));
    boolean b3 = ((false?false:true)?false:true)?false:true;
    System.out.println(m1(b1) + m1(b2) + m1(b3));
}}
What is the result of attempting to compile and run the program?
a.   Prints: FFF
b.  Prints: FFT
c.   Prints: FTF
d.  Prints: FTT
e.   Prints: TFF
f.   Prints: TFT
g.  Prints: TTF
h.  Prints: TTT
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 15

class EBH024 {
  public static void main(String[] args) {
    int i1 = 15;
    String b1 = (i1>30)?"Red":(i1>20)?"Green":(i1>10)?"Blue":"Violet";
    String b2 = (i1>30)?"Red":((i1>20)?"Green":((i1>10)?"Blue":"Violet"));
    System.out.println(b1 + "," + b2);
 
}}
What is the result of attempting to compile and run the program?
a.   Prints: Red,Red
b.  Prints: Green,Green
c.   Prints: Blue,Blue
d.  Prints: Violet,Violet
e.   Prints: Blue,Violet
f.   Prints: Violet,Blue
g.  Prints: Blue,Green
h.  Prints: Green,Blue
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 16

class EBH003 {
  static int m(int i) {System.out.print(i + ", "); return i;}
  public static void main(String s[]) {
    m(m(~1) + m(1|2) + m(1&2) + m(1^3) + m(1<<1));
}}
What is the result of attempting to compile and run the program?
a.    Prints: -2, 3, 0, 3, 0, 6,
b.   Prints: -2, 3, 0, 2, 1, 4,
c.    Prints: -2, 3, 0, 2, 2, 5,
d.   Prints: -2, 3, 0, 3, 2, 6,
e.    Prints: -1, 3, 0, 3, 2, 7,
f.    Prints: -2, 0, 3, 3, 0, 6,
g.   Prints: -1, 0, 3, 2, 1, 4,
h.   Prints: -2, 0, 3, 2, 2, 5,
i.    Prints: -2, 0, 3, 3, 2, 6,
j.    Prints: -1, 0, 3, 3, 2, 7,
k.   Run-time error
l.    Compile-time error
m.  None of the above

Question 17

class EBH021 {
  public static void main(String[] args) {
    System.out.print((-1 & 0x1f) + "," + (8 << -1));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 0,0
b.  Prints: -1,4
c.   Prints: 0x1f,8
d.  Prints: 31,16
e.   Run-time error
f.   Compile-time error
g.  None of the above

No comments:

Post a Comment