Work Book – Session2
DataTypes, Variable and Operators
1.
Data Type cross word puzzle
2. From the following list, Circle the statements that would be
legal if these lines were in a single method
a.
int x = 34.5;
b.
boolean boo = x;
c.
int g = 17;
d.
int y = g;
e.
y = y + 10;
f.
s = y;
g.
byte b = 3;
h.
byte v = b;
i.
short n = 12;
j.
v = n;
k.
byte k = 128;
3. Fill the blanks / Choose the best answer
1. Every letter in a Java keyword is in lowercase?
true
false
2. Which of the following is a valid identifier?
$343
class
9X
8+9
radius
3. Which of the following are correct names for variables according to Java naming conventions?
radius
Radius
RADIUS
findArea
FindArea
4. Which of the following are correct ways to declare variables?
int length; int width;
int length, width;
int length; width;
int length, int width;
5.____________ is the Java assignment operator.
==
:=
=
=:
6. To assign a value 1 to variable x, you write
1 = x;
1 := x;
x = 1;
x == 1;
x := 1;
7. Which of the following assignment statements is incorrect?
i = j = k = 1;
i = 1; j = 1; k = 1;
i = 1 = j = 1 = k = 1;
i == j == k == 1;
8. To improve readability and maintainability, you should declare _________ instead of using
literal values such as 3.14159.
variables
methods
constants
classes
9. According to Java naming convention, which of the following names can be variables?
FindArea
findArea
totalLength
TOTAL_LENGTH
class
10. Which of these data types requires the most amount of memory?
long
int
short
byte
11. Analyze the following code:
public class Test {
public static void main(String[] args) {
int n = 10000 * 10000 * 10000;
System.out.println("n is " + n);
}
}
The program displays n is 1000000000000
The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This
causes an overflow and the program is aborted.
The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This
causes an overflow and the program continues to execute because Java does not report errors on
overflow.
The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This
causes an underflow and the program is aborted.
The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This
causes an underflow and the program continues to execute because Java does not report errors on
underflow.
12. What is the result of 45 / 4?
10
11.25
11
12
13. Which of the following expression results in a value 1?
2%1
25 % 5
15 % 4
37 % 6
14. 25 % 1 is _____
1
4
2
0
3
15. -25 % 5 is
_____
1
4
2
0
3
16. -24 % -5 is _____
3
-4
-3
0
4
17. Math.pow(2, 3) returns __________.
9
8
9.0
8.0
18. The __________ method returns a raised to the power of b.
Math.power(a, b)
Math.exponent(a, b)
Math.pow(a, b)
Math.pow(b, a)
19. To declare an int variable number with initial value 2, you write
int number = 2L;
int number = 2l;
int number = 2;
int number = 2.0;
20. Analyze the following code.
public class Test {
public static void main(String[] args) {
int month = 09;
System.out.println("month is " + month);
}
}
The program displays month is 09
The program displays month is 9
The program displays month is 9.0
The program has a syntax error, because 09 is an incorrect literal value.
21. The expression 4 + 20 / (3 - 1) * 2 is evaluated to
4
20
24
9
25
22. To add a value 1 to variable x, you write
1 + x = x;
x = x + 1;
x += 1;
x = 1 + x;
x := 1;
23. To add number to sum, you write (Note: Java is case-sensitive)
number += sum;
number = sum + number;
sum = Number + sum;
sum += number;
sum = sum + number;
24. Suppose x is 1. What is x after x += 2?
0
1
2
3
4
25. What is x after the following statements?
int x = 1;
x *= x + 1;
x is 1.
x is 2.
x is 3.
x is 4.
26. Which of the following statements are the same?
(A) x -= x + 4
(B) x = x + 4 - x
(C) x = x - (x + 4)
(A) and (B) are the same
(A) and (C) are the same
(B) and (C) are the same
(A), (B), and (C) are the same
27. Are the following four statements equivalent?
number += 1;
number = number + 1;
number++;
++number;
Yes
No
28. What is the value of i printed?
public class Test {
public static void main(String[] args) {
int j = 0;
int i = ++j + j * 5;
System.out.println("What is i? " + i);
}
}
0
5
1
6
29. What is the value of i printed in the following code?
public class Test {
public static void main(String[] args) {
int j = 0;
int i = j++ + j * 5;
System.out.println("What is i? " + i);
}
}
0
5
1
6
30. What is y displayed in the following code?
public class Test {
public static void main(String[] args) {
int x = 1;
int y = x++ + x;
System.out.println("y is " + y);
}
}
y is 1.
y is 3.
y is 2.
y is 4.
31. What is y displayed?
public class Test {
public static void main(String[] args) {
int x = 1;
int y = x + x++;
System.out.println("y is " + y);
}
}
y is 1.
y is 2.
y is 3.
y is 4.
32. To assign a double variable d to a float variable x, you write
x = (long)d
x = (int)d;
x = d;
x = (float)d;
33. Which of the following expressions will yield 0.5?
1/2
1.0 / 2
(double) (1 / 2)
(double) 1 / 2
1 / 2.0
34. What will be displayed by the following code?
double x = 5.5;
int y = (int)x;
System.out.println("x is " + x + " and y is " + y);
x is 5 and y is 6
x is 6.0 and y is 6.0
x is 6 and y is 6
x is 5.5 and y is 5
x is 5.5 and y is 5.0
35. Which of the following assignment statements is illegal?
float f = -34;
int t = 23;
short s = 10;
int t = (int)false;
int t = 4.5;
36. What is the value of (double)5/2?
2
2.5
3
2.0
3.0
37. Suppose i is an int type variable. Which of the following statements display the character
whose Unicode is stored in variable i?
System.out.println(i);
System.out.println((char)i);
System.out.println((int)i);
System.out.println(i + " ");
38. The Unicode of 'a' is 97. What is the Unicode for 'c'?
96
97
98
99
39. Will System.out.println((char)4) display 4?
Yes
No
40. What will be displayed by System.out.println('z' - 'a')?
25
26
a
z
41. An int variable can hold __________.
'x'
120
120.0
"x"
"120"
42. Which of the following assignment statements is correct?
char c = 'd';
char c = 100;
char c = "d";
char c = "100";
43. The expression "Java " + 1 + 2 + 3 evaluates to ________.
Java123
Java6
Java 123
java 123
Illegal expression
44. Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to ________.
66
B
A1
Illegal expression
Program: How to test the arithmetic operator? Rearrange the
coding to get the given output.
2. Program: Fill the space by declaring the variables according to the output.
Cloze test
Output
Tiny Programs
1.
Class ByteDemo {
public static void main(String[] args) {
byte x=10;
byte y=20;
byte z= (byte) (x+y);
System.out.println(z);
}
}
Output?
2.
Class ShortDemo {
public static void main(String[] args) {
short x=10;
short y=20;
short z= (short) (x+y);
System.out.println(z);
}
}
Output?
3.
Class IntDemo {
public static void main(String[] args) {
int x=10;
int y=20;
int z=x+y;
System.out.println(z);
}
}
Output?
4.
Class LongDemo {
public static void main(String[] args) {
long x=109876677777l;
long y=20666677766l;
long z=x+y;
System.out.println(z);
}
}
Output?
5.
Class FloatDemo {
public static void main(String[] args) {
float x=231231.0031f;
float y= 53423423434.43231f;
float z= x+y;
System.out.println(z);
}
}
Output?
6.
Class DoubleDemo {
public static void main(String[] args) {
double x=231231.0031f;
double y= 53423423434.43231f;
double z= x+y;
System.out.println(z);
}
}
Output?
7.
Class CharDemo {
public static void main(String[] args) {
char a='a';
System.out.println(a);
}
}
Output?
8.
Class BooleanDemo {
public static void main(String[] args) {
boolean x = false;
System.out.println(x);
}
}
Output?
Pick & Code
© Copyright 2026 Paperzz