100+ Java Programming MCQs
1. JDK stands for ____.
- Java development kit
- Java deployment kit
- JavaScript deployment kit
- None of these
Answer: A) Java development kit
2. JRE stands for ___.
- Java run ecosystem
- JDK runtime Environment
- Java Runtime Environment
- None of these
Answer: C) Java Runtime Environment
3. What makes the Java platform independent?
- Advanced programming language
- It uses bytecode for execution
- Class compilation
- All of these
Answer: B) It uses bytecode for execution
4. Can we keep a different name for the java class name and java file name?
- Yes
- No
Answer: A) Yes
5. What are the types of memory allocated in memory in java?
- Heap memory
- Stack memory
- Both A and B
- None of these
Answer: C) Both A and B
6. Multiline comment is created using ___.
- //
- /* */
- <!– — >
- All of these
Answer: B) /* */
7. What is the entry point of a program in Java?
- main() method
- The first line of code
- Last line of code
- main class
Answer: A) main() method
8. Can we write a program without a main method in Java?
- Yes
- No
Answer: A) Yes
9. Can the main() method be overloaded in Java?
- Yes
- No
Answer: A) Yes
10. Which keyword in java is used for exception handling?
- exep
- excepHand
- throw
- All of these
Answer: C) throw
11. Which class in Java is used to take input from the user?
- Scanner
- Input
- Applier
- None of these
Answer: A) Scanner
12. Method used to take a string as input in Java?
- next()
- nextLine()
- Both A. and B.
- None of these
Answer: B) Both A. and B.
13. Which of the following is the correct syntax to create a variable in Java?
- var name;
- int name;
- var name int;
- All of these
Answer: B) int name;
14. Is string mutable in Java?
- Yes
- No
Answer: B) No
15. Which of these is a type of variable in Java?
- Instance Variable
- Local Variable
- Static Variable
- All of these
Answer: D) All of these
16. What will be the output of following Java code?
public class Main {
public static void main(String[] args) {
String str = "Hello";
str = "Bye";
System.out.println(str);
}
}
- Hello
- Bye
- Error
- All of these
Answer: B) Bye
17. What is type casting in Java?
- It is converting type of a variable from one type to another
- Casting variable to the class
- Creating a new variable
- All of these
Answer: A) It is converting type of a variable from one type to another
18. Which type of casting is lossy in Java?
- Widening typecasting
- Narrowing typecasting
- Manual typecasting
- All of these
Answer: B) Narrowing typecasting
19. Which of the following can be declared as final in java?
- Class
- Method
- Variable
- All of these
Answer: D) All of these
20. Finally block is attached to?
- Try-catch block
- Class block
- Method block
- All of these
Answer: A) Try-catch block
21. The break statement in Java is used to ___.
- Terminates from the loop immediately
- Terminates from the program immediately
- Skips the current iteration
- All of these
Answer: A) Terminates from the loop immediately
22. What will be the output of following Java code?
public class Main {
public class Main {
public static void main(String arg[]) {
int i;
for (i = 1; i <= 12; i += 2) {
if (i == 8) {
System.out.println(i);
break;
}
}
}
}
- 1
- No output
- 8
- 1357911
Answer: B) No output
23. Can the Java program accept input from the command line?
- Yes, using command-line arguments
- Yes, by access command prompt
- No
- None of these
Answer: A) Yes, using command-line arguments
24. Array in java is ___.
- Collection of similar elements
- Collection of elements of different types
- The data type of consisting of characters
- None of these
Answer: A) Collection of similar elements
25. Which of these is the correct method to create an array in java?
- int[] arr = {1, 3, 5};
- int[] arr;
- arr = new int[] {3, 1, 8};
- int arr[] = {1, 4, 6};
- All of these
Answer: E) All of these
26. Object in java are ___.
- Classes
- References
- Iterators
- None of these
Answer: B) References
27. What is garbage collection in java?
- Method to manage memory in java
- Create new garbage values
- Delete all values
- All of these
Answer: A) Method to manage memory in java
28. Static variables in java are declared as ___.
- final variables
- new variables
- Constants
- All of these
Answer: C) Constants
29. BigInteger Class is used to ___.
- Store very long range of number
- Store integer values
- A class that stores large range of integer
- All of these
Answer: D) All of these
30. ‘this’ keyword in java is ___.
- Used to hold the reference of the current object
- Holds object value
- Used to create a new instance
- All of these
Answer: A) Used to hold the reference of the current object
31. What will be the output of following Java code?
import java.util.Scanner;
class ThisKeyword {
private int a = 4;
private int b = 1;
void getSum(int a, int b) {
this.a = a;
this.b = b;
System.out.println(this.a + this.b);
}
}
public class Main {
public static void main(String args[]) {
ThisKeyword T = new ThisKeyword();
T.getSum(3, 5);
}
}
- 5
- 9
- 8
- 4
Answer: C) 8
32. The ‘super’ keyword is used to ___.
- Access instance of the parent class
- Access instance of the same class
- Access instance of child class
- Access instance of friend class
Answer: A) Access instance of the parent class
33. The super() method is used to ___.
- Call constructor of friend class
- Is a declared method
- Call constructor of the parent class
- Call constructor
Answer: C) Call constructor of the parent class
34. Wrapper class in java is ___.
- Used to encapsulate primitive data types
- Declare new classes called wrapper
- Create a new instance of the class
- None of these
Answer: A) Used to encapsulate primitive data types
35. Boxing is ___.
- Creating new box
- Creating object
- Converting primitive type of object instance
- All of these
Answer: C) Converting primitive type of object instance
36. Abstract class is ___.
- Created using abstract keyword
- Contains only abstract method
- Needs to be inherited to be used
- All of these
Answer: D) All of these
37. What is file handling in java?
- It is creating, deleting, and modifying files using a java program.
- Creating new method
- Filing method to different file to extract them better
- All of these
Answer: A) It is creating, deleting, and modifying files using a java program
38. How can we access methods for file handling in java?
- Java.files
- Java.io
- Java.io.File
- Java.FileHandling
Answer: C) Java.io.File
39. Which is the correct absolute path of a file in Java?
- C:\Program Files\Java\jdk1.8.0_131\bin\file_name.txt
- C:\Program Files\Java\file_name.txt
- C:\Program Files\Java\jdk1.8.0_131\file_name.txt
- C:\Program Files\Java\jdk1.8.0_131\bin\File Handling\file_name.txt
Answer: A) C:\Program Files\Java\jdk1.8.0_131\bin\file_name.txt
40. Which method is used to add a new line to file in Java?
- file.addLine()
- file.nextLine()
- file.write()
- file.line()
Answer: C) file.write()
41. Which method deletes a file in Java?
- file.delete()
- file.remove()
- file.garbage()
- file.dump()
Answer: A) file.delete()
42. Which method in java is used to read lines from file?
- file.read()
- file.nextLine()
- file.getLine()
- All of these
Answer: C) file.getLine()
43. The correct syntax to import the math library in java is ___.
- import java.lang.math
- import math
- import java.math
- All of these
Answer: A) import java.lang.math
44. Which is/are valid method(s) of math library in java?
- max()
- cbrt()
- log10()
- All of these
Answer: D) All of these
45. Which method in java is used to generate random numbers in Java?
- random.nextInt()
- random()
- rand()
- All of these
Answer: A) random.nextInt()
46. In java, recursion is ___.
- Method
- A process allowing methods to call itself
- The process to call methods
- None of these
Answer: B) A process allowing methods to call itself
47. What is stringBuffer in java?
- Class to create a string array
- Class to create a mutable string in java
- Class to create a string from i/o buffer
- All of these
Answer: B) Class to create a mutable string in java
48. Which of the following is a valid data structure in java?
- Array
- List
- Vector
- All of these
Answer: D) All of these
49. Which syntax is valid to create a vector in java?
- Vector < string > names = new Vector < String > ();
- Vector name = new string;
- int name = new vector ()
- All of these
Answer: A) Vector < string > names = new Vector < String > ();
50. What will be the output of following Java code?
import java.util.Scanner;
class ThisKeyword {
private int a = 4;
private int b = 1;
void getSum(int a, int b) {
this.a = a;
this.b = b;
System.out.println(this.a + this.b);
}
}
public class Main {
public static void main(String args[]) {
ThisKeyword T = new ThisKeyword();
T.getSum(3, 5);
}
}
- Error
- [mango, orange, guava, mango, apple]
- [mango, orange, guava, apple]
- None of these
Answer: B) [mango, orange, guava, mango, apple]
51. Which of these is true for interfaces in java?
- The keyword interface is used to create a method
- All the methods of an interface are abstract
- It does not contain constructors
- All of these
Answer: D) All of these
52. Encapsulation is ___.
- Wrapping up of data and related functions into a single entity
- Creating special methods
- Creating special data structure
- All of these
Answer: A) Wrapping up of data and related functions into a single entity
53. Which Java method is used to convert an object to string?
- createString()
- toString()
- object.string()
- newString()
Answer: B) toString()
54. What is a comparator in Java?
- Interface to compare integer
- Comparison method for lists
- Interface to compare two objects in java
- All of these
Answer: C) Interface to compare two objects in java
55. Which of the following methods are present in comparator interface?
- compare()
- equate()
- isEqual()
- All of these
Answer: A) compare()
56. Which of the following statements is not correct for vectors in Java?
- It was created using vector keyword
- It can store an object of different classes
- It is asynchronous
- None of these
Answer: C) It is asynchronous
57. What will be the output of following Java code?
public class Main {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("pro");
sb.append("gie");
System.out.println(sb);
}
}
- Error
- pro
- gie
- progie
Answer: D) progie
58. What is a deadlock in Java?
- State when all processes have complete working and are dead
- State when threads are in hold state forever
- State when threads are not ready
- All of these
Answer: B) State when threads are in hold state forever
59. Which graph is used to check for deadlock in Java?
- Deadlock graph
- Time graph
- Wait-for-graph
- None of these
Answer: C) Wait-for-graph
60. Batch processing in java is ___.
- Used to execute a group of queries or a batch as executing a single query, again and again, is time taking and reduce the performance
- Used to processing multiple queries can be executed at once
- Used to increase program’s performance
- All of these
Answer: D) All of these
61. Null in Java is ___.
- Reserved keyword
- Literal value
- Used in exception handling
- All of these
Answer: D) All of these
62. Enumeration in Java is ___.
- Data type which contains fixed set of constants
- Method
- Class
- None of these
Answer: A) Data type which contains fixed set of constants
63. Can we pass objects to method arguments in Java?
- Yes
- No
Answer: A) Yes
64. Which of the following ways is the correct way to create an object in Java?
- Using the new keyword
- Using newInstance() method
- clone() method
- All of these
Answer: D) All of these
65. Which statement is correct for private member in Java?
- Access outside the class is allowed
- Any class can access
- Declared using private keyword
- All of these
Answer: C) Declared using private keyword
66. Which keyword is used to inherit classes in Java?
- extends
- inheritance
- isChild
- None of these
Answer: A) extends
67. Which of the following inheritance of class is invalid in Java?
- Single
- Multiple
- Multi-level
- Hierarchical
Answer: B) Multiple
68. The ‘implements’ keyword is used to ___.
- Implement the function of a class
- Inherit an interface in Java
- Inherit a class in java
- All of these
Answer: B) Inherit an interface in Java
69. What is polymorphism in Java?
- Performing a single task in multiple ways
- Performing multiple tasks using multiple methods
- Creating a new class for each task
- All of these
Answer: A) Performing a single task in multiple ways
70. What are packages in Java?
- Methods of a friend class
- Methods of the main class
- Way to encapsulate a group of classes, sub-packages, and interface
- All of these
Answer: C) Way to encapsulate a group of classes, sub-packages, and interface
71. Empty interface in Java is called?
- Marker interface
- Abstract class
- Derived class
- None of these
Answer: A) Marker interface
72. Which of these is a non-access modifier?
- public
- private
- native
- All of these
Answer: C) native
73. When a finally block executed in Java?
- Try block is executed without any exception
- Exception has occurred
- Executed at last
- None of these
Answer: C) Executed at last
74. What is boolean in Java?
- A value consisting of only true and false value
- A value consisting of 8 values
- Truthy value in java
- All of these
Answer: A) A value consisting of only true and false value
75. Which of these is not a valid Boolean method in Java?
- equals() method
- hashCode() method
- toString() method
- All of these
Answer: D) All of these
76. Which method in Java is used to check for NaN values?
- isNan()
- checkNan()
- isNotNan()
- All of these
Answer: A) isNan()
77. Which of these is a property of threads in Java?
- Multiple threads can be executed concurrently
- Has its own priority
- Both A. and B.
- None of these
Answer: C) Both A. and B.
78. Which thread is executed in the background?
- New thread
- User-created thread
- Daemon thread
- All of these
Answer: C) Daemon thread
79. Multithreading in java is ___.
- Executing multiple processes simultaneously
- Creating more threads at a time
- Blocking threads
- All of these
Answer: A) Executing multiple processes simultaneously
80. What will be the output of following Java code?
public class Main {
public static void main(String[] args) {
System.out.println(Math.copySign(100.6, -200.6));
}
}
- 100.6
- -100.6
- -200.6
- 200.6
Answer: B) -100.6
81. Which method is used to convert radians to degree in Java?
- convertRadtoDeg()
- toDegrees()
- degree()
- All of these
Answer: B) toDegrees()
82. Which of the following methods is used to extract the length of a string in Java?
- length()
- len()
- sizeof()
- size()
Answer: A) length()
83. The trim() method in Java used to ___.
- Remove the given character
- Remove the values after the given index
- Remove leading and trailing spaces
- None of these
Answer: C) Remove leading and trailing spaces
84. What are regexes in Java?
- API to define a pattern for searching strings
- String
- Array to create a new integer
- Wrapper class
Answer: A) API to define a pattern for searching strings
85. What is a map in Java?
- Data structure
- Defined in java.util package
- Represented using key-value pairs
- All of these
Answer: D) All of these
86. What is a set in Java?
- Represented in the form of values
- Used to store key-value pairs
- Primary structures
- All of these
Answer: A) Represented in the form of values
87. What will be the output of following Java code?
import java.util.Hashtable;
public class HashTableClass {
int hashcode;
HashTableClass(int hashcode) {
this.hashcode = hashcode;
}
public int hashCode() {
return hashcode;
}
public String toString() {
return hashcode + " ";
}
public static void main(String[] args) {
Hashtable ht = new Hashtable();
ht.put(new HashTableClass(10), "Java");
ht.put(new HashTableClass(3), "C");
ht.put(new HashTableClass(4), "C++");
ht.put(new HashTableClass(5), "Ruby");
ht.put(new HashTableClass(6), "null");
System.out.println(ht);
}
}
- {10 =Java, 3 =C, 4 =C++, 6 =null, 5 =Ruby}
- {10 =Java, 6 =null, 5 =Ruby, 4 =C++, 3 =C}
- {3 =C, 4 =C++, 5 =Ruby, 6 =null, 10 =Java}
- None of these
Answer: B) {10 =Java, 6 =null, 5 =Ruby, 4 =C++, 3 =C}
88. Which of the following sorts the elements were inserted?
- Hashtable
- Map
- Array
- None of these
Answer: A) Hashtable
89. Which Java method is used to clear element of ArrayList?
- deleteAll()
- delete()
- clearAll()
- clear()
Answer: D) clear()
90. Which Java method is used to add all of the specified elements to the specified collection?
- addValue()
- copy()
- cpy()
- addAll()
Answer: D) addAll()
91. Which Java method is used to detect the OS in which Java program is being run?
- system.getOSdetails()
- system.get(os.name)
- system.getProperties(“os.name”)
- system.getProperties(“os”)
Answer: C) system.getProperties(“os.name”)
92. What is the default encoding of OutstreamWriter?
- UTF-32
- UTF-16
- UTF-12
- Based on the host platform
Answer: D) Based on the host platform
93. Which method in java is used to get the name of running java VM?
- System.getProperties(“java.vm.name”)
- System.vmName
- Sytem.getVmName
- System.getProperties(“vm.name”)
Answer: A) System.getProperties(“java.vm.name”)
94. Which Java method is used to get the version of running java VM?
- System.vm.version
- System.getProperties(“vm.version”)
- System.getProperties(“java.vm.version”)
- System.getVmVersion
Answer: C) System.getProperties(“java.vm.version”)
95. What is the full form of AWT?
- Absolute window toolKit
- Abstract window toolKit
- Absolute wear kit
- Abstract window tools
Answer: B) Abstract window toolKit
96. Which escape character is used for word character in regex?
- /w
- /c
- /str
- /?
Answer: A) /w
97. Jar in java stands for ___.
- Java ARchive
- Java application runtime
- Java application runner
- None of these
Answer: A) Java ARchive
98. Which Java keyword is used to access features of a package?
- get
- import
- extends
- All of these
Answer: B) import
99. The result of dividing by 0 in Java is ___.
- Error
- Expectation
- Infinite
- None of these
Answer: B) Expectation
100. What will be the output of following Java code?
public class ConcatNull {
public static void main(String[] args) {
String str1 = "pro";
String str2 = "gie";
System.out.println(str1 + str2);
}
}
- progie
- pro
- gie
- None of these
Answer: A) progie
101. What is method overriding in Java?
A. Writing a method in a subclass with the same name of superclass’s method
B. Mentioning the same return type of the method of the superclass
C. The argument list in the method of subclass and the method of superclass should be the same
D. All the above
Answer: D) All the above