Java Online Test

This Java Online Test simulates a real online certification exam. You will be presented with Multiple-Choice Questions (MCQs) based on Core Java Concepts, where you will be given four options. You will select the best suitable answer for each question and then proceed to the next question without wasting time. You will get your online test score after finishing the complete test.

1. What is the default value of a boolean variable in Java?

a) false
b) true
c) null
d) 0

2. Which of the following is not a valid Java data type?

a) int
b) float
c) real
d) double

3. What is the output of the following Java code?

int x = 5;
int y = ++x - x++ + --x;
a) 5
b) 6
c) 7
d) 8

4. What does the following Java code snippet print?

System.out.println(8 % 3);
a) 2
b) 3
c) 2.67
d) 1

5. Which method signature is correct for the main method in a Java application?

a) public static void main(String args[])
b) public void static main(String[] args)
c) public void main(String[] args)
d) static public main(String args[])

6. What is the output of the following code?

int[] array = {1, 2, 3};
a) 1
b) 2
c) 3
d) An exception is thrown

7. In Java, which keyword is used to inherit a class?

a) super
b) this
c) extends
d) implements

8. What will the following Java code snippet output?

int a = 10;
int b = 20;
int min = (a < b) ? a : b;
a) 10
b) 20
c) true
d) false

9. What is the purpose of the final keyword in Java?

a) To make a variable re-assignable
b) To prevent inheritance
c) To create constants
d) To indicate the last variable in a class

10. What is the output of the following Java code?

for (int i = 0; i < 5; i++) {
    if (i % 2 == 0) continue;
    System.out.print(i + " ");
a) 0 2 4
b) 1 3
c) 1 3 5
d) 1 3 4

11. Which collection class allows you to grow or shrink its size and provides indexed access to its elements?

a) HashSet
b) TreeMap
c) ArrayList
d) HashMap

12. What does the following Java code snippet print?

String str1 = "Hello";
String str2 = "Hello";
System.out.println(str1 == str2);
a) true
b) false
c) Compilation error
d) None of the above

13. How do you ensure that a resource is automatically closed at the end of the statement's execution in Java?

a) Using the finally block
b) Using the close method explicitly
c) Using the try-with-resources statement
d) Using the shutdown hook

14. Which interface must an object implement to be directly usable in a for-each loop?

a) Set
b) List
c) Collection
d) Iterable

15. What is the output of the following Java code?

int i = 5;
i += i++ + ++i + i;
a) 22
b) 23
c) 24
d) 25

16. In Java, which of these access specifiers allows visibility only within the same package and subclasses?

a) private
b) protected
c) public
d) default

17. What is the result of compiling and running the following Java code snippet?

abstract class A {
    abstract void method();
A a = new A();
a) Compiles and runs without error
b) Compiles but throws a runtime exception
c) Does not compile
d) Compiles but does nothing

18. Which of the following is not a valid interface in Java?

a) Runnable
b) Readable
c) Startable
d) Cloneable

19. What does the synchronized keyword do in a Java method?

a) Delays the execution of the method
b) Stops the execution of other threads
c) Allows multiple threads to access the method simultaneously
d) Prevents multiple threads from executing the method at the same time

20. What is the output of the following Java code?

int a = 10, b = 20;
System.out.println("Result: " + a + b);
a) Result: 30
b) Result: 1020
c) Result: 20
d) Result: Result

21. Which class is used in Java to handle exceptions?

a) String
b) Exception
c) Error
d) Throwable

22. What is true about the super keyword in Java?

a) It refers to the current class instance
b) It is used to access methods of the base class
c) It overrides a method in the superclass
d) It is used to declare static methods

23. What is the output of the following Java code snippet?

boolean a = true, b = false;
System.out.println(a && b || !a);
a) true
b) false
c) Compilation error
d) None of the above

24. Which keyword is used in Java for including other classes in a source file?

a) package
b) include
c) import
d) extend

25. How is a thread created and started in Java?

a) By extending the Thread class and calling the start() method
b) By implementing the Runnable interface and calling the run() method
c) By using the ExecutorService to submit tasks
d) All of the above

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top