Conditional Operators - Questions & Ans

  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Conditional Operators - Questions & Ans as PDF for free.

More details

  • Words: 655
  • Pages: 2
Conditional Operators

Questions & Ans

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. b. c. d. e. f. g. h. i. j. k.

Prints: 0,0,0 Prints: 0,0,3 Prints: 0,3,0 Prints: 0,3,3 Prints: 3,0,0 Prints: 3,0,3 Prints: 3,3,0 Prints: 3,3,3 Run-time error Compile-time error 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. b. c. d. e. f. g. h. i. j. k.

Prints: false,false,false Prints: false,false,true Prints: false,true,false Prints: false,true,true Prints: true,false,false Prints: true,false,true Prints: true,true,false Prints: true,true,true Run-time error Compile-time error 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

page:1

Conditional Operators c. d. e. f. g. h. i. j. k.

Questions & Ans

Prints: false,true,false Prints: false,true,true Prints: true,false,false Prints: true,false,true Prints: true,true,false Prints: true,true,true Run-time error Compile-time error 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. b. c. d. e. f. g.

Prints: ATBFCT Prints: ATBFCTDF Prints: BFCTAT Prints: BTCTDFAT Run-time error Compile-time error None of the above

Answers Answers: Certified Java Programmer Mock Exam No. 1

Answer j

2 e

3 d

Remark

Compile-time error

Both operands of the conditional and operator and the conditional or operator must be of type boolean.

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.

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.

page:2

Related Documents