How to Compare Integer Values in Java


Choose an answer and hit 'next'. You will receive your score and answers at the end.

question 1 of 3

Examine the following Java code. Based on the values of the integers, what will the compareTo() method return?

Create Your Account To Take This Quiz

As a member, you'll also get unlimited access to over 79,000 lessons in math, English, science, history, and more. Plus, get practice tests, quizzes, and personalized coaching to help you succeed.

Try it risk-free
Try it risk-free for 30 days. Cancel anytime
Already registered? Log in here for access

1. Consider you have two integer variables in Java. The values can fluctuate, but you need to continually compare them. Why would you choose NOT to implement an if/then/else block?

2. In the following Java code, what is the variable counter?

Create your account to access this entire worksheet
A Premium account gives you access to all lesson, practice exams, quizzes & worksheets
Access to all video lessons
Quizzes, practice exams & worksheets
Certificate of Completion
Access to instructors
Create an account to get started Create Account

About This Quiz and Worksheet

For this quiz and worksheet combo, you are looking at the comparison of integer values in the Java language. Questions focus on using the compareTo() method and also the result of an operation with certain values placed into the variables of a given code segment.

Quiz and Worksheet Goals

To find out how well you understand the lesson, take the quiz, which covers:

  • Why you might choose not to use an if/then/else block
  • The use of a variable in a given code segment
  • Identifying missing pieces of code
  • Using the compareTo() method

Skills Practiced

  • Problem solving - ensure that you can explain the end result of a given code sequence
  • Information recall - remember what you have learned about if/then/else blocks
  • Knowledge application - use what you know to answer questions about integer values

Additional Learning

To find out more, visit the lesson titled How to Compare Integer Values in Java. With this in hand, you can dig into the additional topics that are mentioned below:

  • Operator characters
  • Comparing methods
  • Examples of code input