wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Programming Quiz 2

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

What is the correct file extension for a Java source file?

a)

.class

b)

.java

c)

.exe

d)

.jar

2.

Which of the following is true about writing Java code?

a)

Java is case-insensitive

b)

Class names must match the filename

c)

Java allows multiple public classes in a single file

d)

A Java program must have a function named run()

3.

What is the correct naming convention for Java class names?

a)

camelCase

b)

UPPERCASE

c)

PascalCase

d)

snake_case

4.

Which of the following is the correct main method declaration in Java?

a)

public static void Main(String args[])

b)

static public void main(String[] args)

c)

public void static main(String args)

d)

public static void main()

5.

Which of the following is NOT a valid identifier in Java?

a)

_myVariable

b)

$myVar

c)

123abc

d)

myVar123

6.

Which statement about Java identifiers is correct?

a)

Identifiers can start with a digit

b)

Identifiers can use special symbols like @ and #

c)

Identifiers are case-sensitive

d)

Java has a predefined list of identifiers

7.

Which of the following is the size of int in Java?

a)

2 bytes

b)

4 bytes

c)

8 bytes

d)

Depends on the system

8.

What is the default data type of a decimal number in Java?

a)

int

b)

float

c)

double

d)

long

9.

Which of the following data types has the smallest size?

a)

float

b)

byte

c)

int

d)

long

10.

What is the size of a double data type in Java?

a)

2 bytes

b)

4 bytes

c)

8 bytes

d)

16 bytes

11.

What is the correct representation of a binary literal in Java?

a)

B1010

b)

0b1010

c)

2b1010

d)

1010b

12.

Which of the following is a valid way to represent a floating-point literal in Java?

a)

10.5f

b)

10.5

c)

10.5dF

d)

10.5L

13.

Which of the following represents a character literal correctly in Java?

a)

'A'

b)

"A"

c)

A

d)

char A = "A";

14.

What is true about local variables in Java?

a)

They must be initialized before use

b)

They can be accessed from any class

c)

They have a default value of null

d)

They are automatically static

15.

What happens if a local variable is not initialized before use?

a)

It gets a default value

b)

It will compile but throw an error at runtime

c)

A compilation error occurs

d)

It works fine if inside a loop

16.

What will happen if the main method is missing in a Java program?

a)

The program will still execute

b)

The compiler will add it automatically

c)

The program will not compile

d)

Java will assume a default main method

17.

What happens if a semicolon (;) is missing in a Java statement?

a)

The program will run with a warning

b)

Compilation error

c)

It will be automatically fixed

d)

No effect

18.

What is the error in the following Java statement? int number = "10";

a)

"10" is a String and cannot be assigned to int

b)

"10" should be enclosed in single quotes

c)

"10" should be assigned to a char

d)

No error

19.

How is a hexadecimal literal represented in Java?

a)

Prefix with 0x

b)

Prefix with 0h

c)

Prefix with 0b

d)

Prefix with 0o

20.

Which of the following represents an octal literal in Java?

a)

0x17

b)

017

c)

0o17

d)

17o