Questionnaire MS Java
Questionnaire MS Java
1. Collection ______________
a. inherits the Collections class
b. inherits the Iterable interface
c. implements the Serializable interface
d. implements the Traversable interface
Answer: b (Explanation: Collection is an interface and inherits from the Iterable interface. Also
Collection is both a framework and interface)
2. Which of the following interfaces maintains the order in which the elements are inserted?
a. Set
b. List
c. Map
d. All the answers are true
Answer: b (Explanation: List maintains the insertion order. In Set, only the LinkedHashSet
implementation maintains the insertion order. In Map, LinkedHashMap maintains the insertion
order)
Answer: a
6. If large number of items are stored in hash bucket, what happens to the internal structure?
a. The bucket will switch from LinkedList to BalancedTree
b. The bucket will increase its size by a factor of load size defined
c. The LinkedList will be replaced by another hashmap
d. Any further additions will throw Overflow exception
7. If there is a situation where two or more key objects produce the same final hash value and
hence point to the same bucket location or array index, what is it called?
a. Diffusion
b. Replication
c. Collision
d. Duplication
Answer: c (Explanation: In a hash table, if several elements are computing for the same bucket
then there will be a clash among elements. This condition is called Collision. HashMap handles
collision by using a linked list to store map entries ended up in same array location or bucket
location. From Java 8 onwards, HashMap, ConcurrentHashMap, and LinkedHashMap will use the
balanced tree in place of linked list to handle frequently hash collisions)
12. What class can be used instead of [Link]() to get date and time in Java 8?
a. Clock
b. Timer
c. Time
d. Date
Answer: a
14. Which of the following are functional interfaces? (Select ALL that apply)
a. [Link]
b. [Link]
c. [Link]
d. [Link]
e. [Link]
Answer: b, c, d, e (Explanation: The interface [Link] is not a functional
interface–it has numerous abstract methods. The other four options are functional interfaces.
The functional interface [Link] has an abstract method with the signature
void accept(T t).
The functional interface [Link] has an abstract method with the signature T
get().
The functional interface [Link] has an abstract method with the signature
boolean test(T t);
The functional interface [Link] has an abstract method with the signature R
apply(T t).)
15. PermGen space has been replaced with which of these in Java 8
a. PermSpace
b. PermSpaceGen
c. Metaspace
d. MetaGenSpace
Answer: c (Explanation: PermGen always has a fixed maximum size & inefficient garbage
collection. MetaSpace grows automatically by default. Here, the garbage collection is
automatically triggered when the class metadata usage reaches its maximum metaspace size,
leading to efficient garbage collection)
Answer: a, b and d (Explanation: limit, peek, and skip are intermediate operations. The
anyMatch is a terminal operation.)
Answer: c (Explanation: max is the only terminal operation. sorted, flatMap, and distinct are
intermediate operations.)
18. What is the process of defining a method in a subclass having same name & type signature as a
method in its superclass?
a. Method overloading
b. Method overriding
c. Method hiding
d. None of the mentioned
Answer: b (Explanation: If a subclass provides the specific implementation of the method that
has been declared by one of its parent class, it is known as method overriding. Method
overriding is used to provide the specific implementation of a method which is already provided
by its superclass. Method overriding is used for runtime polymorphism- Java virtual machine
determines the proper method to call at the runtime, not at the compile time. It is also called
dynamic or late binding.)
21. What is the process of defining two or more methods within same class that have same name
but different parameters declaration?
a. Method overloading
b. Method overriding
c. Method hiding
d. none of the mentioned
Answer: a (Explanation: Two or more methods can have same name as long as their parameters
declaration is different, the methods are said to be overloaded and process is called method
overloading. Method overloading is a way by which Java implements polymorphism. Advantage
of method overloading is it increases the readability of the program.
There are 2 different ways to overload the method a) By changing number of arguments and b)
By changing the data type. Method overloading is used for compile time polymorphism- At
compile-time, java knows which method to call by checking the method signatures. So this is
called compile-time polymorphism or static or early binding)
GET: This is used for fetching details from the server and is basically a read-only operation.
POST: This method is used for the creation of new resources on the server.
PUT: This method is used to update the old/existing resource on the server or to replace the
resource.
OPTIONS: This fetches the list of supported options of resources present on the server.
The POST, GET, PUT, DELETE corresponds to the create, read, update, delete operations which
are most commonly called CRUD Operations. GET, HEAD, OPTIONS are safe and idempotent
methods whereas PUT and DELETE methods are only idempotent. POST and PATCH methods are
neither safe nor idempotent
([Link]
and-explain-its-relevancy-in-restful-web-services))
23. What is the difference between intermediate and terminal operations? (Select ALL that apply)
a. The intermediate operation produces stream pipelining, Terminal operation terminate
the pipeline
b. Intermediate operations can be chained multiple times on a stream, Terminal
operations cannot be chained various times.
c. Intermediate operations cannot be evaluated independently; it needs a terminal
operation for evaluation. Terminal Operations can be evaluated independently.
d. All the above
Answer: d (Explanation: All the above)
24. Method reference is used to refer method of functional interface (Select True or False)
a. True
b. False
25. The Stream API is used to process a group of objects. A stream is a series of objects which
supports different methods that can be pipelined to produce the expected result.
a. A stream takes the input from the Collections, an Arrays or the I/O channels.
b. Streams don’t alter the original data structure; they only give the result according to the
pipelined methods.
c. Each intermediate operation is executed in a lazy manner, and as a result, it returns a
stream. Hence various intermediate processes can be pipelined. The terminal operations
remain at the end of the pipelining process. It replaces the final value, and the pipeline
is terminated.
d. All the Java stream API interfaces and classes are in [Link] package
e. Streams are not modifiable i.e one can’t add or remove elements from streams.
f. It stores/holds all the data that the data structure currently has in a particular data
structure like Set, List or Map,
g. They don’t use functional interfaces.
Answer: a, b, c, d and e (Explanation: f. is not true as Streams doesn’t store data, it operates
on the source data structure i.e.; collection. g. is not true as Streams use functional
interfaces like lambda which makes it a good fit for programming language.)
26. What is the difference between Comparable and Comparator? (Select ALL that apply)
a. Comparable provides a single sorting sequence. In other words, we can sort the
collection on the basis of a single element such as id, name, and price. The Comparator
provides multiple sorting sequences. In other words, we can sort the collection on the
basis of multiple elements such as id, name, and price etc.
b. Comparable affects the original class, i.e., the actual class is modified. Comparator
doesn't affect the original class, i.e., the actual class is not modified.
c. Comparable provides compareTo() method to sort elements. Comparator provides
compare() method to sort elements.
d. Comparable is present in [Link] package. A Comparator is present in the [Link]
package.
e. We can sort the list elements of Comparable type by [Link](List) method. We
can sort the list elements of Comparator type by [Link](List, Comparator)
method.
f. All the above
Answer: a and d
Answer: d (Explanation: Singleton scope in the spring framework is the default bean scope in
the IOC container. It tells the container to exactly create a single instance of the object. This
single instance is stored in the cache and all the subsequent requests for that named bean
return the cached instance. The word “Singleton” in Spring is used for a bean scope, which
means that the bean will only be created once for the entire application. Singleton usually
stands for the GOF (Gang of Four) pattern. It is an object-oriented model ensuring that there will
only be one instance of a class.)
Answer: b (Explanation: In Spring, the objects that form the backbone of your application and
that are managed by the Spring IoC container are called beans. A bean is an object that is
instantiated, assembled, and otherwise managed by a Spring IoC container)
30. How does spring achieve Dependency Injection or IOC (Inversion of Control)?
a. Service locator pattern
b. Factory pattern
c. Abstract factory pattern
d. Singleton pattern
Answer: a (Explanation: The service locator pattern has the same goal as dependency injection.
It removes the dependency that a client has on the concrete implementation. The following
quote from Martin Fowler’s article summaries the core idea:
“The basic idea behind a service locator is to have an object that knows how to get hold of all of
the services that an application might need. So, a service locator for this application would have
a method that returns a ‘service’ when one is needed.”)
Answer: a (Explanation: No, singleton beans are not thread-safe in Spring framework.)
}
}
d) void merge_sort(int arr [], int left , int right){
if (left < right) {
int mid = (right - left)/2;
merge(arr, left, mid, right); //function to merge sorted arrays
merge_sort(arr, left,mid);
merge_sort(arr, mid+1 , right);
}
}
Correct Answer: B
Explanation:Merge Sort is a Divide & Conquer principle based algorithm. It divides input array in
two halves, calls itself for the two halves and then it merges the two sorted halves to obtain
sorted array.
38. Using stream api, filter list of employees having experience greater than 2 from city Delhi.
Correct Answer: C
40. Choose the code to merge two arrays and sort it.
a) public void merge(int[] nums1, int m, int[] nums2, int n) {
for (int i = 0; i < n; i++) {
nums1[i + m] = nums2[i];
}
[Link](nums1);
}
b) public void merge(int[] nums1, int m, int[] nums2, int n) {
for (int i = 0; i < n+1; i++) {
nums1[i + m] = nums2[i];
}
[Link](nums1);
}
c) public void merge(int[] nums1, int m, int[] nums2, int n) {
for (int i = 0; i < n+1; i++) {
nums1[] = nums2[i+n];
}
[Link](nums1);
}
d) public void merge(int[] nums1, int m, int[] nums2, int n) {
for (int i = 1; i < n; i++) {
nums1[i + m] = nums2[i];
}
[Link](nums2);
}
Correct Answer: A
PriorityQueue
log(n) operations: offer(), poll(), remove(), add()
O(1) opearations: peek(), size()
O(n) operations: remove(object), contains(object)
42. While using Mockito which annotation is used to create a real object and spy on that real object.
a) @Mock
b) @captor
c) @spy
d) @InjectMocks
Correct Answer: D
Explanation:
The @Spy annotation is used to create a real object and spy on that real object. A spy helps to
call all the normal methods of the object while still tracking every interaction, just as we would
with a mock.
Correct Answer: A
Explanation: The class must implement the [Link] interface to achieve serialization.
44. Which layer in spring boot architecture, handles the HTTP requests, translates the JSON
parameter to object, and authenticates the request and transfer it further layer.
a) Presentation Layer
b)Business Layer
c)Persistence Layer
d)Database Layer
Correct Answer: A
Explanation:
Presentation Layer: The presentation layer handles the HTTP requests, translates the JSON
parameter to object, and authenticates the request and transfer it to the business layer. In
short, it consists of views i.e., frontend part.
45. Which of the following dependency is used to implement Aop in spring boot?
a) spring-boot-starter-aspect
b) spring-boot-starter-aop
c) spring-boot-starter-aspectj
d)None of the above
Correct Answer: B
Explanation:
To implement AOP, start with including the spring-boot-starter-aop module in the application
dependencies. It transitively imports spring-aop and aspectjweaver dependencies into the
application.
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
Correct Answer: B
Explanation:
Unchecked exception caught at run time when we execute the java program. Unchecked java
exceptions example are ArithmeticException, null pointer exception etc. let’s say at the run time
in the program if a number divide by zero occurs then arithmetic exception happens.
48. ArrayList is better for _______ data. LinkedList is better for _______ data.
a) storing, accessing
b) storing and accessing, manipulating
c) manipulating, storing
d) storing, manipulating
Correct Answer: B
Explanation:
ArrayList is better for storing and accessing data. LinkedList is better for manipulating data.
49. The object of Criteria can be obtained by calling the ________ method of Session interface.
a) addCriteria()
b) setCriteria()
c) createCriteria()
d) getCriteria()
Correct Answer: C
Explanation:
The Criteria interface provides many methods to specify criteria. The object of Criteria can be
obtained by calling the createCriteria() method of Session interface.
Syntax:
Crietria c=[Link]([Link]);
50. By default, Spring Boot will instantiate its default DataSource with the configuration properties
prefixed by [Link].*:
a)True
b)False
Correct Answer: A
Explanation:
If multiple databases are configured in spring boot application then By default, Spring Boot will
instantiate its default DataSource with the configuration properties prefixed by
[Link].*:
51. What is the below output of the below java program with Method Overriding?
Class Bus
{
Void seatingCapacity ()
{
[Link](“Superclass Seat=32”);
}
}
Class ElectricBus extends Bus
{
Void seatingCapacity()
{
[Link](“Subclass Seats=20”);
}
Void showInfo()
{
seatingCapacity();
[Link]();
}
}
Public class MethodOverriding1
{
Public static void main (String[] args)
{
ElectricBus eb = new ElectricBus();
[Link]();
}
}
a)
Subclass seats =20
Superclass Seats =32
b)
Superclass Seats =32
Subclass Seats=20
c)
Superclass Seats=32
Superclass Seats=32
d)
Subclass Seats=20
Subclass Seats=20
Correct Answer: d
Explanation: Using the keyword “this” calls the local method of the class but nit the method of a
superclass.
52. Identify INVALID java Method Overriding in the below code snippets?
Follow the notation “superclass Method” and “subclass Method”.
a)
void superclassMethod(int a,float b) {}
void subclassMethod(int a,float b){}
b)
void superclassMethod(){}
void subclassMethod(){}
c)
int superclassMethod(int a, float b){}
void subclassMethod(int a,float b){}
d)
None.
Correct Answer: c
Explanation:
The return types are different. So it is not a successful method override.
53. What is the output of the below java program with method overriding?
Class Cat
{
int jumpingHeight( int weight )
{
[Link](10);
return 10;
}
}
Class WildCat extends Cat
{
Void jumpingHeight( int weight)
{
[Link](“20);
}
}
Public class MethodOverriding3
{
Public static void main( String [] args)
{
WildCat wc =new Wildcat();
[Link](30);
}
}
a)10
b)20
c)30
d)Compiler error
Correct Answer: d
Explanation: if the argument list is the same, the return types cannot be the incompatible-
[Link] the compiler reports an error “the return type is incompatible with
[Link](int).
54. What is the output of the below code snippet?
class Sweet
{
void price()
{
[Link](“sweet=$10”);
}
}
class Sugar extend Sweet
{
Void price()
{
[Link]();
[Link](“Sugar=$20”);
}
}
Class sugar extends Sweet
{
Void price()
{
[Link]();
[Link](“Sugar =$20”);
}
}
public class JavaInheritance1
{
Public static void main (String[] args)
{
Sugar su =new Sugar();
[Link]();
}
}
a) Sweet =310 Sugar =$20
b) Sweet =$10 Sugar=$10
c) Sweet =$20 Sugar=$20
d) Compiler error
Correct Answer : a
Explanation:
Notice the use of the keyword “super”. Using this keyword ,you can call super class’s methods
and variables.
55. Which SQL Keyword must be used to remove duplicate rows from the result?
a) Delete
b) Unique
c) Distinct
d) Not Exist
Answer: option c
Explanation: Remove the duplicates columns from the result set.
Answer c:
Explanation: NTILE divides the partitioned result set into specified number of groups in an order.
60. Which of the following function is used when you want all tied rows to have the same ranking?
a) RANK
b) NTILE
c) ROW_NUMBER
d) None of the mentioned
Answer: a
Explanation: The Numbers assigned by RANK are not necessarily consecutive.
61. What type of joins needed when you wish to include rows that do not have matching values?
a) Equi-join
b) Natural join
c) Outer join
d) All of the mentioned
Answer: c
Explanation: Outer Join is the only join which shows the unmatched rows.
Answer: a
Explanation: Subquery also referred to as an inner query or inner select is SELECT statement
embedded within the data manipulation language (DML) Statement or nested within another
subquery.
Answer: b
Explanation: “WHERE” is also used to filter the row values in MySQL.
65. Which of the following belongs to an “Aggregate function”?
a) Count
b) sum/Avg
c) Min/Max
d) All of the mentioned
Answer: d
68. Which key declares that an index in one table is related to that in another?
a) Primary
b) Secondary
c) Foreign
d) cross
Answer: c
Explanation:
In MySQL a foreign key is the one which facilitates index relations across tables. It Declares that
an index in one table is related to that in another and place constraints.
70. To add a Primary key constraint after table is created which clause is used?
a) Update
b) Add
c) Alter
d) Join
Answer:c
Explanation: ALTER Used to add ,delete/drop or modify columns in the existing table.
75. A ___________ is special kind of a store procedure that executes in response to certain action
on the table like insertion,deletion or updation of data.
a) Procedures
b) Triggers
c)Functions
d)None of the mentioned
Answer:b
Explanation:
Triggers are automatically generated when particular operation take place
83. If there is two table one table is family table and another one is Bills table. family table have the
two columns family name and customer id and bill table have two column customer id, amount.
please find the family name which have highest bill in the table.
Correct the Query ?
SELECT [Link], _____([Link])
FROM family
_______ bills
ON f. customerId = b. customerId
GROUP BY [Link]
a) MIN, JOIN
b) MAX, INNER JOIN
c) AVG, FULL JOIN
d) None
Answer: b
Explanation: INNER JOIN (Combining rows that have matching values in two or more tables.)
84. If there is two table one table is customer table and another one is account table. customer
table have the two columns name and customer id and account table have three column
customer id, balance, account id. print the customer id and the sum of the balance.
Fill the blanks to achieve the above scenario?
SELECT customer id, sum(balance) total from (select customer id, name from customer ______
select customer id, balance from account) t group by customer id
a) Union All
b) Union
c) None
d) Intersect
Answer : a
Explanation: Union all operator is used to combine the result set of 2 or more select statements.