WorksheetsJava Programming Quiz 2
Total questions: 20
Worksheet time: 10mins
What is the correct file extension for a Java source file?
.class
.java
.exe
.jar
Which of the following is true about writing Java code?
Java is case-insensitive
Class names must match the filename
Java allows multiple public classes in a single file
A Java program must have a function named run()
What is the correct naming convention for Java class names?
camelCase
UPPERCASE
PascalCase
snake_case
Which of the following is the correct main method declaration in Java?
public static void Main(String args[])
static public void main(String[] args)
public void static main(String args)
public static void main()
Which of the following is NOT a valid identifier in Java?
_myVariable
$myVar
123abc
myVar123
Which statement about Java identifiers is correct?
Identifiers can start with a digit
Identifiers can use special symbols like @ and #
Identifiers are case-sensitive
Java has a predefined list of identifiers
Which of the following is the size of int in Java?
2 bytes
4 bytes
8 bytes
Depends on the system
What is the default data type of a decimal number in Java?
int
float
double
long
Which of the following data types has the smallest size?
float
byte
int
long
What is the size of a double data type in Java?
2 bytes
4 bytes
8 bytes
16 bytes
What is the correct representation of a binary literal in Java?
B1010
0b1010
2b1010
1010b
Which of the following is a valid way to represent a floating-point literal in Java?
10.5f
10.5
10.5dF
10.5L
Which of the following represents a character literal correctly in Java?
'A'
"A"
A
char A = "A";
What is true about local variables in Java?
They must be initialized before use
They can be accessed from any class
They have a default value of null
They are automatically static
What happens if a local variable is not initialized before use?
It gets a default value
It will compile but throw an error at runtime
A compilation error occurs
It works fine if inside a loop
What will happen if the main method is missing in a Java program?
The program will still execute
The compiler will add it automatically
The program will not compile
Java will assume a default main method
What happens if a semicolon (;) is missing in a Java statement?
The program will run with a warning
Compilation error
It will be automatically fixed
No effect
What is the error in the following Java statement? int number = "10";
"10" is a String and cannot be assigned to int
"10" should be enclosed in single quotes
"10" should be assigned to a char
No error
How is a hexadecimal literal represented in Java?
Prefix with 0x
Prefix with 0h
Prefix with 0b
Prefix with 0o
Which of the following represents an octal literal in Java?
0x17
017
0o17
17o
