Java Questions
1. What is the default value of a boolean variable in Java?
a) true
b) false
c) null
d) undefined
2. Which of the following is not a valid keyword in Java?
a) package
b) import
c) method
d) super
3. What will be the output of the following code?
int x = 5;
int y = 10;
System.out.println(x + y + " is the sum");
a) 15 is the sum
b) 510 is the sum
c) 15 is the sum.
d) Compilation error
4. What is the size of int data type in Java?
a) 16 bits
b) 32 bits
c) 64 bits
d) 128 bits
5. Which of the following is true about constructors in Java?
a) A constructor cannot have parameters.
b) A constructor has the same name as the class.
c) A constructor is automatically called when an object is created.
d) All of the above.
6. What is the output of the following code snippet?
String str = "Hello";
str.concat(" World");
System.out.println(str);
a) Hello World
b) Hello
c) World
d) Compilation error
7. Which method is used to find the length of a string in Java?
a) length()
b) size()
c) getLength()
d) getSize()
8. Which of the following is the root class of all classes in Java?
a) Object
b) Class
c) System
d) Throwable
9. What is the output of the following code?
int x = 2;
int y = 3;
System.out.println(x++ + ++y);
a) 5
b) 6
c) 4
d) 7
10. In Java, which of the following collection classes does not allow
duplicate elements?
a) HashSet
b) TreeSet
c) LinkedList
d) Both a and b
11. Which exception is thrown when you divide by zero in Java?
a) ArithmeticException
b) NullPointerException
c) IOException
d) ArrayIndexOutOfBoundsException
12. What is the default initial value of an instance variable in Java?
a) null
b) 0
c) false
d) Undefined
13. Which of the following is true about Java arrays?
a) Arrays in Java are dynamic in size.
b) Arrays in Java can hold different types of elements.
c) Arrays in Java are indexed from 1.
d) None of the above.
14. What is the purpose of the final keyword in Java?
a) To define a constant
b) To prevent inheritance
c) To prevent method overriding
d) All of the above
15. Which of the following access modifiers in Java can be applied to
classes, methods, and variables?
a) private
b) protected
c) public
d) All of the above
16.. Which component of the Java ecosystem is responsible for providing
libraries, Java Virtual Machine (JVM), and other components to run
applications written in Java?
1. JDK
2. JRE
3. JVM
4. JCL
17. What is the primary purpose of the Java Development Kit (JDK)?
1. To execute Java applications
2. To provide an environment for developing Java applications
3. To interpret Java bytecode into machine code
4. To compile Java bytecode into native code
18.
Which of the following statements is true regarding the relationship
between JDK, JRE, and JVM?
1. The JDK includes the JRE, which in turn includes the JVM.
2. The JVM includes the JDK, which in turn includes the JRE.
3. The JRE includes the JDK, which in turn includes the JVM.
4. The JDK includes the JVM, which in turn includes the JRE.
19 . Which tool is provided by the JDK but not by the JRE?
1. JVM
2. javac
3. java
4. javaw
20. What role does the JVM play in the Java execution process?
1. It provides a development environment for Java programmers.
2. It converts Java source code into bytecode.
3. It interprets and executes Java bytecode.
4. It manages memory allocation for the operating system.