wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Ch 02B: Variables & Data Types

Total questions: 16

Worksheet time: 10mins

Name
Class
Date
1.

Which of the following is NOT a primitive data type in Java?

a)

int

b)

String

c)

boolean

d)

double

2.

What is the default value of a Java `int` variable that is declared as a class member but not initialized?

a)

0

b)

null

c)

false

d)

undefined

3.

Which keyword is used to declare a constant variable in Java?

a)

static

b)

final

c)

const

d)

constant

4.

Which of the following is the correct syntax to declare a variable of type `double` in Java?

a)

double value;

b)

Double value;

c)

double = value;

d)

value double;

5.

Which primitive data type would you use to store a single Unicode character in Java?

a)

char

b)

byte

c)

String

d)

boolean

6.

Which of the following statements correctly casts an `int` variable named `num` to a `double` in Java?

a)

double num = (double) num;

b)

double num = double(num);

c)

double num = num.toDouble();

d)

double num = (int) num;

7.

Which of the following code snippets will result in a compilation error due to type incompatibility?

a)

int a = 5.5;

b)

double b = 5;

c)

char c = 'A';

d)

boolean d = true;

8.

What will be the output of the following code? ```java final int MAX = 100; MAX = 200; System.out.println(MAX); ```

a)

100

b)

200

c)

Compilation error

d)

0

9.

Given the following code, what will be printed? ```java int a = 5; double b = a / 2; System.out.println(b); ```

a)

2.5

b)

2.0

c)

2

d)

2.00

10.

Consider the following code: ```java public class Test { public static void main(String[] args) { int x = 10; { int x = 20; System.out.println(x); } System.out.println(x); } } ``` What will happen when you try to compile and run this code?

a)

It prints 20 and then 10

b)

It prints 10 and then 20

c)

Compilation error due to variable redeclaration

d)

It prints 20 twice

11.

Suppose you want to declare a constant for the value of π\pi in Java. Which of the following is the best way to do it?

a)

final double pi = 3.14159;

b)

double pi = 3.14159;

c)

const double pi = 3.14159;

d)

static double pi = 3.14159;

12.

What is the value stored in x?

double x = (int) (1.8 * 2.0)

a)

3.0

b)

3

c)

3.6

d)

Error

13.

What is the resulting value of

(int)23.5

a)

24

b)

23

c)

23.5

d)

trick question, it's an error

14.

Match the following

a)
int
1.

5

b)
float
2.

5.2

c)
char
3.

'a'

d)
String
4.

"abc"

e)
boolean
5.

true

15.

Which of the following is NOT a numeric data type

a)

int

b)

double

c)

float

d)

decimal

16.
Which type is incorrect?
a)
int
b)
double
c)
float
d)
array