wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Basics

Total questions: 45

Worksheet time: 23mins

Name
Class
Date
1.

int x = 10;

long y = 10;

System.out.println(x == y);

a)

Compile-time error

b)

Runtime error

c)

true

d)

false

2.

Which statement about char literals is false?

a)

char c = 'A';

b)

char c = 65;

c)

char c = '\u0041';

d)

char c = "A";

3.

Which literal is invalid in Java?

a)

1_000_000

b)

_1000

c)

0xCAFE

d)

0b1010

4.

What happens here?
long l = 2147483648;

a)

l stores the value correctly

b)

Runtime overflow

c)

Compile-time error

d)

Value wraps around

5.

What is the output?
System.out.println(1.0 / 0);

a)

Infinity

b)

ArithmeticException

c)

NaN

d)

0

6.

Which is a valid Unicode character literal?

a)

'\u0041'

b)

'\u41'

c)

'u0041'

d)

"\\u0041"

7.

What is printed?
System.out.println('A' + 1);

a)

A1

b)

B

c)

66

d)

Compile-time error

8.

Which literal causes loss of precision?

a)

int i = 10;

b)

double d = 10;

c)

float f = 10.123456789f;

d)

long l = 10L;

9.

What is the output?
int x = 10;

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

a)

21

b)

22

c)

23

d)

Undefined

10.

Which operator has the highest precedence?

a)

*

b)

++

c)

=

d)

?:

11.

What is the output?

System.out.println(5 > 3 && 2 < 4 || 1 > 10);

a)

true

b)

false

c)

Compile-time error

d)

Runtime error

12.

What is the output?
int a = 5, b = 10;

System.out.println(a = b = 20);

a)

5

b)

10

c)

20

d)

Compile-time error

13.

What is the output?
System.out.println(10 << 2 + 1);

a)

41

b)

80

c)

20

d)

Compile-time error

14.

What is the output?
int x = 10;

System.out.println(x++ > 10 && ++x > 12);

a)

true

b)

false

c)

Compile-time error

d)

Runtime error

15.

What is the output?
System.out.println(1 + 2 + "3" + 4 + 5);

a)

339

b)

15

c)

12345

d)

"3345"

16.

What is the output of the following code?
int x = 5;

boolean result = true ? x++ == 5 : ++x == 7;

System.out.println("result = " + result + ", x = " + x);

a)

result = true, x = 6

b)

result = true, x = 5

c)

result = false, x = 6

d)

result = false, x = 7

17.

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

a)

int

b)

float

c)

String

d)

char

18.

What is the size of char data type in Java?

a)

8 bits

b)

16 bits

c)

32 bits

d)

Platform dependent

19.

Which of the following assignments causes a compile-time error?

a)

int x = 10;

b)

float f = 10;

c)

byte b = 128;

d)

double d = 10.5;

20.

Which statement about boolean is correct?

a)

Size is 1 bit

b)

Size is JVM dependent

c)

Can store 0 and 1

d)

Can be type-cast to int

21.

Which of the following is a widening conversion?

a)

int → byte

b)

double → float

c)

char → int

d)

long → int

22.

Who is known as the father of Java?

a)

Dennis Ritchie

b)

James Gosling

c)

Bjarne Stroustrup

d)

Guido van Rossum

23.

Originally, Java was named as:

a)

Java

b)

Oak

c)

Green

d)

Coffee

24.

Which command converts .java source file into .class file?

a)

java

b)

jvm

c)

javac

d)

jar

25.

What does a .class file contain?

a)

Source code

b)

Machine code

c)

Bytecode

d)

Assembly code

26.

Which component converts bytecode into machine code at runtime?

a)

javac

b)

JRE

c)

JVM

d)

JDK

27.

Which of the following is mandatory to run a Java program?

a)

JDK

b)

JVM

c)

javac

d)

JShell

28.

Which of the following is a superset of JRE?

a)

JVM

b)

JDK

c)

Bytecode

d)

javac

29.

Which tool is used to write and test Java code interactively?

a)

javap

b)

jar

c)

jshell

d)

jdb

30.

Which statement about class name and file name is correct?

a)

They must always be same

b)

They can never be same

c)

Must be same if class is public

d)

Java doesn’t care

31.

How many public classes are allowed in a single .java file?

a)

0

b)

1

c)

2

d)

Unlimited

32.

Which environment variable tells OS where to find javac and java?

a)

CLASSPATH

b)

PATH

c)

JAVA_HOME

d)

TEMP

33.

Why is Java called platform independent?

a)

It uses C internally

b)

Bytecode runs on any OS

c)

JVM is platform dependent

d)

Source code is same everywhere

34.

Which Java version introduced JShell?

a)

Java 6

b)

Java 7

c)

Java 8

d)

Java 9

35.

What is the 8-bit signed binary representation of decimal -5?

a)

00000101

b)

11111010

c)

11111011

d)

10000101

36.

What decimal value does the 8-bit signed binary 11111010 represent?

a)

-5

b)

-6

c)

250

d)

5

37.

What is the result of ~-5 in 8-bit signed binary?

a)

00000100

b)

00000101

c)

11111010

d)

11111011

38.

What is the 8-bit signed binary of decimal -128?

a)

01111111

b)

10000000

c)

11111111

d)

00000000

39.

What is the output?

System.out.println(7 / -3 + " " + 7 % -3);

a)

-2 1

b)

-2 -1

c)

-3 1

d)

-3 -1

40.

What is the result of ~0 in 8-bit signed binary?

a)

00000000

b)

11111111

c)

10000000

d)

00000001

41.

How many copies of a static variable exist if 100 objects of the class are created?

a)

0

b)

1

c)

100

d)

JVM dependent

42.

Which variable cannot have default value?

a)

Static variable

b)

Instance variable

c)

Local variable

d)

Class variable

43.

What happens if a local variable is used without initialization?

a)

Runtime error

b)

Garbage value

c)

Compile-time error

d)

JVM initializes it to 0

44.

Which variable is eligible for garbage collection?

a)

Static variable

b)

Instance variable

c)

Local primitive variable

d)

Object referenced by null

45.

What is the output?

class Test {

int x = 10;

static int y = 20;

}

public class Main {

public static void main(String[] args) {

Test t = null;

System.out.println(t.y);

}

}

a)

NullPointerException

b)

20

c)

Compile-time error

d)

0