100+ Java Programming MCQs

                     

1. JDK stands for ____.

  1. Java development kit
  2. Java deployment kit
  3. JavaScript deployment kit
  4. None of these

Answer: A) Java development kit

2. JRE stands for ___.

  1. Java run ecosystem
  2. JDK runtime Environment
  3. Java Runtime Environment
  4. None of these

Answer: C) Java Runtime Environment

3. What makes the Java platform independent?

  1. Advanced programming language
  2. It uses bytecode for execution
  3. Class compilation
  4. 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?

  1. Yes
  2. No

Answer: A) Yes

5. What are the types of memory allocated in memory in java?

  1. Heap memory
  2. Stack memory
  3. Both A and B
  4. None of these

Answer: C) Both A and B

6. Multiline comment is created using ___.

  1. //
  2. /* */
  3. <!–  — >
  4. All of these

Answer: B) /* */

7. What is the entry point of a program in Java?

  1. main() method
  2. The first line of code
  3. Last line of code
  4. main class

Answer: A) main() method

8. Can we write a program without a main method in Java?

  1. Yes
  2. No

Answer: A) Yes

9. Can the main() method be overloaded in Java?

  1. Yes
  2. No

Answer: A) Yes

10. Which keyword in java is used for exception handling?

  1. exep
  2. excepHand
  3. throw
  4. All of these

Answer: C) throw

11. Which class in Java is used to take input from the user?

  1. Scanner
  2. Input
  3. Applier
  4. None of these

Answer: A) Scanner

12. Method used to take a string as input in Java?

  1. next()
  2. nextLine()
  3. Both A. and B.
  4. None of these

Answer: B) Both A. and B.

13. Which of the following is the correct syntax to create a variable in Java?

  1. var name;
  2. int name;
  3. var name int;
  4. All of these

Answer: B) int name;

14. Is string mutable in Java?

  1. Yes
  2. No

Answer: B) No

15. Which of these is a type of variable in Java?

  1. Instance Variable
  2. Local Variable
  3. Static Variable
  4. 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);
  }
}
  1. Hello
  2. Bye
  3. Error
  4. All of these

Answer: B) Bye

17. What is type casting in Java?

  1. It is converting type of a variable from one type to another
  2. Casting variable to the class
  3. Creating a new variable
  4. 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?

  1. Widening typecasting
  2. Narrowing typecasting
  3. Manual typecasting
  4. All of these

Answer: B) Narrowing typecasting

19. Which of the following can be declared as final in java?

  1. Class
  2. Method
  3. Variable
  4. All of these

Answer: D) All of these

20. Finally block is attached to?

  1. Try-catch block
  2. Class block
  3. Method block
  4. All of these

Answer: A) Try-catch block

21. The break statement in Java is used to ___.

  1. Terminates from the loop immediately
  2. Terminates from the program immediately
  3. Skips the current iteration
  4. 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. 1
  2. No output
  3. 8
  4. 1357911

Answer: B) No output

23. Can the Java program accept input from the command line?

  1. Yes, using command-line arguments
  2. Yes, by access command prompt
  3. No
  4. None of these

Answer: A) Yes, using command-line arguments

24. Array in java is ___.

  1. Collection of similar elements
  2. Collection of elements of different types
  3. The data type of consisting of characters
  4. None of these

Answer: A) Collection of similar elements

25. Which of these is the correct method to create an array in java?

  1. int[] arr = {1, 3, 5};
  2. int[] arr;
  3. arr = new int[] {3, 1, 8};
  4. int arr[] = {1, 4, 6};
  5. All of these

Answer: E) All of these

26. Object in java are ___.

  1. Classes
  2. References
  3. Iterators
  4. None of these

Answer: B) References

27. What is garbage collection in java?

  1. Method to manage memory in java
  2. Create new garbage values
  3. Delete all values
  4. All of these

Answer: A) Method to manage memory in java

28. Static variables in java are declared as ___.

  1. final variables
  2. new variables
  3. Constants
  4. All of these

Answer: C) Constants

29. BigInteger Class is used to ___.

  1. Store very long range of number
  2. Store integer values
  3. A class that stores large range of integer
  4. All of these

Answer: D) All of these

30. ‘this’ keyword in java is ___.

  1. Used to hold the reference of the current object
  2. Holds object value
  3. Used to create a new instance
  4. 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);
  }
}
  1. 5
  2. 9
  3. 8
  4. 4

Answer: C) 8

32. The ‘super’ keyword is used to ___.

  1. Access instance of the parent class
  2. Access instance of the same class
  3. Access instance of child class
  4. Access instance of friend class

Answer: A) Access instance of the parent class

33. The super() method is used to ___.

  1. Call constructor of friend class
  2. Is a declared method
  3. Call constructor of the parent class
  4. Call constructor

Answer: C) Call constructor of the parent class

34. Wrapper class in java is ___.

  1. Used to encapsulate primitive data types
  2. Declare new classes called wrapper
  3. Create a new instance of the class
  4. None of these

Answer: A) Used to encapsulate primitive data types

35. Boxing is ___.

  1. Creating new box
  2. Creating object
  3. Converting primitive type of object instance
  4. All of these

Answer: C) Converting primitive type of object instance

36. Abstract class is ___.

  1. Created using abstract keyword
  2. Contains only abstract method
  3. Needs to be inherited to be used
  4. All of these

Answer: D) All of these

37. What is file handling in java?

  1. It is creating, deleting, and modifying files using a java program.
  2. Creating new method
  3. Filing method to different file to extract them better
  4. 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?

  1. Java.files
  2. Java.io
  3. Java.io.File
  4. Java.FileHandling

Answer: C) Java.io.File

39. Which is the correct absolute path of a file in Java?

  1. C:\Program Files\Java\jdk1.8.0_131\bin\file_name.txt
  2. C:\Program Files\Java\file_name.txt
  3. C:\Program Files\Java\jdk1.8.0_131\file_name.txt
  4. 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?

  1. file.addLine()
  2. file.nextLine()
  3. file.write()
  4. file.line()

Answer: C) file.write()

41. Which method deletes a file in Java?

  1. file.delete()
  2. file.remove()
  3. file.garbage()
  4. file.dump()

Answer: A) file.delete()

42. Which method in java is used to read lines from file?

  1. file.read()
  2. file.nextLine()
  3. file.getLine()
  4. All of these

Answer: C) file.getLine()

43. The correct syntax to import the math library in java is ___.

  1. import java.lang.math
  2. import math
  3. import java.math
  4. All of these

Answer: A) import java.lang.math

44. Which is/are valid method(s) of math library in java?

  1. max()
  2. cbrt()
  3. log10()
  4. All of these

Answer: D) All of these

45. Which method in java is used to generate random numbers in Java?

  1. random.nextInt()
  2. random()
  3. rand()
  4. All of these

Answer: A) random.nextInt()

46. In java, recursion is ___.

  1. Method
  2. A process allowing methods to call itself
  3. The process to call methods
  4. None of these

Answer: B) A process allowing methods to call itself

47. What is stringBuffer in java?

  1. Class to create a string array
  2. Class to create a mutable string in java
  3. Class to create a string from i/o buffer
  4. 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?

  1. Array
  2. List
  3. Vector
  4. All of these

Answer: D) All of these

49. Which syntax is valid to create a vector in java?

  1. Vector < string > names = new Vector < String > ();
  2. Vector name = new string;
  3. int name = new vector ()
  4. 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);
  }
}
  1. Error
  2. [mango, orange, guava, mango, apple]
  3. [mango, orange, guava, apple]
  4. None of these

Answer: B) [mango, orange, guava, mango, apple]

51. Which of these is true for interfaces in java?

  1. The keyword interface is used to create a method
  2. All the methods of an interface are abstract
  3. It does not contain constructors
  4. All of these

Answer: D) All of these

52. Encapsulation is ___.

  1. Wrapping up of data and related functions into a single entity
  2. Creating special methods
  3. Creating special data structure
  4. 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?

  1. createString()
  2. toString()
  3. object.string()
  4. newString()

Answer: B) toString()

54. What is a comparator in Java?

  1. Interface to compare integer
  2. Comparison method for lists
  3. Interface to compare two objects in java
  4. All of these

Answer: C) Interface to compare two objects in java

55. Which of the following methods are present in comparator interface?

  1. compare()
  2. equate()
  3. isEqual()
  4. All of these

Answer: A) compare()

56. Which of the following statements is not correct for vectors in Java?

  1. It was created using vector keyword
  2. It can store an object of different classes
  3. It is asynchronous
  4. 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);
  }
}
  1. Error
  2. pro
  3. gie
  4. progie

Answer: D) progie

58. What is a deadlock in Java?

  1. State when all processes have complete working and are dead
  2. State when threads are in hold state forever
  3. State when threads are not ready
  4. All of these

Answer: B) State when threads are in hold state forever

59. Which graph is used to check for deadlock in Java?

  1. Deadlock graph
  2. Time graph
  3. Wait-for-graph
  4. None of these

Answer: C) Wait-for-graph

60. Batch processing in java is ___.

  1. 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
  2. Used to processing multiple queries can be executed at once
  3. Used to increase program’s performance
  4. All of these

Answer: D) All of these

61. Null in Java is ___.

  1. Reserved keyword
  2. Literal value
  3. Used in exception handling
  4. All of these

Answer: D) All of these

62. Enumeration in Java is ___.

  1. Data type which contains fixed set of constants
  2. Method
  3. Class
  4. None of these

Answer: A) Data type which contains fixed set of constants

63. Can we pass objects to method arguments in Java?

  1. Yes
  2. No

Answer: A) Yes

64. Which of the following ways is the correct way to create an object in Java?

  1. Using the new keyword
  2. Using newInstance() method
  3. clone() method
  4. All of these

Answer: D) All of these

65. Which statement is correct for private member in Java?

  1. Access outside the class is allowed
  2. Any class can access
  3. Declared using private keyword
  4. All of these

Answer: C) Declared using private keyword

66. Which keyword is used to inherit classes in Java?

  1. extends
  2. inheritance
  3. isChild
  4. None of these

Answer: A) extends

67. Which of the following inheritance of class is invalid in Java?

  1. Single
  2. Multiple
  3. Multi-level
  4. Hierarchical

Answer: B) Multiple

68. The ‘implements’ keyword is used to ___.

  1. Implement the function of a class
  2. Inherit an interface in Java
  3. Inherit a class in java
  4. All of these

Answer: B) Inherit an interface in Java

69. What is polymorphism in Java?

  1. Performing a single task in multiple ways
  2. Performing multiple tasks using multiple methods
  3. Creating a new class for each task
  4. All of these

Answer: A) Performing a single task in multiple ways

70. What are packages in Java?

  1. Methods of a friend class
  2. Methods of the main class
  3. Way to encapsulate a group of classes, sub-packages, and interface
  4. All of these

Answer: C) Way to encapsulate a group of classes, sub-packages, and interface

71. Empty interface in Java is called?

  1. Marker interface
  2. Abstract class
  3. Derived class
  4. None of these

Answer: A) Marker interface

72. Which of these is a non-access modifier?

  1. public
  2. private
  3. native
  4. All of these

Answer: C) native

73. When a finally block executed in Java?

  1. Try block is executed without any exception
  2. Exception has occurred
  3. Executed at last
  4. None of these

Answer: C) Executed at last

74. What is boolean in Java?

  1. A value consisting of only true and false value
  2. A value consisting of 8 values
  3. Truthy value in java
  4. 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?

  1. equals() method
  2. hashCode() method
  3. toString() method
  4. All of these

Answer: D) All of these

76. Which method in Java is used to check for NaN values?

  1. isNan()
  2. checkNan()
  3. isNotNan()
  4. All of these

Answer: A) isNan()

77. Which of these is a property of threads in Java?

  1. Multiple threads can be executed concurrently
  2. Has its own priority
  3. Both A. and B.
  4. None of these

Answer: C) Both A. and B.

78. Which thread is executed in the background?

  1. New thread
  2. User-created thread
  3. Daemon thread
  4. All of these

Answer: C) Daemon thread

79. Multithreading in java is ___.

  1. Executing multiple processes simultaneously
  2. Creating more threads at a time
  3. Blocking threads
  4. 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));
  }
}
  1. 100.6
  2. -100.6
  3. -200.6
  4. 200.6

Answer: B) -100.6

81. Which method is used to convert radians to degree in Java?

  1. convertRadtoDeg()
  2. toDegrees()
  3. degree()
  4. All of these

Answer: B) toDegrees()

82. Which of the following methods is used to extract the length of a string in Java?

  1. length()
  2. len()
  3. sizeof()
  4. size()

Answer: A) length()

83. The trim() method in Java used to ___.

  1. Remove the given character
  2. Remove the values after the given index
  3. Remove leading and trailing spaces
  4. None of these

Answer: C) Remove leading and trailing spaces

84. What are regexes in Java?

  1. API to define a pattern for searching strings
  2. String
  3. Array to create a new integer
  4. Wrapper class

Answer: A) API to define a pattern for searching strings

85. What is a map in Java?

  1. Data structure
  2. Defined in java.util package
  3. Represented using key-value pairs
  4. All of these

Answer: D) All of these

86. What is a set in Java?

  1. Represented in the form of values
  2. Used to store key-value pairs
  3. Primary structures
  4. 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);
  }
}
  1. {10 =Java, 3 =C, 4 =C++, 6 =null, 5 =Ruby}
  2. {10 =Java, 6 =null, 5 =Ruby, 4 =C++, 3 =C}
  3. {3 =C, 4 =C++, 5 =Ruby, 6 =null, 10 =Java}
  4. 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?

  1. Hashtable
  2. Map
  3. Array
  4. None of these

Answer: A) Hashtable

89. Which Java method is used to clear element of ArrayList?

  1. deleteAll()
  2. delete()
  3. clearAll()
  4. clear()

Answer: D) clear()

90. Which Java method is used to add all of the specified elements to the specified collection?

  1. addValue()
  2. copy()
  3. cpy()
  4. addAll()

Answer: D) addAll()

91. Which Java method is used to detect the OS in which Java program is being run?

  1. system.getOSdetails()
  2. system.get(os.name)
  3. system.getProperties(“os.name”)
  4. system.getProperties(“os”)

Answer: C) system.getProperties(“os.name”)

92. What is the default encoding of OutstreamWriter?

  1. UTF-32
  2. UTF-16
  3. UTF-12
  4. 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?

  1. System.getProperties(“java.vm.name”)
  2. System.vmName
  3. Sytem.getVmName
  4. 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?

  1. System.vm.version
  2. System.getProperties(“vm.version”)
  3. System.getProperties(“java.vm.version”)
  4. System.getVmVersion

Answer: C) System.getProperties(“java.vm.version”)

95. What is the full form of AWT?

  1. Absolute window toolKit
  2. Abstract window toolKit
  3. Absolute wear kit
  4. Abstract window tools

Answer: B) Abstract window toolKit

96. Which escape character is used for word character in regex?

  1. /w
  2. /c
  3. /str
  4. /?

Answer: A) /w

97. Jar in java stands for ___.

  1. Java ARchive
  2. Java application runtime
  3. Java application runner
  4. None of these

Answer: A) Java ARchive

98. Which Java keyword is used to access features of a package?

  1. get
  2. import
  3. extends
  4. All of these

Answer: B) import

99. The result of dividing by 0 in Java is ___.

  1. Error
  2. Expectation
  3. Infinite
  4. 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);
  }
}
  1. progie
  2. pro
  3. gie
  4. 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