wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Test - 1

Total questions: 40

Worksheet time: 40mins

Name
Class
Date
1.
Which keyword is used to create a subclass in Java?
a)

extends

b)

super

c)

this

d)

class

2.
What is the correct syntax for declaring an array in Java?
a)
int arrayName[];
b)
int[] arrayName;
c)
int arrayName;
d)
Both A and B
3.
Which keyword is used to define a method that cannot be overridden in a subclass?
a)

static

b)

final

c)

private

d)

public

4.
Which keyword is used to handle exceptions in Java?
a)
try
b)
catch
c)
throw
d)
All of the above
5.
Which of the following is a valid method declaration in Java?
a)

public void myMethod(int x, y)

b)

public void myMethod(int x, int y)

c)

public void myMethod(int x)

d)

None of the above

6.
Which of the following is a valid declaration of a static method in a Java class?
a)
public static int myMethod()
b)
static public int myMethod()
c)
public int static myMethod()
d)
All of the above
7.
Which of the following is not a primitive data type in Java?
a)
int
b)
double
c)
boolean
d)
String
8.
Which of the following is not a valid way to declare an integer variable in Java?
a)
int x;
b)
int x = 5;
c)
int x, y;
d)
int x = "5";
9.
Which of the following is true about constructors in Java?
a)
They have a return type.
b)
They have the same name as the class.
c)
They can be overridden in a subclass.
d)
They can be static.
10.
Which of the following is a valid way to declare a constant in Java?
a)
public int constant = 5;
b)
static final int constant = 5;
c)
final static int constant = 5;
d)
All of the above
11.
Which of the following is true about interfaces in Java?
a)
They can have constructors.
b)
They can be instantiated.
c)
They can be used for multiple inheritance.
d)
They can have instance variables.
12.
Which of the following is true about static variables in Java?
a)
They are initialized when the class is instantiated.
b)
They are shared among all instances of the class.
c)
They can be accessed using the this keyword.
d)
They can be overridden in a subclass.
13.
Which of the following is not a valid way to handle exceptions in Java?
a)
try-catch block
b)
try-finally block
c)
throw keyword
d)
try-else block
14.
Which of the following is true about inheritance in Java?
a)
A subclass can inherit from multiple superclasses.
b)
A subclass can access the private members of its superclass.
c)
A subclass can override the methods of its superclass.
d)
A subclass must have more instance variables than its superclass.
15.
Which of the following is a valid way to declare a variable of the class type MyClass?
a)
MyClass x = new MyClass();
b)
MyClass x = new();
c)
Both A and B are correct.
d)
Neither A nor B is correct.
16.
Which of the following is true about encapsulation in Java?
a)
It is the process of hiding the implementation details of a class.
b)
It is the process of wrapping code and data together into a single unit.
c)
It is the process of creating a new class from an existing class.
d)
It is the process of making a class available to other classes.
17.
Which of the following is true about polymorphism in Java?
a)
It is the process of creating a new class from an existing class.
b)
It is the ability of an object to take on many forms.
c)
It is the process of hiding the implementation details of a class.
d)
It is the process of wrapping code and data together into a single unit.
18.
Which of the following is true about the finally block in Java?
a)
It is executed before the try block.
b)
It is executed after the try and/or catch blocks.
c)
It is executed only if an exception is not caught.
d)
It is executed only if an exception is caught.
19.
Which of the following is not a valid modifier in Java?
a)
public
b)
protected
c)
private
d)
sealed
20.
Which of the following is true about the break statement in Java?
a)
It is used to skip the rest of the loop and continue with the next iteration.
b)
It is used to exit the entire loop.
c)
Both A and B are correct
d)
Neither A nor B are correct
21.
Which of the following is true about the continue statement in Java?
a)
It is used to skip the rest of the loop and continue with the next iteration.
b)
It is used to exit the entire loop.
c)
Both A and B are correct
d)
None of the above
22.
Which of the following is true about the throw statement in Java?
a)
It is used to catch an exception.
b)
It is used to declare a method.
c)
It is used to exit a loop.
d)
It is used to throw an exception.
23.
What is the output of the following code snippet?
a)
Something did not work!
b)
Something went wrong!
c)
Exception in thread "main" java.lang.Exception: Something went wrong!
d)
None of the above
24.
What is the output of the following code snippet?
a)
None of the options
b)
x is negative
c)
x is positive
d)
x is zero
25.
What is the output of the following code snippet?
a)
x is 5 and y is 5
b)
x is 6 and y is 5
c)
x is 5 and y is 6
d)
None of the above
26.
What is the output of the following code snippet?
a)
one
b)
two
c)
three
d)
None of the above
27.
What is the output of the following code snippet?
a)
-1
b)
0
c)
1
d)
None of the above
28.
What is the output of the following code snippet?
a)
x is 5
b)
Division by zero
c)
Exception in thread "main" java.lang.ArithmeticException: / by zero
d)
None of the above
29.
What is the output of the following code snippet?
a)
1
b)
2
c)
4
d)
3
30.
What is the output of the following code snippet?
a)
1
b)
4
c)
2
d)
3
31.
What is the output of the following code snippet?
a)
x is greater than y
b)
x is less than or equal to y
c)
x is less than y
d)
None of the options
32.
What is the output of the following code snippet?
a)
x is greater than or equal to y
b)
x is less than y
c)
x is greater than y
d)
x is equal to y
33.
What is the output of the following code snippet?
a)
Hello
b)
olleH
c)
ollHe
d)
None of the above
34.
What is the output of the following code snippet?
a)
1, 2, 3
b)
123
c)
1 2 3
d)
None of the above
35.
What is the output of the following code snippet?
a)
1 2 3 4 5
b)
0 1 2 3 5
c)
1 2 3 4
d)
0 1 2 3 4
36.
What is the output of the following code snippet?
a)
W
b)
World
c)
Hello
d)
H
37.
What is the output of the following code snippet?
a)
x + y = (5 + 3)
b)
8
c)
x + y = 5 + 3
d)
x + y = 8
38.
What is the output of the following code snippet?
a)
x is less than 5
b)
x is greater than 5
c)
x is less than or equal to 5
d)
No output will be printed
39.
What is the output of the following code snippet?
a)
o
b)
H
c)
l
d)
W
40.
What is the output of the following code snippet?
a)
1.6667
b)
1
c)
2
d)
0