Question 1
class EBH201 {
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 2
class EBH202 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + 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 EBH203 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = a || (b = true) && (c = true);
System.out.print(a + "," + b + "," + 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 4
class EBH204 {
static boolean m1(String s, boolean b) {
System.out.print(s + (b ? "T" : "F"));
return b;
}
public static void main(String[] args) {
m1("A",m1("B",false) || m1("C",true) || m1("D",false));
}}What is the result of attempting to compile and run the program?
a. Prints: ATBFCT
b. Prints: ATBFCTDF
c. Prints: BFCTAT
d. Prints: BTCTDFAT
e. Run-time error
f. Compile-time error
g. None of the above
No.
|
Answer
|
Remark
|
|
1
|
j
|
Compile-time error
|
Both operands of the conditional and operator and
the conditional or operator must be of type boolean.
|
2
|
e
|
Prints: true,false,false
|
The conditional and expression is not evaluated,
because the left hand operand of the conditional or expression is
true. The original expression is as follows: x
= (a = true) || (b = true) && (c = true). The left hand
operand of the conditional or expression is the result of the
assignment expression, (a = true).
Since the left hand operand of the conditional or expression is true, the right hand operand will not be
evaluated. In this case, the right hand operand is the conditional and
expression. Consequently, neither operand of the conditional and
expression is evaluated and the variables, b
and c, maintain their default values
of false.
|
3
|
d
|
Prints: false,true,true
|
The right hand operand of the conditional or
operator is evaluated only if the left hand operand is false. The right hand operand of the
conditional and operator is only evaluated if the left hand operand is
true. In this case, the left hand
operand of the conditional or operator is false, so the right hand operand must also be evaluated. The
left hand operand of the conditional and operator is the result of the
conditional or expression, true,
so the right hand operand is evaluated.
|
4
|
c
|
Prints: BFCTAT
|
The right operand of the conditional or operator is
evaluated only if the left hand operand is false.
In this case, the left operand of the first conditional or operator is
false so the right hand operand is
evaluated. No further evaluation of the expression is necessary so the right
hand operand of the second conditional or operator is not evaluated.
|
No comments:
Post a Comment