MCQs > IT & Programming > Core Java MCQs > Basic Core Java MCQs

Basic Core Java MCQ

1. Which of the following statements is false about objects?

Answer

Correct Answer: Objects do not permit encapsulation

Note: This Question is unanswered, help us to find answer for this one

2. True or False: All classes that can be instantiated must implement the class Object

Answer

Correct Answer: False

Note: This Question is unanswered, help us to find answer for this one

3. java.lang.Object is the super class of all the interfaces in java

Answer

Correct Answer: false

Note: This Question is unanswered, help us to find answer for this one

4. what is order of preference in method overloading

Answer

Correct Answer: Widening, Boxing, Varargs

Note: This Question is unanswered, help us to find answer for this one

5. Given a class containing the following pieces of code: public static <T> List getList() { return new ArrayList<T>(); } // ... List<?> list = new ArrayList<Integer>(); Adding which of the following lines would cause the class to not compile?

Answer

Correct Answer: List<Integer> list2 = list;

Note: This Question is unanswered, help us to find answer for this one

6. how java is platform independent?

Answer

Correct Answer: it has JVM for different platforms

Note: This Question is unanswered, help us to find answer for this one

7. The JVM allows programmers to manipulate the registers of the underlying system

Answer

Correct Answer: False

Note: This Question is unanswered, help us to find answer for this one

8. What is a LinkedHashSet?

Answer

Correct Answer: A hash set which preserves the order in which objects were inserted.

Note: This Question is unanswered, help us to find answer for this one

9. which is the default package in every java program?

Answer

Correct Answer: java.lang.*

Note: This Question is unanswered, help us to find answer for this one

10. The data members of interface in java are:

Answer

Correct Answer: public static final

Note: This Question is unanswered, help us to find answer for this one

11. ConcurrentModificationException will be thrown only when an object is concurrently modified.

Answer

Correct Answer: False

Note: This Question is unanswered, help us to find answer for this one

12. Which one is most suitable to implement an LRU cache?

Answer

Correct Answer: LinkedHashMap

Note: This Question is unanswered, help us to find answer for this one

13. Do Hashtable allow null keys and null values?

Answer

Correct Answer: False

Note: This Question is unanswered, help us to find answer for this one

14. HashMap is non-synchronized ?

Answer

Correct Answer: True

Note: This Question is unanswered, help us to find answer for this one

15. ConcurrentModificationException extends .......

Answer

Correct Answer: RuntimeException

Note: This Question is unanswered, help us to find answer for this one

16. What is super class of all classes in Java?

Answer

Correct Answer: Object class

Note: This Question is unanswered, help us to find answer for this one

17. A Guarded Block is a concurrency idiom in which:

Answer

Correct Answer: A condition is polled before the execution of a code block can proceed

Note: This Question is unanswered, help us to find answer for this one

18. What will be the output of this code? class Main { static abstract class Base { protected Base() { init(); } abstract void init(); } static class Child extends Base { private final int value; public Child() { value = 5; } @Override public void init() { System.out.println("value = " + value); } } public static void main(String[] args) { Child c = new Child(); } }

Answer

Correct Answer: value = 0

Note: This Question is unanswered, help us to find answer for this one

19. Given a class containing the following pieces of code: ---------------------- public static <T> List getList() { return new ArrayList<T>(); } // ... List<?> list = new ArrayList<Integer>(); ---------------------- Adding which of the following lines would cause the class to not compile?

Answer

Correct Answer: List<Integer> list2 = list;

Note: This Question is unanswered, help us to find answer for this one

20. Livelock describes a situation in which two or more threads block each other, because:

Answer

Correct Answer: Their actions are also responses to the actions of others, such that all are too busy to respond

Note: This Question is unanswered, help us to find answer for this one

21. Which of the following is true about the Cloneable interface?

Answer

Correct Answer: It changes the behavior of the protected clone method to give a field-by-field (reference) copy of the object.

Note: This Question is unanswered, help us to find answer for this one

22. The TreeMap and LinkedHashMap classes:

Answer

Correct Answer: Enable iteration of a map's entries in a deterministic order.

Note: This Question is unanswered, help us to find answer for this one

23. You want to listen TCP connections on port 9000. How would you create the socket?

Answer

Correct Answer: new ServerSocket(9000);

Note: This Question is unanswered, help us to find answer for this one

24. public class Test{ public static void main(String [] arg){ int x=10; if(x++ > 10 && x++ == 12){ ++x; } System.out.println(x); } } What is the Output of this code?

Answer

Correct Answer: 11

Note: This Question is unanswered, help us to find answer for this one

25. What is the output of the below code ? int a = 0; int b = 0; if (a++ == 1 && b++ == 1); System.out.println(a + " " + b);

Answer

Correct Answer: 1 0

Note: This Question is unanswered, help us to find answer for this one

26. What is the output? public static void main(String[] args) { int x=10; if(x++ > 9 && x++ == 12){ ++x; } System.out.println(x); }

Answer

Correct Answer: 12

Note: This Question is unanswered, help us to find answer for this one

27. You need to keep a list of one million objects sorted at all times having 100 random inserts and delete per second. Reads from the list are always done in sorted order. You would:

Answer

Correct Answer: Use a PriorityQueue to keep the list ordered at all times.

Note: This Question is unanswered, help us to find answer for this one

28. Which of the following are not a valid declarations?

Answer

Correct Answer: float f = 1.2;

Note: This Question is unanswered, help us to find answer for this one

29. True or false: Lambdas allow you to change single instance's method logic

Answer

Correct Answer: FALSE

Note: This Question is unanswered, help us to find answer for this one

30. Which code fragments correctly create and initialize a static array of int elements?

Answer

Correct Answer: static final int[] a; static { a=new int[2]; a[0]=100; a[1]=200; }

Note: This Question is unanswered, help us to find answer for this one

31. What is the output of the following program? import java.lang.reflect.Method; class TestImpl { public void method() {} public static void main(String[] args) { Method method = TestImpl.class.getMethod("method", null); System.out.println(method.getName()); } }

Answer

Correct Answer: Compilation Error

Note: This Question is unanswered, help us to find answer for this one

32. I would implement a LRU cache using only JDK classes by...

Answer

Correct Answer: A TreeMap and iteration over values that containing a last accessed timestamp

Note: This Question is unanswered, help us to find answer for this one

33. Which two are valid constructors for Thread? 1. Thread(Runnable r, String name) 2. Thread() 3. Thread(int priority) 4. Thread(Runnable r, ThreadGroup g) 5. Thread(Runnable r, int priority)

Answer

Correct Answer: 1 and 2

Note: This Question is unanswered, help us to find answer for this one

34. The TreeMap class is Java's implementation of which data structure?

Answer

Correct Answer: Red-Black tree

Note: This Question is unanswered, help us to find answer for this one

35. A "blank" final variable (defined without an initial value):

Answer

Correct Answer: Can be initialized later, but only in a single location

Note: This Question is unanswered, help us to find answer for this one

36. The keyword that ensures a field is coherently accessed by multiple threads is:

Answer

Correct Answer: "volatile"

Note: This Question is unanswered, help us to find answer for this one

37. which statement is True ?

Answer

Correct Answer: The notifyAll() method must be called from a synchronized context.

Note: This Question is unanswered, help us to find answer for this one

38. Given the code: Integer i= new Integer("1"); if (i.toString() == i.toString()) System.out.println("Equal"); else System.out.println("Not Equal");

Answer

Correct Answer: Prints "Not Equal"

Note: This Question is unanswered, help us to find answer for this one

39. Which one of the following statements is true about threads in Java

Answer

Correct Answer: the notify() method can only be called from inside a synchronized block or from a synchronized method

Note: This Question is unanswered, help us to find answer for this one

40. Which four options describe the correct default values for array elements of the types indicated? 1. int -> 0 2. String -> "null" 3. Dog -> null 4. char -> '\u0000' 5. float -> 0.0f 6. boolean -> true

Answer

Correct Answer: 1, 3, 4, 5

Note: This Question is unanswered, help us to find answer for this one

41. Does Java 8 allow static methods in interfaces?

Answer

Correct Answer: Yes

Note: This Question is unanswered, help us to find answer for this one

42. What's the output of following error ? class A { public Number getNumber(){ return 1; } } class B extends A { public int getNumber(){ return 2; } } public class Main{ public static void main(String []args){ A a = new B(); System.out.println(a.getNumber()); } }

Answer

Correct Answer: Compilation error

Note: This Question is unanswered, help us to find answer for this one

43. Anonymous inner classes have access to...

Answer

Correct Answer: Final, local variables in the containing scope

Note: This Question is unanswered, help us to find answer for this one

44. All of the classes in the Java Collections Framework:

Answer

Correct Answer: Have methods to retrieve their data as an Array

Note: This Question is unanswered, help us to find answer for this one

45. Float p = new Float(3.14f); if (p > 3) { System.out.print("p is bigger than 3. "); } else { System.out.print("p is not bigger than 3. "); } finally { System.out.println("Have a nice day."); } What is the result?

Answer

Correct Answer: Compilation fails.

Note: This Question is unanswered, help us to find answer for this one

46. In your program, you need to read a zip file (myfile.zip) containing several other data files containing basic Java objects. Which of the following will allow you to construct a InputStream for the task?

Answer

Correct Answer: new ObjectInputStream(new ZipInputStream( new FileInputStream((“myfile.zip”)));

Note: This Question is unanswered, help us to find answer for this one

47. Which statement is true?

Answer

Correct Answer: catch(X x) can catch subclasses of X where X is a subclass of Exception.

Note: This Question is unanswered, help us to find answer for this one

48. To create a single instance of a class, we can go with

Answer

Correct Answer: (none of these)

Note: This Question is unanswered, help us to find answer for this one

49. Is "Method" a part of Java Reflection API

Answer

Correct Answer: Yes

Note: This Question is unanswered, help us to find answer for this one

50. What type should you use for floating point monetary calculations?

Answer

Correct Answer: BigDecimal

Note: This Question is unanswered, help us to find answer for this one

51. Does interrupt() always force all threads to terminate?

Answer

Correct Answer: No, if the interruption is not enabled for the thread, it will not terminate

Note: This Question is unanswered, help us to find answer for this one

52. Which of the following statements about static inner classes is true?

Answer

Correct Answer: A static inner class has no reference to an instance of the enclosing class.

Note: This Question is unanswered, help us to find answer for this one

53. Can the "main" method be overloaded

Answer

Correct Answer: Yes

Note: This Question is unanswered, help us to find answer for this one

54. Java variables are passed into methods as:

Answer

Correct Answer: Pass-by Reference

Note: This Question is unanswered, help us to find answer for this one

55. What is the issue with the following code? String s = ""; for(int i = 0; i < 1000000; i++) { s += Integer.toString(i); }

Answer

Correct Answer: It will perform very slowly because strings are immutable.

Note: This Question is unanswered, help us to find answer for this one

56. Java source code is compiled into

Answer

Correct Answer: Byte Code

Note: This Question is unanswered, help us to find answer for this one

57. A class implementing a singleton pattern has...

Answer

Correct Answer: no public constructors, a public static factory method, a static instance variable.

Note: This Question is unanswered, help us to find answer for this one

58. Which is a valid keyword in java?

Answer

Correct Answer: interface

Note: This Question is unanswered, help us to find answer for this one

59. What is a weak reference?

Answer

Correct Answer: A reference to an object which may have been garbage collected when the object is asked for.

Note: This Question is unanswered, help us to find answer for this one

60. How do you convert int[] to a ArrayList<Integer>?

Answer

Correct Answer: Using the static Arrays.asList method

Note: This Question is unanswered, help us to find answer for this one

61. ’blank" final variable (defined without an initial value:

Answer

Correct Answer: Can be initialized later, but only in a single location

Note: This Question is unanswered, help us to find answer for this one

62. enum Example { ONE, TWO, THREE } Which statement is true?

Answer

Correct Answer: The expressions (ONE == ONE) and ONE.equals(ONE) are both guaranteed to be true.

Note: This Question is unanswered, help us to find answer for this one

63. Which of the following is true about overloading vs overriding methods?

Answer

Correct Answer: Overloading happens at compile time, while overriding happens at runtime

Note: This Question is unanswered, help us to find answer for this one

64. Output of Program: class Test {public static void main(String[] ar) {int i=0; if(i) System.out.print("hi"); else System.out.print("bye"); } }

Answer

Correct Answer: Compilation error: incompatible types

Note: This Question is unanswered, help us to find answer for this one

65. Java's String class is

Answer

Correct Answer: Final, with immutable instances

Note: This Question is unanswered, help us to find answer for this one

66. public class SomeClass { public static void main(String[] args) { System.out.println((String) null); } } What is the result of the following program?

Answer

Correct Answer: The code prints "null" on console.

Note: This Question is unanswered, help us to find answer for this one

67. Java's automatic memory management:

Answer

Correct Answer: Can be tuned using Virtual Machine settings

Note: This Question is unanswered, help us to find answer for this one

68. Which option is true for StringBuffer and StringBuilder

Answer

Correct Answer: StringBuffer are thread safe and StringBuilder are not thread safe

Note: This Question is unanswered, help us to find answer for this one

69. Which of the following is used to see the details of compilation

Answer

Correct Answer: javac -verbose TestExample.java

Note: This Question is unanswered, help us to find answer for this one

70. The List interface has which superinterfaces?

Answer

Correct Answer: Both Collection and Iterable

Note: This Question is unanswered, help us to find answer for this one

71. Which is a reserved word in the Java programming language?

Answer

Correct Answer: native

Note: This Question is unanswered, help us to find answer for this one

72. A method without an access modifier (i.e. public, private, protected) is...

Answer

Correct Answer: package-private

Note: This Question is unanswered, help us to find answer for this one

73. In addition to CORBA, Core Java also supports network services using:

Answer

Correct Answer: Remote Method Invocation

Note: This Question is unanswered, help us to find answer for this one

74. Which one of these lists contains only Java programming language keywords?

Answer

Correct Answer: class, if, void, long, int, continue

Note: This Question is unanswered, help us to find answer for this one

75. Which class/classes is/are thread safe among these?

Answer

Correct Answer: String and StringBuffer

Note: This Question is unanswered, help us to find answer for this one

76. What is the difference between a checked and unchecked exception?

Answer

Correct Answer: Checked exceptions must be caught while unchecked do not need to be caught

Note: This Question is unanswered, help us to find answer for this one

77. Java provides a class for mutable sequences of characters, called:

Answer

Correct Answer: StringBuffer

Note: This Question is unanswered, help us to find answer for this one

78. What is the correct statement for handling RuntimeException?

Answer

Correct Answer: It can be handled by try-catch on place, but it can also be propagated using "throws" keyword

Note: This Question is unanswered, help us to find answer for this one

79. A class may extend:

Answer

Correct Answer: Only one non-final class

Note: This Question is unanswered, help us to find answer for this one

80. Calling System.gc() when using a modern JVM :

Answer

Correct Answer: Does not necessarily force garbage collection to occur, and is not idiomatic java.

Note: This Question is unanswered, help us to find answer for this one

81. Which one of the following statements is true about Java Beans?

Answer

Correct Answer: Java Beans are user defined classes.

Note: This Question is unanswered, help us to find answer for this one

82. Which is the correct command to run an executable JAR file named program.jar?

Answer

Correct Answer: java -jar program.jar

Note: This Question is unanswered, help us to find answer for this one

83. What is the name of the method used to start a thread execution?

Answer

Correct Answer: start();

Note: This Question is unanswered, help us to find answer for this one

84. Why is it important to override hashCode() when you override equals()?

Answer

Correct Answer: Overriding equals without hashCode breaks the contract of hashCode().

Note: This Question is unanswered, help us to find answer for this one

85. An "overloaded" method has what in common with one (or more) methods on the same class?

Answer

Correct Answer: The same name

Note: This Question is unanswered, help us to find answer for this one

86. When the == comparator is used on two objects, it checks to see if they:

Answer

Correct Answer: Are references to exactly the same object

Note: This Question is unanswered, help us to find answer for this one

87. The instanceof operator can be used to determine if an object is:

Answer

Correct Answer: (All of these)

Note: This Question is unanswered, help us to find answer for this one

88. When creating a user defined class for storing objects in a HashMap, which method(s) should be overridden?

Answer

Correct Answer: Both the equals() and hashCode() methods

Note: This Question is unanswered, help us to find answer for this one

89. On which Java major release were Lambdas introduced?

Answer

Correct Answer: Java 8

Note: This Question is unanswered, help us to find answer for this one

90. What are all the different types of access modifiers in Java

Answer

Correct Answer: private, protected, default, public

Note: This Question is unanswered, help us to find answer for this one

91. The "static" keyword marks something as:

Answer

Correct Answer: Belonging to a class, rather than a specific instance

Note: This Question is unanswered, help us to find answer for this one

92. Is it a good pratcice to catch "Throwable"?

Answer

Correct Answer: yes

Note: This Question is unanswered, help us to find answer for this one

93. JDBC addresses the issue of transactions.

Answer

Correct Answer: True

Note: This Question is unanswered, help us to find answer for this one

94. The "java" command line tool is used to:

Answer

Correct Answer: Load and execute java .class files

Note: This Question is unanswered, help us to find answer for this one

95. What is the benefit of ConcurrentHashMap<K,V>?

Answer

Correct Answer: All operations are thread-safe and retrieval operations do not entail locking

Note: This Question is unanswered, help us to find answer for this one

96. How should you create a new class that maps keys to values, using the Java Collections Framework?

Answer

Correct Answer: Implement the Map interface, possibly by extending the AbstractMap class

Note: This Question is unanswered, help us to find answer for this one

97. What is the output of the below code ? int a = 0; int b = 0; if (a++ == 1 || b++ == 1) {} System.out.println(a + " " + b);

Answer

Correct Answer: 1 1

Note: This Question is unanswered, help us to find answer for this one

98. What will below method return? public foo(){ int x = 5; return x; }

Answer

Correct Answer: Compile error

Note: This Question is unanswered, help us to find answer for this one

99. How can we use the class or jar files kept on the network path, within our projects?

Answer

Correct Answer: Including the path and class /jar file name in the Classpath

Note: This Question is unanswered, help us to find answer for this one

100. What will be the output of the program? public class Foo { public static void main(String[] args) { try { return; } finally { System.out.println( "Finally" ); } } }

Answer

Correct Answer: Finally

Note: This Question is unanswered, help us to find answer for this one

101. package test; class Test { } ---------------- package test; class SubTest extends Test{ } Does this code compile without errors if the classes are in separate files?

Answer

Correct Answer: Yes

Note: This Question is unanswered, help us to find answer for this one

102. Reflection mechanism allows to:

Answer

Correct Answer: Modify and inspect properties and methods of objects, even if they are protected or private

Note: This Question is unanswered, help us to find answer for this one

103. What is auto boxing?

Answer

Correct Answer: JVM conversion between primitive types and reference types

Note: This Question is unanswered, help us to find answer for this one

104. Keyword used to access members or methods of superclass?

Answer

Correct Answer: Super

Note: This Question is unanswered, help us to find answer for this one

105. Which of these is true?

Answer

Correct Answer: A class implements an interface but extends a class

Note: This Question is unanswered, help us to find answer for this one

106. What method should you always override when you have overridden the equals() method?

Answer

Correct Answer: hashCode()

Note: This Question is unanswered, help us to find answer for this one

107. When you create a thread with the “new” operator – which one of the following statements is true about its state

Answer

Correct Answer: it will be “runnable” when start() method is called

Note: This Question is unanswered, help us to find answer for this one

108. public class SomeClass { public static void main(String[] args) { System.out.println((String) null); } } Does this code compile without errors?

Answer

Correct Answer: Yes

Note: This Question is unanswered, help us to find answer for this one

109. How can you stop your class from being inherited by another class?

Answer

Correct Answer: Declare the class as final.

Note: This Question is unanswered, help us to find answer for this one

110. Which of the following is a valid constructor signature?

Answer

Correct Answer: public className()

Note: This Question is unanswered, help us to find answer for this one

111. What is the most efficient way to concatenate a large number of strings in Java?

Answer

Correct Answer: The StringBuffer object.

Note: This Question is unanswered, help us to find answer for this one

112. Which of these are advantages of encapsulation in Java?

Answer

Correct Answer: All of these

Note: This Question is unanswered, help us to find answer for this one

113. To document an API, which tool do you use?

Answer

Correct Answer: javadoc

Note: This Question is unanswered, help us to find answer for this one

114. If we want a class not to be overridden,the class must be done as

Answer

Correct Answer: Class should be final

Note: This Question is unanswered, help us to find answer for this one

115. What is the correct syntax for importing java.util.Scanner?

Answer

Correct Answer: import java.util.Scanner;

Note: This Question is unanswered, help us to find answer for this one

116. To define a child class from the Parent class following is used:

Answer

Correct Answer: class Child extends Parent

Note: This Question is unanswered, help us to find answer for this one

117. The part of a "try" block that is always executed is:

Answer

Correct Answer: "finally"

Note: This Question is unanswered, help us to find answer for this one

118. The most reliable way to compare two Strings for equality is by:

Answer

Correct Answer: Using the .equals() or .compareTo() method of one object on the other

Note: This Question is unanswered, help us to find answer for this one

119. Which additional keyword may be used with try-catch blocks?

Answer

Correct Answer: finally

Note: This Question is unanswered, help us to find answer for this one

120. Java handles memory allocation and reuse using a process called:

Answer

Correct Answer: Garbage Collection

Note: This Question is unanswered, help us to find answer for this one

121. The "javac" command line tool is used to:

Answer

Correct Answer: Compile java source files into bytecode class files

Note: This Question is unanswered, help us to find answer for this one

122. What is an example of proper capitalization for a class name?

Answer

Correct Answer: CamelCase

Note: This Question is unanswered, help us to find answer for this one

123. If a method or variable is marked as having the "private" access level, then it can only be accessed from:

Answer

Correct Answer: Inside the same class

Note: This Question is unanswered, help us to find answer for this one

124. A java class which extends another class is usually described with the word:

Answer

Correct Answer: subclass

Note: This Question is unanswered, help us to find answer for this one

125. Which will legally declare, construct, and initialize an array?

Answer

Correct Answer: int myList [] = {4, 3, 7};

Note: This Question is unanswered, help us to find answer for this one

126. The Object.wait() method:

Answer

Correct Answer: Causes the current thread to wait

Note: This Question is unanswered, help us to find answer for this one

127. Interfaces are useful for...

Answer

Correct Answer: creating a design contract that encapsulates implementation

Note: This Question is unanswered, help us to find answer for this one

128. Given following code, what will be the output: import java.lang.reflect.Method; class Bar { private int bar(int a) { return a * a; } } public class Foo { public static void main(String[] args) { try { Bar bar = new Bar(); Method method = bar.getClass().getDeclaredMethod("bar", int.class); method.setAccessible(true); System.out.println(method.invoke(bar, new Integer(5))); } catch (Exception e) { e.printStackTrace(); System.out.println("Error"); } } }

Answer

Correct Answer: 25

Note: This Question is unanswered, help us to find answer for this one

129. True of False? The strictfp keyword ensures that you get the same result on every platform if you perform operations in the floating point variable.

Answer

Correct Answer: True

Note: This Question is unanswered, help us to find answer for this one

130. What is the correct way to create an instance of a class?

Answer

Correct Answer: ClassName varName = new ClassName(arguments);

Note: This Question is unanswered, help us to find answer for this one

131. The Core Java platform provides many benefits to developers, including:

Answer

Correct Answer: A consistent programming interface across multiple hardware platforms

Note: This Question is unanswered, help us to find answer for this one

132. What will be the output of program? class Ab { public static void main(String[] ar) {System.out.print("hi"); } static { System.out.print("how"); } }

Answer

Correct Answer: howhi

Note: This Question is unanswered, help us to find answer for this one

133. Which one of the following is NOT a reserved word in Java?

Answer

Correct Answer: virtual

Note: This Question is unanswered, help us to find answer for this one

search
Core Java Subjects