NEW
Font size
WorksheetsJava Basics
Total questions: 45
Worksheet time: 23mins
int x = 10;
long y = 10;
System.out.println(x == y);
Compile-time error
Runtime error
true
false
Which statement about char literals is false?
char c = 'A';
char c = 65;
char c = '\u0041';
char c = "A";
Which literal is invalid in Java?
1_000_000
_1000
0xCAFE
0b1010
What happens here?
long l = 2147483648;
l stores the value correctly
Runtime overflow
Compile-time error
Value wraps around
What is the output?
System.out.println(1.0 / 0);
Infinity
ArithmeticException
NaN
0
Which is a valid Unicode character literal?
'\u0041'
'\u41'
'u0041'
"\\u0041"
What is printed?
System.out.println('A' + 1);
A1
B
66
Compile-time error
Which literal causes loss of precision?
int i = 10;
double d = 10;
float f = 10.123456789f;
long l = 10L;
What is the output?
int x = 10;
System.out.println(x++ + ++x);
21
22
23
Undefined
Which operator has the highest precedence?
*
++
=
?:
What is the output?
System.out.println(5 > 3 && 2 < 4 || 1 > 10);
true
false
Compile-time error
Runtime error
What is the output?
int a = 5, b = 10;
System.out.println(a = b = 20);
5
10
20
Compile-time error
What is the output?
System.out.println(10 << 2 + 1);
41
80
20
Compile-time error
What is the output?
int x = 10;
System.out.println(x++ > 10 && ++x > 12);
true
false
Compile-time error
Runtime error
What is the output?
System.out.println(1 + 2 + "3" + 4 + 5);
339
15
12345
"3345"
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);
result = true, x = 6
result = true, x = 5
result = false, x = 6
result = false, x = 7
Which of the following is not a primitive data type in Java?
int
float
String
char
What is the size of char data type in Java?
8 bits
16 bits
32 bits
Platform dependent
Which of the following assignments causes a compile-time error?
int x = 10;
float f = 10;
byte b = 128;
double d = 10.5;
Which statement about boolean is correct?
Size is 1 bit
Size is JVM dependent
Can store 0 and 1
Can be type-cast to int
Which of the following is a widening conversion?
int → byte
double → float
char → int
long → int
Who is known as the father of Java?
Dennis Ritchie
James Gosling
Bjarne Stroustrup
Guido van Rossum
Originally, Java was named as:
Java
Oak
Green
Coffee
Which command converts .java source file into .class file?
java
jvm
javac
jar
What does a .class file contain?
Source code
Machine code
Bytecode
Assembly code
Which component converts bytecode into machine code at runtime?
javac
JRE
JVM
JDK
Which of the following is mandatory to run a Java program?
JDK
JVM
javac
JShell
Which of the following is a superset of JRE?
JVM
JDK
Bytecode
javac
Which tool is used to write and test Java code interactively?
javap
jar
jshell
jdb
Which statement about class name and file name is correct?
They must always be same
They can never be same
Must be same if class is public
Java doesn’t care
How many public classes are allowed in a single .java file?
0
1
2
Unlimited
Which environment variable tells OS where to find javac and java?
CLASSPATH
PATH
JAVA_HOME
TEMP
Why is Java called platform independent?
It uses C internally
Bytecode runs on any OS
JVM is platform dependent
Source code is same everywhere
Which Java version introduced JShell?
Java 6
Java 7
Java 8
Java 9
What is the 8-bit signed binary representation of decimal -5?
00000101
11111010
11111011
10000101
What decimal value does the 8-bit signed binary 11111010 represent?
-5
-6
250
5
What is the result of ~-5 in 8-bit signed binary?
00000100
00000101
11111010
11111011
What is the 8-bit signed binary of decimal -128?
01111111
10000000
11111111
00000000
What is the output?
System.out.println(7 / -3 + " " + 7 % -3);
-2 1
-2 -1
-3 1
-3 -1
What is the result of ~0 in 8-bit signed binary?
00000000
11111111
10000000
00000001
How many copies of a static variable exist if 100 objects of the class are created?
0
1
100
JVM dependent
Which variable cannot have default value?
Static variable
Instance variable
Local variable
Class variable
What happens if a local variable is used without initialization?
Runtime error
Garbage value
Compile-time error
JVM initializes it to 0
Which variable is eligible for garbage collection?
Static variable
Instance variable
Local primitive variable
Object referenced by null
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);
}
}
NullPointerException
20
Compile-time error
0
