Java Sample Questions 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Sure!

Here are 50 fill-in-the-blank questions and 50 multiple-choice questions on Java:

Fill-in-the-Blank Questions:

1. In Java, the process of creating a new object based on an existing class is called ___________.
Answer: instantiation

2. The Java keyword used to refer to the current instance of a class within its methods is ___________.
Answer: this

3. The Java keyword used to handle exceptions is ___________.


Answer: try

4. A Java method that has the same name as the class in which it is declared is called a ___________
method.
Answer: constructor

5. In Java, the process of converting an object of a subclass to its superclass type is called ___________.
Answer: upcasting

6. The Java keyword used to prevent a class from being instantiated is ___________.
Answer: abstract

7. A Java interface can contain ___________ methods.


Answer: abstract

8. The process of converting an object of a superclass to its subclass type is called ___________.
Answer: downcasting

9. In Java, a variable that is declared within a method and is accessible only within that method is called a
___________ variable.
Answer: local
10. The default value of a boolean variable in Java is ___________.
Answer: false

11. Java supports ___________ inheritance, meaning a subclass can inherit from only one superclass.
Answer: single

12. The Java keyword used to explicitly call the constructor of a superclass is ___________.
Answer: super

13. The Java keyword used to define a block of code that will be executed if a certain condition is true is
___________.
Answer: if

14. In Java, the process of removing an object's association with the garbage collector is called
___________.
Answer: dereferencing

15. A Java method that does not return any value is declared with the return type ___________.
Answer: void

16. The Java operator used to compare two primitive data types for equality is ___________.
Answer: ==

17. In Java, the process of converting a string into its numeric equivalent is called ___________.
Answer: parsing

18. A Java keyword used to declare a variable whose value cannot be changed is ___________.
Answer: final

19. Java's built-in mechanism for destroying objects that are no longer in use is called ___________.
Answer: garbage collection
20. The Java keyword used to exit a loop prematurely is ___________.
Answer: break

21. The process of hiding the implementation details of a class from the outside world is known as
___________.
Answer: encapsulation

22. In Java, the process of accessing variables and methods of a class without creating an object of that
class is called ___________.
Answer: static

23. The Java keyword used to define a collection of related methods with empty bodies is ___________.
Answer: interface

24. In Java, the process of defining multiple methods with the same name but different parameters is
called ___________.
Answer: method overloading

25. The Java keyword used to define a subclass is ___________.


Answer: extends

26. In Java, a statement that transfers control to another part of the program is called a ___________
statement.
Answer: control

27. The Java operator used to concatenate two strings is ___________.


Answer: +

28. In Java, a collection of characters enclosed within single quotes is called a ___________.
Answer: char literal
29. The Java keyword used to define a block of code that will be executed regardless of whether an
exception is thrown is ___________.
Answer: finally

30. The process of combining data and methods into a single unit is known as ___________.
Answer: encapsulation

31. In Java, a method that has the same name as another method in the same class but different parameters
is called a ___________ method.
Answer: overloaded

32. The Java keyword used to define a class member that belongs to the class rather than any specific
instance of the class is ___________.
Answer: static

33. In Java, a block of code that can catch and handle exceptions is enclosed within a ___________ block.
Answer: try-catch

34. The Java keyword used to prevent a method from being overridden in a subclass is ___________.
Answer: final

35. In Java, a collection of related methods and variables that can be used as a blueprint for creating
objects is called a ___________.
Answer: class

36. The Java operator used to compare two values and return true if they are not equal is ___________.
Answer: !=

37. In Java, a reserved word that has a special meaning and cannot be used as an identifier is called a
___________.
Answer: keyword

38. The process of converting a primitive data type into its corresponding wrapper class object is called
___________.
Answer: boxing

39. In Java, a reserved memory location that stores data is called a ___________.
Answer: variable

40. The method used to initialize an object in Java is called the ___________ method.
Answer: constructor

41. In Java, a reserved word used to declare a variable that cannot be changed after initialization is
___________.
Answer: final

42. The Java operator used to perform division and return the quotient is ___________.
Answer: /

43. In Java, a type of error that occurs during program execution and disrupts the normal flow of the
program is called a ___________.
Answer: runtime error

44. The Java keyword used to define a block of code that will be executed repeatedly as long as a
condition is true is ___________.
Answer: while

45. In Java, a reserved memory location that holds data of a specific type is called a ___________.
Answer: variable

46. The Java keyword used to define a block of code that will be executed if a certain condition is not true
is ___________.
Answer: else

47. In Java, a statement that transfers control to another part of the program is called a ___________
statement.
Answer: control
48. The Java operator used to perform multiplication is ___________.
Answer: *

49. In Java, a block of code that defines the behavior of an object is called a ___________.
Answer: method

50. The Java keyword used to define a block of code that will be executed repeatedly for each element in
a collection is ___________.
Answer: for

Multiple-Choice Questions:

1. What does JVM stand for in Java?


a. Java Variable Manager
b. Java Virtual Machine
c. Just Virtual Machine
d. Java Virtual Manager
Answer: b

2. Which of the following is NOT a primitive data type in Java?


a. int
b. boolean
c. String
d. char
Answer: c

3. What does the "public" keyword mean in a Java method declaration?


a. The method can be accessed from any class.
b. The method can only be accessed within the same class.
c.
The method can only be accessed within the same package.
d. The method can only be accessed within subclasses.
Answer: a

4. Which of the following statements is true about Java arrays?


a. Arrays in Java are dynamic in size.
b. Elements in a Java array must be of the same data type.
c. Arrays in Java can only hold primitive data types.
d. Arrays in Java cannot be passed as parameters to methods.
Answer: b

5. What is the output of the following code snippet?


```
int x = 5;
int y = 2;
System.out.println(x / y);
```
a. 2.5
b. 2
c. 2.0
d. 2.5 (rounded down to 2)
Answer: b

6. Which keyword is used to declare a variable that does not hold any value?
a. empty
b. null
c. void
d. None of the above
Answer: d

7. What is the correct way to declare a variable in Java?


a. variable x;
b. int x;
c. int x = 5;
d. int = x;
Answer: c

8. Which of the following statements is true about Java packages?


a. A package can contain classes from different projects.
b. A package is a collection of classes and interfaces.
c. A package cannot contain sub-packages.
d. A package is optional in Java.
Answer: b

9. Which keyword is used to define a method that can be accessed without creating an instance of the
class?
a. static
b. void
c. final
d. abstract
Answer: a

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


```
String str = "Java";
System.out.println(str.length());
```
a. 4
b. 3
c. 5
d. 6
Answer: a
11. Which of the following is NOT a valid identifier in Java?
a. myVariable
b. _myVariable
c. 1stVariable
d. $myVariable
Answer: c

12. What does the "extends" keyword signify in Java?


a. It specifies that a class is a subclass of another class.
b. It specifies that a class is a superclass of another class.
c. It specifies that a class implements an interface.
d. It specifies that a class is abstract.
Answer: a

13. Which of the following is used to allocate memory dynamically in Java?


a. malloc()
b. new
c. allocate()
d. create()
Answer: b

14. What does the "break" statement do in Java?


a. Terminates the program
b. Terminates the loop and transfers control to the next statement after the loop
c. Terminates the loop and transfers control to the beginning of the loop
d. None of the above
Answer: b

15. Which of the following is NOT a valid Java comment?


a. // This is a comment
b. /* This is a comment */
c. <!-- This is a comment -->
d. /* This is a
multi-line
comment */
Answer: c

16. What is the output of the following code snippet?


```
int[] arr = new int[5];
System.out.println(arr.length);
```
a. 0
b. 1
c. 5
d. Compilation error
Answer: c

17. Which of the following is used to read input from the console in Java?
a. System.out.println()
b. System.in.read()
c. Scanner.nextLine()
d. BufferedReader.readLine()
Answer: d

18. What is the output of the following code snippet?


```
int x = 10;
int y = x++;
System.out.println(y);
```
a. 10
b. 11
c. 9
d. Compilation error
Answer: a

19. What is the result of the expression 10 % 3 in Java?


a. 3
b. 1
c. 2
d. 0
Answer: b

20. Which keyword is used to define a method that cannot be implemented in the same class and must be
implemented in a subclass?
a. static
b. abstract
c. final
d. public
Answer: b

21. Which of the following is NOT a valid Java operator?


a. %
b. &|
c. *
d. ++
Answer: b

22. What does the "super" keyword refer to in Java?


a. It refers to the current instance of a class.
b. It refers to the superclass of a class.
c. It refers to the subclass of a class.
d. It refers to the method being executed.
Answer: b

23. Which of the following is used to loop through the elements of an array in Java?
a. for loop
b. while loop
c. do-while loop
d. All of the above
Answer: d

24. What is the output of the

You might also like