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
int
namedage
and assign it the value 25.Create a variable of type
double
namedtemperature
and assign it the value 98.6.Declare a variable of type
boolean
namedisRaining
and assign it the valuetrue
.Create a variable of type
char
namedgrade
and assign it the value'A'
.Declare a variable of type
String
namedname
and assign it the value "John Doe".Initialize three variables
num1
,num2
, andnum3
of typeint
with values 10, 20, and 30 respectively.Create a variable
pi
of typedouble
and assign it the value of pi (use theMath.PI
constant).Declare a variable
isEven
of typeboolean
and assign it the result of checking whethernum1
is an even number.Initialize a variable
initial
of typechar
with the initial letter of your first name.Declare a variable
city
of typeString
and assign it the name of your favorite city.Create a variable
isPalindrome
of typeboolean
and assign it the result of checking whether the string "madam" is a palindrome.Initialize variables
length
andwidth
of typedouble
with values 5.5 and 2.5 respectively.Declare a variable
isPositive
of typeboolean
and assign it the result of checking whethernum2
is positive.Create a variable
greeting
of typeString
and assign it the concatenated string "Hello, " and the value ofname
.Initialize a variable
totalStudents
of typeint
with the value 100.Declare a variable
isLeapYear
of typeboolean
and assign it the result of checking whether the current year is a leap year.Create variables
price1
andprice2
of typedouble
and assign them values 10.50 and 20.75 respectively.Declare a variable
isVowel
of typeboolean
and assign it the result of checking whether the character ingrade
is a vowel.Initialize variables
hoursWorked
andhourlyRate
of typedouble
with values 40.5 and 15.0 respectively.Declare a variable
isPrime
of typeboolean
and assign it the result of checking whethernum3
is a prime number.
These problems cover a variety of scenarios involving different data types and variable declarations in Java.
Last updated