Professional Documents
Culture Documents
Java Variables 1
Java Variables 1
Java Variables 1
Which of the following types belongs to group of primitive data type and which belongs
to the group of non - primitive data type.
- integer,byte,float,double,string,array
Answer:
System.out.println("Hello World");
4. Inside the main() method insert a variable called text of type String and assign the
value to it. Print the value of result using System.out.println().
Answer:
6. int myHomeNumber;
myHomeNumber = 24;
System.out.println(myHomeNumber);
double myDoubleHomework;
myDoubleHomework = 44;
System.out.println(myDoubleHomework);
9. float myFloatHomework;
myFloatHomework = 4;
System.out.println(myFloatHomework);
10. Create a variable of type char and assign the value to it.
char myCharHomework;
myCharHomework = 'l';
System.out.println(myCharHomework);
boolean myBooleanHomework;
myBooleanHomework = true;
System.out.println(myBooleanHomework);
int myHomeWorkIntegerWithoutValue;
myHomeWorkIntegerWithoutValue = 11;
System.out.println(myHomeWorkIntegerWithoutValue);
12 .Declare a variable of type int without assigning the value, assign the value later.
int myNum;
myNum = 44;
System.out.println(myNum);
myNum =45;
System.out.println(myNum);
Use System.out.println method to print variables value but also use the correct operator
to concatenate two strings.
int r = 27;
int t = 27;
int p = r + t;
System.out.println("r= " +r);
System.out.println("t= " +t);
System.out.println("p= " +p);
String r2 = "27";
String t2 = "27";
String p1 = r2 + t2;
System.out.println("p1= " +p1);
15. Return the index (position) of the first occurrence of "e" in the following string:
Answer: System.out.println(txt.indexOf(“e”));
16. Create two variables of type int. Call the variables num1 and num2. Set an initial value
on both variables.
Create a third int variable, call it result and set its value equal to the sum of num1 and
num2.
17. Set an initial value to variables called x and y. Create variable called z, assign x - y to
it, and display the result.
19. Create three variables of the same type, by using a comma-separated list.
int h,v,s;
int h = 3, v = 4, s = 7;
20. Add the correct data type for the following variables:
int x= 1;
int r = 10;
int t = 5;
int k1=r * t;
int k2=r / t;
System.out.println("k1=" +k1);
int r = 10;
int t = 5;
int k1=r * t;
int k2=r / t;
System.out.println("k2=" +k2);
Or increase the value of the variable by 1 and after print the variable value.(use Increment
Operator or Assignment Operator)
System.out.println(pst+=1);
Decrease the value of the variable by 1.(use Increment Operator or Assignment Operator).
System.out.println(pst-=1);
int x = 10;
int y = 9;
System.out.println( ___________ );
System.out.println(x>=y);
System.out.println(x>y);
System.out.println(x!=y);
26. Create a variable of type int that other can not overwrite her existing value.