Java MCQ Set 1
1. Which of the following has highest memory requirement?
a) Heap
b) Stack
c) JVM
d) Class
Answer
Answer: c [Reason:] JVM is the super set which contains heap, stack, objects, pointers, etc.
2. Where is a new object allotted memory?
a) Young space
b) Old space
c) Young or Old space depending on space availability
d) JVM
Answer
Answer: a [Reason:] A new object is always created in young space.Once young space is full, a special young collection is run where objects which have lived long enough are moved to old space and memory is freed up in young space for new objects.
3. Which of the following is a garbage collection technique?
a) Cleanup model
b) Mark and sweep model
c) Space management model
d) Sweep model
Answer
Answer: b [Reason:] A mark and sweep garbage collection consists of two phases, the mark phase and the sweep phase.I mark phase all the objects reachable by java threads, native handles and other root sources are marked alive and others are garbage. In sweep phase, the heap is traversed to find gaps between live objects and the gaps are marked free list used for allocating memory to new objects.
4. What is -Xms and -Xmx while starting jvm?
a) Initial; Maximum memory
b) Maximum; Initial memory
c) Maximum memory
d) Initial memory
Answer
Answer: a [Reason:] JVM will be started with Xms amount of memory and will be able to use a maximum of Xmx amount of memory. java -Xmx2048m -Xms256m.
5. Which exception is thrown when java is out of memory?
a) MemoryFullException
b) MemoryOutOfBoundsException
c) OutOfMemoryError
d) MemoryError
Answer
Answer: c [Reason:] The Xms flag has no default value, and Xmx typically has a default value of 256MB. A common use for these flags is when you encounter a java.lang.OutOfMemoryError.
6. How to get prints of shared object memory maps or heap memory maps for a given process?
a) jmap
b) memorymap
c) memorypath
d) jvmmap
Answer
Answer: a [Reason:] We can use jmap as jmap -J-d64 -heap pid .
7. What happens to thread when garbage collection kicks off?
a) The thread continues its operation
b) Garbage collection cannot happen until the thread is running
c) The thread is paused while garbage collection runs
d) The thread and garbage collection do not interfere with each other
Answer
Answer: c [Reason:] The thread is paused when garbage collection runs which slows the application performance.
8. Which of the below is not a Java Profiler?
a) JVM
b) JConsole
c) JProfiler
d) Eclipse Profiler
Answer
Answer: a [Reason:] Memory leak is like holding a strong reference to an object although it would never be needed anymore. Objects that are reachable but not live are considered memory leaks. Various tools help us to identify memory leaks.
9. Which of the below is not a memory leak solution?
a) Code changes
b) JVM parameter tuning
c) Process restart
d) GC parameter tuning
Answer
Answer: c [Reason:] Process restart is not a permanent fix to memory leak problem. The problem will resurge again.
10. Garbage Collection can be controlled by program?
a) True
b) False
Answer
Answer: b [Reason:] Garbage Collection cannot be controlled by program.
Java MCQ Set 2
1. Which of the following is not a core interface of Hibernate?
a) Configuration
b) Criteria
c) SessionManagement
d) Session
Answer
Answer: c [Reason:] SessionManagement is not a core interface of Hibernate. Configuration, Criteria, SessionFactory, Session, Query and Transaction are the core interfaces of Hibernate.
2. SessionFactory is a thread-safe object.
a) True
b) False
Answer
Answer: a [Reason:] SessionFactory is a thread-safe object. Multiple threads can access it simultaneously.
3. Which of the following methods returns proxy object?
a) loadDatabase()
b) getDatabase()
c) load()
d) get()
Answer
Answer: c [Reason:] load() method returns proxy object. load() method should be used if it is sure that instance exists.
4. Which of the following methods hits database always?
a) load()
b) loadDatabase()
c) getDatabase()
d) get()
Answer
Answer: d [Reason:] get() method hits database always. Also, get() method does not return proxy object.
5. Which of the following method is used inside session only?
a) merge()
b) update()
c) end()
d) kill()
Answer
Answer: b [Reason:] update() method can only be used inside session. update() should be used if session does not contian presistent object.
6. Which of the following is not a state of object in Hibernate?
a) Attached()
b) Detached()
c) Persistent()
d) Transient()
Answer
Answer: a [Reason:] Attached() is not a state of object in Hibernate. Detached(), Persistent() and Transient() are the only states in Hibernate.
7. Which of the following is not an inheritance mapping strategies?
a) Table per hierarchy
b) Table per concrete class
c) Table per subclass
d) Table per class
Answer
Answer: d [Reason:] Table per class is not an inheritance mapping strategies.
8. Which of the following is not an advantage of using Hibernate Query Language?
a) Database independent
b) Easy to write query
c) No need to learn SQL
d) Difficult to implement
Answer
Answer: d [Reason:] HQL is easy to implement. Also, to implement it HQL it is not dependent on database platform.
9. In which file database table configuration is stored?
a) .dbm
b) .hbm
c) .ora
d) .sql
Answer
Answer: b [Reason:] Database table configuration is stored in .hbm file.
10. Which of the following is not an advantage of Hibernate Criteria API?
a) Allows to use aggregate functions.
b) Cannot order the result set.
c) Allows to fetch only selected columns of result.
d) Can add conditions while fetching results.
Answer
Answer: b [Reason:] addOrder() can be used for ordering the results.
Java MCQ Set 3
1. Which of the following is not introduced with Java 8?
a) Stream API
b) Serialization
c) Spliterator
d) Lambda Expression
Answer
Answer: b [Reason:] Serialization is not introduced with Java 8. It was introduced with earlier version of Java.
2. What is the purpose of BooleanSupplier function interface?
a) represents supplier of Boolean-valued results
b) returns Boolean-valued result
c) There is no such function interface
d) returns null if Boolean is passed as argument
Answer
Answer: a [Reason:] BooleanSupplier function interface represents supplier of Boolean-valued results.
3. What is the return type of lambda expression?
a) String
b) Object
c) void
d) Function
Answer
Answer: d [Reason:] Lambda expression enables us to pass functionality as an argument to another method, such as what action should be taken when someone clicks a button.
4. Which is the new method introduced in java 8 to iterate over a collection?
a) for (String i : StringList)
b) foreach (String i : StringList)
c) StringList.forEach()
d) List.for()
Answer
Answer: c [Reason:] Traversing through forEach method of Iterable with anonymous class.
-
StringList.forEach(new Consumer<Integer>()
-
{
-
public void accept(Integer t)
-
{
-
}
-
});
-
//Traversing with Consumer interface implementation
-
MyConsumer action = new MyConsumer();
-
StringList.forEach(action);
-
}
-
}
5. What are the two types of Streams offered by java 8?
a) sequential and parallel
b) sequential and random
c) parallel and random
d) random and synchronized
Answer
Answer: a [Reason:] Sequential stream and parallel stream are two types of stream provided by java.
-
Stream<Integer> sequentialStream = myList.stream();
-
Stream<Integer> parallelStream = myList.parallelStream();
6. Which feature of java 8 enables us to create a work stealing thread pool using all available processsors at its target?
a) workPool
b) newWorkStealingPool
c) threadPool
d) workThreadPool
Answer
Answer: b [Reason:] Executors newWorkStealingPool() method to create a work-stealing thread pool using all available processors as its target parallelism level.
7. What does Files.lines(Path path) do?
a) It reads all the files at the path specified as a String
b) It reads all the lines from a file as a Stream
c) It reads the filenames at the path specified
d) It counts the number of lines for files at the path specified
Answer
Answer: b [Reason:] Files.lines(Path path) that reads all lines from a file as a Stream.
8. What is Optional object used for ?
a) Optional is used for optional runtime argument
b) Optional is used for optional spring profile
c) Optional is used to represent null with absent value
d) Optional means its not mandatory for method to return object
Answer
Answer: c [Reason:] Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to handle values as ‘available’ or ‘not available’ instead of checking null values.
9. What is the substitute of Rhino javascript engine in java 8?
a) Nashorn
b) V8
c) Inscript
d) Narcissus
Answer
Answer: a [Reason:] Nashorn provides 2 to 10 times faster in terms of performance, as it directly compiles the code in memory and passes the bytecode to JVM. Nashorn uses invokedynamics feature.
10. What does SAM stand for in context of Functional Interface?
a) Single Ambivalue Method
b) Single Abstract Method
c) Simple Active Markup
d) Simple Abstract Markup
Answer
Answer: b [Reason:] SAM Interface stands for Single Abstract Method Interface.Functional Interface is also know as SAM Interface because it contains only one abstract method.
Java MCQ Set 4
1. Which component is used to compile, debug and execute java program?
a) JVM
b) JDK
c) JIT
d) JRE
Answer
Answer: b [Reason:] JDK is core component of Java Environment and provides all the tools, executables and binaries required to compile, debug and execute a Java Program.
2. Which component is responsible for converting byte code into machine specific code?
a) JVM
b) JDK
c) JIT
d) JRE
Answer
Answer: a [Reason:] JVM is responsible to converting byte code to the machine specific code. JVM is also platform dependent and provides core java functions like garbage collection,memory management, security etc.
3. Which component is responsible to run java program?
a) JVM
b) JDK
c) JIT
d) JRE
Answer
Answer: d [Reason:] JRE is the implementation of JVM, it provides platform to execute java programs.
4. Which component is responsible to optimise byte code to machine code?
a) JVM
b) JDK
c) JIT
d) JRE
Answer
Answer: c [Reason:] JIT optimise byte code to machine specific language code by compiling similar byte codes at same time.This reduces overall time taken for compilation of byte code to machine specific language.
5. Which statement is true about java?
a) Platform independent programming language
b) Platform dependent programming language
c) Code dependent programming language
d) Sequence dependent programming language
Answer
Answer: a [Reason:] Java is called ‘Platform Independent Language’ as it primarily works on the principle of ‘compile once, run everywhere’.
6. Which of the below is invalid indentifier with main method?
a) public
b) static
c) private
d) final
Answer
Answer: c [Reason:] main method cannot be private as it is invoked by external method. Other identifier are valid with main method.
7. What is the extension of java code files?
a) .class
b) .java
c) .txt
d) .js
Answer
Answer: b [Reason:] Java files have .java extension.
8. What is the extension of compiled java classes?
a) .class
b) .java
c) .txt
d) .js
Answer
Answer: a [Reason:] The compiled java files have .class extension.
9. How can we identify whether a compilation unit is class or interface from a .class file?
a) Java source file header
b) Extension of compilation unit
c) We cannot differentiate between class and interface
d) The class or interface name should be postfixed with unit type
Answer
Answer: a [Reason:] The Java source file contains a header that declares the type of class or interface, its visibility with respect to other classes, its name and any superclass it may extend, or interface it implements.
10. What is use of interpreter?
a) They convert byte code to machine language code
b) They read high level code and execute them
c) They are intermediated between JIT and JVM
d) It is a synonym for JIT
Answer
Answer: b [Reason:] Interpreters read high level language (interprets it) and execute the program. Interpreters are normally not passing through byte-code and jit compilation.
Java MCQ Set 5
1. JUnits are used for which type of testing?
a) Unit Testing
b) Integration Testing
c) System Testing
d) Blackbox Testing
Answer
Answer: a [Reason:] JUnit is a testing framework for unit testing.It uses java as a programming platform. It is managed by junit.org community.
2. Which of the below statement about JUnit is false?
a) It is an open source framework
b) It provides annotation to identify test methods
c) It provides test runners for running test
d) They cannot be run automatically
Answer
Answer: d [Reason:] JUnits test can be run automatically and they check their own results and provide immediate feedback.
3. Which of the below is an incorrect annotation with respect to JUnits?
a) @Test
b) @BeforeClass
c) @Junit
d) @AfterEach
Answer
Answer: c [Reason:] @Test is used to annotate method under test, @BeforeEach and @AfterEach are called before and after each method respectively. @BeforeClass and @AfterClass are called only once for each class.
4. Which of these is not a mocking framework?
a) EasyMock
b) Mockito
c) PowerMock
d) MockJava
Answer
Answer: d [Reason:] EasyMock, jMock, Mockito, Unitils Mock, PowerMock and JMockit are various mocking framework.
5. Which method is used to verify the actual and expected results in Junits?
a) assert()
b) equals()
c) ==
d) isEqual()
Answer
Answer: a [Reason:] assert method is used to compare actual and expected results in Junit. It has various implementation like assertEquals, assertArrayEquals, assertFalse, assertNotNull, etc.
6. What does assertSame() method use for assertion?
a) equals() method
b) isEqual() method
c) ==
d) compare() method
Answer
Answer: c [Reason:] == is used to compare the objects not the content. assertSame() method compares to check if actual and expected are the same objects. It does not compare their content.
7. How to let junits know that they need to be run using PowerMock?
a) @PowerMock
b) @RunWith(PowerMock)
c) @RunWith(Junits)
d) @RunWith(PowerMockRunner.class)
Answer
Answer: d [Reason:] @RunWith(PowerMockRunner.class) signifies to use PowerMock JUnit runner. Along with that @PrepareForTest(User.class) is used to declare the class being tested. mockStatic(Resource.class) is used to mock the static methods.
8. How can we simulate if then behavior in Junits?
a) if{..} else{..}
b) if(..){..} else{..}
c) Mockito.when(…).thenReturn(…);
d) Mockito.if(..).then(..);
Answer
Answer: c [Reason:] Mockito.when(mockList.size()).thenReturn(100); assertEquals(100, mockList.size()); is the usage to implement if and then behavior.
9. What is used to inject mock fields into the tested object automatically?
a) @InjectMocks
b) @Inject
c) @InjectMockObject
d) @Mock
Answer
Answer: a [Reason:] @InjectMocks annotation is used to inject mock fields into the tested object automatically.
@InjectMocks
MyDictionary dic = new MyDictionary();
10. How can junits be implemented using maven?
a)
-
<dependency>
-
<groupId>junit</groupId>
-
<artifactId>junit</artifactId>
-
<version>4.8.1</version>
-
</dependency>
b)
-
<dependency>
-
<groupId>org.junit</groupId>
-
<artifactId>junit</artifactId>
-
<version>4.8.1</version>
-
</dependency>
c)
-
<dependency>
-
<groupId>mock.junit</groupId>
-
<artifactId>junit</artifactId>
-
<version>4.8.1</version>
-
</dependency>
d)
-
<dependency>
-
<groupId>junits</groupId>
-
<artifactId>junit</artifactId>
-
<version>4.8.1</version>
-
</dependency>
Answer
Answer: a [Reason:] JUnits can be used using dependency tag in maven in pom.xml. The version as desired and available in repository can be used.