Anonymous

Register for more FREE stuff!

my subscriptions

IF Statements

Question 1

[Maximum mark: 4]



We want to check whether a person is tall enough to go on a rollercoaster. Your Java program should prompt the user to input their name and height (in cm), and then it should check if the height is bigger than 150cm.



If it is, the following print statement should be created:

  • "{name}, you are tall enough to go on this ride!"

If it isn't, then the following print statement should be created:

  • "{name} you are not tall enough to go on this ride"

Answers and Explanations

Question 2

[Maximum mark: 4]



Write a Java program that determines if a rectangle is a square. The program should prompt the user to input the length and width of a rectangle.


If the length is equal to the width, the following print statement should be created:

  • "The shape is a square."

If the length is not equal to the width, it should print:

  • "The shape is a rectangle."

Answers and Explanations

Question 3

[Maximum mark: 4]



Write a Java program that checks if a number is even or odd. The program should prompt the user to input an integer.


If the number is even, the following print statement should be created:

  • "The number {number} is even."

If the number is odd, it should print:

  • "The number {number} is odd."

Answers and Explanations

Question 4

[Maximum mark: 4]



Write a Java program to determine if a student has passed, failed, or received distinction based on their exam score. The program should prompt the user to input their name and exam score (out of 100). The program should then print a message based on the following criteria:


If the score is 90 or above, the following print statement should be created:

  • "Excellent work, {name}! You have passed with distinction."

If the score is between 50 and 89, it should print:

  • "Good job, {name}. You have passed."

If the score is below 50, it should print:

  • "Sorry, {name}. You have failed. Please try again."

Answers and Explanations

Question 5

[Maximum mark: 4]



Write a Java program to determine if a person qualifies for a senior citizen discount or a child discount at a movie theater. The program should prompt the user to input their age and gender. Based on the age, the program should print:


If the age is 60 or above and the gender is male, print:

  • "Sir, you qualify for a senior citizen discount."

If the age is 60 or above and the gender is female, print:

  • "Ma'am, you qualify for a senior citizen discount."

If the age is below 12, print:

  • "You qualify for a child discount."

Otherwise, print:

  • "You do not qualify for any discount."

Answers and Explanations

Question 6

[Maximum mark: 4]



Write a Java program that evaluates the temperature. The program should first convert a temperature from Celsius to Fahrenheit using the following formula:


\[ Fahrenheit = \left(\frac{9}{5} \times Celsius\right) + 32 \]


After conversion, the program should evaluate the temperature according to the following rules:


If the Fahrenheit temperature is below 40, print:

  • "At {fahrenheit}°F, it is considered cold."

If the Fahrenheit temperature is between 40 and 68, print:

  • "At {fahrenheit}°F, it is considered warm."

If the Fahrenheit temperature is above 68, print:

  • "At {fahrenheit}°F, it is considered hot."

Answers and Explanations

Question 7

[Maximum mark: 4]



Write a Java program to determine if a given year is a leap year. The program should prompt the user to input a year. A year is a leap year if it meets the following conditions:


A year is a leap year if it is divisible by 4 but not divisible by 100, except if it is also divisible by 400.


If the year is a leap year, print:

  • "The year {year} is a leap year."

If the year is not a leap year, print:

  • "The year {year} is not a leap year."

Answers and Explanations

Question 8

[Maximum mark: 4]



Write a Java program to determine the final salary of an employee based on their base salary and years of service. The program should prompt the user to input their base salary and years of service.


  • If the years of service are 10 or more, add a bonus of 10% of the base salary.
  • If the years of service are between 5 and 9 (inclusive), add a bonus of 5% of the base salary.
  • If the years of service are less than 5, no bonus is added.

The program should print:

  • “With a base salary of {baseSalary} and {yearsOfService} years of service, your final salary is {finalSalary}.”

Answers and Explanations

Question 9

[Maximum mark: 4]



Write a Java program to calculate the final price of an item based on the quantity purchased and whether the customer is a member. The program should prompt the user to input:

  • The base price of the item.
  • The quantity of items purchased.
  • Whether the customer is a member (input "yes" or "no").

The members get a 10% discount regardless of the quantity purchased. The program should then calculate and print the final price based on the following criteria:

  • If the quantity is 10 or more and the person is not a member: Apply a 20% discount on the base price.
  • If the quantity is 10 or more and the person is a member: Apply a 30% discount on the base price.
  • If the quantity is less than 10 and the person is a member: Apply a 10% discount on the base price.
  • If the quantity is less than 10 and the customer is not a member: No discount is applied.

The program should print:

  • “The final price for{qunatity} items at a base price of {basePrice} is {finalPrice}.”

Answers and Explanations

Question 10

[Maximum mark: 4]



Write a Java program which will check how much of a bonus a person should have, based on their type of work. If a person works full-time, then their bonus should be 15%, if the person works part-time, then the bonus should be 10%. This bonus is added monthly. Also, create a variable for the monthly salary of the employee. So, if the monthly salary of a full-time employee is 1000, then the salary with the bonus will be 1150.


The program should print:

  • “As a {workType} employee, your monthly salary before the bonus is {monthlySalary}, and after the bonus it is {salaryBonus}.”

Answers and Explanations