1. What is Java?
a) A type of coffee
b) A programming language c)
An operating system d)
A type of computer
Answer: b
2. Which of the following is not a primitive data type in Java?
a) int
b) float
c) string
d) char
Answer: c
3. What is the output of the following Java code snippet?
java
int x = 5; int y = 2; System.out.println(x / y);
a) 7
b) 2.5
c) 2
d) 2.5 (as a double)
Answer: c
4. Which keyword is used to define a constant in Java?
a) constant
b) final
c) static
d) const
Answer: b
5. What is the purpose of the “public” access modifier in Java?
a) It makes a method or variable accessible only within the same class.
b) It makes a method or variable accessible from any class.
c) It restricts access to a method or variable.
d) It is used to define a constructor. Answer: b
6. What is the Java Virtual Machine (JVM)?
a) A virtual coffee machine
b) A virtualization software
c) A hardware component of a computer
d) A runtime environment for executing Java applications
Answer: d
7. Which keyword is used to create an instance of a class in Java?
a) create
b) instance
c) new
d) object
Answer: c
8. In Java, what is the purpose of the “break” statement?
a) To end a loop or switch statement prematurely
b) To skip the current iteration of a loop
c) To return a value from a method
d) To create a new class
Answer: a
9. What does the “static” keyword mean in Java?
a) It indicates that a variable or method belongs to an instance of a class.
b) It indicates that a variable or method belongs to the class itself, rather than to any specific instance of the class.
c) It makes a variable or method constant.
d) It is used to define a variable that cannot be modified. Answer: b
10. What is the purpose of the “try-catch” block in Java?
a) To declare a new exception
b) To handle exceptions that may occur during program execution
c) To create custom exceptions
d) To terminate the program
Answer: b
11. What is the superclass of all classes in Java?
a) Object
b) Class
c) Super
d) Parent
Answer: a
12. What is the keyword used to declare a method that does not return any value in Java?
a) void
b) null
c) noReturn
d) none
Answer: a
13. Which of the following is not a valid identifier in Java?
a) myVariable
b) 123Variable
c) _variableName
d) $variableName
Answer: b
14. What is the purpose of the “this” keyword in Java?
a) It refers to the current class.
b) It refers to the superclass.
c) It refers to the current instance of the class.
d) It is used to create new objects.
Answer: c
15. What is the default value for an uninitialized integer variable in Java?
a) 0
b) 1
c) -1
d) null
Answer: a
16. Which operator is used for comparing two objects for equality in Java?
a) ==
b) !=
c) .equals()
d) <>
Answer: c
17. What is the purpose of the “super” keyword in Java?
a) To access static members of a class.
b) To call the superclass constructor or method.
c) To create a new instance of a class.
d) To define a parent class.
Answer: b
18. In Java, an interface can have:
a) Instance variables
b) Constructors
c) Static methods
d) All of the above
Answer: c
19. What is the difference between an abstract class and an interface in Java?
a) Abstract classes can have constructors, interfaces cannot.
b) Interfaces can have instance variables, abstract classes cannot.
c) A class can implement multiple interfaces, but it can extend only one abstract class.
d) Abstract classes cannot have abstract methods, interfaces can.
Answer: c
20. What is the purpose of the “StringBuilder” class in Java?
a) To represent a fixed-size sequence of characters.
b) To create immutable strings.
c) To create mutable strings.
d) To concatenate strings.
Answer: c
21. Which keyword is used to define a method that can be overridden by subclasses in Java?
a) override
b) extend
c) virtual
d) final
Answer: d
22. Which of the following statements is true about Java’s garbage collection?
a) Developers must manually free memory using “free()” method.
b) Java automatically reclaims memory by deallocating objects that are no longer reachable.
c) Java does not support garbage collection.
d) Garbage collection can only be used with primitive data types.
Answer: b
23. What is the purpose of the “package” keyword in Java?
a) To declare a package.
b) To import a package.
c) To define a class.
d) To define a method.
Answer: a
24. Which Java keyword is used to exit a loop prematurely?
a) exit b)
break
c) continue
d) return
Answer: b
25. What is the access level of a default (package-private) modifier in Java?
a) Public
b) Protected
c) Private
d) Package-private
Answer: d
26. What is the purpose of the “transient” keyword in Java?
a) It is used to make a variable immutable.
b) It is used to define a class as serializable.
c) It is used to exclude a variable from the serialization process.
d) It is used to define a variable as constant. Answer: c
27. Which collection class in Java uses a dynamic array to store elements?
a) ArrayList
b) LinkedList
c) HashSet
d) TreeMap
Answer: a
28. What is the purpose of the “finally” block in a try-catch-finally statement in Java?
a) To declare a new exception.
b) To handle exceptions.
c) To ensure that a block of code is always executed, whether an exception is thrown or not.
d) To terminate the program.
Answer: c
29. Which of the following is a valid declaration of a multidimensional array in Java?
a) int[][] array = new int[][];
b) int[][] array = new int[3][4];
c) int[3][4] array = new int[][];
d) int[][] array = new int[3][];
Answer: b
30. What is the purpose of the “assert” keyword in Java?
a) To check the equality of two objects.
b) To terminate the program.
c) To test conditions during development and provide debugging information.
d) To create custom exceptions.
Answer: c