Variables and Datatypes
Let's test your knowledge on variables and data types in Java with the following problem statements:
Declare a variable of type
intnamedageand assign it the value 25.Create a variable of type
doublenamedtemperatureand assign it the value 98.6.Declare a variable of type
booleannamedisRainingand assign it the valuetrue.Create a variable of type
charnamedgradeand assign it the value'A'.Declare a variable of type
Stringnamednameand assign it the value "John Doe".Initialize three variables
num1,num2, andnum3of typeintwith values 10, 20, and 30 respectively.Create a variable
piof typedoubleand assign it the value of pi (use theMath.PIconstant).Declare a variable
isEvenof typebooleanand assign it the result of checking whethernum1is an even number.Initialize a variable
initialof typecharwith the initial letter of your first name.Declare a variable
cityof typeStringand assign it the name of your favorite city.Create a variable
isPalindromeof typebooleanand assign it the result of checking whether the string "madam" is a palindrome.Initialize variables
lengthandwidthof typedoublewith values 5.5 and 2.5 respectively.Declare a variable
isPositiveof typebooleanand assign it the result of checking whethernum2is positive.Create a variable
greetingof typeStringand assign it the concatenated string "Hello, " and the value ofname.Initialize a variable
totalStudentsof typeintwith the value 100.Declare a variable
isLeapYearof typebooleanand assign it the result of checking whether the current year is a leap year.Create variables
price1andprice2of typedoubleand assign them values 10.50 and 20.75 respectively.Declare a variable
isVowelof typebooleanand assign it the result of checking whether the character ingradeis a vowel.Initialize variables
hoursWorkedandhourlyRateof typedoublewith values 40.5 and 15.0 respectively.Declare a variable
isPrimeof typebooleanand assign it the result of checking whethernum3is a prime number.
These problems cover a variety of scenarios involving different data types and variable declarations in Java.
Last updated
Was this helpful?