Java operators

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

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.


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.


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.


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.


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.


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!
SarMaroof.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.


Sar Maroof is graduated from HBO Amsterdam "higher professional education". He is a professional computer programming teacher, gives master classes and publishes technical articles. His experience with developing software started from 2001 and he has worked as a software developer for several big as well as small companies and later as a freelancer. He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           
 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

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

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017