Java Variables 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

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:

1. Primitive: integer, byte, float, double,


2. Non-Primitive: string, array

2. Write code to output "Hello World".

System.out.println("Hello World");

3. Insert the missing part:

Single-line comments start with _//_

Multi-line comments start with_/*__  and ends with */__ .

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:

String text = "Domasna zadaca";


System.out.println(text);

5. Create a variable of type int and assign the value to it.

6. int myHomeNumber;
myHomeNumber = 24;
System.out.println(myHomeNumber);

7. Create a variable of type double and assign the value to it.

double myDoubleHomework;
myDoubleHomework = 44;
System.out.println(myDoubleHomework);

8. Create a variable of type float and assign the value to it.

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);

10.Create a variable of type booleanand assign the value to it.

boolean myBooleanHomework;
myBooleanHomework = true;
System.out.println(myBooleanHomework);

11.Declare a variable of type int without assigning the value.

int myHomeWorkIntegerWithoutValue;

After declaring variable assign the value later.

myHomeWorkIntegerWithoutValue = 11;

Print the variable value.

System.out.println(myHomeWorkIntegerWithoutValue);

12 .Declare a variable of type int without assigning the value, assign the value later.

Assign a new value to an existing variable, to overwrite the previous value.

Print the variable value.

int myNum;
myNum = 44;
System.out.println(myNum);
myNum =45;
System.out.println(myNum);

13. Create two variables firstname and lastname of type string.

Use System.out.println method to print variables value but also use the correct operator
to concatenate two strings.

String firstName1 = "Ljubisha";


String lastName1 = "Milosov";
String praznoMesto1 = " ";
System.out.println(firstName1+" " +lastName1);
System.out.println(firstName1+" ".concat( lastName1));

14. Create two variables num3 and num4 of type int.


Use System.out.println method to print variables value but also use the correct operator
to concatenate two int.

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:

String txt = "Hello Everybody";

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.

Print the result.

int jub1 = 13;


int jub2 =7
int result = jub1 + jub2;
System.out.println("result=" +result);

17. Set an initial value to variables called x and y. Create variable called z, assign x - y to
it, and display the result.

18. int x = 13;


int y =7;
int z = jub1 - jub2;
System.out.println("z=" +z);

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;

float num= 8.9f;

char letter = 'A';

boolean result = false;

String text = "Hello World";


21. Multiply 10 with 5, and print the result.

int r = 10;
int t = 5;

int k1=r * t;
int k2=r / t;
System.out.println("k1=" +k1);

22. Divide 10 by 5, and print the result.

int r = 10;
int t = 5;

int k1=r * t;
int k2=r / t;
System.out.println("k2=" +k2);

23. Create variable of type int and assign value 10 to it.

Print the variable value increased by 1.

Or increase the value of the variable by 1 and after print the variable value.(use Increment
Operator or Assignment Operator)

int pst = 10;


System.out.println(pst++);
System.out.println(pst);

System.out.println(pst+=1);

24. Create variable of type int and assign value 10 to it.

Decrease the value of the variable by 1.(use Increment Operator or Assignment Operator).

After print the variable value.

int pst = 10;


System.out.println(--pst);
System.out.println(pst);

System.out.println(pst-=1);

25. Fill in the missing pats to print the result true:

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.

Print the result.

final int MY_FINAL = 88;


System.out.println(MY_FINAL);

You might also like