Java

Java is a widely-used, object-oriented programming language designed for portability, security, and scalability. It follows the “write once, run anywhere” philosophy, allowing Java applications to run on any platform with a Java Virtual Machine (JVM). Java is commonly used in web development, enterprise applications, mobile apps, and large systems.

1. Number of primitive data types in Java are ____
a) 6
b) 7
c) 8
d) 9

2. What is the size of float and double in Java?
a) 32 and 64
b) 32 and 32
c) 64 and 64
d) 64 and 32

3. Automatic type conversion is possible in which of the following cases?
a) Byte to int
b) Int to long
c) Long to int
d) Short to int

4. Find the output of the following code.

int Integer = 24;  
char String = 'I';  
System.out.print(Integer);  
System.out.print(String);  

a) Compiler error
b) Throws exception
c) I
d) 24 I

5. Select the valid statement to declare and initialize an array.
a) int[] A = {}
b) int[] A = {1, 2, 3}
c) int[] A = (1, 2, 3)
d) int[][] A = {1, 2, 3}

6. Arrays in Java are –
a) Object references
b) Objects
c) Primitive data type
d) None

7. When is the object created with new keyword?
a) At run time
b) At compile time
c) Depends on the code
d) None

8. Identify the correct definition of a package.
a) A package is a collection of editing tools
b) A package is a collection of classes
c) A package is a collection of classes and interfaces
d) A package is a collection of interfaces

9. compareTo() returns
a) True
b) False
c) An int value
d) None

10. To which of the following does the class string belong?
a) java.lang
b) java.awt
c) java.applet
d) java.string

11. Which method is used to get the length of an array in Java?
a) length()
b) size()
c) getSize()
d) length

12. Which of the following is not a valid constructor name in Java?
a) MyClass()
b) MyClass(int a)
c) int MyClass()
d) None

13. Which keyword is used to define a constant variable in Java?
a) constant
b) final
c) static
d) constantValue

14. Which of the following is used to import a class in Java?
a) import
b) include
c) using
d) importClass

15. Which method is used to start the execution of a thread?
a) start()
b) run()
c) execute()
d) begin()

16. What is the default value of a boolean variable in Java?
a) false
b) true
c) null
d) 0

17. Which of the following is used to terminate a thread in Java?
a) stop()
b) interrupt()
c) terminate()
d) exit()

18. Which of the following is not a valid type of inheritance in Java?
a) Multiple inheritance
b) Single inheritance
c) Multilevel inheritance
d) Hierarchical inheritance

19. Which of the following is used to create an instance of a class in Java?
a) new
b) instance()
c) create()
d) class()

20. Which class is the superclass of every class in Java?
a) Object
b) String
c) Class
d) System

21. Which of the following methods is used to handle exceptions in Java?
a) try-catch
b) catch-finally
c) try-catch-finally
d) throw-catch

22. What is the default value of a byte variable in Java?
a) 0
b) 1
c) null
d) -1

23. Which of the following is used to declare a class in Java?
a) class MyClass;
b) class MyClass()
c) class MyClass {}
d) class MyClass[]

24. Which of the following methods is used to find the length of a string in Java?
a) getSize()
b) length()
c) size()
d) count()

25. What will the following code print?

String str = "Java";  
System.out.println(str.charAt(2));  

a) J
b) v
c) a
d) Error

26. Which of the following is a valid data type in Java?
a) decimal
b) float
c) real
d) number

27. Which method is used to compare two strings in Java?
a) equals()
b) compare()
c) compareTo()
d) isEqual()

28. What is the correct way to instantiate an ArrayList in Java?
a) ArrayList list = new ArrayList<>();
b) ArrayList list = new ArrayList();
c) ArrayList list = new ArrayList();
d) List list = new ArrayList();

29. Which of the following is not an access modifier in Java?
a) protected internal ✅
b) private
c) public
d) protected

30. Which of the following is used to check if a string contains a substring in Java?
a) contains()
b) match()
c) contains()
d) indexOf()

31. Which method is used to remove the first element from a LinkedList?
a) removeFirst()
b) deleteFirst()
c) pop()
d) remove()

32. Which method is used to get the value associated with a particular key in a HashMap?
a) getKey()
b) getValue()
c) get()
d) find()

33. Which of the following is true about interfaces in Java?
a) An interface can contain abstract methods
b) An interface can contain constructors
c) An interface can contain static methods
d) An interface cannot be implemented

34. Which keyword is used to access a method or variable of a class from a subclass in Java?
a) private
b) super
c) this
d) extend

35. Which of the following is true about a constructor in Java?
a) Constructor has a return type
b) Constructor name is the same as the class name
c) Constructor is inherited
d) Constructor can be abstract

36. Which of the following statements is true about StringBuffer?
a) StringBuffer is immutable
b) StringBuffer is mutable
c) StringBuffer does not have append() method
d) StringBuffer cannot be used for concatenation

37. Which of the following is used to represent characters in Java?
a) char
b) int
c) String
d) byte

38. Which of the following is true about the ‘final’ keyword in Java?
a) A final variable can be modified
b) A final variable cannot be modified
c) A final method can be overridden
d) A final class can be inherited

39. Which of the following is used to handle runtime exceptions in Java?
a) throws
b) try-catch
c) throws-catch
d) catch-finally

40. What is the size of a String in Java?
a) Depends on the number of characters
b) Always 10 bytes
c) Always 20 bytes
d) 32 bytes

41. What is the output of the following code?

String str = "Hello";  
String str2 = "Hello";  
System.out.println(str == str2);  

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

42. Which of the following statements is true about the String class in Java?
a) String class is immutable
b) String class is mutable
c) String class can be used for concatenation
d) String class can be used with StringBuffer only

43. What is the output of the following code?

int x = 10;  
int y = 20;  
System.out.println(x + y);  

a) 1020
b) 30
c) 10
d) Error

44. Which method is used to convert a string to lowercase in Java?
a) toLowerCase()
b) toLower()
c) lowerCase()
d) toLowerString()

45. Which class does the following code create an object of?

Object obj = new Object();  

a) Object
b) String
c) System
d) Throwable

46. Which of the following operators is used for logical AND in Java?
a) &
b) &&
c) |
d) ||

47. What is the output of the following code?

int x = 5;  
System.out.println(++x);  

a) 6
b) 5
c) 7
d) Error

48. Which of the following is the correct way to define a method in Java?
a) void method() {}
b) void { method() }
c) void method() {}
d) method() void {}

49. Which of the following is used to terminate a for loop in Java?
a) break;
b) continue;
c) break;
d) exit;

50. What will the following code print?

int x = 10;  
System.out.println(x++);  

a) 10
b) 11
c) 12
d) Error

51. What is the correct syntax for an if statement in Java?
a) if(condition) { }
b) if condition {}
c) if (condition);
d) if {} condition

52. Which of the following is not a valid loop in Java?
a) for
b) while
c) until
d) do-while

53. Which of the following is used to define a block of code that can be used repeatedly?
a) Method
b) Loop
c) Variable
d) Object

54. Which of the following is true about a Java class?
a) A class is a blueprint for creating objects
b) A class cannot contain methods
c) A class does not support inheritance
d) A class cannot have constructors

55. Which of the following is not a primitive data type in Java?
a) int
b) char
c) String
d) float

56. Which of the following is the correct way to instantiate an object of a class in Java?
a) ClassName obj = new ClassName();
b) Class obj = ClassName();
c) ClassName obj = ClassName();
d) obj = new ClassName();

57. Which of the following is used to handle a method that might throw an exception in Java?
a) throws
b) try-catch
c) throws-catch
d) try-finally

58. Which of the following methods is used to read input from the user in Java?
a) System.read()
b) Scanner.next()
c) input()
d) readInput()

59. Which of the following statements is true about the instanceof operator in Java?
a) It checks if an object is an instance of a specified class
b) It checks if an object is an instance of a primitive type
c) It is used to check if an object is null
d) It is used to convert an object into a specified type

60. Which of the following statements is true about the ‘super’ keyword in Java?
a) It refers to the current class
b) It refers to the superclass of the current class
c) It refers to a method in the current class
d) It refers to a variable in the current class

61. Which of the following is used to create a thread in Java?
a) Implementing Runnable interface
b) Extending Thread class
c) Both a and b
d) None of the above

62. Which of the following methods is used to stop a thread in Java?
a) terminate()
b) stop()
c) end()
d) interrupt()

63. Which of the following is true about method overloading in Java?
a) It occurs when two methods have the same signature
b) It occurs when two methods have the same name but different parameters
c) It occurs when a method is overridden
d) It is not allowed in Java

64. Which of the following is true about method overriding in Java?
a) It changes the implementation of a method in the subclass without altering the method signature
b) It requires the subclass to have the same method signature as the superclass method
c) It is not allowed in Java
d) It is used to prevent the method from being inherited

65. Which of the following operators is used for logical OR in Java?
a) &
b) ||
c) |
d) and

66. Which of the following is the correct way to call a method from a superclass in Java?
a) super.method();
b) super.method();
c) this.method();
d) method.super();

67. Which of the following is not a valid access modifier in Java?
a) public
b) private
c) global
d) protected

68. What is the default value of an instance variable in Java?
a) 0
b) Depends on the data type
c) null
d) undefined

69. Which of the following can be used to change the state of an object in Java?
a) Constructor
b) Getter method
c) Setter method
d) Destructor

70. Which of the following is the correct way to create a new array in Java?
a) int[] arr = new int();
b) int[] arr = new int[10];
c) int[] arr = new int[];
d) int arr = new int[10];

71. What will be the output of the following code?

String str = "Java";  
str = str.concat(" Programming");  
System.out.println(str);  

a) Java
b) Java Programming
c) JavaProgramming
d) Error

72. What is the correct way to declare a constant in Java?
a) constant int x = 10;
b) const int x = 10;
c) final int x = 10;
d) constant int x := 10;

73. Which of the following methods is used to remove an element from an ArrayList in Java?
a) remove()
b) delete()
c) discard()
d) erase()

74. Which of the following statements is true about the ‘this’ keyword in Java?
a) It refers to the superclass of the current class
b) It refers to a method in the current class
c) It refers to the current object of the class
d) It refers to the parent class object

75. Which of the following is used to compare two objects in Java?
a) compare()
b) equals()
c) ==
d) compareTo()

76. Which of the following methods is used to find the index of a character in a string in Java?
a) indexOf()
b) find()
c) charAt()
d) getIndex()

77. Which of the following statements is true about the StringBuilder class in Java?
a) StringBuilder is immutable
b) StringBuilder is mutable
c) StringBuilder cannot be used for concatenation
d) StringBuilder cannot be used in multithreading

78. Which of the following is the correct way to declare a 2D array in Java?
a) int[][] arr = new int[3][3];
b) int[] arr[] = new int[3][3];
c) int[][] arr = new int[3];
d) int arr[3][3];

79. Which of the following statements is true about the throw keyword in Java?
a) throw is used to handle exceptions
b) throw is used to explicitly throw an exception
c) throw is used to define a custom exception
d) throw is used to catch an exception

80. Which of the following is true about the finally block in Java?
a) The finally block is used to handle exceptions
b) The finally block can be skipped
c) The finally block is always executed, regardless of whether an exception occurs or not
d) The finally block can only be used with try-catch

81. Which of the following is a valid way to declare a method in Java?
a) method(int x);
b) void method(int x);
c) method void(int x);
d) int method(int x);

82. Which of the following statements is true about constructors in Java?
a) A constructor can have a return type
b) A constructor is used to initialize the object
c) A constructor has the same name as the class
d) A constructor can be called explicitly

83. Which of the following is used to check if an array is empty in Java?
a) array.length == 0
b) array.empty()
c) array.isEmpty()
d) array == null

84. What is the result of the expression 10 / 3 in Java?
a) 3.33
b) 3
c) 3.0
d) 0

85. Which of the following is used to create a copy of an object in Java?
a) clone()
b) copy()
c) replicate()
d) duplicate()

86. Which method is used to convert a string to an integer in Java?
a) Integer.parse()
b) Integer.parseInt()
c) String.toInt()
d) convertInt()

87. Which of the following is true about arrays in Java?
a) Arrays are zero-indexed
b) Arrays can only store objects
c) Arrays can only store integers
d) Arrays are dynamically sized

88. Which method is used to add elements to an ArrayList in Java?
a) add()
b) insert()
c) push()
d) append()

89. Which of the following is true about the ‘instanceof’ operator in Java?
a) It checks if an object is an instance of a specified class
b) It checks if an object is of a particular type
c) It is used for type casting
d) It can only be used with classes

90. Which of the following statements is true about inheritance in Java?
a) Inheritance allows a class to inherit properties and methods from another class
b) A class can inherit only from one class
c) A class can inherit only public methods
d) A class cannot inherit methods from another class

91. Which of the following statements is true about a constructor in Java?
a) A constructor can be called explicitly
b) A constructor must always return a value
c) A constructor is used to initialize the state of an object
d) A constructor cannot be overloaded

92. Which of the following is the correct syntax for the switch statement in Java?
a) switch(expression) { case value: break; }
b) switch{expression case value;}
c) switch(expression) -> { case value: }
d) switch(expression) { case value: }

93. Which of the following is true about the ‘public’ access modifier in Java?
a) It allows access to the class only within the same package
b) It allows access to the class from anywhere
c) It allows access only within the current class
d) It restricts access to subclasses

94. What will be the output of the following code?

String s1 = "hello";  
String s2 = "hello";  
System.out.println(s1 == s2);  

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

95. Which of the following is a feature of Java?
a) It is a platform-dependent language
b) It is a platform-independent language
c) It is a low-level programming language
d) It does not support object-oriented programming

96. Which of the following methods is used to convert a string to lowercase in Java?
a) toLowerCase()
b) toLower()
c) lowercase()
d) makeLowerCase()

97. Which of the following statements is true about the ‘abstract’ keyword in Java?
a) Abstract classes can be instantiated
b) An abstract class can have a constructor
c) An abstract class cannot be instantiated
d) An abstract class must contain only abstract methods

98. Which of the following is used to create a new thread in Java?
a) Runnable.run()
b) Thread.start()
c) Thread.run()
d) Runnable.start()

99. Which of the following statements is true about a method in Java?
a) A method can be defined inside a class
b) A method cannot return a value
c) A method cannot have parameters
d) A method must always return a value

100. Which of the following is the correct way to declare an array of integers in Java?
a) int[] arr = {1, 2, 3};
b) int arr[] = {1, 2, 3};
c) int[] arr = new int[] {1, 2, 3};
d) int arr = {1, 2, 3};

101. What is the default value of a boolean variable in Java?
a) 1
b) true
c) false
d) null

102. Which of the following is the correct way to create a custom exception in Java?
a) class CustomException { }
b) class CustomException extends Exception { }
c) class CustomException implements Exception { }
d) class CustomException extends Throwable { }

103. Which of the following is the correct way to declare a method that throws an exception in Java?
a) public void method() throws Exception { }
b) public void method() catch Exception { }
c) public void method() throw Exception { }
d) public void method() handle Exception { }

104. Which of the following methods is used to find the length of an array in Java?
a) array.length
b) array.size()
c) array.length()
d) array.getLength()

105. What is the result of the following expression: “10” + 5 in Java?
a) 15
b) 10 + 5
c) 105
d) Error

106. Which of the following is the correct way to import a package in Java?
a) import package;
b) import package.name;
c) import name.package;
d) import name;

107. What will be the output of the following code?

int x = 10;  
if(x > 5) {  
    System.out.println("Greater");  
} else {  
    System.out.println("Smaller");  
}  

a) Greater
b) Smaller
c) Error
d) Nothing

108. Which of the following is used to create a new instance of an object in Java?
a) newInstance()
b) new ClassName()
c) create()
d) instantiate()

109. Which of the following is true about the StringBuffer class in Java?
a) StringBuffer is immutable
b) StringBuffer is mutable
c) StringBuffer is thread-unsafe
d) StringBuffer cannot be used for concatenation

110. Which of the following is used to perform a bitwise AND operation in Java?
a) &&
b) &
c) |
d) AND

111. Which of the following statements is true about the ‘final’ keyword in Java?
a) It can be used to define constants
b) It can only be used with variables
c) It prevents inheritance of a class
d) It prevents overriding of methods

112. Which of the following is the correct way to compare two strings in Java?
a) str1.equals(str2)
b) str1 == str2
c) str1.compareTo(str2)
d) str1.equalsIgnoreCase(str2)

113. Which of the following is a valid identifier in Java?
a) 1variable
b) variable1
c) variable-1
d) var!able

114. What will be the output of the following code?

String str = "java";  
str = str.toUpperCase();  
System.out.println(str);  

a) java
b) JAVA
c) Java
d) Error

115. Which of the following is true about interfaces in Java?
a) An interface can contain only static methods
b) An interface can contain abstract methods
c) An interface cannot be implemented by a class
d) An interface can have constructors

116. Which of the following methods is used to retrieve a value from a HashMap in Java?
a) get()
b) fetch()
c) retrieve()
d) read()

117. Which of the following is true about method signatures in Java?
a) Method signatures must be unique within the class
b) Method signatures include the method name and parameter types
c) Method signatures can only contain the method name
d) Method signatures are case-insensitive

118. Which of the following is used to create an unmodifiable list in Java?
a) Collections.unmodifiableList()
b) List.of()
c) List.unmodifiable()
d) Arrays.asList()

119. Which of the following statements is true about the ‘protected’ access modifier in Java?
a) It allows access within the same package and subclasses
b) It allows access within the same class only
c) It allows access within the same package only
d) It allows access from anywhere

120. Which of the following will create an instance of the ArrayList class in Java?
a) ArrayList arr = new ArrayList();
b) ArrayList arr = new ArrayList<>();
c) List arr = new ArrayList();
d) ArrayList arr = new ArrayList();

121. Which of the following is true about the ‘continue’ keyword in Java?
a) It skips the current iteration of a loop
b) It exits the loop entirely
c) It breaks the loop
d) It is used to exit a method

122. What is the output of the following code?

int x = 20;  
int y = 10;  
System.out.println(x % y);  

a) 0
b) 1
c) 2
d) 10

123. Which of the following methods is used to convert an object into a string in Java?
a) toString()
b) convert()
c) stringOf()
d) stringify()

124. Which of the following is the correct way to catch multiple exceptions in Java?
a) catch(Exception1, Exception2)
b) catch(Exception1 | Exception2)
c) catch(Exception1 & Exception2)
d) catch(Exception1, Exception2, Exception3)

125. Which of the following statements is true about the ‘this’ keyword in Java?
a) It refers to the superclass of the current object
b) It refers to the current object
c) It refers to a method of the class
d) It is used for static variables

126. What is the output of the following code?

String str = "Java";  
str = str + " Programming";  
System.out.println(str);  

a) Java Programming
b) JavaProgramming
c) Java Programming Java
d) Error

127. Which of the following is the correct way to declare a constant variable in Java?
a) static int NUM = 100;
b) final int NUM = 100;
c) constant int NUM = 100;
d) constant NUM = 100;

128. Which of the following methods is used to find the index of an element in an ArrayList in Java?
a) indexOf()
b) indexOf()
c) getIndex()
d) find()

129. Which of the following keywords is used to inherit a class in Java?
a) extends
b) implements
c) extends
d) inherits

130. Which of the following is true about the ‘null’ keyword in Java?
a) It can be assigned to primitive data types
b) It can be assigned to reference variables
c) It can be assigned to both reference and primitive data types
d) It cannot be assigned to any variable

131. Which of the following will cause an error in Java?
a) int x = “Hello”;
b) String str = “Hello”;
c) double d = 10.5;
d) char c = ‘H’;

132. What is the default value of a float variable in Java?
a) 0
b) 0.0
c) null
d) undefined