Loading...
java interview questions and answers

JAVA Interview Questions and Answers

by GangBoard Admin, October 30, 2018

In case you’re searching for Java Interview Questions and answers for Experienced or Freshers, you are at the correct place. There is parcel of chances from many presumed organizations on the planet. The Java advertise is relied upon to develop to more than $5 billion by 2020, from just $180 million, as per Java industry gauges. In this way, despite everything you have the chance to push forward in your vocation in Java Development. Gangboard offers Advanced Java Interview Questions and answers that assist you in splitting your Java interview and procure dream vocation as Java Developer.

Best Java Interview Questions and Answers

Do you believe that you have the right stuff to be a section in the advancement of future Java, the GangBoard is here to control you to sustain your vocation. Various fortune 1000 organizations around the world are utilizing the innovation of Java to meet the necessities of their customers. Java is being utilized as a part of numerous businesses. To have a great development in Java work, our page furnishes you with nitty-gritty data as Java prospective employee meeting questions and answers. Java Interview Questions and answers are prepared by 10+ years experienced industry experts. Java Interview Questions and answers are very useful to the Fresher or Experienced person who is looking for the new challenging job from the reputed company. Our Java Questions and answers are very simple and have more examples for your better understanding.

By this Java Interview Questions and answers, many students are got placed in many reputed companies with high package salary. So utilize our Java Interview Questions and answers to grow in your career. 

Q1) Which of the following main method declarations will fail in compilation?

  1. public static void main(String args)
  2. public static void Main(String args[])
  3. public void main(String args[])
  4. public static main(String args[])

Answer : d
Explanation: All methods in java should have return type.

Q2) Which of the following assignment is incorrect?

  1. int a= (int) 10.0;
  2. double d=10;
  3. float f=3.4;
  4. int b=(byte) 4;

Answer : c
Explanation : Float values should be suffixed with f or should be type casted to float.

Q3) What is the difference between == and equals() method on String class?

Answer: == is used to compare the address of String whereas equals is used to compare the content of the String.

Q4) What is the Output of the following?

class Sample
{        public static void main(String args[])
{
String a=”Dhoni”;
String a1=new String(“Dhoni”);
String a2=”Dhoni”;
System.out.println((a==a1) +”,”+(a==a2));

}
Output: false,true
Explanation:  String a1 will be created in Heap Memory, String a2 will be   returning String a address as the value (“Dhoni”) is already present in String constant pool

Q5) Why primitive data types are still used in Java?

Primitive Data types are performance effective than Wrapper classes in most of the instances, as no object creation is required.

Q6) What is the output of the below program?

class Sample
{
String sam;
public Sample(String val)
{
sam=val;
}
public void print()
{
System.out.println(sam);
}
public static void main(String args[])
{
Sample s1=new Sample(“Test”);
s1.print();
Sample s2=new Sample();
s2.sam=”Test2″;
s2.print();
}

}

  1. Compilation error
  2. Runtime error
  3. Test
  4. Test2

Answer: a
Explanation : When parameterized constructor , default constructor will not be created by JVM.

Q7) What is the use of this keyword in Java?

Answer : ‘this’ keyword is used to refer the current object.

Q8) Select all to which final keyword can be permitted.

  1. Variable
  2. Method
  3. Class
  4. Interface

Answer : a,b,c
Explanation : final is not permitted for interface.

Q9) When will the finalize() method be invoked?

Answer: finalize() will be invoked before an object is destroyed.

Q10) Which of the following is not true about static keyword?

  1. Single copy of static data members is created for all objects.
  2. Static methods can be invoked without creating objects for the class.
  3. Static methods can access only static variables.
  4. Non Static methods cannot access static variables.

    Answer: d

Q11) Explain method overriding?

Explanation: Two methods having same name and same signature in both Parent and Sub class is called method overriding.

Q12) ‘Super’ keyword can be used to access which of the following from Parent class?

  1. Constructors
  2. Methods
  3. Data Members
  4. All the above

Answer : d

Q13)Constructors are called in which of the below order?

  1. Super class to Derived classes
  2. Derived Classes to Super Class

Answer : a

Q14)Objects cannot be created for abstract classes

  1. True
  2. False

Answer : a

Q15)Which of the following is true for the below program?

final class Sample
{
int a;
}

  1. Objects cannot be created for class Sample
  2. Sample class cannot be inherited.
  3. Methods in class Sample cannot be overridden.
  4. Sample class cannot extend any other class.

Answer : b
Explanation : Final classes cannot be inherited.

Q16)Predict the output of below program?

class Sample
{
public static void main(String args[])
{
try
{
int a=5/0;
}
catch(Exception ex)
{
System.out.println(“Exception is printed”);
}
catch(ArithmeticException ex)
{
System.out.println(“ArithmeticException is printed”);
}
}
}

  1. Compilation Error
  2. Runtime Error
  3. Exception is printed
  4. ArithmeticException is printed

Answer: a
Explanation : Exception should be ordered in catch blocks in order Derived class to Super class.

Q17) finally block will be executed only when exception occurs.

  1. True
  2. False

Answer : False

Q18) Checked and Unchecked Exception – Differentiate

Explanation : Checked exceptions are compile time exceptions and Unchecked Exceptions are runtime exceptions.

Q19) A derived class overridden method can throw Checked Exception which was not thrown by same method in Super class.

  1. True
  2. False

Answer : False
Explanation : Overridden methods can only throw runtime exceptions if they are not thrown by super class method.

Q20) Synchronized methods are thread safe methods.

  1. True
  2. False

Answer : True
Explanation : Once a thread starts executing a synchronized method any other thread has to wait until the method finish it.

Q21) What does the below snippet does in a multithreading environment?

  Thread t1=new Thread();
t1.start();

  1. Invokes start() method
  2. Invokes run() method
  3. Invokes begin() method
  4. Invokes main() method

Answer : b
Explanation : When a thread objects calls start(), run() method of Thread class will be executed.

Q22) What is the name of Java concept achieved below?

int i=5;
Integer i1=i;

  1. Enumeration
  2. Autoboxing
  3. Unboxing
  4. Annotation

Answer : Autoboxing
Explanation : Autboxing is the concept of automatically converting a primitive data type into equivalent wrapper class object.

Q23) What is ‘out’ in System.out.println() ?

a.Static member of System class
b.Non Static member of System class
c.Object of System class
d.Final member of System class
Answer : a
Explanation : ‘out’ is static data member in System class of type PrintStream

Q24) In what class print() and println() methods are available?

  1. PrintStream
  2. InputStream
  3. OutputStream
  4. PrintWriter

Answer : PrintWriter

Q25) Which type of classes can work on different types of data?

  1. Abstract Class
  2. Final Class
  3. Super class
  4. Generic class

Answer : d
Explanation : Generic classes are classes which can operate on any type of data.

Q26) StringBuffer and StringBuilder – Difference

Answer : StringBuffer is Thread-safe (Synchronized Methods) & StringBuilder is not thread safe (Non-Synchronized Methods)

Q27) Which of the following doesn’t allow duplicate values?

  1. ArrayList
  2. LinkedList
  3. HashSet
  4. Stack

Answer : HashSet
Explanation : HashSet is a class which implements Set Interface and Set do not allow duplicate values.

Q28)Which of the following is not an interface in java?

  1. Collection
  2. List
  3. Set
  4. Vector

Answer : d

Q29)Which of the following data structure in Java is used to store key value pairs?

  1. List
  2. Set
  3. Map
  4. Stack

Answer : c

Q30) Which of the following is sorted by default

  1. ArrayList
  2. LinkedList
  3. TreeSet
  4. HashSet

Answer : c
Explanation : TreeSet is the class which implements SortedSet interface and SortedSet is the interface which provides sorting by default.

Q31) Which of the following interfaced is required to achieved cloning?

  1. Serializable
  2. Cloneable
  3. Appendable
  4. Closeable

Answer : b

Q32) Method used to deserialize and serialize objects respectively.

  1. writeObject(),readObject()
  2. readObject(),writeObject()
  3. read(),write()
  4. write(),read()

Answer : b

Q33) Which among the following is a Character Stream class?

  1. FileInputStream
  2. FileReader
  3. FileOutputStream
  4. InputStreamReader

Answer : b
Explanation : FileReader is a byte stream class.

Q34) Which of the following is not a markable interface?

  1. Serializable
  2. Cloneable
  3. Remote
  4. ActionListener

Answer : d

Q35) Which of the following packages will be imported automatically by JVM?

  1. util
  2. lang
  3. io
  4. awt

Answer: b

Q36) Which of the following packages Date class belongs to?

  1. util
  2. lang
  3. io
  4. awt

Answer: a

 Q37)Which among the following is super class of all class in Java?

  1. Object
  2. Super
  3. ObjectInput
  4. ObjectInstance

Answer : a

Q38) Which of the following is not a lightweight component?

  1. Jbutton
  2. JLabel
  3. JTextField
  4. TextField

Answer : d
Explanation : TextField belongs to AWT Component which is hard weight by nature.

Q39) Which of the following is not a Listener interface in java?

  1. ActionListener
  2. ComponentListener
  3. ItemListener
  4. MouseListener

Answer : b

Q40) What is the use of Adapter classes in Java?

Adapter classes are used to provide default implementation of the available listeners in java. So as to reduce the overhead of providing implementation of all the methods in the Listener interface.

Q41) Predict the output of the below program.

class Sample
{
public static void main(String args[])
{
int count=0;
for(int i=0;i<5;i++)
{
if(i==3)
{
continue;
}
count++;
}
System.out.println(count);
}
}
Answer : 4
Explanation : Continue will break out of the current iteration.

Q42) Predict the output of the below program.

class Sample
{
public static void main(String args[])
{
int count=0;
for(int i=0;i<5;i++)
{
if(i==3)
{
break;
}
count++;
}
System.out.println(count);
}
}

Answer : 3
Explanation : break will break out of the all consecutive iterations.

Q43) All methods declared in an interface have which of the following property implicitly?

  1. Final
  2. Static
  3. Abstract
  4. Super

Answer : 3
Explanation : All methods in an interface are abstract by nature.

Q44) All data members declared in an interface have which of the following property implicitly?

  1. Final
  2. Static
  3. Abstract
  4. Super

Answer : a
Explanation : All data members in an interface are final by nature.

Q45) Which of following interface is used for multiple sorting?

  1. Comparable
  2. Comparator
  3. Cloneable
  4. Serializable

Answer : b

Q46) Which of the following interface has compareTo method declaration?

  1. Comparable
  2. Comparator
  3. Cloneable
  4. Serializable

Answer : a
Explanation : Comparable provides compareTo method, Comparator provides compare method declaration.

Q47) Predict the output of the below program.

class Sample
{
public static void main(String args[])
{
double d=5.0/0.0;
System.out.println(d);
}
}

  1. Compilation Error
  2. Arithmetic Exception on runtime
  3. Infinity
  4. Exception on runtime

Answer : c
Explanation : Infinity constant will be printed if divide by scenario happens in double variable.

Q48) Which of the following is not a type of access modifier in Java?

  1. Public
  2. Private
  3. Default
  4. Protected
  5. None of the above

Answer : e

Q49) Which of the following is not part of java.net package?

  1. Socket
  2. ServerSocket
  3. URL
  4. Serialize

Answer : d

Q50) Which of the following created immutable Strings?

  1. String
  2. StringBuffer
  3. StringBuilder
  4. StringAppender

Answer : a      

Q51) What will be the output if you execute the below code snippet?

public class Sample {

public static void main(String[] args) {

HashMap<Integer,String> hm = new HashMap<Integer,String>();
hm.put(1,”a”);
hm.put(2,”b”);
hm.put(3,”c”);
hm.put(1,”d”);

System.out.println(hm.toString());

}
}

  1. {1=d, 2=b, 3=c}
  2. {1=d, 2=b, 1=a}
  3. {1=a, 2=b, 3=c,1=d}
  4. {1=a, 2=b, 3=c}

Answer : a

 Q52) What will be the output if you execute the below code snippet?

public class Sample {

public static void main(String args[]){
int i=2;
switch(i)
{
case 1:
System.out.println(“1”);
break;
case 2:
System.out.println(“2”);
break;
case 3:
System.out.println(“3”);
break;
case 4:
System.out.println(“4″);
break;
case 2:
System.out.println(” “);
break;
default:
System.out.println(“Default”);

}
}
}

  1. The code will show compile time errors
  2. The code runs successfully and displays nothing
  3. The code runs successfully and displays 2.
  4. The code will show run time errors

Answer : a

Q53)   What will be the output if you execute the below code snippet?

class Square {

public int calculateArea(int a) {

return a*a;
}

}

public class Cube extends Square {

public int calculateArea(int a) {

return a*a*a;
}

public static void main(String[] args) {
Square b = new Cube();
System.out.println(b.calculateArea(3));
}

}

  1. The output 27 is displayed
  2. The output 9 is displayed
  3. The code shows runtime error
  4. The code shows compile time error

Answer : a

Q54)   Which of the following is true? Choose only one

  1. static methods can be overloaded and overridden.
  2. static methods can be overloaded but cannot be overridden.
  3. static methods can be overridden but cannot be overloaded.
  4. static methods can neither be overloaded nor be overridden.

Answer : b

Q55)   The below code snippet will compile. Say true or false

class Base {
int foo(int a) {
return a;
}
}

public class Derived extends Base{

protected int foo(int a) {
return a+a;
}
public static void main(String[] args) {
Base b = new Derived();
}
}

  1. True
  2. False

Answer : a

Q56)   A constructor can be static and final.  Say true or false

  1. True
  2. False

Answer : b
 

Q57)   All the fields in the interface are

  1. public
  2. b. static , final, public
  3. abstract
  4. public and final

Answer : b

Q58)   Which of the following statement is correct. Choose only one.

  1. StringBuilder is thread safe and StringBuffer is not thread safe.
  2. Both StringBuffer and StringBuilder are thread safe.
  3. Both StringBuilder and StringBuffer are mutable. StringBuilder is not thread safe

but   StringBuffer is thread safe.

  1. StringBuilder is not threadsafe and immutable. StringBuffer is thread safe and mutable.

Answer : c

Q59)   Which of the following is not a method of String class?

 

  1.      append(String s)
  2. replace(char oldChar, char newChar)
  3. charAt(int index)
  4. matches(String regex)

Answer : a

Q60) Suppose there is an entity named Car and another entity named Engine. Which of the following is more appropriate for the relationship between Car and Engine?

  1. Aggregation
  2. Composition
  3. Inheritance
  4. Association

Answer : b

Q61) What will be the output if you execute the below code snippet?

public class Sample {

static void replaceString(String s)
{
s=s.replace(“A”,”Z”);
System.out.println(s);
}
public static void main(String[] args) {

String s1=”A”;
replaceString(s1);
System.out.println(s1);

}

}

  1. A A
  2. A Z
  3. Z A
  4. Z Z

Answer : c

Q62) Which of the following would you use in case of storing unique elements with the order of insertion?

 

  1. LinkedHashSet
  2. HashSet
  3. HashMap
  4. TreeMap

Answer : a

Q63) What will happen if the following code snippet is run?

public class Sample2 {

public static void main(String args[]){
try {
throw new InterruptedException();
}
catch(Exception e){
System.out.println(e);
}

}
}
 

  1. InterruptedException is caught
  2. Exception is caught
  3. Exception is not caught
  4. Nothing is displayed

Answer : a

Q64) What are the different types of inheritance among classes in java?

  1. Multilevel, Multiple, Single, Heirarchial
  2. Multilevel, Multiple, Single, Hybrid
  3. Multilevel, Multiple, Single, Heirarchial,Hybrid
  4. Multilevel, Single, Heirarchial,Hybrid

Answer : d

Q65) What will be the output if the following code snippet is run?

class Animal { }
public class Dog extends Animal {
public static void findInstance(Animal a) {
if (a instanceof Dog) {
System.out.println(“This is Dog instance”);
} else if (a instanceof Animal) {
System.out.println(“This is Animal instance!”);
}
}
public static void main(String args[]) {
Animal a = new Dog();
findInstance(a);
}
}

  1. This is Dog instance
  2. This is Animal instance
  3. ClassCastException is thrown
  4. Runtime Error occurs

Answer : a

Q66) What will be the output if the following code snippet is run?

public class Sample {
public static void main(String st[]){
String s1 = new String(“string”);
String s2 = “string”;

boolean b1 = s1==s2;
boolean b2 = s1.equals(s2);
System.out.print(b1);
System.out.print(” “+b2);
}
}

  1. true true
  2. false true
  3. true false
  4. false false

Answer : b

Q67) Overloading of methods cannot be   done by changing only the

 

  1. type of arguments
  2. order of arguments
  3. number, type and order of arguments
  4. return type of method

Answer : d

Q68) What will be the output if the following code snippet is run?

public class Sample5 {
static void m1(byte b) {
System.out.println(++b);
}

public static void main(String[] args) {
m1(1);
}
}

  1. 2 is displayed
  2. 1 is displayed
  3. Runtime exception is thrown
  4. Code will not compile

Answer : d

Q69) Try block cannot be blank and must contain a statement. Say True or False

 

  1. true
  2. false

Q70)   Which of the following is true about local variables?

 

  1. a. Local variables can be final but cannot be static or transient. Local variables cannot be public, private or protected.
  2. Local variables can be final or static but cannot be transient. Local variables can be private only.
  3. Local variables can be final , static and transient. Local variables can be private only.
  4. Local variables cannot be final, static, transient.

Answer : a

Q71)  What will be the output if the following code snippet is run?

public class Test {
public static void dec(double d) {
d = d – 3;
}
public static void main(String args[]) {
double d = 13;
dec(d);
System.out.println(d);
}
}

  1. 13 is displayed
  2. 0 is displayed
  3. 10 is displayed
  4. 0 is displayed

Answer : b

Q72)  What will be the output if the following code snippet is run?

public class Sample
{
public static void main(String[] args)
{
byte b= 2;
int n = 100;
long l = n;
Double d = (double)Integer.valueOf(n);

System.out.println(b);
System.out.println(n);
System.out.println(n);
System.out.println(d);
}
}

  1. ClassCastException will be thrown
  2. Compiler time error occurs
  3. Displays output as

2
100
100
100.0

  1. Displays output as

2
100
100 l
100.0d

Answer : c

Q73) What will be the output if the following code snippet is run?

public class Sample {
public static void main(String args[])
{
boolean b = false;
do{
System.out.println(“inside do while loop”);
b=true;
}while(!b);
}
}

  1. Nothing is printed
  2. do while loop keeps on running after entering an infinite loop
  3. it prints “inside do while loop”
  4. Runtime Exception is thrown

Answer : c

Q74)   The use of this() and super() is allowed inside static method and constructor. Say true or false.

  1. true
  2. false

Answer : b
 

Q75)

interface A {
public int display();
}
abstract class B implements A {
}
public class Example extends B {

public int display() {
return 0;
}
}
In the above code, what changes need to be done to compile it successfully?

  • Implement the display method in class B
  • Overload the display method in class Example
  1. Remove the class B
  • No changes needed.

Answer : d

Q76)   How to send a cookie in the response?

 

  1. HttpServletResponse.addCookie(Cookie).
  2. HttpServletResponse.setCookie(Cookie).
  3. HttpServletResponse.sendCookie(Cookie).
  4. setCookie(HttpServletResponse response).

Answer : a

Q77)   How to import   a package in jsp file?

  1. <%@ page import=”packagename.classname” %>
  2. <%@ import=”packagename.classname” %>
  3. <% page import=”packagename.classname” %>
  4. <%@ import=”packagename.classname” %>

Answer :

Q78) Which of the following method is used to send control to another servlet and is defined under    RequestDispacther interface?

  1. sendRedirect()
  2. b. forward()
  3. send()
  4. redirect()

Answer : b

Q79)  What will be the output if the following code snippet is run?

<%! int a=10; %>
<% int a=5; %>
<% int b=2; %>
Result is <%= a*b %>

  1. Displays 20
  2. You cannot re declare a
  3. Displays 10
  4. Error is displayed

Answer : c

Q80)   This is responsible for managing the lifecycle of a servlet.

  1. Application server
  2. Web container
  3. Application container
  4. Web server

Answer : b

Q81)   With java 7, which of the following is supported in switch statement?

  1. Enum
  2. Character
  3. String
  4. constant

Answer : c

Q82) Which of the following code gives minimum value in the given list ?

  1. list.stream()

.mapToInt(v -> v)
.findMin() ;

 

  1. list.stream()

.mapToInt(v -> v)
.min()
.orElse(Integer.MAX_VALUE);

  1. list.stream()

.min(Comparator.naturalOrder)
.get();

d.List.stream(list)
.mapToInt(v -> v)
.min();
Answer : b

Q83)  What is the type of lambda expressions?

  1. Object
  2. functional interface
  3. Interface
  4. Object variable

Answer : b

Q84) Which of the following is not a method of Optional class in java 8?

  1. IsPresent()
  2. OfNullable(T val)
  3. OrElse(T t)
  4. IsPresentorNull()

Answer : d

Q85) What will be the output if the following code snippet is run?

public class Sample {
public static void main (String[] args)
{
String myString = “123s4”;
int foo = Integer.parseInt(myString);
System.out.println(foo);
}
}

  1. ClassCast exception is thrown
  2. Compile time Exception is thrown
  3. NumberFormatException is thrown
  4. Code displays “123s4”

Answer : c

Q86) Which of the following is not a built  in java annotation?

  1. @Override
  2. @SuppressWarnings
  3. @Deprecated
  4. @Inherit

Answer : d

Q87) The below code does not compile. Please identify the cause.

import java.io.IOException;

class Super
{
void show() {
System.out.println(“super class “);
}
}

public class Sub extends Super
{
void show() throws IOException
{
System.out.println(“sub  class”);
}
}

  1. CheckedException cannot be used with throws keyword
  2. Sub class overridden method cannot throw CheckedException
  3. CheckedException should be handled with try and catch block
  4. The code is correct

Answer : b

Q88) Which of the following is not true about lambda expressions?

 

  1. Lambda expressions are a way to replace anonymous classes
  2. Lambda expressions helps in the implementation of functional interfaces
  3. The scope of lambda expressions is not limited to its enclosing class
  4. The lambda expressions uses target typing to infer the type of arguments

Answer : c

Q89) Which of the following illustrates the relationship in the below statement correctly?

House has a room which is luxurious

  1. Class Room extends House{ luxury();}
  2. Class House {private LuxuryType Room;}
  3. Class Room {private House (Luxury luxury);}
  4. Class Room {private Luxury house;}

Answer : b

Q90) Which of the following is correct implementation of NavigableSet?

NavigableSet<String> al=new NavigableSet <String>();
al.add(“Ram”);
al.add(“Veer”);
al.add(“Ram”);
al.add(“Ajay”);
NavigableSet<String> al=new TreeSet<String>();
al.add(“Ram”);
al.add(“Veer”);
al.add(“Ram”);
al.add(“Ajay”);
Set<String> al=new NavigableSet <String>();
al.add(“Ram”);
al.add(“Veer”);
al.add(“Ram”);
al.add(“Ajay”);
TreeSet <String> al=new NavigableSet <String>();
al.add(“Ram”);
al.add(“Veer”);
al.add(“Ram”);
al.add(“Ajay”);

Answer : b

Q91) Which of these methods will cause the currently running threads to stop executing until the thread it joins with completes its task?

  1. join()
  2. wait()
  3. wait(int num)
  4. resume()

Answer : a

Q92) The keyword synchronized can be applied to

  1. Methods, variables, block
  2. Methods, blocks, static blocks
  3. Variables, methods
  4. Methods and blocks

Answer : b

Q93) Which tag is used to force the container to load as the server starts?

 

  1. <load-on-startup>
  2. <load-init>
  3. <load-startup>
  4. <server-startup>

Answer : a

Q94) Which of the following is true about ServletContext?

  1. One per web application
  2. One per session
  3. One per context
  4. One per web config

Answer : a

Q95) Which of the following  is not a http method?

  1. GET
  2. REPLACE
  3. PUT
  4. HEAD

Answer : b

Q96) Which of the following not a class in collection hierarchy?

  1. SortedSet
  2. TreeSet
  3. LinkedHashSet
  4. HashSet

Answer : a

Q97) Which of the below  driver uses middleware (application server) that converts JDBC calls directly or indirectly into the vendor-specific database protocol?

  1. JDBC-ODBC bridge driver
  2. Native-API driver (partially java driver)
  3. Network Protocol driver
  4. Vendor thin driver

Answer : c

Q98) The class java.io.BufferedInputStream  extends which of the below class?

  1. DataInputStream
  2. FilterInputStream
  3. ReaderStream
  4. ObjectInputStream

Answer :

Q99) Which of the following is not a keyword in java?

  1. implements
  2. package
  3. throws
  4. Integer

Answer : d

Q100) How to find the size of the given array named arrayNum?

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

Answer : f

Q100) why java is called platform independent?

Class files generated in windows OS can be ran on different machines irrespective of OS such as mac,linux etc .

Q101) Is JVM platform idependent or Platform dependent.

JVM is platform dependent.

Q102) explain why JVM is platform dependent?

Different OS has respective JVM installed in systems which makes JVM platform dependent and java platform independent.

Q103) Where are objects stored in JVM?

In Heap

Q104) How many copies exist when String Object is created?

Two copies are created when String s1=new String(); is executed.
One in String Pool and in Heap

Q105) How would you handle Null pointer exception, explain through code.

String s1=”Hello Besant!!”;
If(null!=s1 && s1.isEmpty)
{System.out.println(“Hello Karthik”);
}

Q106) what is an Exception?

Exception is an Object  thrown due to run time errors or abortion of system.

Q107) can main() be overloaded?

Yes it can be overloaded

Q108) What is String Literal ?

when String is declared as below it is called as String Literal.
String s1;
String literals are stored in only String pool unlike String objects.

Q109) what is default scope of spring bean?

Singleton

Q110) What is IOC?

IOC is called as Inversion of control where the Spring container looks after the object creation and the life cysle of cycle .

Q111) what is output of below?

String s1=”hi”;
String s2=new String ();
s2=”hi”;
If(s1==s2)
{
System.out.println(“hello Karthik”);
}
Else
{
Sytem.out,println(“Hi beasant”);
}
Output:
Hi besant
Q112) what is output of below?
String s1=”hi”;
String s2=new String ();
s2=”hi”;
If(S1.equals(s2))
{
System.out.println(“hello Karthik”);
}
Else
{
Sytem.out,println(“Hi beasant”);
}

Answer: hello Karthik

Q113) what is output of below?

String s1=”hello Besant!!”;
String s2=s1.subString(0,4);
System.out.println(s2);
output:hell

Q114) which exceptions are required to handle?

checked exceptions

Q115) Example of unchecked exceptions.

ArrayOutOfBoundsException,null pointer,divide by zero

Q116) Can a method be invoked without creating the object of that class?

yes, static classes n methods doesn’t require object to be created to invoke a method.

Q117) Which methods are loaded into JVM when program is executed?

static methods

Q118) When ArrayList and LinkedList are recommended to use?

  • When there are frequent read operations ArrayList is best to use as it uses RandomAccess
  • When there are frequent Removal/ Write operations LinkedList is best to use because in ArrayList positions should be adjusted but in LinkedList works storing the address of next element.

Q119) why generics are used?

Generics are type safe
ArrayList<String> a1=new ArrayList<String>();
It allows only String to be stored in this case.

Q120) Which collection do Hashmap internally uses?

LinkedList

Q121) What is java?

  1. Java is fast, secure, reliable and object oriented programming language.It can be used for creating desktop application,web application,mobile application.

Q122) What is oops?

Object oriented programming system..Its a one kind of methodology used to create program

Q123) Oops concepts in java?

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

Q124) Why Encapsulation?

It provide security and we can modify easily

Q125) Why Polymorphism?

It has many method with same name.Its used to avoid naming complexcity.

Q126) Why Inheritance?

It is used for Resusability

Q127) Why Abstraction?

Hide the data and showing essential details only

Q128) Types of Polymorphism?

Static or comple time polymorphism.

  1. Example : method Overloading

Dynamic or runtime polymorphism.

  • Example : method Overring

Q129) Difference between overload and override?

  1. Overload means same method name with different parameter in a single class.
  2. Override means same method in a parent and child class

Q130) How many types of relationshilp in java?

  1. IS_A example inheritace
  2. HAS_A example Aggrigation

Q131) What is Inheritance?

Reusability.Derived the data from parent class to child class

Q132) Is java support multiple Inheritance?

No. because multiple inheritance can cause ambiguity

Q133) Why object oriented programming?

  • We con eliminate the redundant code use of inheritance
  • We can easily upgrade from small to large programs
  • We can reduce program complexity

Q134) What is class?

It is a combination of variables and methods.It is used to define template or blueprint

Q135) How to achieve multiple inheritance in java?

By interface

Q136) How to avoid method override in java?

If you make method as final  can not override.

Q137) What is the this keyword in java?

It is used to refer current class variable

Q138) How to achieve encapsulation in java?

By private keyword

Q139) What is the use of getter and setter method in java?

It is used to access private data variables in outside of class.

Q140) What is the use of package in java?

We can remove naming collision.

Q141) What is wrapper class in java?

It is a converting mechanism used to convert primitive to object and object to primitive

Q142) What is autoboxing?

It is a automatic conversion compiler convert primitive type to object wrapper class

Q143) What is unboxing?

It is a automatic conversion compiler convert object to primitive type.

Q144) What is contructor?

It is a method will invoke at the time of object creation and initialize the values to properties

Q145) Types of contructor in java?

  1. Default constructor
  2. Parameterized constructor

Q146) What is checked exceptions in java?

  • This kinds of exception will check at compile time.
  • For example : IOException,ClassNotFoundException,FileNotFoundException

 

Q147) What is unchecked exceptions in java?

  • This kinds of exception will check at run compile time.
  • For example : ArithmeticException,NullPointerException.

Q148) Difference between final and finally?

  • Final is a non access modifiers.
  • Finally is a block in exception handling.

 Q149) Difference between throw and throws?

  • Throw is used to call custom exception.
  • Thorws used to declare multiple exceptions in method signature

Q149) Can I start thread two times?

  • No

  Q150) What is serialization?

  • It is a interface used to convert object to byte.

Q151) What is deserialization?

  • Reverse operation of serialization
  • byte value is converted to object.

Q152) Can we remove the objects during iteration?

Using list-iterator
ListIterator<Balls> itr= balls.listiterator()
While()
{
Itr.hasNext()
Itr.remove();

}
}

Q153) How to sort the collection ?

  • Using sort() method with the help of

    combarable

    interface.

Q154)Tree set is used for what purpose ?

  • Getting the collection in ascending order or sorted order
  • Duplicate values not allowed in treeset

Q155) How to make a class immutable ?

  • Declare the class is final
  • Do not provide setter methods
  • Field should be final

Q156) What is yield() method in java?

  • This method is used in the concepts of thread
  • Thread which is not doing any important task during the execution any other thread which is important to run during the process, so yield method helps to achieve the completion of important thread, else current thread continue to run.

Q157) Default scope in spring?

  • Singleton scope

Q158) How to handle exception in spring mvc?

@ExceptionHandler methods to handle , For  own exceptions and in ResponseBodywe can write the custom exception to show.

Q159) What is the use of @Qualifier in spring annotation?

  • When more beans created in same name to identify to which one call first based on the request we can use to identify and do the @Autowired

Q160) What is externalization?

  • Used for serialization
  • Externalization which extends serialization

Q161)  What is transient keyword?

  • Transient keyword used in serialization
  • When the particular variable in a file which don’t need to save in the receiving end file, then we use transient keyword

Q162) Please explain about ops concept with real time example?

Polymorphism
Abstraction
Encapsulation
Inheritance

  • Polymorphism is in which we can identified by one action has different forms, Suppose talking is the feature given god to humans, but for animals the way of communication will be different
  • Abstraction is the process in which we can relate it with phone, We are able to dial the numbers but what happening inside we exactly don’t know
  • Making an entity into single unit, Suppose we have a pack of biscuit, But biscuit is made up of different ingredients, like wheat, vanilla, etc
  • Inheritance= Inheritance is the concept in which we can compare the relationship between parent and child, A child have two set of 23 chromosomes which is from parents.

Q163) Difference in overloading and overriding, In your project have you use

  • Overiding

    in which happening in two different class

Methods and signature will be same

  • Overloading mostly in one class , Method is same but signature is different

In my project i have use overriding

Q164) Have you use super keyword?

  • Super keyword is used to invoke the parent method or class and getting the features/characteristics in the child class

Q165) How the memory related concept are you familiar with JVM, Can you explain about that?

  • Heap ,stack,Pc,Program counter register
  • Native method, Class area

Q166) Did multiple inheritance is supported in java?

  • No

Q167) How to achieve that?

  • Using interface

Q168) How you write interface in code

  • Interfaces is used replacement of multiple inheritance
  • In interface we will defining the methods that action is going t perform
  • In the implementation class we will overriding all the methods and write the logic

Q169) If there is parking of vehicle project is given to you which collection you will use to solve the issue

  • ConcurrentHashMap

Q170) What is singleton class

  • A class which returns only one object is called singleton class

Q171) When we clone the singleton we get a same object but as per singleton only one object created so how to resolve the issue

  • By override clone method and write a throw an exception CloneNotSupportedException, so we can avoid the issues

Related Post

No Comments


Leave a Reply

Your email address will not be published Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

Online Training Quick Enquiry






Get Free Online training