Java Operators

Java supports different types of operators. Here the Relational operators and the Logical operators are explained using Java Codes and exercises.

Java Operators: Article  Quizzes

operators

1- Relational Operators

Relational operators are binary operators, and their operands are numeric expressions. The result of the evaluation is either true or false.

Example is: if ( i == 20 )  this asks “does i equal to 20?” if it is true, the statement is executed. The statement is ignored if i is not equal to 20, because the result is false.

The relational operators in Java are:

== Equal
!= Not equal
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to

Exercise 1

What is written to the standard output as the result of executing the following statements?

class RelationalOperator {

	public static void main(String[] args){

		int i = 8;

		if(i != 7) {
			System.out.print("X");
		}
		else {
			System.out.print("Y");
		}
	}
}

Select the correct answer.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

Exercise 2

What is written to the standard output as the result of executing the following statements?

class RationalOperator {

	public static void main(String[] args){

		int i = 100;

		if(i <= 30) {
			System.out.print("M");
		}
		else {
			System.out.print("O");
		}
	}
}

Select the correct answer.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

Exercise 3

What is written to the standard output as the result of executing the following statements?

class RelationalOperator {

	public static void main(String[] args){

		int i = 10;
		int i2 = 20;
		int i3 = 30;

		if(i3 <= 30) {
			System.out.print("K");
		}
		if(i == i3) {
			System.out.print("L");
		}
		if(i3 >= 22) {
			System.out.print("M");
		}
		if(i2 > 18) {
			System.out.print("N");
		}
		if(i != 10) {
			System.out.print("O");
		}
	}
}

Select the correct answer.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

2- Conditional Operators

The conditional operators in Java ( &&, || ) are used in conditional statements such as if and while statements. The following example shows the difference between “&&” and “||”

int i = 4;   int r = 6;

if(i == 4 && r ==3 )  // By using  “&&” the result is false, because r is not equal to 3. One false results in false.

if(i ==4 || r ==3) // By using  “||”  the result is true, because i is equal to 4. One true results in true.

&& AND
|| OR

Exercise 4

What is written to the standard output as the result of executing the following statements?

class LogicalOperator {

	public static void main(String[] args){

		int i = 10;

		if(i > 9 && i != 10) {
			System.out.print("P");
		}
		else {
			System.out.print("Q");
		}
	}
}

Select the correct answer.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

Exercise 5

What is written to the standard output as the result of executing the following statements?

class LogicalOperator {

	public static void main(String[] args){

		int i = 10;

		if(i != 9 || i > 20) {
			System.out.print("W");
		}
		else {
			System.out.print("Z");
		}
	}
}

Select the correct answer.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

Exercise 6

What is written to the standard output as the result of executing the following statements?

class LogicalOperator {

	public static void main(String[] args){

		int i = 10;
		int i2 = 20;
		int i3 = 30;

		if(i3 == 30 && i2 > 20) {
			System.out.print("R");
		}
		if(i != i3 || i2 == 21) {
			System.out.print("S");
		}
		if(i3 >= 22 && i != 11) {
			System.out.print("T");
		}
		if(i2 > 18 || i == 11) {
			System.out.print("U");
		}
	}
}

Select the correct answer.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

Megisoft.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes. The author of Megisoft is Sar Maroof who has more than a decade of experience with web development mainly with Java, MySql, HTML and more..
Java Operators: Article  Quizzes

Suggested Articles:


Sar Maroof's New Book "Java Quizmaster For Beginners"
Author: Sar Maroof
Many years of experience with developing web applications mainly with Java.
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
Bachellor Science in Physics at Basra university.
 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

3 Comments

Add a Comment
  1. The accident of finding this post has my day

  2. Nice article and easy to understand!

    1. Created the greatest artcis, you have.

Leave a Reply

megisoft © 2016