Anonymous

Register for more FREE stuff!

my subscriptions

Variables and Input

Question 1

[Maximum mark: 3]



Write a Java program that defines and initializes the following variables and then creates print statements:

  • int called myAge with a value of 25
  • double called myShoeSize with a value of 42.5
  • String called myName with a value of "Jack"

Your program should print:

  • "My name is {myName}"
  • "My shoe size is {myShoeSize}"
  • "I am {myAge} years old"

Answers and Explanations

Question 2

[Maximum mark: 4]



Write a Java program that defines and initializes the following variables and then creates print statements:

  • double called myHeight with a value of 1.8
  • char called initial with a value of 'J'
  • boolean called isStudent with a value of true

Your program should print:

  • "My height is {myHeight} meters"
  • "My initial is {initial}"
  • "Am I a student? {isStudent}"

Answers and Explanations

Question 3

[Maximum mark: 4]



Write a Java program that defines and initializes the following variables and then creates print statements:

  • int called number1 with a value of 8
  • int called number2 with a value of 5
  • double called sum to store the result of adding number1 and number2
  • double called product to store the result of multiplying number1 and number2

Your program should print:

  • "The sum of {number1} and {number2} is {sum}"
  • "The product of {number1} and {number2} is {product}"

Answers and Explanations

Question 4

[Maximum mark: 4]



Write a Java program that defines and initializes the following variables and then creates print statements:

  • int called numberA with a value of 12
  • int called numberB with a value of 7
  • double called sum to store the result of adding numberA and numberB
  • double called difference to store the result of subtracting numberB from numberA
  • double called product to store the result of multiplying numberA and numberB
  • double called quotient to store the result of dividing numberA by numberB

Your program should print:

  • "The sum of {numberA} and {numberB} is {sum}"
  • "The difference when {numberB} is subtracted from {numberA} is {difference}"
  • "The product of {numberA} and {numberB} is {product}"
  • "The quotient when {numberA} is divided by {numberB} is approximately {quotient}"

Answers and Explanations

Question 5

[Maximum mark: 5]



Write a Java program which will allow the user to input their name and age.


Your program should print:

  • "Hi! My name is {name} and I am {age} years old!"

Answers and Explanations

Question 6

[Maximum mark: 4]



Write a Java program that performs the following tasks:

  • Prompt the user to input their name and age.
  • Calculate the year the user was born (assuming the current year is 2024).

Your program should print:

  • "Hi! My name is {name} and I am {age} years old!"
  • "I was born in {birthYear}"

Answers and Explanations

Question 7

[Maximum mark: 6]



Write a Java program that performs the following tasks:

  • Prompt the user to input their name and their monthly salary.
  • Calculate the following based on the user’s monthly salary:
    • Monthly bonus, which is 8% of the salary
    • Total monthly earnings including the bonus
    • Annual earnings including the bonus


Your program should print:

  • "Hi! My name is {name} and my monthly salary is ${salary}."
  • "Your monthly bonus is ${bonus}, making your total monthly earnings ${totalMonthlyEarnings}."
  • "Your annual earnings including the bonus are ${annualEarnings}."

Answers and Explanations

Question 8

[Maximum mark: 8]



Write a Java program that performs the following tasks:

  • Prompt the user to input their name, monthly salary, and the number of hours worked per week.
  • Calculate the following based on the user’s salary and hours worked:
    • Monthly bonus as 10% of the salary.
    • Hourly rate calculated as the monthly salary with the bonus divided by the total number of hours worked in a month (assuming 4 weeks per month).
    • Total monthly earnings including the bonus
    • Annual earnings including the bonus

Your program should print:

  • "Hi! My name is {name}, my monthly salary is ${salary}, and I work {hoursWorked} hours per week."
  • "Your hourly rate is ${hourlyRate}, and your monthly bonus is ${bonus}, making your total monthly earnings ${totalMonthlyEarnings}."
  • "Your annual earnings including the bonus are ${annualEarnings}."

Answers and Explanations