Computer Applications
The logical operators are used in between two conditions, which results in either 'True' or 'False' depending on the outcome of different conditions. Java uses three logical operators viz. AND, OR and NOT. Your friend has created a Java snippet that contains some errors due to which he is not able to execute it.
The program snippet created by him is as shown below:
int p=11, q=12, r=15;
(a) System.out.println ((p==q) AND (q!=r));
(b) System.out.println(!(p=q));
(c) System.out.println (p!==q);
(d) System.out.println ((p!=q) OR (q!=r));
Refer to the above snippet and help him by detecting the errors so that the snippet may execute successfully.
Values & Data Types Java
17 Likes
Answer
(a) System.out.println ((p==q)&&(q!=r));
The symbol of AND operator (&&) should be used in the expression.
(b) System.out.println(!(p==q));
'=' is an assignment operator. Using it in the expression p=q will assign the value of q to p. We use equality operator '==' to compare the values.
(c) System.out.println (p!=q);
The not equal operator is written incorrectly. The correct not equal operator is !=.
(d) System.out.println((p!=q) || (q!=r));
The symbol of OR operator (||) should be used in the expression.
Answered By
12 Likes